-
Notifications
You must be signed in to change notification settings - Fork 472
Implement query signature verification #449
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
Open
Jguer
wants to merge
5
commits into
crewjam:main
Choose a base branch
from
grafana:jguer/validate-query-signatures
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
ea6eee2
implement query signature verification
Jguer 29539f6
Merge branch 'main' into jguer/validate-query-signatures
Jguer f920bb2
Fix query signature CI issues
Jguer 234f435
Fix lint CI Go version mismatch
Jguer 21da9e3
Add detached signature validation fixes
Jguer 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
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,142 @@ | ||
| package saml | ||
|
|
||
| import ( | ||
| "crypto" | ||
| "crypto/rsa" | ||
| "crypto/sha1" // #nosec G505 | ||
| "crypto/sha256" | ||
| "crypto/sha512" | ||
| "crypto/x509" | ||
| "encoding/base64" | ||
| "errors" | ||
| "fmt" | ||
| "net/url" | ||
|
|
||
| dsig "github.com/russellhaering/goxmldsig" | ||
| ) | ||
|
|
||
| type reqType string | ||
|
|
||
| const ( | ||
| samlRequest reqType = "SAMLRequest" | ||
| samlResponse reqType = "SAMLResponse" | ||
| ) | ||
|
|
||
| var ( | ||
| // ErrInvalidQuerySignature is returned when the query signature is invalid | ||
| ErrInvalidQuerySignature = errors.New("invalid query signature") | ||
| // ErrNoQuerySignature is returned when the query does not contain a signature | ||
| ErrNoQuerySignature = errors.New("query Signature or SigAlg not found") | ||
| ) | ||
|
|
||
| // Sign Query with the SP private key. | ||
| // Returns provided query with the SigAlg and Signature parameters added. | ||
| func (sp *ServiceProvider) signQuery(reqT reqType, query, body, relayState string) (string, error) { | ||
| signingContext, err := GetSigningContext(sp) | ||
|
|
||
| // Encode Query as standard demands. query.Encode() is not standard compliant | ||
| toHash := string(reqT) + "=" + url.QueryEscape(body) | ||
| if relayState != "" { | ||
| toHash += "&RelayState=" + url.QueryEscape(relayState) | ||
| } | ||
|
|
||
| toHash += "&SigAlg=" + url.QueryEscape(sp.SignatureMethod) | ||
|
|
||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| sig, err := signingContext.SignString(toHash) | ||
| if err != nil { | ||
| return "", err | ||
| } | ||
|
|
||
| query += "&SigAlg=" + url.QueryEscape(sp.SignatureMethod) | ||
| query += "&Signature=" + url.QueryEscape(base64.StdEncoding.EncodeToString(sig)) | ||
|
|
||
| return query, nil | ||
| } | ||
|
|
||
| // validateSig validation of the signature of the Redirect Binding in query values | ||
| // Query is valid if return is nil | ||
| func (sp *ServiceProvider) validateQuerySig(query url.Values) error { | ||
| sig := query.Get("Signature") | ||
| alg := query.Get("SigAlg") | ||
| if sig == "" || alg == "" { | ||
| return ErrNoQuerySignature | ||
| } | ||
|
|
||
| certs, err := sp.getIDPSigningCerts() | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| respType := "" | ||
| switch { | ||
| case query.Get("SAMLResponse") != "": | ||
| respType = "SAMLResponse" | ||
| case query.Get("SAMLRequest") != "": | ||
| respType = "SAMLRequest" | ||
| default: | ||
| return fmt.Errorf("no SAMLResponse or SAMLRequest found in query") | ||
| } | ||
|
|
||
| // Encode Query as standard demands. | ||
| // query.Encode() is not standard compliant | ||
| // as query encoding order matters | ||
| res := respType + "=" + url.QueryEscape(query.Get(respType)) | ||
|
|
||
| relayState := query.Get("RelayState") | ||
| if relayState != "" { | ||
| res += "&RelayState=" + url.QueryEscape(relayState) | ||
| } | ||
|
|
||
| res += "&SigAlg=" + url.QueryEscape(alg) | ||
|
|
||
| // Signature is base64 encoded | ||
| sigBytes, err := base64.StdEncoding.DecodeString(sig) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to decode signature: %w", err) | ||
| } | ||
|
|
||
| var ( | ||
| hashed []byte | ||
| hashAlg crypto.Hash | ||
| sigAlg x509.SignatureAlgorithm | ||
| ) | ||
|
|
||
| // Hashed Query | ||
| switch alg { | ||
| case dsig.RSASHA256SignatureMethod: | ||
| hashed256 := sha256.Sum256([]byte(res)) | ||
| hashed = hashed256[:] | ||
| hashAlg = crypto.SHA256 | ||
| sigAlg = x509.SHA256WithRSA | ||
| case dsig.RSASHA512SignatureMethod: | ||
| hashed512 := sha512.Sum512([]byte(res)) | ||
| hashed = hashed512[:] | ||
| hashAlg = crypto.SHA512 | ||
| sigAlg = x509.SHA512WithRSA | ||
| case dsig.RSASHA1SignatureMethod: | ||
| hashed1 := sha1.Sum([]byte(res)) // #nosec G401 | ||
| hashed = hashed1[:] | ||
| hashAlg = crypto.SHA1 | ||
| sigAlg = x509.SHA1WithRSA | ||
| default: | ||
| return fmt.Errorf("unsupported signature algorithm: %s", alg) | ||
| } | ||
|
|
||
| // validate signature | ||
| for _, cert := range certs { | ||
| // verify cert is RSA | ||
| if cert.SignatureAlgorithm != sigAlg { | ||
| continue | ||
| } | ||
|
|
||
| if err := rsa.VerifyPKCS1v15(cert.PublicKey.(*rsa.PublicKey), hashAlg, hashed, sigBytes); err == nil { | ||
| return nil | ||
| } | ||
| } | ||
|
|
||
| return ErrInvalidQuerySignature | ||
| } |
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.
I see here that the PR validates a signature coming via query elements.
However, in line 1547 the code still calls
sp.validateSignature(doc.Root()), whichexpects a signature element in the XML document, and wants to verify it.
So, this PR seems to expect a signature in two places, required in the XML document, and optional in the query.
With keycloak I have a signature in the query element, and nothing in the XML document.
I expect this to fail.
I am missing something ?
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.
Added a querySig flag for myself, recording when a proper query sig is seen.
Check this flag if xml sign errors out with not present to avoid passing on that error
IOW
and
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.
Tested the modification against Keycloak and works. As expected it verifies the detached sig, and then ignores the missing embedded sig.
Also extended further to generate a proper detached sig for logout requests. That solved issues with OKTA.
References:
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.
@andreas-kupries - have you verified both IDP initiated and SP initiated single logouts? I cannot see any reference to handle LogoutRequest?
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.
Hi. According to our QA (First two points at rancher/rancher#38494 (comment)) both IDP initiated (logout of Okta) and SP initiated (logout of Rancher) were tested.
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.
Nice, I was thinking is there a way we can bring the tested fixes to this repo?
cc @crewjam
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.
I believe the following signature verification checking is not needed for HTTP-Redirect binding SAMLResponse as it is already done in the previous step by this PR, looks like ValidateLogoutResponseRequest handle the
HTTP-Redirectbinding and ValidateLogoutResponseForm handlesHTTP-POST Binding