-
Notifications
You must be signed in to change notification settings - Fork 189
Harden/xmlsecuritydsig static analysis #287
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
1c98e9e
Harden XMLSecurityDSig after static analysis
robrichards cb3800a
Restore enveloped Signature after canonicalization
robrichards 9fcf688
Harden XMLSecurityDSig types after PHPStan analysis.
robrichards f670ad7
Restore verifyDocument() trusted-key guard.
robrichards File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --TEST-- | ||
| Enveloped transform restores Signature before validating later references | ||
| --FILE-- | ||
| <?php | ||
| require(dirname(__FILE__) . '/../xmlseclibs.php'); | ||
| use RobRichards\XMLSecLibs\XMLSecurityDSig; | ||
| use RobRichards\XMLSecLibs\XMLSecurityKey; | ||
|
|
||
| $doc = new DOMDocument(); | ||
| $doc->loadXML('<Envelope><Payload>data</Payload></Envelope>'); | ||
|
|
||
| $signer = new XMLSecurityDSig(); | ||
| $signer->setCanonicalMethod(XMLSecurityDSig::EXC_C14N); | ||
| $signer->addReference( | ||
| $doc, | ||
| XMLSecurityDSig::SHA256, | ||
| array(XMLSecurityDSig::ENVELOPED, XMLSecurityDSig::EXC_C14N), | ||
| array('force_uri' => true) | ||
| ); | ||
|
|
||
| $object = $signer->addObject('inside-object'); | ||
| $object->setAttribute('Id', 'obj1'); | ||
| $signer->addReference( | ||
| $object, | ||
| XMLSecurityDSig::SHA256, | ||
| array(XMLSecurityDSig::EXC_C14N), | ||
| array('overwrite' => false) | ||
| ); | ||
|
|
||
| $privateKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'private')); | ||
| $privateKey->loadKey(dirname(__FILE__) . '/privkey.pem', true); | ||
| $signer->sign($privateKey); | ||
| $signer->appendSignature($doc->documentElement); | ||
|
|
||
| $verifier = new XMLSecurityDSig(); | ||
| $publicKey = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'public')); | ||
| $publicKey->loadKey(dirname(__FILE__) . '/mycert.pem', true); | ||
| $verifier->allowedSignatureAlgorithms = array(XMLSecurityKey::RSA_SHA256); | ||
| $verifier->allowedDigestAlgorithms = array(XMLSecurityDSig::SHA256); | ||
|
|
||
| try { | ||
| $nodes = $verifier->verifyDocument($publicKey, $doc); | ||
| echo "VERIFY: OK\n"; | ||
| echo "SIGNATURES: ".$doc->getElementsByTagNameNS(XMLSecurityDSig::XMLDSIGNS, 'Signature')->length."\n"; | ||
| echo isset($nodes['obj1']) ? "OBJECT: OK\n" : "OBJECT: missing\n"; | ||
| } catch (Exception $e) { | ||
| echo "VERIFY: ".$e->getMessage()."\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| VERIFY: OK | ||
| SIGNATURES: 1 | ||
| OBJECT: OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --TEST-- | ||
| getRefNodeID rejects external URIs consistently with processRefNode | ||
| --FILE-- | ||
| <?php | ||
| require(dirname(__FILE__) . '/../xmlseclibs.php'); | ||
| use RobRichards\XMLSecLibs\XMLSecurityDSig; | ||
|
|
||
| error_reporting(E_ALL); | ||
| $objDSig = new XMLSecurityDSig(); | ||
|
|
||
| $cases = array( | ||
| 'EXTERNAL' => 'http://example.com/data.xml#abc', | ||
| 'QUERY' => '?x=1', | ||
| 'BAREHASH' => '#', | ||
| 'FRAGMENT' => '#abc', | ||
| 'EMPTY' => '', | ||
| ); | ||
|
|
||
| foreach ($cases as $label => $uri) { | ||
| $doc = new DOMDocument(); | ||
| $doc->loadXML('<ds:Reference xmlns:ds="http://www.w3.org/2000/09/xmldsig#" URI="'.htmlspecialchars($uri, ENT_QUOTES).'"/>'); | ||
| $id = $objDSig->getRefNodeID($doc->documentElement); | ||
| echo "$label: ".var_export($id, true)."\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| EXTERNAL: NULL | ||
| QUERY: NULL | ||
| BAREHASH: NULL | ||
| FRAGMENT: 'abc' | ||
| EMPTY: NULL |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| --TEST-- | ||
| idKeys attribute names cannot inject XPath operators | ||
| --FILE-- | ||
| <?php | ||
| require(dirname(__FILE__) . '/../xmlseclibs.php'); | ||
| use RobRichards\XMLSecLibs\XMLSecurityDSig; | ||
|
|
||
| $xml = <<<XML | ||
| <?xml version="1.0"?> | ||
| <Root> | ||
| <A Id="target">one</A> | ||
| <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> | ||
| <ds:SignedInfo> | ||
| <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> | ||
| <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> | ||
| <ds:Reference URI="#target"> | ||
| <ds:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"/> | ||
| <ds:DigestValue>2jmj7l5rSw0yVb/vlWAYkK/YBwk=</ds:DigestValue> | ||
| </ds:Reference> | ||
| </ds:SignedInfo> | ||
| <ds:SignatureValue>AA==</ds:SignatureValue> | ||
| </ds:Signature> | ||
| </Root> | ||
| XML; | ||
|
|
||
| $doc = new DOMDocument(); | ||
| $doc->loadXML($xml); | ||
| $objXMLSecDSig = new XMLSecurityDSig(); | ||
| $objXMLSecDSig->idKeys = array('foo or bar'); | ||
| $objXMLSecDSig->locateSignature($doc); | ||
| $objXMLSecDSig->canonicalizeSignedInfo(); | ||
| try { | ||
| $objXMLSecDSig->validateReference(); | ||
| print "INJECT: unexpected success\n"; | ||
| } catch (Exception $e) { | ||
| print "INJECT: ".$e->getMessage()."\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| INJECT: Invalid idKeys attribute name |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| --TEST-- | ||
| locateSignature resets XPath context across documents | ||
| --FILE-- | ||
| <?php | ||
| require(dirname(__FILE__) . '/../xmlseclibs.php'); | ||
| use RobRichards\XMLSecLibs\XMLSecurityDSig; | ||
|
|
||
| $sigXml = '<Root><ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"><ds:SignedInfo><ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/><ds:SignatureMethod/></ds:SignedInfo></ds:Signature></Root>'; | ||
|
|
||
| $doc1 = new DOMDocument(); | ||
| $doc1->loadXML($sigXml); | ||
|
|
||
| $doc2 = new DOMDocument(); | ||
| $doc2->loadXML(str_replace('<Root>', '<Other>', str_replace('</Root>', '</Other>', $sigXml))); | ||
|
|
||
| $objDSig = new XMLSecurityDSig(); | ||
| /* Prime an XPath context against the constructor template document. */ | ||
| $objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N); | ||
| $objDSig->locateSignature($doc1); | ||
| $objDSig->canonicalizeSignedInfo(); | ||
|
|
||
| try { | ||
| $objDSig->locateSignature($doc2); | ||
| $objDSig->canonicalizeSignedInfo(); | ||
| echo "REUSE: OK\n"; | ||
| } catch (Throwable $e) { | ||
| echo "REUSE: ".$e->getMessage()."\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| REUSE: OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --TEST-- | ||
| Object Id under nested Signature resolves before enveloped strip | ||
| --FILE-- | ||
| <?php | ||
| require(dirname(__FILE__) . '/../xmlseclibs.php'); | ||
| use RobRichards\XMLSecLibs\XMLSecurityDSig; | ||
| use RobRichards\XMLSecLibs\XMLSecurityKey; | ||
|
|
||
| $doc = new DOMDocument(); | ||
| $doc->loadXML('<Envelope><Payload>data</Payload></Envelope>'); | ||
|
|
||
| $objDSig = new XMLSecurityDSig(); | ||
| $objDSig->setCanonicalMethod(XMLSecurityDSig::EXC_C14N); | ||
| $objDSig->idKeys = array('xml:id'); | ||
|
|
||
| $wrapped = $objDSig->sigNode->ownerDocument->createElement('Wrapped'); | ||
| $wrapped->setAttributeNS('http://www.w3.org/XML/1998/namespace', 'xml:id', 'obj1'); | ||
| $wrapped->appendChild($objDSig->sigNode->ownerDocument->createTextNode('inside-object')); | ||
| $objDSig->addObject($wrapped); | ||
|
|
||
| $objDSig->addReference( | ||
| $wrapped, | ||
| XMLSecurityDSig::SHA256, | ||
| array(XMLSecurityDSig::ENVELOPED, XMLSecurityDSig::EXC_C14N), | ||
| array( | ||
| 'id_name' => 'id', | ||
| 'overwrite' => false, | ||
| 'prefix' => 'xml', | ||
| 'prefix_ns' => 'http://www.w3.org/XML/1998/namespace', | ||
| ) | ||
| ); | ||
|
|
||
| $key = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'private')); | ||
| $key->loadKey(dirname(__FILE__) . '/privkey.pem', true); | ||
| $objDSig->sign($key); | ||
| $objDSig->appendSignature($doc->documentElement); | ||
|
|
||
| $verify = new XMLSecurityDSig(); | ||
| $verify->idKeys = array('xml:id'); | ||
| $pub = new XMLSecurityKey(XMLSecurityKey::RSA_SHA256, array('type' => 'public')); | ||
| $pub->loadKey(dirname(__FILE__) . '/mycert.pem', true); | ||
| $verify->allowedSignatureAlgorithms = array(XMLSecurityKey::RSA_SHA256); | ||
| $verify->allowedDigestAlgorithms = array(XMLSecurityDSig::SHA256); | ||
|
|
||
| try { | ||
| $nodes = $verify->verifyDocument($pub, $doc); | ||
| echo isset($nodes['obj1']) ? "OBJECT_REF: OK\n" : "OBJECT_REF: missing node\n"; | ||
| } catch (Exception $e) { | ||
| echo "OBJECT_REF: ".$e->getMessage()."\n"; | ||
| } | ||
| ?> | ||
| --EXPECTF-- | ||
| OBJECT_REF: OK |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| --TEST-- | ||
| Duplicate SignedInfo children are rejected | ||
| --FILE-- | ||
| <?php | ||
| require(dirname(__FILE__) . '/../xmlseclibs.php'); | ||
| use RobRichards\XMLSecLibs\XMLSecurityDSig; | ||
|
|
||
| function checkStruct($xml, $label) { | ||
| $doc = new DOMDocument(); | ||
| $doc->loadXML($xml); | ||
| $objXMLSecDSig = new XMLSecurityDSig(); | ||
| $objXMLSecDSig->locateSignature($doc); | ||
| try { | ||
| $objXMLSecDSig->canonicalizeSignedInfo(); | ||
| print "$label: unexpected success\n"; | ||
| } catch (Exception $e) { | ||
| print "$label: ".$e->getMessage()."\n"; | ||
| } | ||
| } | ||
|
|
||
| $dupCanon = <<<XML | ||
| <?xml version="1.0"?> | ||
| <Root> | ||
| <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> | ||
| <ds:SignedInfo> | ||
| <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> | ||
| <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> | ||
| <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> | ||
| </ds:SignedInfo> | ||
| <ds:SignatureValue>AA==</ds:SignatureValue> | ||
| </ds:Signature> | ||
| </Root> | ||
| XML; | ||
| checkStruct($dupCanon, 'DUP_CANON'); | ||
|
|
||
| $dupSigMethod = <<<XML | ||
| <?xml version="1.0"?> | ||
| <Root> | ||
| <ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#"> | ||
| <ds:SignedInfo> | ||
| <ds:CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#"/> | ||
| <ds:SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1"/> | ||
| <ds:SignatureMethod Algorithm="http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"/> | ||
| </ds:SignedInfo> | ||
| <ds:SignatureValue>AA==</ds:SignatureValue> | ||
| </ds:Signature> | ||
| </Root> | ||
| XML; | ||
| checkStruct($dupSigMethod, 'DUP_SIGMETHOD'); | ||
| ?> | ||
| --EXPECTF-- | ||
| DUP_CANON: Invalid structure - Too many CanonicalizationMethod elements found | ||
| DUP_SIGMETHOD: Invalid structure - Too many SignatureMethod elements found |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What are you trying to filter here? XML-attributes? With optional prefix?