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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 8: Code Access <strong>Security</strong> in Practice 203For example:[<strong>Security</strong>Permission(<strong>Security</strong>Action.LinkDem<strong>and</strong>,Flags=<strong>Security</strong>PermissionFlag.ControlPrincipal)]public struct SomeStruct{// This explicit constructor is protected by the link dem<strong>and</strong>public SomeStruct(int i){field = i;}private int field;}The following two lines of code both result in a new structure with the fieldinitialized to zero. However, only the first line that uses the explicit constructor issubject to a link dem<strong>and</strong>.SomeStruct s = new SomeStruct(0);SomeStruct s = new SomeStruct();The second line is not subject to a link dem<strong>and</strong> because a default constructor is notgenerated. If this were a class instead of a structure, the compiler would generate adefault constructor annotated with the specified link dem<strong>and</strong>.Virtual Methods <strong>and</strong> Link Dem<strong>and</strong>sIf you use link dem<strong>and</strong> to protect a method override in a derived class, make sureyou also put it on the corresponding virtual base class method. Otherwise, if the JITcompiler sees a reference to the base class type where no link dem<strong>and</strong> is present, nolink dem<strong>and</strong> is performed.Assert <strong>and</strong> RevertAssertYou can call the CodeAccessPermission.Assert method to prevent a dem<strong>and</strong>propagating beyond the current stack frame. By using Assert, you vouch for thetrustworthiness of your code’s callers. Because of the potential for luring attacks,Assert needs to be used with caution.Asserts are most often used to s<strong>and</strong>box privileged code. If you develop code that callsAssert, you need to ensure that there are alternate security measures in place toauthorize the calling code. The following recommendations help you to minimize therisks.● Use the dem<strong>and</strong> / assert pattern● Reduce the Assert duration

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

Saved successfully!

Ooh no, something went wrong!