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.

Chapter 8: Code Access <strong>Security</strong> in Practice 209Constraining Registry AccessTo constrain code to reading data from specific registry keys, you can use theRegistryPermissionAttribute together with <strong>Security</strong>Action.PermitOnly. Thefollowing attribute ensures that the code can only read from the YourApp key (<strong>and</strong>subkeys) beneath HKEY_LOCAL_MACHINE\SOFTWARE.[RegistryPermissionAttribute(<strong>Security</strong>Action.PermitOnly,Read=@"HKEY_LOCAL_MACHINE\SOFTWARE\YourApp")]public static string GetConfigurationData( string key, string namedValue ){return (string)Registry.LocalMachine.OpenSubKey(key).GetValue(namedValue);}Requesting RegistryPermissionTo document the permission requirements of your code, <strong>and</strong> to ensure your assemblycannot load if it is granted insufficient registry access from code access securitypolicy, add an assembly level RegistryPermissionAttribute with<strong>Security</strong>Action.RequestMinimum as shown in the following example.[assembly: RegistryPermissionAttribute(<strong>Security</strong>Action.RequestMinimum,Read=@"HKEY_LOCAL_MACHINE\SOFTWARE\YourApp")]Data AccessThe ADO.NET SQL Server data provider supports partial trust callers. The other dataproviders including the OLE DB, Oracle, <strong>and</strong> ODBC providers currently require fulltrust callers.If you connect to SQL Server using the SQL Server data provider, your data accesscode requires the SqlClientPermission. You can use SqlClientPermission to restrictthe allowable range of name/value pairs that can be used on a connection stringpassed to the SqlConnection object. In the following code, theCheckProductStockLevel method has been enhanced with an additional securitycheck to ensure that blank passwords cannot be used in the connection string. If thecode retrieves a connection string with a blank password, a <strong>Security</strong>Exception isthrown.

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

Saved successfully!

Ooh no, something went wrong!