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.

264 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sConstrain, Then SanitizeStart by constraining input <strong>and</strong> check for known good data by validating for type,length, format, <strong>and</strong> range. Sometimes you also need to sanitize input <strong>and</strong> makepotentially malicious input safe. For example, if your application supports freeformatinput fields, such as comment fields, you might want to permit certain “safe”HTML elements, such as <strong>and</strong> , <strong>and</strong> strip out any other HTML elements. Thefollowing table summarizes the options that are available for constraining <strong>and</strong>sanitizing data:Table 10.1 Options for Constraining <strong>and</strong> Sanitizing DataRequirementType checksOptions.NET Framework type system. Parse string data, convert to a strong type, <strong>and</strong>then h<strong>and</strong>le FormatExceptions.Regular expressions. Use ASP.NET RegularExpressionValidator control orRegex class.Length checksRegular expressionsString.Length propertyFormat checksRegular expressions for pattern matching.NET Framework type systemRange checksASP.NET RangeValidator control (supports currency, date, integer, double, <strong>and</strong>string data)Typed data comparisonsRegular ExpressionsYou can use regular expressions to restrict the range of valid characters, to stripunwanted characters, <strong>and</strong> to perform length <strong>and</strong> format checks. You can constraininput format by defining patterns that the input must match. ASP.NET provides theRegularExpressionValidator control <strong>and</strong> the Regex class is available from theSystem.Text.RegularExpressions namespace.If you use the validator controls, validation succeeds if the control is empty. Form<strong>and</strong>atory fields, use a RequiredFieldValidator. Also, the regular expressionvalidation implementation is slightly different on the client <strong>and</strong> server. On the client,the regular expression syntax of Microsoft JScript ® development software is used. Onthe server, System.Text.RegularExpressions.Regex syntax is used. Since JScriptregular expression syntax is a subset of System.Text.RegularExpressions.Regexsyntax, it is recommended that JScript regular expression syntax be used to yield thesame results on both the client <strong>and</strong> the server.

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

Saved successfully!

Ooh no, something went wrong!