Skip to content

Annotate unsupported APIs in System.Security.* #50528

Closed

Description

@buyaa-n

As part of #47228 i am running an analyzer to detect APIs throwing PNSE but not annotated with Obsolete/SupportedOSPlatform/UnsupportedOSPlatform attributes, we need to annotate them so that developers get warnings when they use them unexpectedly

For now, I have results only for cross-platform builds, analysis of targeted builds are coming soon where more APIs could be detected

API Comment Suggestion Location
'SecurityContext.Capture()' throws unconditionally All APIs throws [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(11,51)
'SecurityContext.CreateCopy()' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(12,47)
'SecurityContext.Dispose()' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(13,33)
'SecurityContext.IsFlowSuppressed()' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(14,49)
'SecurityContext.IsWindowsIdentityFlowSuppressed()' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(15,64)
'SecurityContext.RestoreFlow()' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(16,44)
'SecurityContext.Run(SecurityContext, ContextCallback, object)' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(17,107)
'SecurityContext.SuppressFlow()' throws unconditionally [Obsolete] the type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(18,57)
'SecurityContext.SuppressFlowWindowsIdentity()' throws unconditionally [Obsolete] entire type libraries\System.Security.Permissions\src\System\Security\SecurityContext.cs(19,72)

EDIT: Adding APIs found for netstandard targeted builds

API Comment Suggestion Location
'ECDiffieHellmanCng.FromXmlString(string, ECKeyXmlFormat)' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Cng\src\System\Security\Cryptography\ECDiffieHellmanCng.Xml.cs(10,13)
'ECDiffieHellmanCng.ToXmlString(ECKeyXmlFormat)' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Cng\src\System\Security\Cryptography\ECDiffieHellmanCng.Xml.cs(15,13)
'ECDiffieHellmanCngPublicKey.ToXmlString()' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Cng\src\System\Security\Cryptography\ECDiffieHellmanCngPublicKey.cs(37,13)
'ECDiffieHellmanCngPublicKey.FromXmlString(string)' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Cng\src\System\Security\Cryptography\ECDiffieHellmanCngPublicKey.cs(42,13)
'ECDsaCng.FromXmlString(string, ECKeyXmlFormat)' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Cng\src\System\Security\Cryptography\ECDsaCng.cs(131,16)
'ECDsaCng.ToXmlString(ECKeyXmlFormat)' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Cng\src\System\Security\Cryptography\ECDsaCng.cs(143,16)
'ECDiffieHellmanOpenSslPublicKey.ToXmlString()' Internal API, unconditionally throwing obsolet if referenced in public API libraries\Common\src\System\Security\Cryptography\ECDiffieHellmanOpenSslPublicKey.cs(42,17)
'ECDiffieHellmanOpenSslPublicKey.ToByteArray()' Internal API, unconditionally throwing obsolete if referenced in public API libraries\Common\src\System\Security\Cryptography\ECDiffieHellmanOpenSslPublicKey.cs(47,17)
'CmsSigner.CmsSigner(CspParameters)' It has comment possibly adding windows support but no issue attached, obsolete libraries\System.Security.Cryptography.Pkcs\src\System\Security\Cryptography\Pkcs\CmsSigner.cs(63,55)
'SignerInfo.ComputeCounterSignature()' Uncondidionally throws obsolete libraries\System.Security.Cryptography.Pkcs\src\System\Security\Cryptography\Pkcs\SignerInfo.cs(296,13)
'PermissionSet.PermitOnly()' Uncondidionally throws it has ifdef-ed obsolete libraries\System.Private.CoreLib\src\System\Security\PermissionSet.cs(43,36)
'CodeAccessPermission.PermitOnly()' Uncondidionally throws it has ifdef-ed obsolete libraries\System.Security.Permissions\src\System\Security\CodeAccessPermission.cs(22,36)

Note: We are suggesting to add [Obsolete] for the APIs only supported in .Net framework but not supported in .NetCore, with the corresponding Message, DiagnosticId, and UrlFormat

[Obsolete(Obsoletions.AuthenticationManagerMessage, DiagnosticId = Obsoletions.AuthenticationManagerDiagId, UrlFormat = Obsoletions.SharedUrlFormat)]
You can claim the next available DiagnosticId from Obsoletions.cs .

If it is only supported on a specific platform(s) consider adding [SupportedOSPlatform("platformName")] or if it is unsupported on a specific platform(s) [UnsupportedOSPlatform("platformName")] attribute instead

cc @jeffhandley @terrajobst @GrabYourPitchforks

Activity

  1. added this to the 6.0.0 milestone on Mar 31, 2021
  2. ghost added
    untriaged
    on Mar 31, 2021
  3. buyaa-n commented on Apr 6, 2021

    @buyaa-n
    ContributorAuthor

    Notes need to consider:

    • If the API has an issue associated with future support we are not suggesting to add [Obsolete], we need to make sure that we are not gonna support the API ever and related issue should be closed, then we can add [Obsolete]
    • If the API is internal/private the annotation is not necessary, but they could point you to the public API needs annotation
  4. removed
    untriaged
    on Apr 20, 2021
  5. vcsjones commented on Jul 6, 2021

    @vcsjones
    Member

    Some of these APIs that "unconditionally throw" are virtual and are expected to be overridden in derived classes, so while the base class throws, they aren't necessarily obsolete because derived classes override them and some functionality depend on these classes. I think this would be the case for all of the methods on the HMAC class. So I do not think those should be obsolete or otherwise annotated.

  6. buyaa-n commented on Jul 21, 2021

    @buyaa-n
    ContributorAuthor

    I think this would be the case for all of the methods on the HMAC class. So I do not think those should be obsolete or otherwise annotated.

    That is right, removed HMAC related APIs, but others looks valid

  7. buyaa-n commented on Jul 22, 2021

    @buyaa-n
    ContributorAuthor

    Adding few more:

    API Comment Suggestion Location
    'ECDsa.ECDsaImplementation.ECDsaSecurity Transforms.ExportExplicitParameters(bool)' unconditionally throwing, reachable on iOS, tvOS, OSX unreachable: browser UnsupportedOSPlatform("iOS"), UnsupportedOSPlatform("tvOS"), UnsupportedOSPlatform("tOSX") \libraries\Common\src\System\Security\ Cryptography\ECDsaSecurityTransforms.cs(209,21)
    'Rfc2898DeriveBytes.CryptDeriveKey(string, string, int, byte[])' unconditionally throwing reachable on Android, Unix, iOS, tvOs, OSX, Windows unreachable: browser Only implemented for PasswordDeriveBytes and has SupportedOSPlatform("windows") attribute, should we obsolete or also add SupportedOSPlatform("windows") libraries\System.Security.Cryptography.Algorithms\src \System.Security.Cryptography.Algorithms.csproj]

    CC @bartonjs

  8. vcsjones commented on Jul 22, 2021

    @vcsjones
    Member

    DSA.CreateCore()

    The public entry, Create is already marked with UnsupportedOSPlatform:

    So I think that one is already taken care of.

  9. buyaa-n commented on Jul 23, 2021

    @buyaa-n
    ContributorAuthor

    Removed, thanks

  10. vcsjones commented on Aug 4, 2021

    @vcsjones
    Member

    @buyaa-n is this something you are working on or is this something I can contribute to? Some of these look reasonable.

  11. buyaa-n commented on Aug 4, 2021

    @buyaa-n
    ContributorAuthor

    @buyaa-n is this something you are working on

    Not really, my intent here (and all other issues i filed) was to detect the PNSE and let the area owners know because some of the findings might be invalid or need more investigation. For now, I resolved this only for the areas I own, but if the security area owners are OK with the proposed change then i can go ahead and do updates

    or is this something I can contribute to? Some of these look reasonable.

    Sure, that would be great, thanks!

  12. vcsjones commented on Aug 5, 2021

    @vcsjones
    Member

    'ECDsa.ECDsaImplementation.ECDsaSecurity Transforms.ExportExplicitParameters(bool)'

    I'm not sure adding an UnsupportedOSPlatform here would be ergonomic. The issue is that the public API ExportExplicitParameters is accessible via the virtual ECDsa.ExportExplicitParameters. We would have to decorate the virtual with the UOSP attribute, which would not interact well with derived classes (our own or developers' own). To illustrate:

    using System.Runtime.Versioning;
    
    Foo f = new Bar();
    f.Do(); // This is actually a `Bar` but since it's called through the base type, a compiler warning appears.
    
    public class Foo
    {
        [UnsupportedOSPlatform("iOS")]
        public virtual object Do() => throw new PlatformNotSupportedException();
    }
    
    public class Bar : Foo
    {
        public override object Do() => new();
    }

    Similar concerns lie with ToXmlString, FromXmlString, and ToByteArray. It seems a little odd to obsolete an override when a user derived type could override one of these from the base class and implement it.

    Some of these do make sense to me to obsolete:

    1. Rfc2898DeriveBytes.CryptDeriveKey - it's unimplemented and can't be implemented xplat since it's a Windows thing. I'm unaware of demand to bring it back.
    2. CmsSigner.CmsSigner(CspParameters) seems weird. It manufactures a certificate on the fly based on those CspParameters. It makes sense to obsolete since it can only be implemented on Windows. If we really, really wanted "make me a cert on the fly" behavior, it probably makes sense to implement a new API that accepted an AsymmetricAlgorithm or something.
    3. SignerInfo.ComputeCounterSignature() There's an overload that accepts the CmsSigner, so I think it makes sense to obsolete.

    I don't see any immediately actionable things for 6.0. Presumably the obsoletions need to go through API review, and I don't know if we're taking any more obsoletions for 6.0 (granted all of them throw, so it's not exactly a breaking change).

  13. jeffhandley commented on Aug 6, 2021

    @jeffhandley
    Member

    We could take these 3 obsoletions without going through API Review--just code review from @bartonjs and @GrabYourPitchforks. I'm comfortable taking them before the August 17th cutoff for .NET 6, especially since it's not a breaking change.

  14. 19 remaining items

  15. modified the milestones: 6.0.0, 7.0.0 on Sep 4, 2021
  16. jeffhandley commented on Sep 4, 2021

    @jeffhandley
    Member

    Moved to 7.0.0.

  17. vcsjones commented on Mar 28, 2022

    @vcsjones
    Member

    @bartonjs

    I think that only leaves ECDiffieHellmanPublicKey.ToByteArray and ECDiffieHellmanPublicKey..ctor(byte[]). I'd support Obsoleting these. I... thought... we had added SPKI export on this type, but I don't see it

    I didn't quite take this as an affirmative "let's obsolete these". Do we want to obsolete ECDiffieHellmanPublicKey.ToByteArray and ECDiffieHellmanPublicKey..ctor(byte[])?

  18. bartonjs commented on Mar 28, 2022

    @bartonjs
    Member

    Do we want to obsolete ECDiffieHellmanPublicKey.ToByteArray and ECDiffieHellmanPublicKey..ctor(byte[])?

    Yes, please. The method is easy:

    [Obsolete("ECDiffieHellmanPublicKey.ToByteArray() does not have a consistent and interoperable implementation on all platforms. Use ECDiffieHellmanPublicKey.ExportSubjectPublicKeyInfo() instead.", ...)]

    The constructor is... harder? The best message I've come up with is "The bytes-based constructor for ECDiffieHellmanPublicKey does not have an associated standard data format. This constructor only provides the bytes to the default implementation of the obsolete ToByteArray method, and is not necessary to call from a derived type."

    But that's clunky and awkward. So maybe that one is better as a docs-remark instead of an Obsolete.

  19. vcsjones commented on Mar 28, 2022

    @vcsjones
    Member

    The constructor is... harder?

    Yeah. But then it feels weird to have a constructor that takes the key blob and no not-obsolete way to do anything with it.

    Since it's a protected constructor on a class that no one is deriving from I would wager we could take a swing at obsoleting it with less than perfect wording.

  20. bartonjs commented on Mar 28, 2022

    @bartonjs
    Member

    "ECDiffieHellmanPublicKey.ToByteArray() and the associated constructor do not..." might work?

  21. ghost added
    in-pr
    on Mar 28, 2022
  22. vcsjones commented on Mar 29, 2022

    @vcsjones
    Member

    @bartonjs What do you think about obsoleting ECKeyXmlFormat? It's only used on obsolete methods now, only had one enum value, etc.

  23. bartonjs commented on Mar 29, 2022

    @bartonjs
    Member

    I'm not really a fan of obsoleting types, as that makes every member declaration that uses it have to suppress the obsoletion. I think we just ignore it.

  24. terrajobst commented on Mar 29, 2022

    @terrajobst
    Contributor

    That’s why we added the diagnostic ID. This way, you can add a global supression that’s still targeted. Existing code would do that, new code wouldn’t use it. Not sure that’s justified here though.

  25. vcsjones commented on Mar 29, 2022

    @vcsjones
    Member

    as that makes every member declaration that uses it have to suppress the obsoletion.

    I would imagine it would get obsolete under SYSLIB0042, if it were pursued.

    I don't know if there is telemetry on this, but I would be surprised if developers were using ECKeyXmlFormat as a parameter type, especially since it never contained anything more than one enum value.

    For dotnet/runtime members, it would only be half a dozen or so suppression I suspect, which seems manageable.

    From my own searching on GitHub, I didn't see a single use of ECKeyXmlFormat that was not a fork of the runtime, or calling To/FromXmlString (which is going to hit the diagnostic anyway).

  26. ghost removed
    in-pr
    on Mar 30, 2022
  27. ghost locked as resolved and limited conversation to collaborators on Apr 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions