You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
RawData setter not accounted for in some X.509 / Pkcs extensions' memoization #81844
This will, incorrectly, print "Wasabi" to the console twice. This is because the first call to EnumerateRelativeDistinguishedNames memoizes the RDNs and it isn't aware that the RawData changed out from underneath of it.
A number of X.509 extensions I think have this issue, including X509SubjectAlternativeNameExtension, X500DistinguishedName, X509SubjectKeyIdentifierExtension, etc. In addition, some AsnEncodedData derived types in S.S.Cryptography.Pkcs memoize their results like Pkcs9LocalKeyId
There are a few ways I think we can fix this.
Stop memoizing decoded data. This will incur a performance hit.
Add a hook for ourselves when the RawData property is set:
publicbyte[]RawData{// Get omittedset{ArgumentNullException.ThrowIfNull(value);_rawData=value.CloneByteArray();RawDataChanged();}}privateprotectedvirtualvoidRawDataChanged(){// Override this in types that need to clear out computed values.}
This works for S.S.Cryptography, but it would not work for S.S.Cryptography.Pkcs without either making an API proposal or reflection (and it isn't a very good API, either). I considered suggesting making RawData virtual, but it turns out that's a breaking change.
Hm, one other option: since RawData doesn't return defensive copies, we could hold on to the reference to RawData that was used during memoization. When we need look up a memoized value, like in EnumerateRelativeDistinguishedNames, we check to see if the reference from RawData changed from when we cached it:
Don't /all/ of the types that interpret RawData have this behavior (so all of the Pkcs9 and X.509 extension derived types)?
This feels like one of those things that "I've always known, and rolled my eyes at". Assuming we're already fairly consistent then docs is probably the better answer.
It's not. Every X.509 extension has this problem. The default constructor for all of them do some method of "hey I already decoded everything and there is nothing to decode".
I guess we can fix all of them.
But more to the original point, the setter just doesn't work as soon as you do something that causes memoization. Why have it then, except for object initialization syntax?
How breaking would it be if we did something like we did with Oid "Set once, and after that it throws"?
Activity
vcsjones commentedon Feb 8, 2023
Hm, one other option: since
RawDatadoesn't return defensive copies, we could hold on to the reference toRawDatathat was used during memoization. When we need look up a memoized value, like inEnumerateRelativeDistinguishedNames, we check to see if the reference fromRawDatachanged from when we cached it:That feels fragile though.
bartonjs commentedon Feb 9, 2023
Don't /all/ of the types that interpret RawData have this behavior (so all of the Pkcs9 and X.509 extension derived types)?
This feels like one of those things that "I've always known, and rolled my eyes at". Assuming we're already fairly consistent then docs is probably the better answer.
vcsjones commentedon Feb 9, 2023
@bartonjs
Probably. I've probably never noticed until recently because most of the extensions weren't that useful for reading data.
This is more noticeable when the memoization occurs in the constructor:
This prints false. It should print true.
That just seems broken to me.
bartonjs commentedon Feb 9, 2023
Huh. Most of the extensions (I think/thought) don't process any of their properties until first read, which allows for RawData to get assigned late.
If basic constraints is the odd man out, we should just consider fixing'm.
vcsjones commentedon Feb 9, 2023
It's not. Every X.509 extension has this problem. The default constructor for all of them do some method of "hey I already decoded everything and there is nothing to decode".
I guess we can fix all of them.
But more to the original point, the setter just doesn't work as soon as you do something that causes memoization. Why have it then, except for object initialization syntax?
How breaking would it be if we did something like we did with
Oid"Set once, and after that it throws"?vcsjones commentedon Feb 9, 2023
Meh that gets weird with
CopyFrom. I guess we can just make the object initialization syntax work.vcsjones commentedon Jun 6, 2023
Will pick this up soon, but it's existing behavior, no one else cares but me, so marking as Future.
vcsjones commentedon Jun 25, 2026
Closed in favor of #129544.