Monthly Archives: September 2014

Defining a GUID type in XSD

It often happens that an element or an attribute in an XML document contains a GUID value. Because XML documents are useless without the corresponding XSD, it also often happens, that you have to define a GUID in XSD. Although XSD has support for several built-in types, unfortunately GUID is not one of them, and you have to use the classic regex solution:

<xs:schema ...>

  <xs:simpleType name="guid">
    <xs:restriction base="xs:string">
      <xs:pattern value="[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-
[0-9a-fA-F]{4}-[0-9a-fA-F]{12}" />
    </xs:restriction>
  </xs:simpleType>

After this you can refer to your new guid type just like the built-in ones:

<xs:attribute name="id" type="guid" use="required" />

Note that the regex pattern does not have $ and ^  characters in the beginning and in the end, because the pattern should always match the full value.

 

Technorati-címkék: ,
Advertisements

HTTPS: Do. Or do not. There is no try.

Last summer I had the chance to visit Bologna, Italy, and I was happy to see that there is free wifi service in the airport. I probably had to be suspicious from the beginning, but it all started to be strange for me, when I saw this “welcome” page in the browser after connecting:

airport-ssl-warning

According to the message, the site’s security certificate is “a bit” invalid. Actually it could be more invalid only if it were already revoked.

If you decide to continue you will see this website of the airport:

airport-webpage

Really original design. Right, Bologna is not a huge metropolis, but I’m pretty sure it would be easy to find a student of the local university, who could click together a prettier website during a weekend.

This page made me curious and I could quickly find out, that the website has nothing to do with accessing the public internet.

There are many unusual and suspicious aspects here:

  • Certificate
  • IP address
  • Design
  • Phone number collection

This was the moment when I stood up and started to look for Troy Hunt and his Pineapple 🙂

Most of these concerns of me could be swept away with a single valid SSL certificate. But these invalid certificates do not guarantee anything, except nervous average users and pro users who are worrying about the security of their data.

If you do HTTPS, please do it correctly. Or don’t do it at all. Don’t try.

 

Technorati-címkék: ,

JSON or not JSON: that is the question

When you write unit tests for a REST API, you probably want to test whether the given response is in the expected format. For example you want to ensure, that the response string is a valid JSON or not.

You can find a very simple tip on StackOverflow and in other blogs as well: just check whether the first character of the response is a < or { character, because JSON is about Object Notation, right? The problem with this approach is not only that it does not perform a thorough analysis, but also that its basic statement is simply not true. According to json.org, a JSON can also contain only a single value, the specification does not require it to be an object or an array at all:

json-value

So I love JSON, 123, true and false are all valid JSON strings.

Unfortunately I could not find a simple IsValidJson method, but I could come up with this solution using Newtonsoft JSON.NET library:

try
{
    JToken.Parse(input);
    return true;
}
catch (JsonReaderException)
{
    return false;
}

Is there a better solution?

 

Technorati-címkék: ,,

IP filtering in IIS running in the Amazon cloud

You can setup highly scalable webservices very easily in the Amazon EC2 cloud: just create two new virtual machines, connect them to a load balancer and you’re done! The dark clouds will begin to gather over your head, when you realize that the carefully setup IP filtering does not work in IIS, and anyone can access your website.

The problem is that the IIS running in the virtual machine sees the load balancer as the client, and not the original browser. (Obviously, IP filtering would work perfectly for that internal address, but I’m pretty sure you don’t need that.) If you don’t believe me, check your IIS log files.

Thankfully Amazon load balancers support the Proxy protocol which forwards the IP address of the real client in the X-Forwarded-For HTTP request header. By default IIS doesn’t log the value of this field, but you can add it to your logs with a few clicks:

iis-proxy-logging

The second good news is that you can configure IIS to use the X-Forwarded-For header for IP filtering. In IIS 7 you can do this with the Dynamic IP Restriction module, and from IIS 8 you can get this functionality built into the IP Address and Domain Restrictions module. It is not enabled by default, but you can activate it with a single click:

iis-proxy-mode

 

Technorati-címkék: ,,