CS.SV.LINK_DEMAND.TRANSP

透過的なコードは LinkDemands で保護してはなりません。

この規則は、アクセスするために LinkDemands が必要な透過的なメソッドで発動します。セキュリティが透過的なコードは、演算のセキュリティを検証する責任を負うべきではないため、許可を要求してはいけません。透過的なメソッドは、セキュリティ的に中立であると想定されているため、セキュリティに関する決定を行うべきではありません。さらに、セキュリティに関する決定を行っているセーフクリティカルなコードは、以前にそのような決定を行った透過的なコードに依存するべきではありません。

軽減と防止

この規則の違反を修正するには、透過的なメソッドでリンク要求を削除するか、メソッドがセキュリティ要求などのセキュリティチェックを実行している場合は、SecuritySafeCriticalAttribute 属性でそのメソッドをマークします。

脆弱コード例

コピー
  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()
         {
         }
     }
 }

この例では、メソッドは透過的であり、LinkDemand を含む LinkDemand PermissionSet でマークされているため、規則はそのメソッドで発動します。