Skip to content

Proposed code fixer for obsoleted CAS APIs #39235

Closed

Description

@GrabYourPitchforks

Per dotnet/designs#139, we want to obsolete some of the CAS-related APIs in .NET 5.0. The obsoleted APIs are primarily classes that derive from CodeAccessSecurityAttribute and CodeAccessPermission.

The fixer should help apps migrate away from these now-obsolete types.

For CodeAccessSecurityAttribute-derived which are obsolete (except PrincipalPermissionAttribute), the fixer should look at the SecurityAction parameter passed into the attribute ctor. If the value is one of SecurityAction.Assert, Demand, InheritanceDemand, LinkDemand, RequestMinimum, or RequestOptional, the fixer should recommend that the attribute declaration be removed entirely. If the SecurityAction enum value is anything else, the fixer should make no recommendation.

For CodeAccessPermission-derived types which are obsolete (n.b. PrincipalPermission does not subclass this type and should be excluded), the fixer should suggest calls to Assert and Demand be removed. If any other API is called, the fixer should make no recommendation.

Examples:

// fixer should recommend removal
[SecurityPermission(SecurityAction.Demand, ...)]
public void SomeMethod() { /* ... */ }

// fixer should NOT recommend removal
// dev needs to take a closer look at what they were trying to accomplish
[SecurityPermission(SecurityAction.Deny, ...)]
public void SomeMethod() { /* ... */ }

// note: this is already obsolete *as error*
// fixer should NOT recommend removal
// dev needs to take a closer look at what they were trying to accomplish
[PrincipalPermission(SecurityAction.Demand, ...)]
public void SomeMethod() { /* ... */ }

public void SomeMethod()
{
    // fixer should recommend removal of this line
    new SecurityPermission(...).Demand();

    // fixer should NOT recommend removal
    // dev needs to take a closer look at what they were trying to accomplish
    // n.b. this call will always result in PNSE at runtime
    new SecurityPermission(...).Deny();

    // fixer should NOT recommend removal; PrincipalPermission does not subclass CodeAccessPermission
    // dev needs to take a closer look at what they were trying to accomplish
    new PrincipalPermission(...).Demand().
}

For PrincipalPermissionAttribute and PrincipalPermission specifically, we should steer the developer toward using ASP.NET's built-in AuthN / AuthZ features.

Activity

  1. added
    code-fixer
    on Jul 13, 2020
  2. added this to the 5.0.0 milestone on Jul 13, 2020
  3. added
    untriaged
    on Jul 13, 2020
  4. GrabYourPitchforks commented on Jul 14, 2020

    @GrabYourPitchforks
    MemberAuthor

    To clarify the above "fixer shouldn't do anything" examples a bit...

    // fixer should NOT recommend removal
    // dev needs to take a closer look at what they were trying to accomplish
    [SecurityPermission(SecurityAction.Deny, ...)]
    public void SomeMethod() { /* ... */ }
    
    // note: this is already obsolete *as error*
    // fixer should NOT recommend removal
    // dev needs to take a closer look at what they were trying to accomplish
    [PrincipalPermission(SecurityAction.Demand, ...)]
    public void SomeMethod() { /* ... */ }

    In the cases above, the .NET Core runtime does not honor the attributes. The runtime itself behaves as if the attributes were not present. But the only logical reason for a dev to have written these attributes in the first place is that they expected the runtime to perform a check on method entry. The dev should be made aware of this disconnect: "You put these because presumably you wanted a side effect, but the current runtime will no-op when it sees them. What did you actually intend to do?"

    public void SomeMethod()
    {
        // fixer should NOT recommend removal
        // dev needs to take a closer look at what they were trying to accomplish
        // n.b. this call will always result in PNSE at runtime
        new SecurityPermission(...).Deny();
    }

    In the case above, the call to Deny has the side effect of always throwing PNSE. The fixer cannot suggest removing this because it will change the control flow of the method. Instead, the dev should be made aware of the fact that this is an "always PNSE" API, and they need to take the course of action that is appropriate for their scenario.

    public void SomeMethod()
    {
        // fixer should NOT recommend removal; PrincipalPermission does not subclass CodeAccessPermission
        // dev needs to take a closer look at what they were trying to accomplish
        new PrincipalPermission(...).Demand().
    }

    In the case above, the call to Demand may throw an exception. The fixer cannot suggest removing this because it may change the control flow of the method.

  5. modified the milestones: 5.0.0, Future on Aug 26, 2020
  6. removed
    untriaged
    on Aug 31, 2020
  7. jeffhandley commented on Aug 1, 2021

    @jeffhandley
    Member

    @GrabYourPitchforks / @terrajobst - I've removed this from scope for .NET 6. Do you think we need to present this in API Review, or could we could ahead and mark it as up-for-grabs?

  8. dotnet-policy-service commented on Jan 31, 2026

    @dotnet-policy-service
    Contributor

    Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process.

    This process is part of our issue cleanup automation.

  9. added
    backlog-cleanup-candidate
    on Jan 31, 2026
  10. dotnet-policy-service commented on Feb 14, 2026

    @dotnet-policy-service
    Contributor

    This issue will now be closed since it had been marked no-recent-activity but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.

  11. removed this from the Future milestone on Feb 14, 2026
  12. locked and limited conversation to collaborators on Mar 17, 2026
  13. removed
    backlog-cleanup-candidate
    on Mar 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Securitycode-fixer

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions