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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

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

Check<br />

Yes<br />

Yes<br />

Description<br />

Do not expose methods protected by a LinkDemand security check. Some method calls<br />

in the .NET Framework are annotated with a LinkDemand. If they are called from within<br />

an application block’s methods, the calling code is not checked for any security permissions.<br />

An example of this is the Logging Application Block’s CollectIntrinsicProperties<br />

method. This method calls the AppDomain.CurrentDomain property, which issues a link<br />

demand to unmanaged code. Because of this, it is necessary to protect the CollectIntrinsicProperties<br />

method with the SecurityPermissionFlag.UnmanagedCode enumeration.<br />

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermission-<br />

Flag.UnmanagedCode)]<br />

private void CollectIntrinsicProperties()<br />

{<br />

this.TimeStamp = DateTime.UtcNow;<br />

this.ActivityId = Trace.CorrelationManager.ActivityId;<br />

try<br />

{<br />

MachineName = Environment.MachineName;<br />

}<br />

catch (Exception e)<br />

{<br />

this.MachineName =<br />

String.Format(Properties.Resources.Culture,<br />

Properties.Resources.IntrinsicPropertyError, e.Message);<br />

}<br />

try<br />

{<br />

//AppDomain.CurentDomain issues a link demand to<br />

//unamanaged code. Link demands only check the immediate<br />

//caller. (In this case, this is the<br />

// CollectIntrinsicProperties method.)<br />

appDomainName = AppDomain.CurrentDomain.FriendlyName;<br />

None of the application block’s methods should include Assert statements or<br />

LinkDemand security checks.

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

Saved successfully!

Ooh no, something went wrong!