次の方法で共有


CA2108: 値型での宣言セキュリティを確認します

TypeName

ReviewDeclarativeSecurityOnValueTypes

CheckId

CA2108

分類

Microsoft.Security

互換性に影響する変更点

なし

原因

パブリックまたはプロテクトの値型が、.NET Framework におけるデータとモデリングまたはリンク確認要求で保護されています。

規則の説明

値型は、既定のコンストラクターが割り当てて初期化してから、別のコンストラクターが実行します。値型が Demand または LinkDemand によって保護され、呼び出し元にそのセキュリティ チェックに適合するアクセス許可がない場合、既定以外のコンストラクターは失敗し、セキュリティ例外がスローされます。値型の領域は解放されません。既定のコンストラクターで設定された状態のままになります。値型のインスタンスを渡す呼び出し元に、そのインスタンスの作成またはアクセスの許可があると仮定しないでください。

違反の修正方法

この規則違反を修正するには、型からセキュリティ チェックを取り除き、代わりにメソッド レベルのセキュリティ チェックを使用する必要があります。この方法で違反を修正すると、適切なアクセス許可を持たない呼び出し元が、その値型のインスタンスを取得することを防ぐことはできません。既定の状態にある値型のインスタンスによって機密情報が公開されないこと、不正な方法でそのインスタンスを使用できないことを確認します。

警告を抑制する状況

呼び出し元が既定の状態にある値型のインスタンスを取得するときに、セキュリティ上の脅威がない場合は、この規則による警告を抑制できます。

使用例

この規則に違反する値型を含むライブラリの例を次に示します。StructureManager 型では、その値型のインスタンスを渡す呼び出し元に、そのインスタンスの作成またはアクセスの許可があると仮定しています。

using System;
using System.Security;
using System.Security.Permissions;

[assembly:AllowPartiallyTrustedCallers]  

namespace SecurityRulesLibrary
{
   // Violates rule: ReviewDeclarativeSecurityOnValueTypes.
   [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Demand, Name="FullTrust")]

   public struct SecuredTypeStructure 
   {
      internal double xValue;
      internal double yValue;

      public SecuredTypeStructure(double x, double y) 
      {
         xValue = x;
         yValue = y;
         Console.WriteLine("Creating an instance of SecuredTypeStructure.");
      }     
      public override string ToString()
      {
         return String.Format ("SecuredTypeStructure {0} {1}", xValue, yValue);
      }
   }

   public class StructureManager
   {
      // This method asserts trust, incorrectly assuming that the caller must have  
      // permission because they passed in instance of the value type.
      [System.Security.Permissions.PermissionSetAttribute(System.Security.Permissions.SecurityAction.Assert, Name="FullTrust")]

      public static SecuredTypeStructure AddStepValue(SecuredTypeStructure aStructure)
      {
         aStructure.xValue += 100;
         aStructure.yValue += 100;
         Console.WriteLine ("New values {0}", aStructure.ToString());
         return aStructure;
      }
   }
}

ライブラリの弱点を説明するアプリケーション例を次に示します。

using System;
using System.Runtime.InteropServices;
using System.Security;
using System.Security.Permissions;
using SecurityRulesLibrary;

// Run this test code with partial trust.
[assembly: System.Security.Permissions.PermissionSetAttribute(
   System.Security.Permissions.SecurityAction.RequestRefuse, 
   Name="FullTrust")]

namespace TestSecurityExamples
{
   public class TestDemandOnValueType
   {
      static SecuredTypeStructure mystruct;

      [STAThread]
      public static void Main() 
      {
         try
         {
            mystruct = new SecuredTypeStructure(10,10);

         }
         catch (SecurityException e)
         {
            Console.WriteLine(
               "Structure custom constructor: {0}", e.Message);
         }

         // The call to the default constructor  
         // does not throw an exception. 
         try 
         {
            mystruct = StructureManager.AddStepValue(
               new SecuredTypeStructure());
         }
         catch (SecurityException e)
         {
            Console.WriteLine(
               "Structure default constructor: {0}", e.Message);
         }

         try 
         {
            StructureManager.AddStepValue(mystruct);
         } 

         catch (Exception e)
         {  
            Console.WriteLine(
               "StructureManager add step: {0}", e.Message);
         }
      }
   }
}

この例を実行すると、次の出力が生成されます。

  

参照

概念

リンク確認要求

その他の技術情報

.NET Framework におけるデータとモデリング