03.11.2016 Views

Beginning ASP.NET 4.5 in CSharp and VB Opsylum

Create successful ePaper yourself

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

Gather<strong>in</strong>g Data from the User x 333<br />

FIGURE 9-10<br />

The <strong>ASP</strong>.<strong>NET</strong> run time does this to prevent users from enter<strong>in</strong>g HTML or JavaScript that can<br />

potentially mess with the design or work<strong>in</strong>gs of your website or that could lead to security risks. If<br />

you’re sure you want to allow your users to enter HTML, you have a few options available. First,<br />

you can disable request validation by sett<strong>in</strong>g the ValidateRequest attribute <strong>in</strong> the @ Page directive<br />

to False:<br />

<br />

With this sett<strong>in</strong>g set to False, users can enter HTML without caus<strong>in</strong>g an error. In previous versions<br />

of <strong>ASP</strong>.<strong>NET</strong>, this was the only way to enable a user to send HTML to your page. The downside of<br />

this solution is that it’s all or noth<strong>in</strong>g. By turn<strong>in</strong>g off request validation at the page level, all controls<br />

<strong>in</strong> the page will now accept HTML. This unnecessarily opens up your page for potential abuse. As<br />

a good security mechanism, you should allow HTML only for specific fields that you determ<strong>in</strong>e<br />

explicitly.<br />

Fortunately, <strong>ASP</strong>.<strong>NET</strong> <strong>4.5</strong> now makes this very easy; the Control class (from which classes such as<br />

TextBox <strong>in</strong>herit) has been extended with a ValidateRequestMode property that controls the check<br />

for <strong>in</strong>valid data. When set to Enabled, the control does not accept HTML; when set to Disabled<br />

the control does accept HTML, similar to how ValidateRequest enabled this for all controls <strong>in</strong> the<br />

page. The f<strong>in</strong>al value for ValidateRequestMode is Inherit, which gives it the value set by the parent<br />

of the control such as a PlaceHolder. This is convenient because you can wrap multiple controls<br />

that should accept HTML <strong>in</strong> a PlaceHolder <strong>and</strong> then configure the ValidateRequestMode on the<br />

conta<strong>in</strong>er <strong>in</strong>stead of on each <strong>in</strong>dividual control.<br />

Before you set this property on a control, make sure you really want to allow users to submit HTML<br />

<strong>in</strong> your controls. This opens up your site for Cross Site Script<strong>in</strong>g attacks when you’re not careful.<br />

Depend<strong>in</strong>g on the situation, you may need to sanitize the data by remov<strong>in</strong>g dangerous pieces of code<br />

such as elements. When display<strong>in</strong>g the data on a page, you could use a Literal control,<br />

with its Mode set to Encode <strong>in</strong> order for the HTML to be encoded <strong>and</strong> rendered harmless. With this<br />

property set, the text is displayed verbatim, without be<strong>in</strong>g <strong>in</strong>terpreted as HTML or JavaScript.

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

Saved successfully!

Ooh no, something went wrong!