This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/saml-cache in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit b748c5df02a2a73e46683d40f27a8b069e401a3b Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Sat Dec 5 15:01:37 2026 +0000 Fix the inverted replay check in SAMLSSOResponseValidator --- .../core/samlsso/SAMLSSOResponseValidator.java | 5 +- .../core/samlsso/SAMLSSOResponseValidatorTest.java | 138 +++++++++++++++++++++ 2 files changed, 140 insertions(+), 3 deletions(-) diff --git a/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidator.java b/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidator.java index 5e15ec93..a79c1207 100644 --- a/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidator.java +++ b/plugins/core/src/main/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidator.java @@ -227,12 +227,11 @@ public class SAMLSSOResponseValidator { // Need to keep bearer assertion IDs based on NotOnOrAfter to detect replay attacks if (postBinding && replayCache != null) { if (replayCache.contains(id)) { - Instant expires = subjectConfData.getNotOnOrAfter().toDate().toInstant(); - replayCache.add(id, expires); - } else { LOG.debug("Replay attack with token id: " + id); throw new WSSecurityException(WSSecurityException.ErrorCode.FAILURE, "invalidSAMLsecurity"); } + Instant expires = subjectConfData.getNotOnOrAfter().toDate().toInstant(); + replayCache.add(id, expires); } // Check address diff --git a/plugins/core/src/test/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidatorTest.java b/plugins/core/src/test/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidatorTest.java new file mode 100644 index 00000000..444191fe --- /dev/null +++ b/plugins/core/src/test/java/org/apache/cxf/fediz/core/samlsso/SAMLSSOResponseValidatorTest.java @@ -0,0 +1,138 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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.apache.cxf.fediz.core.samlsso; + +import java.util.Collections; +import java.util.UUID; + +import javax.xml.parsers.DocumentBuilderFactory; + +import org.w3c.dom.Document; + +import org.apache.cxf.fediz.core.SAML2CallbackHandler; +import org.apache.wss4j.common.cache.MemoryReplayCache; +import org.apache.wss4j.common.cache.ReplayCache; +import org.apache.wss4j.common.ext.WSSecurityException; +import org.apache.wss4j.common.saml.OpenSAMLUtil; +import org.apache.wss4j.common.saml.SAMLCallback; +import org.apache.wss4j.common.saml.SAMLUtil; +import org.apache.wss4j.common.saml.SamlAssertionWrapper; +import org.apache.wss4j.common.saml.bean.AudienceRestrictionBean; +import org.apache.wss4j.common.saml.bean.ConditionsBean; +import org.apache.wss4j.common.saml.bean.SubjectConfirmationDataBean; +import org.apache.wss4j.common.saml.builder.SAML2Constants; +import org.joda.time.DateTime; +import org.opensaml.saml.saml2.core.Response; +import org.opensaml.saml.saml2.core.Status; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +/** + * Tests for the bearer replay check in SAMLSSOResponseValidator (POST binding). + */ +public class SAMLSSOResponseValidatorTest { + + private static final String TEST_REQUEST_URL = "https://localhost/fedizhelloworld/"; + private static final String TEST_IDP_ISSUER = "http://url_to_the_issuer"; + + @BeforeAll + public static void init() { + OpenSAMLUtil.initSamlEngine(); + } + + @Test + public void testFirstUseAcceptedReplayRejected() throws Exception { + Response response = createResponse(); + try (ReplayCache replayCache = new MemoryReplayCache()) { + SAMLSSOResponseValidator validator = createValidator(replayCache); + + // First use must be accepted and the assertion ID cached + validator.validateSamlResponse(response, true); + Assertions.assertTrue(replayCache.contains(response.getAssertions().get(0).getID())); + + // Replay must be rejected + Assertions.assertThrows(WSSecurityException.class, + () -> validator.validateSamlResponse(response, true)); + } + } + + @Test + public void testNoReplayCheckWithoutPostBinding() throws Exception { + Response response = createResponse(); + try (ReplayCache replayCache = new MemoryReplayCache()) { + SAMLSSOResponseValidator validator = createValidator(replayCache); + + validator.validateSamlResponse(response, false); + validator.validateSamlResponse(response, false); + Assertions.assertFalse(replayCache.contains(response.getAssertions().get(0).getID())); + } + } + + private static SAMLSSOResponseValidator createValidator(ReplayCache replayCache) { + SAMLSSOResponseValidator validator = new SAMLSSOResponseValidator(); + validator.setIssuerIDP(TEST_IDP_ISSUER); + validator.setAssertionConsumerURL(TEST_REQUEST_URL); + validator.setSpIdentifier(TEST_REQUEST_URL); + validator.setEnforceAssertionsSigned(false); + validator.setReplayCache(replayCache); + return validator; + } + + private static Response createResponse() throws Exception { + String requestId = UUID.randomUUID().toString(); + + SAML2CallbackHandler callbackHandler = new SAML2CallbackHandler(); + callbackHandler.setAlsoAddAuthnStatement(true); + callbackHandler.setStatement(SAML2CallbackHandler.Statement.ATTR); + callbackHandler.setConfirmationMethod(SAML2Constants.CONF_BEARER); + callbackHandler.setIssuer(TEST_IDP_ISSUER); + callbackHandler.setSubjectName("alice"); + + ConditionsBean cp = new ConditionsBean(); + AudienceRestrictionBean audienceRestriction = new AudienceRestrictionBean(); + audienceRestriction.getAudienceURIs().add(TEST_REQUEST_URL); + cp.setAudienceRestrictions(Collections.singletonList(audienceRestriction)); + callbackHandler.setConditions(cp); + + SubjectConfirmationDataBean subjectConfirmationData = new SubjectConfirmationDataBean(); + subjectConfirmationData.setInResponseTo(requestId); + subjectConfirmationData.setNotAfter(new DateTime().plusMinutes(5)); + subjectConfirmationData.setRecipient(TEST_REQUEST_URL); + callbackHandler.setSubjectConfirmationData(subjectConfirmationData); + + SAMLCallback samlCallback = new SAMLCallback(); + SAMLUtil.doSAMLCallback(callbackHandler, samlCallback); + SamlAssertionWrapper assertion = new SamlAssertionWrapper(samlCallback); + + Status status = SAML2PResponseComponentBuilder.createStatus( + "urn:oasis:names:tc:SAML:2.0:status:Success", null); + Response response = SAML2PResponseComponentBuilder.createSAMLResponse( + requestId, assertion.getIssuerString(), status); + response.getAssertions().add(assertion.getSaml2()); + + DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); + dbf.setNamespaceAware(true); + Document doc = dbf.newDocumentBuilder().newDocument(); + doc.appendChild(OpenSAMLUtil.toDom(response, doc)); + return response; + } +}
