CA2151: クリティカル型のフィールドはセキュリティ クリティカルである必要があります
TypeName |
|
CheckId |
CA2151 |
カテゴリ |
Microsoft.Security |
互換性に影響する変更点 |
あり |
原因
透過的セキュリティ フィールドまたはセーフ クリティカル フィールドが宣言されました。その型は、セキュリティ クリティカルとして指定されています。たとえば、次のようにします。
[assembly: AllowPartiallyTrustedCallers]
[SecurityCritical]
class Type1 { } // Security Critical type
class Type2 // Security transparent type
{
Type1 m_field; // CA2151, transparent field of critical type
}
この例では、m_field はセキュリティ クリティカルな型の透過的セキュリティ フィールドです。
規則の説明
セキュリティ クリティカルな型を使用するには、型を参照するコードがセキュリティ クリティカルであるか、セキュリティ セーフ クリティカルである必要があります。これは、参照が間接的である場合にも当てはまります。たとえば、クリティカルな型を持つ透過的フィールドを参照する場合、コードはセキュリティ クリティカルであるか、セキュリティ セーフである必要があります。そのため、透過的セキュリティまたはセキュリティ セーフ クリティカルなフィールドが存在すると、透過的なコードはこのフィールドにアクセスできないので、紛らわしくなります。
違反の修正方法
この規則違反を修正するには、フィールドを SecurityCriticalAttribute 属性でマークするか、フィールドによって参照される型を透過的セキュリティまたはセーフ クリティカルにします。
// Fix 1: Make the referencing field security critical
[assembly: AllowPartiallyTrustedCallers]
[SecurityCritical]
class Type1 { } // Security Critical type
class Type2 // Security transparent type
{
[SecurityCritical]
Type1 m_field; // Fixed: critical type, critical field
}
// Fix 2: Make the referencing field security critical
[assembly: AllowPartiallyTrustedCallers]
class Type1 { } // Type1 is now transparent
class Type2 // Security transparent type
{
[SecurityCritical]
Type1 m_field; // Fixed: critical type, critical field
}
警告を抑制する状況
この規則による警告は抑制しないでください。
コード
using System;
using System.Runtime.InteropServices;
using System.Security;
namespace TransparencyWarningsDemo
{
public class SafeNativeMethods
{
// CA2145 violation - transparent method marked SuppressUnmanagedCodeSecurity. This should be fixed by
// marking this method SecurityCritical.
[DllImport("kernel32.dll", SetLastError = true)]
[SuppressUnmanagedCodeSecurity]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool Beep(uint dwFreq, uint dwDuration);
}
}