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 199Consider Protecting Cached DataIf you access a resource by using one of the .NET Framework classes, a permissiondem<strong>and</strong> appropriate for the resource type in question is issued by the class. If yousubsequently cache data for performance reasons, you should consider issuing anexplicit code access permission dem<strong>and</strong> prior to accessing the cached data. Thisensures the calling code is authorized to access the specific type of resource. Forexample, if you read data from a file <strong>and</strong> then cache it, <strong>and</strong> you want to ensure thatcalling code is authorized, issue a FileIOPermission dem<strong>and</strong> as shown in thefollowing example.// The following dem<strong>and</strong> assumes the cached data was originally retrieved from// C:\SomeDir\SomeFile.datnew FileIOPermission(FileIOPermissionAccess.Read,@"C:\SomeDir\SomeFile.dat").Dem<strong>and</strong>();// Now access the cache <strong>and</strong> return the data to the callerProtect Custom Resources with Custom PermissionsIf you expose a resource or operation by using unmanaged code, you should s<strong>and</strong>boxyour wrapper code <strong>and</strong> consider dem<strong>and</strong>ing a custom permission to authorize thecalling code.Full trust callers are granted the custom permission automatically as long as thepermission type implements the IUnrestrictedPermission interface. Partial trustcallers will not have the permission unless it has been specifically granted by codeaccess security policy. This ensures that non-trusted code cannot call your assemblyto access the custom resources that it exposes. S<strong>and</strong>boxing also means that you arenot forced to grant the powerful UnmanagedCodePermission to any code that needsto call your code.For more information about calling unmanaged code, see the “Unmanaged Code”section later in this chapter. For an example implementation of a custom permission,see “How To: Create a Custom Encryption Permission” in the “How To” section ofthis guide.Link Dem<strong>and</strong>sA link dem<strong>and</strong> differs from a regular permission dem<strong>and</strong> in that the run-timedem<strong>and</strong>s permissions only from the immediate caller <strong>and</strong> does not perform a fullstack walk. Link dem<strong>and</strong>s are performed at JIT compilation time <strong>and</strong> can only bespecified declaratively.

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

Saved successfully!

Ooh no, something went wrong!