-
Notifications
You must be signed in to change notification settings - Fork 8.7k
Fix SAML artifact binding failing to validate signed nested samlp:Response #50862
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
pskopek
merged 2 commits into
keycloak:main
from
jimmychakkalakal:bug/Fix-50684-SAML_validate_signed_nested_samlp_Response
Jul 17, 2026
Merged
Changes from all commits
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
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
188 changes: 188 additions & 0 deletions
188
saml-core/src/test/java/org/keycloak/saml/processing/api/saml/v2/sig/SAML2SignatureTest.java
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,188 @@ | ||
| /* | ||
| * Copyright 2025 Red Hat, Inc. and/or its affiliates | ||
| * and other contributors as indicated by the @author tags. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
| package org.keycloak.saml.processing.api.saml.v2.sig; | ||
|
|
||
| import java.security.KeyPair; | ||
| import java.security.KeyPairGenerator; | ||
| import javax.xml.crypto.dsig.CanonicalizationMethod; | ||
| import javax.xml.crypto.dsig.DigestMethod; | ||
| import javax.xml.crypto.dsig.SignatureMethod; | ||
|
|
||
| import org.keycloak.rotation.HardcodedKeyLocator; | ||
| import org.keycloak.saml.common.util.DocumentUtil; | ||
| import org.keycloak.saml.processing.core.util.XMLSignatureUtil; | ||
|
|
||
| import org.junit.BeforeClass; | ||
| import org.junit.Test; | ||
| import org.w3c.dom.Document; | ||
| import org.w3c.dom.Element; | ||
|
|
||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
| import static org.hamcrest.Matchers.is; | ||
|
|
||
| public class SAML2SignatureTest { | ||
|
|
||
| private static final String PROTOCOL_NS = "urn:oasis:names:tc:SAML:2.0:protocol"; | ||
| private static final String ASSERTION_NS = "urn:oasis:names:tc:SAML:2.0:assertion"; | ||
|
|
||
| private static KeyPair keyPair; | ||
|
|
||
| @BeforeClass | ||
| public static void generateKeys() throws Exception { | ||
| KeyPairGenerator gen = KeyPairGenerator.getInstance("RSA"); | ||
| gen.initialize(2048); | ||
| keyPair = gen.generateKeyPair(); | ||
| } | ||
|
|
||
| /** | ||
| * | ||
| * {@link SAML2Signature#validate(Document, org.keycloak.rotation.KeyLocator)} | ||
| * must return {@code true} when the document has a signed nested | ||
| * {@code samlp:Response} inside an unsigned {@code samlp:ArtifactResponse}. | ||
| */ | ||
| @Test | ||
| public void validateAcceptsSignedNestedResponseInsideArtifactResponse() throws Exception { | ||
| Document doc = buildArtifactResponseWithSignedNestedResponse(); | ||
|
|
||
| SAML2Signature saml2Signature = new SAML2Signature(); | ||
| HardcodedKeyLocator locator = new HardcodedKeyLocator(keyPair.getPublic()); | ||
|
|
||
| assertThat(saml2Signature.validate(doc, locator), is(true)); | ||
| } | ||
|
|
||
| /** | ||
| * Sanity guard: signing the {@code ArtifactResponse} root element (the | ||
| * pre-existing supported path) must continue to validate correctly after the fix. | ||
| */ | ||
| @Test | ||
| public void validateAcceptsSignedArtifactResponseRoot() throws Exception { | ||
| String artifactResponseId = "AR_outer_2"; | ||
| String xml = "<samlp:ArtifactResponse" | ||
| + " xmlns:samlp=\"" + PROTOCOL_NS + "\"" | ||
| + " xmlns:saml=\"" + ASSERTION_NS + "\"" | ||
| + " ID=\"" + artifactResponseId + "\"" | ||
| + " InResponseTo=\"req2\"" | ||
| + " IssueInstant=\"2025-01-01T00:00:00Z\"" | ||
| + " Version=\"2.0\">" | ||
| + "<saml:Issuer>test-idp</saml:Issuer>" | ||
| + "</samlp:ArtifactResponse>"; | ||
|
|
||
| Document doc = DocumentUtil.getDocument(xml); | ||
| SAML2Signature saml2Signature = new SAML2Signature(); | ||
| saml2Signature.setSignatureMethod(SignatureMethod.RSA_SHA256); | ||
| saml2Signature.setDigestMethod(DigestMethod.SHA256); | ||
| saml2Signature.signSAMLDocument(doc, "test-key", keyPair, CanonicalizationMethod.EXCLUSIVE); | ||
|
|
||
| HardcodedKeyLocator locator = new HardcodedKeyLocator(keyPair.getPublic()); | ||
|
|
||
| assertThat(saml2Signature.validate(doc, locator), is(true)); | ||
| } | ||
|
|
||
| /** | ||
| * Regression test: an ArtifactResponse containing a signed LogoutResponse | ||
| * followed by an unsigned Response must be rejected. Without this guard an | ||
| * attacker could replay a signed protocol element alongside a forged | ||
| * unsigned Response and bypass signature validation. | ||
| */ | ||
| @Test | ||
| public void validateRejectsSignedSiblingPayloadWithUnsignedResponse() throws Exception { | ||
| String xml = "<samlp:ArtifactResponse" | ||
| + " xmlns:samlp=\"" + PROTOCOL_NS + "\"" | ||
| + " xmlns:saml=\"" + ASSERTION_NS + "\"" | ||
| + " ID=\"AR_multi\"" | ||
| + " Version=\"2.0\"" | ||
| + " IssueInstant=\"2025-01-01T00:00:00Z\">" | ||
| + "<samlp:Status/>" | ||
| + "<samlp:LogoutResponse" | ||
| + " ID=\"LR_signed\"" | ||
| + " Version=\"2.0\"" | ||
| + " IssueInstant=\"2025-01-01T00:00:00Z\">" | ||
| + "<saml:Issuer>test</saml:Issuer>" | ||
| + "</samlp:LogoutResponse>" | ||
| + "<samlp:Response" | ||
| + " ID=\"R_unsigned\"" | ||
| + " Version=\"2.0\"" | ||
| + " IssueInstant=\"2025-01-01T00:00:00Z\">" | ||
| + "<saml:Issuer>attacker</saml:Issuer>" | ||
| + "</samlp:Response>" | ||
| + "</samlp:ArtifactResponse>"; | ||
|
|
||
| Document doc = DocumentUtil.getDocument(xml); | ||
|
|
||
| Element logoutResponse = (Element) doc.getElementsByTagNameNS(PROTOCOL_NS, "LogoutResponse").item(0); | ||
| logoutResponse.setIdAttribute("ID", true); | ||
|
|
||
| XMLSignatureUtil.sign( | ||
| logoutResponse, | ||
| logoutResponse.getFirstChild(), | ||
| "test-key", | ||
| keyPair, | ||
| DigestMethod.SHA256, | ||
| SignatureMethod.RSA_SHA256, | ||
| "#LR_signed", | ||
| CanonicalizationMethod.EXCLUSIVE); | ||
|
|
||
| doc = DocumentUtil.getDocument(DocumentUtil.getDocumentAsString(doc)); | ||
|
|
||
| SAML2Signature saml2Signature = new SAML2Signature(); | ||
| HardcodedKeyLocator locator = new HardcodedKeyLocator(keyPair.getPublic()); | ||
|
|
||
| assertThat(saml2Signature.validate(doc, locator), is(false)); | ||
| } | ||
|
|
||
| /** | ||
| * Builds a document shaped like a real Shibboleth artifact response and | ||
| * signs only the <em>nested</em> {@code samlp:Response} sub-element: | ||
|
|
||
| * This matches the document shape that Shibboleth IdP 5.x produces by | ||
| * default and that triggered the bug. | ||
| */ | ||
| private Document buildArtifactResponseWithSignedNestedResponse() throws Exception { | ||
|
|
||
| String xml = "<samlp:ArtifactResponse" | ||
| + " xmlns:samlp=\"urn:oasis:names:tc:SAML:2.0:protocol\"" | ||
| + " xmlns:saml=\"urn:oasis:names:tc:SAML:2.0:assertion\"" | ||
| + " ID=\"" + "AR_outer_1" + "\"" | ||
| + " Version=\"2.0\"" | ||
| + " IssueInstant=\"2025-01-01T00:00:00Z\">" | ||
| + "<samlp:Status/>" | ||
| + "<samlp:Response" | ||
| + " ID=\"" + "R_inner_1" + "\"" | ||
| + " Version=\"2.0\"" | ||
| + " IssueInstant=\"2025-01-01T00:00:00Z\">" | ||
| + "<saml:Issuer>test</saml:Issuer>" | ||
| + "</samlp:Response>" | ||
| + "</samlp:ArtifactResponse>"; | ||
|
|
||
| Document doc = DocumentUtil.getDocument(xml); | ||
|
|
||
| Element nestedResponse = (Element) doc.getElementsByTagNameNS(PROTOCOL_NS, "Response").item(0); | ||
| nestedResponse.setIdAttribute("ID", true); | ||
|
|
||
| XMLSignatureUtil.sign( | ||
| nestedResponse, | ||
| nestedResponse.getFirstChild(), | ||
| "test-key", | ||
| keyPair, | ||
| DigestMethod.SHA256, | ||
| SignatureMethod.RSA_SHA256, | ||
| "#" + "R_inner_1", | ||
| CanonicalizationMethod.EXCLUSIVE); | ||
|
|
||
| return DocumentUtil.getDocument(DocumentUtil.getDocumentAsString(doc)); | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.