CA2108: 値型での宣言セキュリティをレビューします
Item | [値] |
---|---|
規則 ID | CA2108 |
カテゴリ | Microsoft.Security |
互換性に影響する変更点 | なし |
原因
パブリックまたは保護されている値の型が、データとモデリングまたはリンク確認要求によってセキュリティで保護されています。
Note
このルールは非推奨とされました。 詳細については、「非推奨の規則」を参照してください。
規則の説明
値の型は、他のコンストラクターが実行される前に、既定のコンストラクターによって割り当てられ、初期化されます。 値の型が Demand または LinkDemand によってセキュリティで保護されていて、呼び出し元にセキュリティ チェックを満たすアクセス許可がない場合、既定以外のコンストラクターは失敗し、セキュリティ例外がスローされます。 値の型の割り当ては解除されません。既定のコンストラクターによって設定された状態のままになります。 値の型のインスタンスを渡す呼び出し元が、インスタンスの作成またはアクセスのためのアクセス許可を持っているとは想定しないでください。
違反の修正方法
型からセキュリティ チェックを削除し、代わりにメソッド レベルのセキュリティ チェックを使用しない限り、この規則の違反を修正することはできません。 この方法で違反を修正しても、不適切なアクセス許可を持つ呼び出し元が、値の型のインスタンスを取得するのを防ぐことにはなりません。 値の型のインスタンスが、その既定の状態で機密情報を公開していないことを確認し、有害な方法でそれを使用できないようにする必要があります。
どのようなときに警告を抑制するか
すべての呼び出し元が、セキュリティに脅威を与えることなく、既定の状態で値の型のインスタンスを取得できる場合、この規則による警告を抑制できます。
例 1
次の例は、この規則に違反する値の型が含まれるライブラリを示しています。 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;
}
}
}
例 2
次のアプリケーションは、このライブラリの弱点を示しています。
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);
}
}
}
}
この例を実行すると、次の出力が生成されます。
Structure custom constructor: Request failed.
New values SecuredTypeStructure 100 100
New values SecuredTypeStructure 200 200
関連項目
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示