This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch 3.6.x-fixes in repository https://gitbox.apache.org/repos/asf/cxf.git
commit e6b23834d8dc0de436b74826246f93aba4e5b474 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Wed Dec 2 14:17:31 2026 +0000 Apply security.subject.cert.constraints in the SamlTokenInterceptor (#3812) (cherry picked from commit 200e5586975b0c1b4890464588d9cb112a5c3d0e) --- .../apache/cxf/common/util/PackageUtilsTest.java | 2 - .../apache/cxf/rt/security/SecurityConstants.java | 3 +- .../cxf/rt/security/utils/SecurityUtils.java | 32 +++++ .../cxf/rt/security/utils/SecurityUtilsTest.java | 82 ++++++++++++ .../ws/security/wss4j/SamlTokenInterceptor.java | 1 + .../SamlTokenInterceptorCertConstraintsTest.java | 148 +++++++++++++++++++++ 6 files changed, 265 insertions(+), 3 deletions(-) diff --git a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java index 9fc761d6ab5..c2b17d7b048 100644 --- a/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java +++ b/core/src/test/java/org/apache/cxf/common/util/PackageUtilsTest.java @@ -138,8 +138,6 @@ public class PackageUtilsTest { PackageUtils.getPackageNameByNameSpaceURI("http://www.case.cxf.apache.org")); assertEquals("org.apache.cxf._case", PackageUtils.getPackageNameByNameSpaceURI("http://www.Case.cxf.apache.org")); - assertEquals("org.apache.cxf._case", - PackageUtils.getPackageNameByNameSpaceURI("https://www.Case.cxf.apache.org")); assertEquals("org.apache.cxf._1_0", PackageUtils.getPackageNameByNameSpaceURI("http://cxf.apache.org/1.0")); // a leading character that is not valid anywhere in an identifier must not survive diff --git a/rt/security/src/main/java/org/apache/cxf/rt/security/SecurityConstants.java b/rt/security/src/main/java/org/apache/cxf/rt/security/SecurityConstants.java index d29f91c4dbb..69a06ba16a9 100644 --- a/rt/security/src/main/java/org/apache/cxf/rt/security/SecurityConstants.java +++ b/rt/security/src/main/java/org/apache/cxf/rt/security/SecurityConstants.java @@ -205,7 +205,8 @@ public class SecurityConstants { /** * A String of regular expressions (separated by the value specified for CERT_CONSTRAINTS_SEPARATOR) * which will be applied to the subject DN of the certificate used for signature validation, after trust - * verification of the certificate chain associated with the certificate. + * verification of the certificate chain associated with the certificate. It is applied by the WS-Security + * (DOM + streaming) interceptors, by the SamlTokenInterceptor and by the JAX-RS XML Signature handlers. */ public static final String SUBJECT_CERT_CONSTRAINTS = "security.subject.cert.constraints"; diff --git a/rt/security/src/main/java/org/apache/cxf/rt/security/utils/SecurityUtils.java b/rt/security/src/main/java/org/apache/cxf/rt/security/utils/SecurityUtils.java index 9526d91ddf5..a1c004e179d 100644 --- a/rt/security/src/main/java/org/apache/cxf/rt/security/utils/SecurityUtils.java +++ b/rt/security/src/main/java/org/apache/cxf/rt/security/utils/SecurityUtils.java @@ -23,8 +23,13 @@ import java.io.IOException; import java.io.InputStream; import java.net.URI; import java.net.URL; +import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.Properties; import java.util.logging.Logger; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; import javax.security.auth.callback.CallbackHandler; @@ -35,6 +40,7 @@ import org.apache.cxf.common.util.PropertyUtils; import org.apache.cxf.message.Message; import org.apache.cxf.phase.PhaseInterceptorChain; import org.apache.cxf.resource.ResourceManager; +import org.apache.cxf.rt.security.SecurityConstants; /** * Some common functionality @@ -190,4 +196,30 @@ public final class SecurityUtils { } return defaultValue; } + + /** + * Get the subject certificate constraints configured via SecurityConstants.SUBJECT_CERT_CONSTRAINTS, split + * using SecurityConstants.CERT_CONSTRAINTS_SEPARATOR (a comma by default) and compiled to regular + * expressions. An empty collection is returned if no constraints are configured. + * + * @throws PatternSyntaxException if a configured constraint is not a valid regular expression + */ + public static Collection<Pattern> getSubjectCertConstraints(Message message) { + Object certConstraints = getSecurityPropertyValue(SecurityConstants.SUBJECT_CERT_CONSTRAINTS, message); + if (!(certConstraints instanceof String) || ((String)certConstraints).isEmpty()) { + return Collections.emptyList(); + } + String separator = + (String)getSecurityPropertyValue(SecurityConstants.CERT_CONSTRAINTS_SEPARATOR, message); + if (separator == null || separator.isEmpty()) { + separator = ","; + } + + String[] certConstraintsList = ((String)certConstraints).split(separator); + Collection<Pattern> subjectCertConstraints = new ArrayList<>(certConstraintsList.length); + for (String certConstraint : certConstraintsList) { + subjectCertConstraints.add(Pattern.compile(certConstraint.trim())); + } + return subjectCertConstraints; + } } diff --git a/rt/security/src/test/java/org/apache/cxf/rt/security/utils/SecurityUtilsTest.java b/rt/security/src/test/java/org/apache/cxf/rt/security/utils/SecurityUtilsTest.java new file mode 100644 index 00000000000..c5b867ca7d4 --- /dev/null +++ b/rt/security/src/test/java/org/apache/cxf/rt/security/utils/SecurityUtilsTest.java @@ -0,0 +1,82 @@ +/** + * 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.rt.security.utils; + +import java.util.Collection; +import java.util.List; +import java.util.regex.Pattern; +import java.util.regex.PatternSyntaxException; +import java.util.stream.Collectors; + +import org.apache.cxf.message.Message; +import org.apache.cxf.message.MessageImpl; +import org.apache.cxf.rt.security.SecurityConstants; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertThrows; +import static org.junit.Assert.assertTrue; + +public class SecurityUtilsTest { + + @Test + public void testNoSubjectCertConstraints() { + assertTrue(SecurityUtils.getSubjectCertConstraints(new MessageImpl()).isEmpty()); + + Message message = new MessageImpl(); + message.put(SecurityConstants.SUBJECT_CERT_CONSTRAINTS, ""); + assertTrue(SecurityUtils.getSubjectCertConstraints(message).isEmpty()); + } + + @Test + public void testSubjectCertConstraints() { + Message message = new MessageImpl(); + message.put(SecurityConstants.SUBJECT_CERT_CONSTRAINTS, ".*CN=alice.*, .*CN=bob.*"); + assertEquals(List.of(".*CN=alice.*", ".*CN=bob.*"), + patterns(SecurityUtils.getSubjectCertConstraints(message))); + } + + @Test + public void testSubjectCertConstraintsLegacyPropertyName() { + Message message = new MessageImpl(); + message.put("ws-" + SecurityConstants.SUBJECT_CERT_CONSTRAINTS, ".*CN=alice.*"); + assertEquals(List.of(".*CN=alice.*"), patterns(SecurityUtils.getSubjectCertConstraints(message))); + } + + @Test + public void testSubjectCertConstraintsCustomSeparator() { + Message message = new MessageImpl(); + message.put(SecurityConstants.SUBJECT_CERT_CONSTRAINTS, "CN=alice,O=apache;CN=bob,O=apache"); + message.put(SecurityConstants.CERT_CONSTRAINTS_SEPARATOR, ";"); + assertEquals(List.of("CN=alice,O=apache", "CN=bob,O=apache"), + patterns(SecurityUtils.getSubjectCertConstraints(message))); + } + + @Test + public void testInvalidSubjectCertConstraint() { + Message message = new MessageImpl(); + message.put(SecurityConstants.SUBJECT_CERT_CONSTRAINTS, "CN=(alice"); + assertThrows(PatternSyntaxException.class, () -> SecurityUtils.getSubjectCertConstraints(message)); + } + + private static List<String> patterns(Collection<Pattern> constraints) { + return constraints.stream().map(Pattern::pattern).collect(Collectors.toList()); + } +} diff --git a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java index 3c954c61961..68b84172e9c 100644 --- a/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java +++ b/rt/ws/security/src/main/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptor.java @@ -204,6 +204,7 @@ public class SamlTokenInterceptor extends AbstractTokenInterceptor { PropertyUtils.isTrue(SecurityUtils.getSecurityPropertyValue(SecurityConstants.ENABLE_REVOCATION, message)); data.setEnableRevocation(enableRevocation); + data.setSubjectCertConstraints(SecurityUtils.getSubjectCertConstraints(message)); WSDocInfo wsDocInfo = new WSDocInfo(tokenElement.getOwnerDocument()); data.setWsDocInfo(wsDocInfo); diff --git a/rt/ws/security/src/test/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptorCertConstraintsTest.java b/rt/ws/security/src/test/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptorCertConstraintsTest.java new file mode 100644 index 00000000000..aad5d00a7d2 --- /dev/null +++ b/rt/ws/security/src/test/java/org/apache/cxf/ws/security/wss4j/SamlTokenInterceptorCertConstraintsTest.java @@ -0,0 +1,148 @@ +/** + * 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.ws.security.wss4j; + +import java.io.InputStream; +import java.security.KeyStore; +import java.util.Collections; + +import javax.xml.namespace.QName; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import org.apache.cxf.binding.soap.SoapFault; +import org.apache.cxf.binding.soap.SoapHeader; +import org.apache.cxf.binding.soap.SoapMessage; +import org.apache.cxf.helpers.DOMUtils; +import org.apache.cxf.message.ExchangeImpl; +import org.apache.cxf.message.MessageImpl; +import org.apache.cxf.security.SecurityContext; +import org.apache.cxf.ws.policy.AssertionInfoMap; +import org.apache.cxf.ws.security.SecurityConstants; +import org.apache.wss4j.common.WSS4JConstants; +import org.apache.wss4j.common.crypto.Crypto; +import org.apache.wss4j.common.crypto.Merlin; +import org.apache.wss4j.common.saml.SAMLCallback; +import org.apache.wss4j.common.saml.SamlAssertionWrapper; +import org.apache.wss4j.common.saml.bean.SubjectBean; +import org.apache.wss4j.common.saml.bean.Version; +import org.apache.wss4j.common.saml.builder.SAML2Constants; +import org.apache.wss4j.dom.engine.WSSConfig; + +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertThrows; + +/** + * Check that SecurityConstants.SUBJECT_CERT_CONSTRAINTS is applied by the SamlTokenInterceptor when validating + * the signature of a received SAML Assertion. + */ +public class SamlTokenInterceptorCertConstraintsTest { + + private static Crypto signingCrypto; + private static Crypto caOnlyCrypto; + + @BeforeClass + public static void setUp() throws Exception { + WSSConfig.init(); + + KeyStore keyStore = KeyStore.getInstance("JKS"); + try (InputStream is = + SamlTokenInterceptorCertConstraintsTest.class.getResourceAsStream("/alice.jks")) { + keyStore.load(is, "password".toCharArray()); + } + Merlin merlin = new Merlin(); + merlin.setKeyStore(keyStore); + signingCrypto = merlin; + + // Only trust the CA, so that the signing certificate is validated via chain trust + KeyStore trustStore = KeyStore.getInstance("JKS"); + trustStore.load(null, null); + trustStore.setCertificateEntry("cxfca", keyStore.getCertificate("cxfca")); + Merlin caMerlin = new Merlin(); + caMerlin.setTrustStore(trustStore); + caOnlyCrypto = caMerlin; + } + + @Test + public void testNoConstraints() throws Exception { + assertPrincipal(process(null)); + } + + @Test + public void testMatchingConstraint() throws Exception { + assertPrincipal(process(".*CN=alice.*")); + } + + @Test + public void testNonMatchingConstraint() throws Exception { + SoapMessage message = createMessage(".*CN=Lab STS.*"); + assertThrows(SoapFault.class, () -> new SamlTokenInterceptor().processToken(message)); + assertNull(message.get(SecurityContext.class)); + } + + private static SoapMessage process(String constraints) throws Exception { + SoapMessage message = createMessage(constraints); + new SamlTokenInterceptor().processToken(message); + return message; + } + + private static void assertPrincipal(SoapMessage message) { + SecurityContext sc = message.get(SecurityContext.class); + assertNotNull(sc); + assertEquals("administrator", sc.getUserPrincipal().getName()); + } + + private static SoapMessage createMessage(String constraints) throws Exception { + SoapMessage message = new SoapMessage(new MessageImpl()); + ExchangeImpl exchange = new ExchangeImpl(); + exchange.setInMessage(message); + message.setExchange(exchange); + // The SAML OneTimeUse replay cache is not relevant to this test + message.put(SecurityConstants.ENABLE_SAML_ONE_TIME_USE_CACHE, "false"); + message.put(AssertionInfoMap.class, new AssertionInfoMap(Collections.emptyList())); + message.put(SecurityConstants.SIGNATURE_CRYPTO, caOnlyCrypto); + if (constraints != null) { + message.put(SecurityConstants.SUBJECT_CERT_CONSTRAINTS, constraints); + } + + Document doc = DOMUtils.createDocument(); + Element security = doc.createElementNS(WSS4JConstants.WSSE_NS, "wsse:Security"); + doc.appendChild(security); + security.appendChild(createSignedAssertion(doc)); + message.getHeaders().add(new SoapHeader(new QName(WSS4JConstants.WSSE_NS, "Security"), security)); + return message; + } + + private static Element createSignedAssertion(Document doc) throws Exception { + SAMLCallback callback = new SAMLCallback(); + callback.setSamlVersion(Version.SAML_20); + callback.setIssuer("sts"); + callback.setSubject(new SubjectBean("administrator", null, SAML2Constants.CONF_BEARER)); + + SamlAssertionWrapper assertion = new SamlAssertionWrapper(callback); + assertion.signAssertion("alice", "password", signingCrypto, false); + return assertion.toDOM(doc); + } +}
