CS.SV.SER_CTOR

セキュアなシリアル化コンストラクター。

この規則は、カスタムのシリアル化をサポートしている型に関連しています。型が System.Runtime.Serialization.ISerializable インターフェイスを実装している場合、その型はカスタムのシリアル化をサポートします。シリアル化コンストラクターは必要であり、ISerializable.GetObjectData メソッドを使用してシリアル化されたオブジェクトを逆シリアル化または再作成するために使用されます。シリアル化コンストラクターはオブジェクトを割り当てて初期化するため、通常のコンストラクターに存在するセキュリティチェックも、シリアル化コンストラクターに存在する必要があります。この規則に違反すると、そうする以外にインスタンスを作成できなかった呼び出し元は、シリアル化コンストラクターを使用してこれを行うことができます。

軽減と防止

この規則の違反を修正するには、他のコンストラクターを保護するものと同一のセキュリティ要求で、シリアル化コンストラクターを保護します。

脆弱コード例

コピー
 using System;
 using System.IO;
 using System.Runtime.Serialization;
 using System.Runtime.Serialization.Formatters.Binary;
 using System.Security;
 using System.Security.Permissions;
 
 [assembly: AllowPartiallyTrustedCallersAttribute()]
 namespace SecurityRulesLibrary
 {   
     [Serializable]
     public class SerializationConstructorsRequireSecurity : ISerializable 
     {
         private   int n1;
         // This is a regular constructor secured by a demand.
         [FileIOPermissionAttribute(SecurityAction.Demand, Unrestricted = true)]
         public SerializationConstructorsRequireSecurity ()
         {
            n1 = -1;
         }
         // This is the serialization constructor. 
         // Violates rule: SecureSerializationConstructors. 
         protected SerializationConstructorsRequireSecurity (SerializationInfo info, StreamingContext context)
         {
            n1 = (int) info.GetValue("n1", typeof(int));
         }
         void ISerializable.GetObjectData(SerializationInfo info, StreamingContext context)
         {
            info.AddValue("n1", n1);
         }
     }
 
  }

この例は、規則に違反している型を示しています。