Conversation
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
…RI is missing in ExtractIdFromLocalUri Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/b257b674-5bad-4736-8f43-34bef56534e6 Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR updates XML encryption/decryption code paths to throw a CryptographicException (instead of surfacing a NullReferenceException) when a <RetrievalMethod> element omits the required URI attribute, and adds a regression test to cover the scenario.
Changes:
- Add null/empty guard in
Utils.ExtractIdFromLocalUrito throwCryptographicExceptionwithSR.Cryptography_Xml_UriRequired. - Add a regression test validating
EncryptedXml.DecryptDocument()throwsCryptographicExceptionwhen<RetrievalMethod/>has noURI.
Show a summary per file
| File | Description |
|---|---|
src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs |
Adds early validation to avoid null dereference and throw a crypto-appropriate exception. |
src/libraries/System.Security.Cryptography.Xml/tests/EncryptedXmlTests.cs |
Adds a regression test exercising the missing-URI RetrievalMethod case. |
Copilot's findings
Comments suppressed due to low confidence (1)
src/libraries/System.Security.Cryptography.Xml/src/System/Security/Cryptography/Xml/Utils.cs:334
- The exception message resource
SR.Cryptography_Xml_UriRequiredsays "A Uri attribute is required for a CipherReference element." After this change it can surface from other contexts (e.g.,KeyInfo/RetrievalMethodprocessing viaExtractIdFromLocalUri), which is misleading. Consider switching this helper to a more generic SR (or introducing a generic one) so callers get an accurate error message when a non-CipherReference URI is missing/invalid.
if (string.IsNullOrEmpty(uri))
throw new CryptographicException(SR.Cryptography_Xml_UriRequired);
- Files reviewed: 2/2 changed files
- Comments generated: 1
🤖 Copilot Code Review — PR #127093Holistic AssessmentMotivation: The PR addresses a real, reproducible bug where a missing Approach: The fix is placed in the centralized Summary: ✅ LGTM. This is a clean, minimal fix that converts an uninformative Detailed Findings✅ Correctness — Fix is well-placed and correctThe
✅ Test — Regression test exercises the right pathThe test constructs a 💡 Suggestion — Remove null-forgiving operators at callers (follow-up)
// Line 373 and 478: change from
string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri!);
// to
string idref = Utils.ExtractIdFromLocalUri(kiRetrievalMethod.Uri);This is a cleanliness improvement, not a correctness issue. 💡 Suggestion — Error message mentions "CipherReference" specifically (pre-existing)The Models contributing to this review: Claude Opus 4.6 (primary), GPT-5.3-Codex (sub-agent). Goldeneye sub-agent timed out and did not contribute.
|
Description
Utils.ExtractIdFromLocalUridereferences its nullableuriparameter withuri!.Substring(1)without a null guard. WhenEncryptedXml.DecryptDocument()processes aKeyInfo/RetrievalMethodelement that omits theURIattribute, this surfaces as aNullReferenceException— an exception type callers of a cryptography API should never need to handle.Changes
Utils.cs: Added null/empty check at the top ofExtractIdFromLocalUrithat throwsCryptographicExceptionwithSR.Cryptography_Xml_UriRequired, consistent with the validation inCipherReference.LoadXmlandXmlDecryptionTransform.LoadInnerXml.EncryptedXmlTests.cs: Regression test exercisingDecryptDocumentwith a<RetrievalMethod/>element missing itsURIattribute.Reproduction