Repository navigation
Description
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
runtime/src/libraries/System.Net.Requests/src/System/Net/AuthenticationManager.cs
Line 17 in cecc76a
| [Obsolete(Obsoletions.AuthenticationManagerMessage, DiagnosticId = Obsoletions.AuthenticationManagerDiagId, UrlFormat = Obsoletions.SharedUrlFormat)] |
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
Activity
buyaa-n commentedon Apr 6, 2021
Notes need to consider:
vcsjones commentedon Jul 6, 2021
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
HMACclass. So I do not think those should be obsolete or otherwise annotated.buyaa-n commentedon Jul 21, 2021
That is right, removed HMAC related APIs, but others looks valid
buyaa-n commentedon Jul 22, 2021
Adding few more:
CC @bartonjs
vcsjones commentedon Jul 22, 2021
The public entry,
Createis already marked withUnsupportedOSPlatform:runtime/src/libraries/System.Security.Cryptography.Algorithms/src/System/Security/Cryptography/DSA.cs
Line 38 in a2f2a62
So I think that one is already taken care of.
buyaa-n commentedon Jul 23, 2021
Removed, thanks
vcsjones commentedon Aug 4, 2021
@buyaa-n is this something you are working on or is this something I can contribute to? Some of these look reasonable.
buyaa-n commentedon Aug 4, 2021
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
Sure, that would be great, thanks!
vcsjones commentedon Aug 5, 2021
I'm not sure adding an
UnsupportedOSPlatformhere would be ergonomic. The issue is that the public APIExportExplicitParametersis accessible via the virtualECDsa.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:Similar concerns lie with
ToXmlString,FromXmlString, andToByteArray. It seems a little odd to obsolete anoverridewhen 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:
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.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 anAsymmetricAlgorithmor something.SignerInfo.ComputeCounterSignature()There's an overload that accepts theCmsSigner, 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).jeffhandley commentedon Aug 6, 2021
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.
19 remaining items
jeffhandley commentedon Sep 4, 2021
Moved to 7.0.0.
vcsjones commentedon Mar 28, 2022
@bartonjs
I didn't quite take this as an affirmative "let's obsolete these". Do we want to obsolete
ECDiffieHellmanPublicKey.ToByteArrayandECDiffieHellmanPublicKey..ctor(byte[])?bartonjs commentedon Mar 28, 2022
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.
vcsjones commentedon Mar 28, 2022
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.
bartonjs commentedon Mar 28, 2022
"ECDiffieHellmanPublicKey.ToByteArray() and the associated constructor do not..." might work?
vcsjones commentedon Mar 29, 2022
@bartonjs What do you think about obsoleting
ECKeyXmlFormat? It's only used on obsolete methods now, only had one enum value, etc.bartonjs commentedon Mar 29, 2022
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.
terrajobst commentedon Mar 29, 2022
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.
vcsjones commentedon Mar 29, 2022
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
ECKeyXmlFormatas 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
ECKeyXmlFormatthat was not a fork of the runtime, or calling To/FromXmlString (which is going to hit the diagnostic anyway).