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.

78 Part II: Designing Secure <strong>Web</strong> <strong>Application</strong>sValidate Data for Type, Length, Format, <strong>and</strong> RangeUse strong type checking on input data wherever possible, for example, in the classesused to manipulate <strong>and</strong> process the input data <strong>and</strong> in data access routines. Forexample, use parameterized stored procedures for data access to benefit from strongtype checking of input fields.String fields should also be length checked <strong>and</strong> in many cases checked forappropriate format. For example, ZIP codes, personal identification numbers, <strong>and</strong> soon have well defined formats that can be validated using regular expressions.Thorough checking is not only good programming practice; it makes it more difficultfor an attacker to exploit your code. The attacker may get through your type check,but the length check may make executing his favorite attack more difficult.Reject Known Bad InputDeny “bad” data; although do not rely completely on this approach. This approach isgenerally less effective than using the “allow” approach described earlier <strong>and</strong> it isbest used in combination. To deny bad data assumes your application knows all thevariations of malicious input. Remember that there are multiple ways to representcharacters. This is another reason why “allow” is the preferred approach.While useful for applications that are already deployed <strong>and</strong> when you cannot affordto make significant changes, the “deny” approach is not as robust as the “allow”approach because bad data, such as patterns that can be used to identify commonattacks, do not remain constant. Valid data remains constant while the range of baddata may change over time.Sanitize InputSanitizing is about making potentially malicious data safe. It can be helpful when therange of input that is allowed cannot guarantee that the input is safe. This includesanything from stripping a null from the end of a user-supplied string to escaping outvalues so they are treated as literals.Another common example of sanitizing input in <strong>Web</strong> applications is using URLencoding or HTML encoding to wrap data <strong>and</strong> treat it as literal text rather thanexecutable script. HtmlEncode methods escape out HTML characters, <strong>and</strong> UrlEncodemethods encode a URL so that it is a valid URI request.

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

Saved successfully!

Ooh no, something went wrong!