-
Notifications
You must be signed in to change notification settings - Fork 238
Description
Why
As part of MMF-3716, we want to close the gap between C# and other languages regarding cryptography related rules support. S6377 is one of the rules that is not currently supported by this analyzer.
What
S6377 aims to detect when XML signatures are insecurely validated. We want to add support for this behavior for both .NET core and .NET framework. Note that XML related cryptographic features are implemented as part of system.security.cryptography.xml a .NET platform extension.
Detection logic
This rule should raise any time code validates a signature without relying on a trusted public key. In that case, the could would use the signature-embedded public key to perform the validation and would be open to forgery attacks.
We want to raise when:
System.Security.Cryptography.Xml.SignedXml.CheckSignature()is called (without a parameter).System.Security.Cryptography.Xml.SignedXml.CheckSignatureReturningKeyis called.
Example code
XmlDocument xmlDoc = new()
{
PreserveWhitespace = true
};
xmlDoc.Load("/data/login.xml");
SignedXml signedXml = new(xmlDoc);
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");
signedXml.LoadXml((XmlElement?)nodeList[0]);
if (signedXml.CheckSignature()) { // Noncompliant
// Process the XML content
} else {
// Raise an error
}CspParameters cspParams = new()
{
KeyContainerName = "MY_RSA_KEY"
};
RSACryptoServiceProvider rsaKey = new(cspParams);
XmlDocument xmlDoc = new()
{
PreserveWhitespace = true
};
xmlDoc.Load("/data/login.xml");
SignedXml signedXml = new(xmlDoc);
XmlNodeList nodeList = xmlDoc.GetElementsByTagName("Signature");
signedXml.LoadXml((XmlElement?)nodeList[0]);
if (signedXml.CheckSignature(rsaKey)) { // Compliant
// Process the XML content
} else {
// Raise an error
}RSPEC
This rule's RSPEC (from this PR SonarSource/rspec#3814) contains information regarding messages and highlighting.
Message
Change this code to only accept signatures computed from a trusted party.
Highlight
The call to the signature verification function:
- System.Security.Cryptography.Xml.SignedXml.CheckSignature
- System.Security.Cryptography.Xml.SignedXml.CheckSignatureReturningKey