-
Notifications
You must be signed in to change notification settings - Fork 170
[SECURITY] upgrade xml-crypto, fix signature xpath #215
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -243,10 +243,21 @@ decrypt_assertion = (dom, private_keys, cb) -> | |
| # This checks the signature of a saml document and returns either array containing the signed data if valid, or null | ||
| # if the signature is invalid. Comparing the result against null is NOT sufficient for signature checks as it doesn't | ||
| # verify the signature is signing the important content, nor is it preventing the parsing of unsigned content. | ||
| check_saml_signature = (xml, certificate) -> | ||
| check_saml_signature = (_xml, certificate) -> | ||
| # xml-crypto requires that whitespace is normalized as such: | ||
| # https://github.com/yaronn/xml-crypto/commit/17f75c538674c0afe29e766b058004ad23bd5136#diff-5dfe38baf287dcf756a17c2dd63483781b53bf4b669e10efdd01e74bcd8e780aL69 | ||
| xml = _xml.replace(/\r\n?/g, '\n') | ||
| doc = (new xmldom.DOMParser()).parseFromString(xml) | ||
|
|
||
| signature = xmlcrypto.xpath(doc, "./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']") | ||
| # Find the correct section of the XML doc to check the signature for | ||
| maybe_req = xmlcrypto.xpath(doc, "//*[local-name(.)='AuthnRequest']") | ||
| maybe_req = maybe_req && maybe_req[0] | ||
| maybe_resp = xmlcrypto.xpath(doc, "//*[local-name(.)='Response']") | ||
| maybe_resp = maybe_resp && maybe_resp[0] | ||
| maybe_assert = xmlcrypto.xpath(doc, "//*[local-name(.)='Assertion']") | ||
| maybe_assert = maybe_assert && maybe_assert[0] | ||
| to_check = maybe_req || maybe_resp || maybe_assert | ||
| signature = xmlcrypto.xpath(to_check, "./*[local-name(.)='Signature' and namespace-uri(.)='http://www.w3.org/2000/09/xmldsig#']") | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TIL that you could return matches via this way! I thought no-op, just thought it was neat. :) |
||
| return null unless signature.length is 1 | ||
| sig = new xmlcrypto.SignedXml() | ||
| sig.keyInfoProvider = getKey: -> format_pem(certificate, 'CERTIFICATE') | ||
|
|
||
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
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.
Is there ever a case where we might want to check an
Assertionfirst over anAuthnRequest? Or is it strictly this order?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.
Good question, I'm not entirely sure; I put them in this order just because it happened to let the tests pass - I don't even know what the set of accepted combinations of authreq, response, assertion should be... I think I would need to review the saml spec or something to get to the bottom of this.
Uh oh!
There was an error while loading. Please reload this page.
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.
This seems possible to validate an improper
AuthnRequestif there happens to be aResponsenested within it. An actual test case eludes me, but it does seem possible.To address these multiple checks, I think we have to consider two things: why the initial
xpathchange was made, and what happens with this new version ofxpath.Why the initial
xpathchange was madePreviously, we selected all Signatures, and relied on only one
<Signature>being present. #82 changed this to be more flexible in cases where a<Response>and<Assertion>could both have a<Signature>.From how I'm interpreting section 5.3, page 68-69 of the SAML spec:
We need to check for the presence of a signature on both the request or response, or the enclosed assertion.
The internal
<Assertion>does not have a signature, but the parent SAML element does have a signature and the signature applies to the<Assertion>plus its children, then the<Assertion>takes on the signature of the parent SAML element.We never have a case (defined in spec) when both the signature of the parent element and the internal
<Assertion>does have a signature. Do we verify both? Verify the parent element's signature applies to the wrapped<Assertion>and validate only the parent?Ideally, as we're fixing this signature
xpath, we're deliberate on these cases to properly justify which element's<Signature>we should validate. In this PR, it seems like we would do<Assertion>><Response>><AuthnRequest>, which doesn't seem right. Changing this PR to validate the<Signature>closest to the root would be in line with what this library has done in the past, but it still might not be right.What happens with this new version of
xpathI'm initially only testing the cases for
sign_authn_request_with_embedded_signatureandcheck_saml_signature.If we use the version in this PR and specifically scope it to the entire document:
we end up failing the check because no signature is properly grabbed:
If we use the current version on
master:we can see that those tests pass because the proper signatures are grabbed:
Ideally, we change it so that the signatures are being grabbed properly.
If we change it to undo #82, we pass all the checks (grab signatures within each document), but fail when we grab signatures in both the
<Assertion>and the<Response>.If we change the
xpathto point to thedocumentElementof the parsed XML, things pass:and passes all the other checks. It still feels wrong (taking the first direct descendant for
Signature) but it does seem to preserve the original semantics. Happy to discuss further if that's the right approach.