CA2108:必須檢查實值型別上的宣告式安全性

型別名稱

ReviewDeclarativeSecurityOnValueTypes

CheckId

CA2108

分類

Microsoft.Security

中斷變更

不中斷

原因

公用或保護的實值型別 (Value Type) 會受到資料存取和模型連結要求的保護。

規則描述

在執行其他建構函式 (Constructor) 之前,會以預設的建構函式配置並初始化實值型別。 如果實值型別受到 Demand 或 LinkDemand 的保護,且呼叫端不具備可以滿足安全性檢查的使用權限,則預設建構函式以外的建構函式將會失敗,並擲回例外狀況。 此時無法取消實值型別的配置,它將保持在預設建構函式所設定的狀態。 請勿假設會傳遞實值型別執行個體的呼叫端具有建立或存取執行個體的使用權限。

如何修正違規

除非從型別移除安全性檢查並使用方法層級的安全性檢查,否則無法修正此規則的違規情形。 請注意,以這種方式修正違規並無法防止不具適當使用權限之呼叫端取得實值型別的執行個體。 您必須確保實值型別的執行個體在預設狀態下不會公開 (Expose) 敏感性資訊,且不會用於有害的用途。

隱藏警告的時機

如果呼叫端在預設狀態下可以取得實值型別的執行個體,而且不會造成安全性威脅,則可以隱藏此規則的警告。

範例

在下列範例中,程式碼會顯示因包含實值型別而違反此規則的程式庫。 請注意,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);
         }
      }
   }
}

這個範例會產生下列輸出。

      

請參閱

概念

連結要求

其他資源

資料存取和模型