CS.SV.LINK_DEMAND.TRANSP

Transparent code should not be protected with LinkDemands.

This rule fires on transparent methods which require LinkDemands to access them. Security transparent code should not be responsible for verifying the security of an operation, and therefore should not demand permissions. Because transparent methods are supposed to be security neutral, they should not be making any security decisions. Additionally, safe critical code, which does make security decisions, should not be relying on transparent code to have previously made such a decision.

Mitigation and prevention

To fix a violation of this rule, remove the link demand on the transparent method or mark the method with SecuritySafeCriticalAttribute attribute if it is performing security checks, such as security demands.

Vulnerable code example

Copy
  using System;
  using System.Security.Permissions;
  
  namespace TransparencyWarningsDemo
  {
 
      public class TransparentMethodsProtectedWithLinkDemandsClass
      {
         // CA2142 violation - transparent code using a LinkDemand.  This can be fixed by removing the LinkDemand 
         // from the method.
         [PermissionSet(SecurityAction.LinkDemand, Unrestricted = true)]
         public void TransparentMethod()
         {
         }
     }
 }

In this example, the rule fires on the method because the method is transparent and is marked with a LinkDemand PermissionSet that contains an LinkDemand.