This is an automated email from the ASF dual-hosted git repository. coheigea pushed a commit to branch coheigea/url-encode in repository https://gitbox.apache.org/repos/asf/cxf-fediz.git
commit 262a55568c2de2f7d281a018210f68b4522ff17c Author: Colm O hEigeartaigh <[email protected]> AuthorDate: Sat Dec 5 15:29:46 2026 +0000 URL-encode parameters when the IdP redirects a sign-in request to itself --- .../idp/beans/samlsso/LocalRedirectCreator.java | 12 ++- .../LocalFederationRedirectCreator.java} | 37 +++---- .../idp/beans/LocalRedirectCreatorTest.java | 118 +++++++++++++++++++++ .../WEB-INF/flows/federation-validate-request.xml | 12 +-- 4 files changed, 147 insertions(+), 32 deletions(-) diff --git a/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java b/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java index 03798120..7d75aaec 100644 --- a/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java +++ b/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java @@ -37,18 +37,22 @@ public class LocalRedirectCreator { redirectURL.append(idp.getIdpUrl().toString()).append('?'); String relayState = (String)WebUtils.getAttributeFromFlowScope(context, "RelayState"); - redirectURL.append("RelayState=").append(relayState).append('&'); + if (relayState != null) { + redirectURL.append("RelayState=").append(URLEncoder.encode(relayState, "UTF-8")).append('&'); + } String samlRequest = (String)WebUtils.getAttributeFromFlowScope(context, "SAMLRequest"); redirectURL.append("SAMLRequest=").append(URLEncoder.encode(samlRequest, "UTF-8")); String signature = (String)WebUtils.getAttributeFromFlowScope(context, "Signature"); if (signature != null) { - redirectURL.append('&'); - redirectURL.append("Signature=").append(URLEncoder.encode(signature, "UTF-8")); + String sigAlg = (String)WebUtils.getAttributeFromFlowScope(context, "SigAlg"); + if (sigAlg != null) { + redirectURL.append("&SigAlg=").append(URLEncoder.encode(sigAlg, "UTF-8")); + } + redirectURL.append("&Signature=").append(URLEncoder.encode(signature, "UTF-8")); } return redirectURL.toString(); } - } diff --git a/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java b/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/wsfed/LocalFederationRedirectCreator.java similarity index 50% copy from services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java copy to services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/wsfed/LocalFederationRedirectCreator.java index 03798120..ba957192 100644 --- a/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/samlsso/LocalRedirectCreator.java +++ b/services/idp-core/src/main/java/org/apache/cxf/fediz/service/idp/beans/wsfed/LocalFederationRedirectCreator.java @@ -16,39 +16,40 @@ * specific language governing permissions and limitations * under the License. */ -package org.apache.cxf.fediz.service.idp.beans.samlsso; +package org.apache.cxf.fediz.service.idp.beans.wsfed; import java.io.UnsupportedEncodingException; import java.net.URLEncoder; +import org.apache.cxf.fediz.core.FederationConstants; import org.apache.cxf.fediz.service.idp.domain.Idp; import org.apache.cxf.fediz.service.idp.util.WebUtils; import org.springframework.stereotype.Component; import org.springframework.webflow.execution.RequestContext; /** - * Parse the parameters to create the URL for local redirection + * Create the (URL-encoded) URL used to redirect a WS-Federation sign-in request to the local IdP */ @Component -public class LocalRedirectCreator { +public class LocalFederationRedirectCreator { public String createRedirectURL(RequestContext context, Idp idp) throws UnsupportedEncodingException { - StringBuilder redirectURL = new StringBuilder(25); - redirectURL.append(idp.getIdpUrl().toString()).append('?'); - - String relayState = (String)WebUtils.getAttributeFromFlowScope(context, "RelayState"); - redirectURL.append("RelayState=").append(relayState).append('&'); - String samlRequest = (String)WebUtils.getAttributeFromFlowScope(context, "SAMLRequest"); - redirectURL.append("SAMLRequest=").append(URLEncoder.encode(samlRequest, "UTF-8")); - - String signature = (String)WebUtils.getAttributeFromFlowScope(context, "Signature"); - if (signature != null) { - redirectURL.append('&'); - redirectURL.append("Signature=").append(URLEncoder.encode(signature, "UTF-8")); - } - + StringBuilder redirectURL = new StringBuilder(idp.getIdpUrl().toString()); + redirectURL.append("?wa=").append(FederationConstants.ACTION_SIGNIN); + appendParameter(redirectURL, context, FederationConstants.PARAM_REPLY); + appendParameter(redirectURL, context, FederationConstants.PARAM_TREALM); + appendParameter(redirectURL, context, FederationConstants.PARAM_CONTEXT); + appendParameter(redirectURL, context, FederationConstants.PARAM_FRESHNESS); + appendParameter(redirectURL, context, FederationConstants.PARAM_HOME_REALM); + appendParameter(redirectURL, context, FederationConstants.PARAM_REQUEST); return redirectURL.toString(); } - + private static void appendParameter(StringBuilder redirectURL, RequestContext context, String name) + throws UnsupportedEncodingException { + Object value = WebUtils.getAttributeFromFlowScope(context, name); + if (value != null) { + redirectURL.append('&').append(name).append('=').append(URLEncoder.encode(value.toString(), "UTF-8")); + } + } } diff --git a/services/idp-core/src/test/java/org/apache/cxf/fediz/service/idp/beans/LocalRedirectCreatorTest.java b/services/idp-core/src/test/java/org/apache/cxf/fediz/service/idp/beans/LocalRedirectCreatorTest.java new file mode 100644 index 00000000..32417ea6 --- /dev/null +++ b/services/idp-core/src/test/java/org/apache/cxf/fediz/service/idp/beans/LocalRedirectCreatorTest.java @@ -0,0 +1,118 @@ +/** + * 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.service.idp.beans; + +import java.net.URL; +import java.net.URLDecoder; +import java.nio.charset.StandardCharsets; +import java.util.HashMap; +import java.util.Map; + +import org.apache.cxf.fediz.service.idp.beans.samlsso.LocalRedirectCreator; +import org.apache.cxf.fediz.service.idp.beans.wsfed.LocalFederationRedirectCreator; +import org.apache.cxf.fediz.service.idp.domain.Idp; +import org.springframework.webflow.test.MockRequestContext; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class LocalRedirectCreatorTest { + + private static final String IDP_URL = "https://localhost:9443/fediz-idp/federation"; + + @Test + public void testSAMLRedirectEncodesRelayState() throws Exception { + MockRequestContext context = new MockRequestContext(); + String relayState = "abc&Signature=injected&x=1%2+"; + context.getFlowScope().put("RelayState", relayState); + context.getFlowScope().put("SAMLRequest", "fZJdT8Iw="); + context.getFlowScope().put("SigAlg", "http://www.w3.org/2001/04/xmldsig-more#rsa-sha256"); + context.getFlowScope().put("Signature", "c2ln+/="); + + String url = new LocalRedirectCreator().createRedirectURL(context, createIdp()); + + assertTrue(url.startsWith(IDP_URL + "?")); + Map<String, String> params = parseQuery(url); + assertEquals(4, params.size()); + assertEquals(relayState, params.get("RelayState")); + assertEquals("fZJdT8Iw=", params.get("SAMLRequest")); + assertEquals("http://www.w3.org/2001/04/xmldsig-more#rsa-sha256", params.get("SigAlg")); + assertEquals("c2ln+/=", params.get("Signature")); + } + + @Test + public void testSAMLRedirectWithoutSignature() throws Exception { + MockRequestContext context = new MockRequestContext(); + context.getFlowScope().put("RelayState", "state"); + context.getFlowScope().put("SAMLRequest", "fZJdT8Iw="); + + Map<String, String> params = parseQuery(new LocalRedirectCreator().createRedirectURL(context, createIdp())); + assertEquals(2, params.size()); + assertFalse(params.containsKey("SigAlg")); + assertFalse(params.containsKey("Signature")); + } + + @Test + public void testFederationRedirectEncodesParameters() throws Exception { + MockRequestContext context = new MockRequestContext(); + String wctx = "ctx&wreply=https://evil.example/&a=%41+b"; + context.getFlowScope().put("wreply", "https://localhost/app/?a=1&b=2"); + context.getFlowScope().put("wtrealm", "urn:org:apache:cxf:fediz:fedizhelloworld"); + context.getFlowScope().put("wctx", wctx); + context.getFlowScope().put("wfresh", "10"); + context.getFlowScope().put("whr", "urn:org:apache:cxf:fediz:idp:realm-B"); + + String url = new LocalFederationRedirectCreator().createRedirectURL(context, createIdp()); + + assertTrue(url.startsWith(IDP_URL + "?wa=wsignin1.0&")); + Map<String, String> params = parseQuery(url); + assertEquals(6, params.size()); + assertEquals("wsignin1.0", params.get("wa")); + assertEquals("https://localhost/app/?a=1&b=2", params.get("wreply")); + assertEquals("urn:org:apache:cxf:fediz:fedizhelloworld", params.get("wtrealm")); + assertEquals(wctx, params.get("wctx")); + assertEquals("10", params.get("wfresh")); + assertEquals("urn:org:apache:cxf:fediz:idp:realm-B", params.get("whr")); + assertFalse(params.containsKey("wreq")); + } + + private static Idp createIdp() throws Exception { + Idp idp = new Idp(); + idp.setIdpUrl(new URL(IDP_URL)); + return idp; + } + + /** + * Parse the query string, failing if a parameter name occurs more than once + */ + private static Map<String, String> parseQuery(String url) { + Map<String, String> params = new HashMap<>(); + for (String pair : url.substring(url.indexOf('?') + 1).split("&")) { + int idx = pair.indexOf('='); + String name = URLDecoder.decode(pair.substring(0, idx), StandardCharsets.UTF_8); + String value = URLDecoder.decode(pair.substring(idx + 1), StandardCharsets.UTF_8); + assertFalse(params.containsKey(name), "Duplicate parameter " + name); + params.put(name, value); + } + return params; + } +} diff --git a/services/idp/src/main/webapp/WEB-INF/flows/federation-validate-request.xml b/services/idp/src/main/webapp/WEB-INF/flows/federation-validate-request.xml index 7ab8967f..db158696 100644 --- a/services/idp/src/main/webapp/WEB-INF/flows/federation-validate-request.xml +++ b/services/idp/src/main/webapp/WEB-INF/flows/federation-validate-request.xml @@ -282,16 +282,8 @@ <end-state id="redirectToLocalIDP" view="externalRedirect:#{flowScope.localIdpUrl}"> <on-entry> - <set name="flowScope.localIdpUrl" - value="flowScope.idpConfig.idpUrl - +'?wa=wsignin1.0' - +'&wreply='+flowScope.wreply - +'&wtrealm='+flowScope.wtrealm - +(flowScope.wctx != null ? '&wctx='+flowScope.wctx : '') - +(flowScope.wfresh != null ? '&wfresh='+flowScope.wfresh : '') - +(flowScope.whr != null ? '&whr='+flowScope.whr : '') - +(flowScope.wreq != null ? '&wreq='+flowScope.wreq : '')"> - </set> + <evaluate expression="localFederationRedirectCreator.createRedirectURL(flowRequestContext, flowScope.idpConfig)" + result="flowScope.localIdpUrl"/> </on-entry> </end-state>
