次の方法で共有


CA2112: セキュリティで保護された型はフィールドを公開してはなりません

TypeName

SecuredTypesShouldNotExposeFields

CheckId

CA2112

[カテゴリ]

Microsoft.Security

互換性に影響する変更点

あり

原因

パブリック型またはプロテクト型に、パブリック フィールドが含まれ、リンク確認要求で保護されています。

規則の説明

リンク確認要求で保護されている型のインスタンスに対するアクセス権がコードにある場合、その型のフィールドにアクセスするためにリンク確認要求に適合する必要はありません。

違反の修正方法

この規則違反を修正するには、フィールドをパブリック以外にし、フィールド データを返すパブリックのプロパティまたはメソッドを追加します。型に対する LinkDemand セキュリティ チェックで、この型のプロパティおよびメソッドへのアクセス権は保護されます。ただし、コード アクセス セキュリティはフィールドに適用されません。

警告を抑制する状況

セキュリティ問題と適切なデザインの観点から、パブリック フィールドをパブリック以外に変更して違反を修正してください。保護する必要のある情報がフィールドに格納されておらず、フィールドのコンテンツに依存しない場合は、この規則による警告を抑制できます。

使用例

次の例は、保護されていないフィールドを持つライブラリの型 (SecuredTypeWithFields)、ライブラリの型のインスタンスを作成し、型を作成するアクセス許可のない型のインスタンスを誤って渡す可能性がある型 (Distributor)、および型を保護するアクセス許可がないのにインスタンスのフィールドを読み取ることができるアプリケーション コードで構成されます。

この規則に違反するライブラリのコード例を次に示します。

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

namespace SecurityRulesLibrary
{
   // This code requires immediate callers to have full trust.
   [System.Security.Permissions.PermissionSetAttribute(
       System.Security.Permissions.SecurityAction.LinkDemand, 
       Name="FullTrust")]
   public class SecuredTypeWithFields 
   {
      // Even though the type is secured, these fields are not.
      // Violates rule: SecuredTypesShouldNotExposeFields.
      public double xValue;
      public double yValue;

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

安全な型を保護するリンク確認要求があるため、アプリケーションでインスタンスを作成できません。次のクラスを使用すると、安全な型のインスタンスをアプリケーションで取得できます。

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

// This assembly executes with full trust. 

namespace SecurityRulesLibrary
{
   // This type creates and returns instances of the secured type.
   // The GetAnInstance method incorrectly gives the instance 
   // to a type that does not have the link demanded permission.

   public class Distributor
   {
      static SecuredTypeWithFields s = new SecuredTypeWithFields(22,33);
      public static SecuredTypeWithFields GetAnInstance ()
      {
            return s;
      }

      public static void DisplayCachedObject ()
      {
         Console.WriteLine(
            "Cached Object fields: {0}, {1}", s.xValue , s.yValue);
      }
   }
}

次のアプリケーションでは、安全な型のメソッドへのアクセス許可がなくてもコードからフィールドにアクセスできる方法を説明しています。

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

// This code executes with partial trust.
[assembly: System.Security.Permissions.PermissionSetAttribute(
   System.Security.Permissions.SecurityAction.RequestRefuse,
   Name = "FullTrust")]
namespace TestSecurityExamples
{
    public class TestLinkDemandOnField
    {
        [STAThread]
        public static void Main()
        {
            // Get an instance of the protected object.
            SecuredTypeWithFields secureType = Distributor.GetAnInstance();

            // Even though this type does not have full trust,
            // it can directly access the secured type's fields.
            Console.WriteLine(
               "Secured type fields: {0}, {1}",
               secureType.xValue,
               secureType.yValue);
            Console.WriteLine("Changing secured type's field...");
            secureType.xValue = 99;

            // Distributor must call ToString on the secured object.
            Distributor.DisplayCachedObject();

            // If the following line is uncommented, a security 
            // exception is thrown at JIT-compilation time because 
            // of the link demand for full trust that protects 
            // SecuredTypeWithFields.ToString().

            // Console.WriteLine("Secured type {0}",secureType.ToString());
        }
    }
}

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

  
  
  

関連規則

CA1051: 参照できるインスタンス フィールドを宣言しないでください

参照

概念

リンク確認要求

その他の技術情報

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