次の方法で共有


CA2122: リンク確認要求で間接的にメソッドを公開しないでください

TypeName

DoNotIndirectlyExposeMethodsWithLinkDemands

CheckId

CA2122

[カテゴリ]

Microsoft.Security

互換性に影響する変更点

なし

原因

パブリック メンバーまたはプロテクト メンバーはリンク確認要求を含み、セキュリティ チェックを実行しないメンバーから呼び出されています。

規則の説明

リンク確認要求では、直接の呼び出し元のアクセス許可しかチェックされません。メンバー X によって呼び出し元のセキュリティ要求が作成されず、リンク確認要求によって保護されたコードが呼び出される場合、必要なアクセス許可を持たない呼び出し元でも、X を使用して保護されたメンバーにアクセスできます。

違反の修正方法

リンク確認要求で保護されたメンバーに不正にアクセスできないように、セキュリティの.NET Framework におけるデータとモデリングまたはリンク確認要求をメンバーに追加します。

警告を抑制する状況

この規則による警告を安全に抑制するには、破壊的な方法で使用できる操作またはリソースに対するアクセス権を呼び出し元に付与しないようにします。

使用例

規則に違反するライブラリと、ライブラリの弱点を説明するアプリケーションを次の例に示します。サンプル ライブラリには、規則に違反する 2 つのメソッドがあります。EnvironmentSetting メソッドは、環境変数へのアクセスを無制限にするリンク確認要求によって保護されています。DomainInformation メソッドは、EnvironmentSetting を呼び出す前に、呼び出し元のセキュリティ要求を作成しません。

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

namespace SecurityRulesLibrary
{
   public class DoNotIndirectlyExposeMethodsWithLinkDemands
   {
      // Violates rule: DoNotIndirectlyExposeMethodsWithLinkDemands.
      public static string DomainInformation()
      {
         return EnvironmentSetting("USERDNSDOMAIN");
      }

      // Library method with link demand.
      // This method holds its immediate callers responsible for securing the information.
      // Because a caller must have unrestricted permission, the method asserts read permission
      // in case some caller in the stack does not have this permission. 

      [EnvironmentPermissionAttribute(SecurityAction.LinkDemand, Unrestricted=true)]
      public static string EnvironmentSetting(string environmentVariable)
      {
         EnvironmentPermission envPermission = new EnvironmentPermission( EnvironmentPermissionAccess.Read,environmentVariable);
         envPermission.Assert();

         return Environment.GetEnvironmentVariable(environmentVariable);
      }
   }
}

次のアプリケーションでは、保護されていないライブラリ メンバーを呼び出します。

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

// You have no permission to access the sensitive information,
// but you will get data from the unprotected method.
[assembly:EnvironmentPermissionAttribute(
   SecurityAction.RequestRefuse,Unrestricted=true)]
namespace TestUnsecuredMembers
{
   class TestUnsecured
   {
      [STAThread]
      static void Main(string[] args)
      {
         string value = null;
         try 
         {
            value = DoNotIndirectlyExposeMethodsWithLinkDemands.DomainInformation();
         }
         catch (SecurityException e) 
         {
            Console.WriteLine(
               "Call to unsecured member was stopped by code access security! {0}",
               e.Message);
            throw;
         }
         if (value != null) 
         {
            Console.WriteLine("Value from unsecured member: {0}", value);
         }
      }
   }
}

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

  

参照

概念

リンク確認要求

その他の技術情報

安全なコーディングのガイドライン

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