07.11.2014 Views

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

Enterprise Library Test Guide - Willy .Net

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.

<strong>Test</strong>ing for Security Best Practices 153<br />

Check<br />

Yes<br />

Description<br />

Specify an assembly’s file I/O requirements with declarative security attributes (this<br />

should be SecurityAction.RequestMinimum). For example, the Caching Application<br />

Block’s FileDependecy method calls the EnsureTargetFileAccessible method. This<br />

method demands Read permission to access a file in case it is a protected resource.<br />

The following code example demonstrates this.<br />

[assembly : FileIOPermission(SecurityAction.RequestMinimum)]<br />

In the Caching Application Block, the FileDependency class calls the<br />

EnsureTargetFileAccessible method to demand I/O permission to read a<br />

file.<br />

public FileDependency(string fullFileName)<br />

{<br />

if (Object.Equals(fullFileName, null))<br />

{<br />

throw new ArgumentNullException("fullFileName",<br />

SR.ExceptionNullFileName);<br />

}<br />

if (fullFileName.Length == 0)<br />

{<br />

throw new ArgumentOutOfRangeException("fullFileName",<br />

SR.ExceptionEmptyFileName);<br />

}<br />

dependencyFileName = Path.GetFullPath(fullFileName);<br />

EnsureTargetFileAccessible();<br />

if (!File.Exists(dependencyFileName))<br />

{<br />

throw new ArgumentException(SR.ExceptionInvalidFileName,<br />

"fullFileName");<br />

}<br />

}<br />

this.lastModifiedTime =<br />

File.GetLastWriteTime(fullFileName);<br />

Yes<br />

Yes<br />

private void EnsureTargetFileAccessible()<br />

{<br />

FileIOPermission permission = new<br />

FileIOPermission(FileIOPermissionAccess.Read,<br />

dependencyFileName);<br />

permission.Demand();<br />

}<br />

Use the EnvironmentPermission class to restrict code that accesses environment<br />

variables. This is especially important if untrusted code can call the application block.<br />

Declare environment permission requirements with declarative security attributes (use<br />

SecurityAction.RequestMinimum).

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

Saved successfully!

Ooh no, something went wrong!