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.

270 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>s(continued)}}// Validate the supplied nameif ( !RegEx.Match(Request.Form["name"],@"[A-Za-z'\- ]",RegexOptions.None).Success){// Invalid name}Validating Input Used for Data AccessIf you are generating dynamic SQL queries based on user input, a SQL injectionattack can inject malicious SQL comm<strong>and</strong>s that can be executed by the database. In atypical <strong>Web</strong>-based data access scenario, the following defense in depth strategy canbe used:● Use regular expressions to constrain input within your page class.●●Sanitize or reject input. For defense in depth, you can choose to use a helpermethod to strip null characters or other known bad characters.Use parameterized stored procedures for data access to ensure that type <strong>and</strong>length checks are performed on the data used in SQL queries.For more information about using parameters for data access <strong>and</strong> about writingsecure data access code, see Chapter 14, “Building Secure Data Access.”Validating Input Used For File I/OIn general, you should avoid writing code that accepts file input or path input fromthe caller. Instead, use fixed file names <strong>and</strong> locations when reading <strong>and</strong> writing data.This ensures that your code cannot be coerced into accessing arbitrary files. It alsoensures that your code is not vulnerable to canonicalization bugs.If you do need to accept input file names, there are two main challenges. First, is theresulting file path <strong>and</strong> name a valid file system name? Second, is the path valid in thecontext of your application? For example, is it beneath the application’s virtualdirectory root?To canonicalize the file name, use System.IO.Path.GetFullPath. To check that the filepath is valid in the context of your application, you can use .NET code access securityto grant the precise FileIOPermission to your code so that is able to access only filesfrom specific directories. For more information, see the “File I/O” sections inChapter 7, “Building Secure Assemblies” <strong>and</strong> Chapter 8, “Code Access <strong>Security</strong> inPractice.”

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

Saved successfully!

Ooh no, something went wrong!