Share via


CA2119:密封满足私有接口的方法

类型名

SealMethodsThatSatisfyPrivateInterfaces

CheckId

CA2119

类别

Microsoft.Security

是否重大更改

原因

可继承的公共类型为 internal(在 Visual Basic 中为 Friend)接口提供可重写的方法实现。

规则说明

接口方法具有公共可访问性,不能通过该实现类型来更改。 内部接口会创建一个不应在定义该接口的程序集外部实现的协定。 使用 virtual(在 Visual Basic 中为 Overridable)修饰符实现内部接口的方法的公共类型允许程序集外部的派生类型重写该方法。 如果定义程序集中的第二个类型调用该方法,并期望创建一个纯内部协定,则当执行在外部程序集中重写的方法时,可能会对行为的安全性造成威胁。 这会产生安全漏洞。

如何解决冲突

若要修复与该规则的冲突,请使用下面的方法之一防止该方法在程序集外部被重写:

  • 使该声明类型成为 sealed(在 Visual Basic 中为 NotInheritable)。

  • 将声明类型的可访问性更改为 internal(在 Visual Basic 中为 Friend)。

  • 从该声明类型中移除所有公共构造函数。

  • 在不使用 virtual 修饰符的情况下实现该方法。

  • 显式实现该方法。

何时禁止显示警告

经认真检查后,认定如果该方法在程序集外部被重写,并不存在可能被利用的安全问题,则可以安全地禁止显示此规则发出的警告。

示例

下面的示例演示了一个违反该规则的类型 BaseImplementation。

Imports System

Namespace SecurityLibrary

   Interface IValidate
      Function UserIsValidated() As Boolean
   End Interface

   Public Class BaseImplementation
      Implements IValidate

      Overridable Function UserIsValidated() As Boolean _ 
         Implements IValidate.UserIsValidated
         Return False
      End Function

   End Class

   Public Class UseBaseImplementation

      Sub SecurityDecision(someImplementation As BaseImplementation)

         If(someImplementation.UserIsValidated() = True)
            Console.WriteLine("Account number & balance.")
         Else
            Console.WriteLine("Please login.")
         End If

      End Sub

   End Class

End Namespace
using System;

namespace SecurityLibrary
{
   // Internal by default.
   interface IValidate
   {
      bool UserIsValidated();
   }

   public class BaseImplementation : IValidate
   {
      public virtual bool UserIsValidated()
      {
         return false;
      }
   }

   public class UseBaseImplementation
   {
      public void SecurityDecision(BaseImplementation someImplementation)
      {
         if(someImplementation.UserIsValidated() == true)
         {
            Console.WriteLine("Account number & balance.");
         }
         else
         {
            Console.WriteLine("Please login.");
         }
      }
   }
}
using namespace System;

namespace SecurityLibrary
{
   // Internal by default.
   interface class IValidate
   {
      bool UserIsValidated();
   };

   public ref class BaseImplementation : public IValidate
   {
   public:
      virtual bool UserIsValidated()
      {
         return false;
      }
   };

   public ref class UseBaseImplementation
   {
   public:
      void SecurityDecision(BaseImplementation^ someImplementation)
      {
         if(someImplementation->UserIsValidated() == true)
         {
            Console::WriteLine("Account number & balance.");
         }
         else
         {
            Console::WriteLine("Please login.");
         }
      }
   };
}

下面的示例利用上面示例的虚方法实现。

Imports System

Namespace SecurityLibrary

   Public Class BaseImplementation

      Overridable Function UserIsValidated() As Boolean
         Return False
      End Function

   End Class

   Public Class UseBaseImplementation

      Sub SecurityDecision(someImplementation As BaseImplementation)

         If(someImplementation.UserIsValidated() = True)
            Console.WriteLine("Account number & balance.")
         Else
            Console.WriteLine("Please login.")
         End If

      End Sub

   End Class

End Namespace
using System;

namespace SecurityLibrary
{
    public class BaseImplementation 
    {
        public virtual bool UserIsValidated()
        {
            return false;
        }
    }

    public class UseBaseImplementation
    {
        public void SecurityDecision(BaseImplementation someImplementation)
        {
            if (someImplementation.UserIsValidated() == true)
            {
                Console.WriteLine("Account number & balance.");
            }
            else
            {
                Console.WriteLine("Please login.");
            }
        }
    }
}
using namespace System;

namespace SecurityLibrary
{
   public ref class BaseImplementation
   {
   public:
      virtual bool UserIsValidated()
      {
         return false;
      }
   };

   public ref class UseBaseImplementation
   {
   public:
      void SecurityDecision(BaseImplementation^ someImplementation)
      {
         if(someImplementation->UserIsValidated() == true)
         {
            Console::WriteLine("Account number & balance.");
         }
         else
         {
            Console::WriteLine("Please login.");
         }
      }
   };
}

请参见

参考

接口(C# 编程指南)

其他资源

接口 (Visual Basic)