This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch 2_4_x-fixes in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git
commit 1cbbfe0314eec8e0c2e234bac8487ed0cce421a0 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Fri Nov 6 07:17:37 2026 +0000 Cache a OneTimeUse SAML Assertion ID only once the Assertion is trusted (#772) --- .../wss4j/dom/validate/SamlAssertionValidator.java | 10 ++- .../apache/wss4j/dom/saml/SamlNegativeTest.java | 76 ++++++++++++++++++++++ .../stax/validate/SamlTokenValidatorImpl.java | 12 ++-- 3 files changed, 91 insertions(+), 7 deletions(-) diff --git a/ws-security-dom/src/main/java/org/apache/wss4j/dom/validate/SamlAssertionValidator.java b/ws-security-dom/src/main/java/org/apache/wss4j/dom/validate/SamlAssertionValidator.java index 9b1e3dff3..1750fa72c 100644 --- a/ws-security-dom/src/main/java/org/apache/wss4j/dom/validate/SamlAssertionValidator.java +++ b/ws-security-dom/src/main/java/org/apache/wss4j/dom/validate/SamlAssertionValidator.java @@ -118,9 +118,6 @@ public class SamlAssertionValidator extends SignatureTrustValidator { // Check the AuthnStatements of the assertion (if any) checkAuthnStatements(samlAssertion); - // Check OneTimeUse Condition - checkOneTimeUse(samlAssertion, data); - // Validate the assertion against schemas/profiles validateAssertion(samlAssertion); @@ -128,6 +125,13 @@ public class SamlAssertionValidator extends SignatureTrustValidator { if (samlAssertion.isSigned()) { verifySignedAssertion(samlAssertion, data); } + + // Check OneTimeUse Condition. This must come last, as it caches the Assertion ID: caching it + // before the Assertion has been validated would let an attacker poison the cache with an + // Assertion (e.g. one signed by an untrusted key) that carries the ID of a genuine one, so + // that the genuine Assertion is subsequently rejected as a replay. + checkOneTimeUse(samlAssertion, data); + return credential; } diff --git a/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlNegativeTest.java b/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlNegativeTest.java index c18c967d9..449a46d21 100644 --- a/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlNegativeTest.java +++ b/ws-security-dom/src/test/java/org/apache/wss4j/dom/saml/SamlNegativeTest.java @@ -31,6 +31,8 @@ import org.apache.wss4j.dom.engine.WSSConfig; import org.apache.wss4j.dom.engine.WSSecurityEngine; import org.apache.wss4j.dom.handler.RequestData; import org.apache.wss4j.dom.handler.WSHandlerResult; +import org.apache.wss4j.common.cache.MemoryReplayCache; +import org.apache.wss4j.common.cache.ReplayCache; import org.apache.wss4j.common.crypto.Crypto; import org.apache.wss4j.common.crypto.CryptoFactory; import org.apache.wss4j.common.crypto.CryptoType; @@ -38,6 +40,7 @@ import org.apache.wss4j.common.crypto.Merlin; import org.apache.wss4j.common.ext.WSSecurityException; import org.apache.wss4j.common.saml.SAMLCallback; import org.apache.wss4j.common.saml.SAMLUtil; +import org.apache.wss4j.common.saml.bean.ConditionsBean; import org.apache.wss4j.common.saml.bean.SubjectBean; import org.apache.wss4j.common.saml.bean.Version; import org.apache.wss4j.common.saml.builder.SAML1Constants; @@ -383,6 +386,79 @@ public class SamlNegativeTest { } } + /** + * An Assertion with a OneTimeUse Condition that is rejected because its signature is not trusted + * must not have its ID cached. Otherwise an attacker could pre-empt a genuine Assertion by sending + * one with the same ID, signed by any key, so that the genuine one is rejected as a replay. + */ + @Test + public void testSAML2OneTimeUseUntrustedDoesNotPoisonCache() throws Exception { + ReplayCache replayCache = new MemoryReplayCache(); + String assertionId = "_" + java.util.UUID.randomUUID().toString(); + + // An attacker's Assertion with the ID of the genuine one, signed by an untrusted key + Document forgedDoc = createOneTimeUseSignedAssertionDoc( + assertionId, "16c73ab6-b892-458f-abf5-2f875f74882e", "security", + CryptoFactory.getInstance("crypto.properties") + ); + try { + verifyOneTimeUse(forgedDoc, replayCache); + fail("Failure expected on an untrusted signed assertion"); + } catch (WSSecurityException ex) { + assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.FAILURE); + } + + // The genuine Assertion, signed by the trusted issuer, must still be accepted + Document genuineDoc = + createOneTimeUseSignedAssertionDoc(assertionId, "wss40_server", "security", issuerCrypto); + verifyOneTimeUse(genuineDoc, replayCache); + + // ...but only once + try { + verifyOneTimeUse(genuineDoc, replayCache); + fail("Expected failure on a replay attack"); + } catch (WSSecurityException ex) { + assertTrue(ex.getErrorCode() == WSSecurityException.ErrorCode.INVALID_SECURITY); + } + + replayCache.close(); + } + + private Document createOneTimeUseSignedAssertionDoc( + String assertionId, String alias, String password, Crypto signingCrypto + ) throws Exception { + SAML2CallbackHandler callbackHandler = new SAML2CallbackHandler(); + callbackHandler.setStatement(SAML2CallbackHandler.Statement.AUTHN); + callbackHandler.setIssuer("www.example.com"); + callbackHandler.setConfirmationMethod(SAML2Constants.CONF_BEARER); + + ConditionsBean conditions = new ConditionsBean(); + conditions.setTokenPeriodMinutes(5); + conditions.setOneTimeUse(true); + callbackHandler.setConditions(conditions); + + SAMLCallback samlCallback = new SAMLCallback(); + SAMLUtil.doSAMLCallback(callbackHandler, samlCallback); + SamlAssertionWrapper samlAssertion = new SamlAssertionWrapper(samlCallback); + samlAssertion.getSaml2().setID(assertionId); + samlAssertion.signAssertion(alias, password, signingCrypto, false); + + Document doc = SOAPUtil.toSOAPPart(SOAPUtil.SAMPLE_SOAP_MSG); + WSSecHeader secHeader = new WSSecHeader(doc); + secHeader.insertSecurityHeader(); + + WSSecSAMLToken wsSign = new WSSecSAMLToken(secHeader); + return wsSign.build(samlAssertion); + } + + private WSHandlerResult verifyOneTimeUse(Document doc, ReplayCache replayCache) throws Exception { + RequestData requestData = new RequestData(); + requestData.setCallbackHandler(callbackHandler); + requestData.setSigVerCrypto(trustCrypto); + requestData.setSamlOneTimeUseReplayCache(replayCache); + return secEngine.processSecurityHeader(doc, requestData); + } + /** * Verifies the soap envelope * diff --git a/ws-security-stax/src/main/java/org/apache/wss4j/stax/validate/SamlTokenValidatorImpl.java b/ws-security-stax/src/main/java/org/apache/wss4j/stax/validate/SamlTokenValidatorImpl.java index 805cfbe86..1d0b150cd 100644 --- a/ws-security-stax/src/main/java/org/apache/wss4j/stax/validate/SamlTokenValidatorImpl.java +++ b/ws-security-stax/src/main/java/org/apache/wss4j/stax/validate/SamlTokenValidatorImpl.java @@ -128,13 +128,17 @@ public class SamlTokenValidatorImpl extends SignatureTokenValidatorImpl implemen // Check the Subject Confirmation requirements verifySubjectConfirmationMethod(samlAssertionWrapper); - // Check OneTimeUse Condition - checkOneTimeUse(samlAssertionWrapper, - tokenContext.getWssSecurityProperties().getSamlOneTimeUseReplayCache()); - // Validate the assertion against schemas/profiles validateAssertion(samlAssertionWrapper); + // Check OneTimeUse Condition. This must come after every other check, as it caches the + // Assertion ID, and caching the ID of an Assertion that is then rejected would let an + // attacker poison the cache against a genuine Assertion with the same ID. The signature of + // a signed Assertion has already been verified, and its signing key trusted, by + // SAMLTokenInputHandler before this method is called. + checkOneTimeUse(samlAssertionWrapper, + tokenContext.getWssSecurityProperties().getSamlOneTimeUseReplayCache()); + Crypto sigVerCrypto = null; if (samlAssertionWrapper.isSigned()) { sigVerCrypto = tokenContext.getWssSecurityProperties().getSignatureVerificationCrypto();
