11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 12: Building Secure <strong>Web</strong> Services 327In the preceding example, the .NET Framework type system performs type checksautomatically. To validate the range of characters that are supplied through the namefield, you can use a regular expression. For example, the following code shows howto use the System.Text.RegularExpressions.Regex class to constrain the possiblerange of input characters <strong>and</strong> also to validate the parameter length.if (!Regex.IsMatch(name, @"[a-zA-Z'.`-´\s]{1,40}")){// Invalid name}For more information about regular expressions, see the “Input Validation “ sectionin Chapter 10, “Building Secure ASP.NET Pages <strong>and</strong> Controls.” The followingexample shows a <strong>Web</strong> method that accepts a custom Employee data type.using Employees; // Custom namespace[<strong>Web</strong>Method]public void CreateEmployee(Employee emp) { ... }The consumer needs to know the XSD schema to be able to call your <strong>Web</strong> service. Ifthe consumer is a .NET Framework client application, the consumer can simply passan Employee object as follows:using Employees;Employee emp = new Employee();// Populate Employee fields// Send Employee to the <strong>Web</strong> servicewsProxy.CreateEmployee(emp);Consumer applications that are not based on the .NET Framework must construct theXML input manually, based on the schema definition provided by the organizationresponsible for the <strong>Web</strong> service.The benefit of this strong typing approach is that the .NET Framework parses theinput data for you <strong>and</strong> validates it based on the type definition. However, inside the<strong>Web</strong> method you might still need to constrain the input data. For example, while thetype system confirms a valid Employee object, you might still need to performfurther validation on the Employee fields. You might need to validate that anemployee’s date of birth is greater than 18 years ago. You might need to use regularexpressions to constrain the range of characters that can be used in name fields, <strong>and</strong>so on.For more information about constraining input, see the “Input Validation” section inChapter 10, “Building Secure ASP.NET Pages <strong>and</strong> Controls.”

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!