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.

242 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>s3. Wrap database calls with an Assert statement to assert full trust. Wrap a matchingRevertAssert call to reverse the effect of the assert. Although not strictly necessary,it is a good practice to place the call to RevertAssert in a finally block.Because the OLE DB provider dem<strong>and</strong>s full trust, the wrapper must assertfull-trust. Asserting an OleDbPermission is not sufficient. Step 7 explains howto improve the security of using CodeAccessPermission.Assert.public OleDbDataReader GetProductList(){try{// Assert full trust (the unrestricted permission set)new PermissionSet(PermissionState.Unrestricted).Assert();OleDbConnection conn = new OleDbConnection("Provider=SQLOLEDB; Data Source=(local);" +"Integrated <strong>Security</strong>=SSPI; Initial Catalog=Northwind");OleDbComm<strong>and</strong> cmd = new OleDbComm<strong>and</strong>("spRetrieveProducts", conn);cmd.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.StoredProcedure;conn.Open();OleDbDataReader reader =cmd.ExecuteReader(Comm<strong>and</strong>Behavior.CloseConnection);return reader;}catch(OleDbException dbex){// Log <strong>and</strong> h<strong>and</strong>le exception}catch(Exception ex){// Log <strong>and</strong> h<strong>and</strong>le exception}finally{CodeAccessPermission.RevertAssert();}return null;}4. Build the assembly <strong>and</strong> install it in the GAC with the following comm<strong>and</strong>:gacutil -i oledbwrapper.dllTo ensure that the assembly is added to the GAC after each subsequent rebuild,add the following post build event comm<strong>and</strong> line (available from the project’sproperties in Visual Studio.NET) to your wrapper assembly project:"C:\Program Files\Microsoft Visual Studio .NET 2003\SDK\v1.1\Bin\gacutil.exe"/i $(TargetPath)

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

Saved successfully!

Ooh no, something went wrong!