共用方式為


CA2116:APTCA 方法應該只呼叫 APTCA 方法

型別名稱

AptcaMethodsShouldOnlyCallAptcaMethods

CheckId

CA2116

分類

Microsoft.Security

中斷變更

中斷

原因

在含有 System.Security.AllowPartiallyTrustedCallersAttribute 屬性 (Attribute) 之組件中的方法,會呼叫不具有屬性之組件中的方法。

規則描述

根據預設,具有強式名稱 (Strong Name) 之組件中之公用或保護的方法會由連結要求隱含保護以達到完全信任,只有完全信任的呼叫端可以存取強式名稱組件。 以 AllowPartiallyTrustedCallersAttribute (APTCA) 屬性標記的強式名稱組件則沒有這種保護。 屬性會停用連結要求,讓沒有完全信任的呼叫端可以存取組件,例如從內部網路或網際網路執行的程式碼。

當完全信任的組件中出現 APTCA 屬性,並且組件在不允許部分信任呼叫端的其他組件中執行程式碼時,可能需要利用到安全性。 如果這兩個方法 M1 和 M2 符合下列條件,惡意呼叫端可以使用方法 M1 略過保護 M2 的隱含完全信任連結要求:

  • M1 是公用方法,會在具有 APTCA 屬性的完全信任組件中宣告。

  • M1 會在 M1 的組件外呼叫方法 M2。

  • M2 的組件沒有 APTCA 屬性,因此不應該由部分信任的呼叫端執行或代表呼叫端。

部分信任的呼叫端 X 可以呼叫方法 M1,因此使 M1 呼叫 M2。 由於 M2 沒有 APTCA 屬性,它的立即呼叫端 (M1) 必須滿足連結要求才會有完全信任。M1 具有完全信任因此會滿足這項檢查。 安全性風險則是因為 X 沒有滿足連接需求,無法保護 M2 不會受到不信任的呼叫端攻擊。 因此,具有 APTCA 屬性的方法絕對不可呼叫不具有屬性的方法。

如何修正違規

如果需要 APCTA 屬性,請使用要求來保護呼叫至完全信任組件中的方法。 您要求的精確使用權限將視方法所公開 (Expose) 的功能而定。 可能的話,請以完全信任需求保護方法,以確定基礎功能未對部分信任的呼叫端公開。 如果不可能這樣做,請選取可有效保護公開之功能的一組使用權限。 如需要求的詳細資訊,請參閱要求

隱藏警告的時機

若要放心地隱藏這項規則的警告,您必須確定方法所公開的功能不會直接或間接允許呼叫端存取可能遭到惡意使用的機感資訊、作業或資源。

範例

下列範例會使用兩個組件與一個測試應用程式,說明這項規則所偵測的安全性弱點。 第一個組件沒有 APTCA 屬性且不應該讓部分信任的呼叫端存取 (在前面討論中是以 M2 表示)。

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

// This code is compiled into a strong-named
// assembly that requires full trust and does 
// not allow partially trusted callers. 

namespace AptcaTestLibrary
{
   public class ClassRequiringFullTrust
   {
      public static void DoWork()
      {
        Console.WriteLine("ClassRequiringFullTrust.DoWork was called.");
      }
   }
}

第二個組件是完全信任並允許部分信任的呼叫端 (在前面討論中是以 M1 表示)。

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

// This assembly executes with full trust and 
// allows partially trusted callers. 

[assembly:AllowPartiallyTrustedCallers]  

namespace AptcaTestLibrary
{
   public class AccessAClassRequiringFullTrust
   {
      public static void Access()
      {    
         // This security check fails if the caller 
         // does not have full trust. 
         NamedPermissionSet pset= new NamedPermissionSet("FullTrust");

         // This try-catch block shows the caller's permissions.
         // Correct code would either not catch the exception,
         // or would rethrow it.
         try 
         {
            pset.Demand();
         }
         catch (SecurityException e)
         {
            Console.WriteLine("Demand for full trust:{0}", e.Message);
         }
         // Call the type that requires full trust.
         // Violates rule AptcaMethodsShouldOnlyCallAptcaMethods.
         ClassRequiringFullTrust.DoWork();
     }
   }
}

測試應用程式 (在前面討論中是以 X 表示) 為部分信任。

using System;
using AptcaTestLibrary;

// If this test is run from the local computer, it gets full trust by default.
// Remove full trust.
[assembly:System.Security.Permissions.PermissionSetAttribute(
   System.Security.Permissions.SecurityAction.RequestRefuse, Name="FullTrust")]

namespace TestSecLibrary
{
   class TestApctaMethodRule
   {
      public static void Main()
      {
          // Indirectly calls DoWork in the full-trust class.
          ClassRequiringFullTrust testClass = new ClassRequiringFullTrust();
          testClass.Access();
      }
   }
}

這個範例產生下列輸出。

  
  

相關規則

CA2117:APTCA 型別應該只擴充 APTCA 基底型別

請參閱

概念

部分信任程式碼可呼叫的 .NET Framework 組件

從部分受信任程式碼使用程式庫

要求

連結要求

其他資源

安全程式碼撰寫方針

.NET Framework 中的資料與模型化