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.

lxviii<strong>Improving</strong> <strong>Web</strong> <strong>Application</strong> <strong>Security</strong>: <strong>Threats</strong> <strong>and</strong> Countermeasures●●●●How to write least privileged codeYou can restrict what code can do regardless of the account used to run the code.You can use code access security to constrain the resources <strong>and</strong> operations thatyour code is allowed to access, either by configuring policy or how you write yourcode. If your code does not need to access a resource or perform a sensitiveoperation such as calling unmanaged code, you can use declarative securityattributes to ensure that your code cannot be granted this permission by anadministrator.For more information, see Chapter 8, “Code Access <strong>Security</strong> in Practice.”How to constrain file I/OYou can use code access security to constrain an assembly’s ability to access areasof the file system <strong>and</strong> perform file I/O. For example, you can constrain a <strong>Web</strong>application so that it can only perform file I/O beneath its virtual directoryhierarchy. You can also constrain file I/O to specific directories. You can do thisprogrammatically or by configuring code access security policy.For more information, see “File I/O” in Chapter 8, “Code Access <strong>Security</strong> inPractice” <strong>and</strong> “Medium Trust” in Chapter 9, “Using Code Access <strong>Security</strong> withASP.NET.” For more information about configuring code access security policy,see “How To: Use Code Access <strong>Security</strong> Policy to Constrain an Assembly” in the“How To” section of this guide.How to prevent SQL injectionUse parameterized stored procedures for data access. The use of parametersensures that input values are checked for type <strong>and</strong> length. Parameters are alsotreated as safe literal values <strong>and</strong> not executable code within the database. If youcannot use stored procedures, use SQL statements with parameters. Do not buildSQL statements by concatenating input values with SQL comm<strong>and</strong>s. Also, ensurethat your application uses a least privileged database login to constrain itscapabilities in the database.For more information about SQL injection <strong>and</strong> for further countermeasures, see“SQL Injection” in Chapter 14, “Building Secure Data Access.”How to prevent cross-site scriptingValidate input for type, length, format, <strong>and</strong> range, <strong>and</strong> encode output. Encodeoutput if it includes input, including <strong>Web</strong> input. For example, encode form fields,query string parameters, cookies <strong>and</strong> so on, <strong>and</strong> encode input read from adatabase (especially a shared database) where you cannot assume the data is safe.For free format input fields that you need to return to the client as HTML, encodethe output <strong>and</strong> then selectively remove the encoding on permitted elements suchas the or tags for formatting.For more information, see “Cross-Site Scripting” in Chapter 10, “BuildingASP.NET Pages <strong>and</strong> Controls.”

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

Saved successfully!

Ooh no, something went wrong!