大多数代码访问安全 API 已过时

.NET 中大多数与代码访问安全性 (CAS) 相关的类型均已过时,并作为警告显示。 这包括 CAS 属性,例如 SecurityPermissionAttributeCAS 权限对象,例如 SocketPermissionEvidenceBase派生类型和其他支持 API。

更改描述

在 .NET Framework 2.x - 4.x 中,CAS 属性和 API 可能会影响代码执行过程,包括确保 CAS 需求堆栈成功或失败。

// In .NET Framework, the attribute causes CAS stack walks
// to terminate successfully when this permission is demanded.
[SocketPermission(SecurityAction.Assert, Host = "contoso.com", Port = "443")]
public void DoSomething()
{
    // open a socket to contoso.com:443
}

在 .NET Core 2.x - 3.x 中,运行时不遵循 CAS 属性或 CAS API。 运行时忽略方法条目上的属性,大多数编程 API 都不起作用。

// The .NET Core runtime ignores the following attribute.
[SocketPermission(SecurityAction.Assert, Host = "contoso.com", Port = "443")]
public void DoSomething()
{
    // open a socket to contoso.com:443
}

此外,对扩展 API(Assert) 的编程调用始终成功,而对限制性 API (DenyPermitOnly) 的编程调用始终在运行时引发异常。 (PrincipalPermission 是此规则的例外。请参阅下面的 “建议作 ”部分。

public void DoAssert()
{
    // The line below has no effect at run time.
    new SocketPermission(PermissionState.Unrestricted).Assert();
}

public void DoDeny()
{
    // The line below throws PlatformNotSupportedException at run time.
    new SocketPermission(PermissionState.Unrestricted).Deny();
}

在 .NET 5 及更高版本中,大多数与 CAS 相关的 API 已过时,并生成编译时警告 SYSLIB0003

[SocketPermission(SecurityAction.Assert, Host = "contoso.com", Port = "443")] // warning SYSLIB0003
public void DoSomething()
{
    new SocketPermission(PermissionState.Unrestricted).Assert(); // warning SYSLIB0003
    new SocketPermission(PermissionState.Unrestricted).Deny(); // warning SYSLIB0003
}

这仅是编译时的更改。 当前版本的 .NET Core 与以前版本相比没有运行时更改。 在 .NET Core 2.x - 3.x 中不执行任何操作的方法将在 .NET 5 及更高版本的运行时继续不执行操作。 在 .NET Core 2.x - 3.x 中引发 PlatformNotSupportedException 的方法将继续在 .NET 5 及更高版本中的运行时引发 PlatformNotSupportedException

更改原因

代码访问安全性(CAS) 是一种不支持的旧技术。 启用 CAS 的基础结构仅存在于 .NET Framework 2.x - 4.x 中,但已弃用,未接收服务或安全修补程序。

由于 CAS 的弃用,支持基础结构未引入 .NET Core 或 .NET 5+。 但是,API 已提出,以便应用可以针对 .NET Framework 和 .NET Core 进行交叉编译。 这导致了“失败开启”情况,其中存在一些与 CAS 相关的 API,可以调用,但在运行时不执行任何操作。 这一情况可能导致那些依赖运行时遵循 CAS 相关属性或程序化 API 调用的组件出现安全问题。 为了更好地传达运行时不尊重这些属性或 API,我们在 .NET 5.0 中已过时大部分属性。

已引入的版本

5.0

  • 如果要断言任何安全权限,请删除声明权限的属性或调用。

    // REMOVE the attribute below.
    [SecurityPermission(SecurityAction.Assert, ControlThread = true)]
    public void DoSomething()
    {
    }
    
    public void DoAssert()
    {
        // REMOVE the line below.
        new SecurityPermission(SecurityPermissionFlag.ControlThread).Assert();
    }
    
  • 如果拒绝或限制(通过 PermitOnly)任何权限,请联系安全顾问。 由于 .NET 5+ 运行时不遵循 CAS 属性,因此如果应用程序错误地依赖 CAS 基础结构来限制对这些方法的访问,则应用程序可能会有安全漏洞。

    // REVIEW the attribute below; could indicate security vulnerability.
    [SecurityPermission(SecurityAction.Deny, ControlThread = true)]
    public void DoSomething()
    {
    }
    
    public void DoPermitOnly()
    {
        // REVIEW the line below; could indicate security vulnerability.
        new SecurityPermission(SecurityPermissionFlag.ControlThread).PermitOnly();
    }
    
  • 如果要求任何权限(除外 PrincipalPermission),请删除需求。 在运行时,所有请求都将成功。

    // REMOVE the attribute below; it will always succeed.
    [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
    public void DoSomething()
    {
    }
    
    public void DoDemand()
    {
        // REMOVE the line below; it will always succeed.
        new SecurityPermission(SecurityPermissionFlag.ControlThread).Demand();
    }
    
  • 如果要求 PrincipalPermission,请参阅 PrincipalPermissionAttribute 已过时,报告为错误指南。 该指南均适用于 PrincipalPermissionPrincipalPermissionAttribute

  • 如果绝对必须禁用这些警告(不建议这样做),则可以在代码中禁止显示 SYSLIB0003 警告。

    #pragma warning disable SYSLIB0003 // disable the warning
    [SecurityPermission(SecurityAction.Demand, ControlThread = true)]
    #pragma warning restore SYSLIB0003 // re-enable the warning
    public void DoSomething()
    {
    }
    
    public void DoDemand()
    {
    #pragma warning disable SYSLIB0003 // disable the warning
        new SecurityPermission(SecurityPermissionFlag.ControlThread).Demand();
    #pragma warning restore SYSLIB0003 // re-enable the warning
    }
    

    还可以取消项目文件中的警告。 这样做会对项目中所有源文件禁用该警告。

    <Project Sdk="Microsoft.NET.Sdk">
      <PropertyGroup>
        <TargetFramework>net5.0</TargetFramework>
        <!-- NoWarn below suppresses SYSLIB0003 project-wide -->
        <NoWarn>$(NoWarn);SYSLIB0003</NoWarn>
      </PropertyGroup>
    </Project>
    

    注释

    取消 SYSLIB0003 仅禁用与 CAS 相关的过时警告。 它不会禁用任何其他警告或更改 .NET 5+ 运行时的行为。

  • 安全

受影响的 API