Skip to content

Fix unsafe XML crypto snippets under System.Security.Cryptography.Xml - #13013

Open
krwq wants to merge 1 commit into
dotnet:mainfrom
krwq:safe-crypto-xml-snippets
Open

Fix unsafe XML crypto snippets under System.Security.Cryptography.Xml#13013
krwq wants to merge 1 commit into
dotnet:mainfrom
krwq:safe-crypto-xml-snippets

Conversation

@krwq

@krwq krwq commented Aug 20, 2026

Copy link
Copy Markdown
Member

Apply safe-by-default patterns to signing/verification and encryption samples:

  • Wrap XmlDocument.Load in XmlReader.Create so DTD processing is prohibited and no XmlResolver is used.
  • Replace parameterless SignedXml.CheckSignature() with the overload that takes a caller-provided trusted key.
  • For the CheckSignatureReturningKey sample, compare the returned key against a trusted key before accepting the signature.

Internal previews

Toggle expand/collapse
File Preview link
snippets/csharp/System.Security.Cryptography.Xml/DataObject/Overview/source1.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/DataReference/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/KeyReference/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/Reference/.ctor/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/Signature/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/sample.cs Preview published page
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.cs Preview published page

Note

This table shows the first 30 preview links (sorted alphabetically by file path) found in the OPS build report. For the full list, select OpenPublishing.Build Details within checks.

Apply safe-by-default patterns to signing/verification and encryption samples:

- Wrap XmlDocument.Load in XmlReader.Create so DTD processing is prohibited and no XmlResolver is used.

- Replace parameterless SignedXml.CheckSignature() with the overload that takes a caller-provided trusted key.

- For the CheckSignatureReturningKey sample, compare the returned key against a trusted key before accepting the signature.
Copilot AI lite review requested due to automatic review settings August 20, 2026 12:30
@krwq
krwq requested a review from a team as a code owner August 20, 2026 12:30

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR hardens the System.Security.Cryptography.Xml C# documentation snippets to be safer by default when loading XML and validating XML signatures, reducing the risk of XXE-style issues and “trust any key” signature-validation mistakes.

Changes:

  • Updates many snippets to load XML via XmlReader instead of direct XmlDocument.Load(...).
  • Replaces parameterless SignedXml.CheckSignature() with overloads that require a caller-provided trusted key.
  • Improves the CheckSignatureReturningKey sample to validate that the returned signing key matches a trusted key.

Reviewed changes

Copilot reviewed 36 out of 36 changed files in this pull request and generated 38 comments.

Show a summary per file
File Description
snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXsltTransform/Overview/sample.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/XmlDsigXPathTransform/Overview/sample.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampleenvelope.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/XmlDsigC14NWithCommentsTransform/Overview/sampledetached.cs Passes a trusted key into detached-signature verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/X509IssuerSerial/Overview/sample.cs Switches XML loading to XmlReader in signing/verification flow.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NWithCommentsTransformUrl/sample.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/XmlDsigExcC14NTransformUrl/example.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/sample.cs Adds trusted-key signature verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/Overview/exampleenvelope.cs Switches XML loading to XmlReader in envelope signing/verification.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignatureReturningKey/exampleenvelope.cs Verifies signature then compares returned key to a trusted key; switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigenvkeyedhashalg.cs Switches XML loading to XmlReader in keyed-hash signature example.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/xmldsigdetachedkeyedhashalg.cs Switches XML loading to XmlReader in detached keyed-hash signature example.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/sample.cs Switches XML loading to XmlReader in certificate-subject verification flow.
snippets/csharp/System.Security.Cryptography.Xml/SignedXml/CheckSignature/exampledetached.cs Switches XML loading to XmlReader in detached signature verification flow.
snippets/csharp/System.Security.Cryptography.Xml/Signature/Overview/sample.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/Reference/.ctor/sample.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/KeyReference/Overview/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/Overview/examplecreateenvelope.cs Switches XML loading to XmlReader during signing.
snippets/csharp/System.Security.Cryptography.Xml/KeyInfoX509Data/.ctor/sample.cs Switches XML loading to XmlReader in signing/verification flow.
snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigenv.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/KeyInfo/Overview/xmldsigdetach.cs Passes a trusted key into detached-signature verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/IRelDecryptor/Overview/sample.cs Requires a trusted key for signature checking and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptionProperty/Overview/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample4.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample3.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample2.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample1.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/Overview/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedXml/AddKeyNameMapping/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedType/Overview/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedKey/Overview/example.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/EncryptedData/Overview/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampleenvelope.cs Passes a trusted key into verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/DSAKeyValue/Overview/exampledetached.cs Passes a trusted key into detached-signature verification and switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/DataReference/Overview/sample.cs Switches XML loading to XmlReader.
snippets/csharp/System.Security.Cryptography.Xml/DataObject/Overview/source1.cs Introduces trusted-key signature verification and switches XML loading to XmlReader.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +15 to 17
public static void CheckSignatureWithEncryptedGrant(string fileName, IRelDecryptor decryptor, AsymmetricAlgorithm trustedKey)
{
// Create a new XML document.
Comment on lines +141 to +156
// Verify the signature and retrieve the key that produced it.
AsymmetricAlgorithm signingKey;
if (!signedXml.CheckSignatureReturningKey(out signingKey))
return false;

// A valid signature only proves possession of some key.
// The caller must confirm the returned key matches a key it trusts.
if (signingKey is RSA rsa)
{
RSAParameters expected = TrustedKey.ExportParameters(false);
RSAParameters actual = rsa.ExportParameters(false);
return expected.Modulus.SequenceEqual(actual.Modulus)
&& expected.Exponent.SequenceEqual(actual.Exponent);
}

return false;
@mconnew

mconnew commented Aug 20, 2026

Copy link
Copy Markdown
Member

The comments by Copilot about passing an XmlReaderSettings isn't needed now. It used to be needed as the default behavior on .NET Framework was target framework version dependent. If you had an app targeting .NET 4.5, the default behavior was to do DTD processing and provide a default implementation of the XmlResolver which would download referenced/included files. Because you might not know which target framework version your code would be running against, if it was library code, then it's safest to be explicit. All currently supported versions of .NET Framework and .NET have safe defaults so it's not needed. It's only if someone is still targeting an out of support version of .NET Framework, or explicitly opted in to the unsafe behavior that this would be a concern.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants