CA2108:检查有关值类型的声明性安全

类型名

ReviewDeclarativeSecurityOnValueTypes

CheckId

CA2108

类别

Microsoft.Security

是否重大更改

原因

公共或受保护值类型受 数据访问和建模链接需求 保护。

规则说明

在其他构造函数执行之前,值类型的默认构造函数会分配并初始化值类型。 如果值类型受到 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);
         }
      }
   }
}

该示例产生下面的输出。

      

请参见

概念

链接需求

其他资源

数据访问和建模