This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/stax-timestamp in repository https://gitbox.apache.org/repos/asf/ws-wss4j.git
commit 2ce083b77f002df2865a62cc8445abbaa78ff472 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Tue Nov 10 07:39:43 2026 +0000 Require a processed Timestamp for IncludeTimestamp (StAX policy) --- .../wss4j/policy/stax/enforcer/PolicyEnforcer.java | 57 ++++++++++++------- .../policy/stax/test/VulnerabliltyVectorsTest.java | 65 +++++++++++++++++++++- 2 files changed, 102 insertions(+), 20 deletions(-) diff --git a/ws-security-policy-stax/src/main/java/org/apache/wss4j/policy/stax/enforcer/PolicyEnforcer.java b/ws-security-policy-stax/src/main/java/org/apache/wss4j/policy/stax/enforcer/PolicyEnforcer.java index c3280c74f..7a82fdf2f 100644 --- a/ws-security-policy-stax/src/main/java/org/apache/wss4j/policy/stax/enforcer/PolicyEnforcer.java +++ b/ws-security-policy-stax/src/main/java/org/apache/wss4j/policy/stax/enforcer/PolicyEnforcer.java @@ -102,6 +102,7 @@ import org.apache.wss4j.policy.stax.assertionStates.TokenProtectionAssertionStat import org.apache.wss4j.policy.stax.assertionStates.UsernameTokenAssertionState; import org.apache.wss4j.policy.stax.assertionStates.X509TokenAssertionState; import org.apache.wss4j.stax.ext.WSSConstants; +import org.apache.wss4j.stax.utils.WSSUtils; import org.apache.wss4j.stax.securityEvent.NoSecuritySecurityEvent; import org.apache.wss4j.stax.securityEvent.OperationSecurityEvent; import org.apache.wss4j.stax.securityEvent.UsernameTokenSecurityEvent; @@ -400,6 +401,42 @@ public class PolicyEnforcer implements SecurityEventListener { return true; } + /** + * Add the assertion states for the Timestamp property of a binding (WSP1.3, 6.2). + */ + private void addTimestampAssertionStates(List<Assertable> assertableList, AbstractBinding abstractBinding, + AbstractSecurityAssertion abstractSecurityAssertion) + throws WSSPolicyException { + //Where a Timestamp is required, the assertion only holds once a TIMESTAMP event has been + //received, i.e. once a Timestamp in the Security header for this actor/role has been + //validated. The RequiredElements and SignedElements assertions below only compare the + //element path, which a Timestamp moved into the Security header of another actor/role + //also matches, although it is never validated. + IncludeTimeStampAssertionState includeTimeStampAssertionState = + new IncludeTimeStampAssertionState(abstractBinding, policyAsserter, !abstractBinding.isIncludeTimestamp()); + assertableList.add(includeTimeStampAssertionState); + if (abstractBinding.isIncludeTimestamp()) { + List<QName> timestampElementPath = new LinkedList<>(); + if (soap12) { + timestampElementPath.addAll(WSSConstants.SOAP_12_WSSE_SECURITY_HEADER_PATH); + } else { + timestampElementPath.addAll(WSSConstants.SOAP_11_WSSE_SECURITY_HEADER_PATH); + } + timestampElementPath.add(WSSConstants.TAG_WSU_TIMESTAMP); + includeTimeStampAssertionState.setErrorMessage( + "Element " + WSSUtils.pathAsString(timestampElementPath) + " must be present"); + RequiredElementsAssertionState requiredElementsAssertionState = + new RequiredElementsAssertionState(abstractBinding, policyAsserter, false); + requiredElementsAssertionState.addElement(timestampElementPath); + assertableList.add(requiredElementsAssertionState); + + SignedElementsAssertionState signedElementsAssertionState = + new SignedElementsAssertionState(abstractSecurityAssertion, policyAsserter, true); + signedElementsAssertionState.addElement(timestampElementPath); + assertableList.add(signedElementsAssertionState); + } + } + protected List<Assertable> getAssertableForAssertion(AbstractSecurityAssertion abstractSecurityAssertion) throws WSSPolicyException { List<Assertable> assertableList = new LinkedList<>(); @@ -486,25 +523,7 @@ public class PolicyEnforcer implements SecurityEventListener { } //WSP1.3, 6.2 Timestamp Property - assertableList.add(new IncludeTimeStampAssertionState(abstractBinding, policyAsserter, true)); - if (abstractBinding.isIncludeTimestamp()) { - List<QName> timestampElementPath = new LinkedList<>(); - if (soap12) { - timestampElementPath.addAll(WSSConstants.SOAP_12_WSSE_SECURITY_HEADER_PATH); - } else { - timestampElementPath.addAll(WSSConstants.SOAP_11_WSSE_SECURITY_HEADER_PATH); - } - timestampElementPath.add(WSSConstants.TAG_WSU_TIMESTAMP); - RequiredElementsAssertionState requiredElementsAssertionState = - new RequiredElementsAssertionState(abstractBinding, policyAsserter, false); - requiredElementsAssertionState.addElement(timestampElementPath); - assertableList.add(requiredElementsAssertionState); - - SignedElementsAssertionState signedElementsAssertionState = - new SignedElementsAssertionState(abstractSecurityAssertion, policyAsserter, true); - signedElementsAssertionState.addElement(timestampElementPath); - assertableList.add(signedElementsAssertionState); - } + addTimestampAssertionStates(assertableList, abstractBinding, abstractSecurityAssertion); } else if (abstractSecurityAssertion instanceof Wss10) { Wss10 wss10 = (Wss10)abstractSecurityAssertion; String namespace = wss10.getName().getNamespaceURI(); diff --git a/ws-security-policy-stax/src/test/java/org/apache/wss4j/policy/stax/test/VulnerabliltyVectorsTest.java b/ws-security-policy-stax/src/test/java/org/apache/wss4j/policy/stax/test/VulnerabliltyVectorsTest.java index 7a6ba07b8..46d984054 100644 --- a/ws-security-policy-stax/src/test/java/org/apache/wss4j/policy/stax/test/VulnerabliltyVectorsTest.java +++ b/ws-security-policy-stax/src/test/java/org/apache/wss4j/policy/stax/test/VulnerabliltyVectorsTest.java @@ -303,4 +303,67 @@ public class VulnerabliltyVectorsTest extends AbstractTestBase { assertEquals(((WSSecurityException) throwable).getFaultCode(), WSSecurityException.INVALID_SECURITY); } } -} \ No newline at end of file + + /** + * IncludeTimestamp must be satisfied by a Timestamp that has actually been processed, i.e. one + * in the Security header for this actor/role. Here the signed Timestamp is moved, unchanged, + * into a Security header for another actor: it is never validated, so its expiry and the replay + * cache are not checked, but its element path still matches the RequiredElements and + * SignedElements assertions, and its signature still verifies. + */ + @Test + public void testSignedTimestampMovedToSecurityHeaderOfAnotherActor() throws Exception { + InputStream sourceDocument = this.getClass().getClassLoader().getResourceAsStream("testdata/plain-soap-1.1.xml"); + + String action = WSHandlerConstants.TIMESTAMP + " " + WSHandlerConstants.SIGNATURE + " " + WSHandlerConstants.ENCRYPTION; + Properties properties = new Properties(); + properties.setProperty(WSHandlerConstants.SIGNATURE_PARTS, "{Element}{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp;{Element}{http://schemas.xmlsoap.org/soap/envelope/}Body;"); + properties.setProperty(WSHandlerConstants.ENC_SYM_ALGO, "http://www.w3.org/2001/04/xmlenc#aes256-cbc"); + Document securedDocument = doOutboundSecurityWithWSS4J(sourceDocument, action, properties); + + // The genuine message meets the policy + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + TRANSFORMER_FACTORY.newTransformer().transform(new DOMSource(securedDocument), new StreamResult(baos)); + verifyWithGoodPolicy(baos.toByteArray()); + + // Move the signed Timestamp into a Security header for another actor + Element timestamp = (Element) securedDocument.getElementsByTagNameNS( + WSSConstants.TAG_WSU_TIMESTAMP.getNamespaceURI(), WSSConstants.TAG_WSU_TIMESTAMP.getLocalPart()).item(0); + assertNotNull(timestamp); + Element headerElement = (Element) timestamp.getParentNode().getParentNode(); + timestamp.getParentNode().removeChild(timestamp); + Element otherSecurityHeader = securedDocument.createElementNS( + WSSConstants.TAG_WSSE_SECURITY.getNamespaceURI(), "wsse:" + WSSConstants.TAG_WSSE_SECURITY.getLocalPart()); + otherSecurityHeader.setAttributeNS(WSSConstants.NS_SOAP11, "soap:actor", "urn:another-actor"); + otherSecurityHeader.appendChild(timestamp); + headerElement.appendChild(otherSecurityHeader); + + baos = new ByteArrayOutputStream(); + TRANSFORMER_FACTORY.newTransformer().transform(new DOMSource(securedDocument), new StreamResult(baos)); + + try { + verifyWithGoodPolicy(baos.toByteArray()); + fail("Expected XMLStreamException"); + } catch (XMLStreamException e) { + Throwable throwable = e.getCause(); + assertNotNull(throwable); + assertTrue(throwable instanceof WSSecurityException); + assertEquals("Element /{http://schemas.xmlsoap.org/soap/envelope/}Envelope/{http://schemas.xmlsoap.org/soap/envelope/}Header/{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd}Security/{http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd}Timestamp must be present", + throwable.getMessage()); + assertEquals(WSSecurityException.INVALID_SECURITY, ((WSSecurityException) throwable).getFaultCode()); + } + } + + private void verifyWithGoodPolicy(byte[] message) throws Exception { + WSSSecurityProperties inSecurityProperties = new WSSSecurityProperties(); + inSecurityProperties.setCallbackHandler(new CallbackHandlerImpl()); + inSecurityProperties.loadSignatureVerificationKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray()); + inSecurityProperties.loadDecryptionKeystore(this.getClass().getClassLoader().getResource("receiver.jks"), "default".toCharArray()); + + PolicyEnforcerFactory policyEnforcerFactory = PolicyEnforcerFactory.newInstance(this.getClass().getClassLoader().getResource("testdata/wsdl/actionSpoofing.wsdl")); + PolicyEnforcer policyEnforcer = policyEnforcerFactory.newPolicyEnforcer("goodPolicy", false, null, 0, false); + inSecurityProperties.addInputProcessor(new PolicyInputProcessor(policyEnforcer, inSecurityProperties)); + + doInboundSecurity(inSecurityProperties, xmlInputFactory.createXMLStreamReader(new ByteArrayInputStream(message)), policyEnforcer); + } +}
