This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/xss in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit cda5c95361a6442da93c11107b56e29a1ff39ca3 Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Mon Nov 23 07:29:44 2026 +0000 Fix reflected XSS and unvalidated ACS URL in IdP response forms --- .../webapp/WEB-INF/flows/saml-validate-request.xml | 10 +- .../WEB-INF/views/samlsigninresponseform.jsp | 4 +- .../WEB-INF/views/samlsignoutresponseform.jsp | 4 +- .../webapp/WEB-INF/views/signinresponseform.jsp | 4 +- .../apache/cxf/fediz/systests/samlsso/IdpTest.java | 109 +++++++++++++++++---- 5 files changed, 106 insertions(+), 25 deletions(-) diff --git a/services/idp/src/main/webapp/WEB-INF/flows/saml-validate-request.xml b/services/idp/src/main/webapp/WEB-INF/flows/saml-validate-request.xml index ab809066..5f7165ab 100644 --- a/services/idp/src/main/webapp/WEB-INF/flows/saml-validate-request.xml +++ b/services/idp/src/main/webapp/WEB-INF/flows/saml-validate-request.xml @@ -274,8 +274,16 @@ <!-- See if we managed to at least parse the request to get the response URL --> <if test="requestScope.samlAction == null or requestScope.samlAction.isEmpty() or requestScope.samlAction == '/'" then="viewBadRequestParsingError"/> + <!-- For a LogoutRequest the response URL is the configured logout endpoint, not request-supplied --> <if test="flowScope.saml_logout_request != null" - then="viewBadLogoutRequestResponse" else="viewBadRequestResponse"/> + then="viewBadLogoutRequestResponse"/> + <!-- The AssertionConsumerServiceURL comes from a request that failed validation (possibly + unsigned), so only POST the error response to it if it is an endpoint configured for + the application, as on the normal signin path (see validateReturnAddress) --> + <if test="commonsURLValidator.isValid(flowRequestContext, requestScope.samlAction) + and endpointAddressValidator.isValidSigninAddress(flowRequestContext, requestScope.samlAction, + authnRequestParser.retrieveRealm(flowRequestContext))" + then="viewBadRequestResponse" else="viewBadRequestParsingError"/> </decision-state> <end-state id="viewBadRequestResponse" view="samlsigninresponseform"> diff --git a/services/idp/src/main/webapp/WEB-INF/views/samlsigninresponseform.jsp b/services/idp/src/main/webapp/WEB-INF/views/samlsigninresponseform.jsp index fdb4eec4..93eece0a 100644 --- a/services/idp/src/main/webapp/WEB-INF/views/samlsigninresponseform.jsp +++ b/services/idp/src/main/webapp/WEB-INF/views/samlsigninresponseform.jsp @@ -8,8 +8,8 @@ </head> <body onload='documentLoaded()'> <form:form method="POST" id="samlsigninresponseform" name="samlsigninresponseform" action="${samlAction}" htmlEscape="true"> - <input type="hidden" name="SAMLResponse" value="${samlResponse}" /><br /> - <input type="hidden" name="RelayState" value="${relayState}" /><br /> + <input type="hidden" name="SAMLResponse" value="<spring:escapeBody htmlEscape="true">${samlResponse}</spring:escapeBody>" /><br /> + <input type="hidden" name="RelayState" value="<spring:escapeBody htmlEscape="true">${relayState}</spring:escapeBody>" /><br /> <noscript> <p>Script is disabled. Click Submit to continue.</p> <input type="submit" name="_eventId_submit" value="Submit" /><br /> diff --git a/services/idp/src/main/webapp/WEB-INF/views/samlsignoutresponseform.jsp b/services/idp/src/main/webapp/WEB-INF/views/samlsignoutresponseform.jsp index ca3eaefe..c1495b24 100644 --- a/services/idp/src/main/webapp/WEB-INF/views/samlsignoutresponseform.jsp +++ b/services/idp/src/main/webapp/WEB-INF/views/samlsignoutresponseform.jsp @@ -8,8 +8,8 @@ </head> <body onload='documentLoaded()'> <form:form method="POST" id="samlsignoutresponseform" name="samlsignoutresponseform" action="${samlAction}" htmlEscape="true"> - <input type="hidden" name="SAMLResponse" value="${samlResponse}" /><br /> - <input type="hidden" name="RelayState" value="${relayState}" /><br /> + <input type="hidden" name="SAMLResponse" value="<spring:escapeBody htmlEscape="true">${samlResponse}</spring:escapeBody>" /><br /> + <input type="hidden" name="RelayState" value="<spring:escapeBody htmlEscape="true">${relayState}</spring:escapeBody>" /><br /> <noscript> <p>Script is disabled. Click Submit to continue.</p> <input type="submit" name="_eventId_submit" value="Submit" /><br /> diff --git a/services/idp/src/main/webapp/WEB-INF/views/signinresponseform.jsp b/services/idp/src/main/webapp/WEB-INF/views/signinresponseform.jsp index afa703e7..392a2483 100644 --- a/services/idp/src/main/webapp/WEB-INF/views/signinresponseform.jsp +++ b/services/idp/src/main/webapp/WEB-INF/views/signinresponseform.jsp @@ -12,9 +12,9 @@ <input type="hidden" name="wresult" value="${fedWResult}" /><br /> <% String wctx = (String)request.getAttribute("fedWCtx"); if (wctx != null && !wctx.isEmpty()) { %> - <input type="hidden" name="wctx" value="${fedWCtx}" /><br /> + <input type="hidden" name="wctx" value="<spring:escapeBody htmlEscape="true">${fedWCtx}</spring:escapeBody>" /><br /> <% } %> - <input type="hidden" name="wtrealm" value="${fedWTrealm}" /><br /> + <input type="hidden" name="wtrealm" value="<spring:escapeBody htmlEscape="true">${fedWTrealm}</spring:escapeBody>" /><br /> <noscript> <p>Script is disabled. Click Submit to continue.</p> <input type="submit" name="_eventId_submit" value="Submit" /><br /> diff --git a/systests/samlsso/src/test/java/org/apache/cxf/fediz/systests/samlsso/IdpTest.java b/systests/samlsso/src/test/java/org/apache/cxf/fediz/systests/samlsso/IdpTest.java index d2be8827..4d13691f 100644 --- a/systests/samlsso/src/test/java/org/apache/cxf/fediz/systests/samlsso/IdpTest.java +++ b/systests/samlsso/src/test/java/org/apache/cxf/fediz/systests/samlsso/IdpTest.java @@ -853,12 +853,14 @@ public class IdpTest { new UsernamePasswordCredentials(USER, PWD)); webClient.getOptions().setJavaScriptEnabled(false); - final HtmlPage idpPage = webClient.getPage(url); - - org.opensaml.saml.saml2.core.Response samlResponse = - parseSAMLResponse(idpPage, relayState, consumerURL, authnRequest.getID()); - String expected = "urn:oasis:names:tc:SAML:2.0:status:Requester"; - Assertions.assertEquals(expected, samlResponse.getStatus().getStatusCode().getValue()); + // The AssertionConsumerServiceURL cannot be checked against a configured application, + // so the IdP must not POST a response to it + try { + webClient.getPage(url); + Assertions.fail("Failure expected on an unknown Issuer"); + } catch (FailingHttpStatusCodeException ex) { + Assertions.assertEquals(400, ex.getStatusCode()); + } webClient.close(); } @@ -891,12 +893,14 @@ public class IdpTest { new UsernamePasswordCredentials(USER, PWD)); webClient.getOptions().setJavaScriptEnabled(false); - final HtmlPage idpPage = webClient.getPage(url); - - org.opensaml.saml.saml2.core.Response samlResponse = - parseSAMLResponse(idpPage, relayState, consumerURL, authnRequest.getID()); - String expected = "urn:oasis:names:tc:SAML:2.0:status:Requester"; - Assertions.assertEquals(expected, samlResponse.getStatus().getStatusCode().getValue()); + // The AssertionConsumerServiceURL cannot be checked against a configured application, + // so the IdP must not POST a response to it + try { + webClient.getPage(url); + Assertions.fail("Failure expected on a missing Issuer"); + } catch (FailingHttpStatusCodeException ex) { + Assertions.assertEquals(400, ex.getStatusCode()); + } webClient.close(); } @@ -1077,6 +1081,74 @@ public class IdpTest { webClient.close(); } + @org.junit.jupiter.api.Test + public void testUnsignedRequestUnknownConsumerURL() throws Exception { + OpenSAMLUtil.initSamlEngine(); + + // Unsigned request with an AssertionConsumerServiceURL that is not configured for the application + String consumerURL = "https://www.example.com/acs"; + AuthnRequest authnRequest = + new DefaultAuthnRequestBuilder().createAuthnRequest( + null, "urn:org:apache:cxf:fediz:fedizhelloworld", consumerURL + ); + authnRequest.setDestination("https://localhost:" + getIdpHttpsPort() + "/fediz-idp/saml"); + + String authnRequestEncoded = encodeAuthnRequest(authnRequest); + + String relayState = UUID.randomUUID().toString(); + String url = "https://localhost:" + getIdpHttpsPort() + "/fediz-idp/saml?" + + SSOConstants.RELAY_STATE + "=" + relayState + + "&" + SSOConstants.SAML_REQUEST + "=" + URLEncoder.encode(authnRequestEncoded, UTF_8.name()); + + final WebClient webClient = new WebClient(); + webClient.getOptions().setUseInsecureSSL(true); + + webClient.getOptions().setJavaScriptEnabled(false); + try { + webClient.getPage(url); + Assertions.fail("Failure expected on an unknown AssertionConsumerServiceURL"); + } catch (FailingHttpStatusCodeException ex) { + Assertions.assertEquals(400, ex.getStatusCode()); + } + + webClient.close(); + } + + @org.junit.jupiter.api.Test + public void testUnsignedRequestMaliciousRelayState() throws Exception { + OpenSAMLUtil.initSamlEngine(); + + String consumerURL = "https://localhost:" + getRpHttpsPort() + "/" + + getServletContextName() + "/secure/fedservlet"; + AuthnRequest authnRequest = + new DefaultAuthnRequestBuilder().createAuthnRequest( + null, "urn:org:apache:cxf:fediz:fedizhelloworld", consumerURL + ); + authnRequest.setDestination("https://localhost:" + getIdpHttpsPort() + "/fediz-idp/saml"); + + String authnRequestEncoded = encodeAuthnRequest(authnRequest); + + // The RelayState must be HTML-escaped in the response form rather than injected as markup + String relayState = "\"><script>alert(document.domain)</script>"; + String url = "https://localhost:" + getIdpHttpsPort() + "/fediz-idp/saml?" + + SSOConstants.RELAY_STATE + "=" + URLEncoder.encode(relayState, UTF_8.name()) + + "&" + SSOConstants.SAML_REQUEST + "=" + URLEncoder.encode(authnRequestEncoded, UTF_8.name()); + + final WebClient webClient = new WebClient(); + webClient.getOptions().setUseInsecureSSL(true); + + webClient.getOptions().setJavaScriptEnabled(false); + final HtmlPage idpPage = webClient.getPage(url); + + Assertions.assertFalse(idpPage.getWebResponse().getContentAsString().contains(relayState)); + org.opensaml.saml.saml2.core.Response samlResponse = + parseSAMLResponse(idpPage, relayState, consumerURL, authnRequest.getID()); + String expected = "urn:oasis:names:tc:SAML:2.0:status:Requester"; + Assertions.assertEquals(expected, samlResponse.getStatus().getStatusCode().getValue()); + + webClient.close(); + } + @org.junit.jupiter.api.Test public void testEmptySeparateSignature() throws Exception { OpenSAMLUtil.initSamlEngine(); @@ -1335,12 +1407,13 @@ public class IdpTest { new UsernamePasswordCredentials(USER, PWD)); webClient.getOptions().setJavaScriptEnabled(false); - final HtmlPage idpPage = webClient.getPage(url); - - org.opensaml.saml.saml2.core.Response samlResponse = - parseSAMLResponse(idpPage, relayState, consumerURL, authnRequest.getID()); - String expected = "urn:oasis:names:tc:SAML:2.0:status:Requester"; - Assertions.assertEquals(expected, samlResponse.getStatus().getStatusCode().getValue()); + // The IdP must not POST a response to an AssertionConsumerServiceURL that is not configured + try { + webClient.getPage(url); + Assertions.fail("Failure expected on an unknown AssertionConsumerServiceURL"); + } catch (FailingHttpStatusCodeException ex) { + Assertions.assertEquals(400, ex.getStatusCode()); + } webClient.close(); }
