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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

328 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sLoosely Typed ParametersIf you use string parameters or byte arrays to pass arbitrary data, you lose many ofthe benefits of the .NET Framework type system. You must parse the input datamanually to validate it because the auto-generated WSDL simply describes theparameters as string input of type xsd:string. You need to programmatically checkfor type, length, format, <strong>and</strong> range as shown in the following example.[<strong>Web</strong>Method]public void SomeEmployeeFunction(string dateofBirth, string SSN){. . .// EXAMPLE 1: Type check the datetry{DateTime dt = DateTime.Parse(dateofBirth).Date;}// If the type conversion fails, a FormatException is throwncatch( FormatException ex ){// Invalid date}}// EXAMPLE 2: Check social security number for length, format, <strong>and</strong> rangeif( !Regex.IsMatch(empSSN,@"\d{3}-\d{2}-\d{4}",RegexOptions.None)){// Invalid social security number}XML DataIn a classic business-to-business scenario, it is common for consumers to pass XMLdata that represents business documents such as purchase orders or sales invoices.The validity of the input data must be programmatically validated by the <strong>Web</strong>method before it is processed or passed to downstream components.The client <strong>and</strong> the server have to establish <strong>and</strong> agree on a schema that describesthe XML. The following code fragment shows how a <strong>Web</strong> method can use theSystem.Xml.XmlValidatingReader class to validate the input data, which, in thisexample, describes a simple book order. Notice that the XML data is passed througha simple string parameter.

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

Saved successfully!

Ooh no, something went wrong!