Skip to content

Commit

Permalink
check <loginParameter> encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
rrayst committed Mar 14, 2024
1 parent f3eb450 commit 8412f04
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public void setAfterLoginUrl(String afterLoginUrl) {
this.afterLoginUrl = afterLoginUrl;
}

@Override
public void init() throws Exception {
for (LoginParameter loginParameter : loginParameters)
loginParameter.init();
}

@Override
public Outcome handleRequest(Exchange exc) throws Exception {
// remove session
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,20 @@
limitations under the License. */
package com.predic8.membrane.core.interceptor.oauth2client;

import com.bornium.http.util.UriUtil;
import com.predic8.membrane.annot.MCAttribute;
import com.predic8.membrane.annot.MCElement;
import com.predic8.membrane.core.exchange.Exchange;
import com.predic8.membrane.core.util.URIFactory;
import com.predic8.membrane.core.util.URLParamUtil;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static java.nio.charset.StandardCharsets.UTF_8;

@MCElement(name = "loginParameter")
public class LoginParameter {

Expand All @@ -38,6 +40,11 @@ public LoginParameter(String name, String value) {
this.value = value;
}

public void init() throws UnsupportedEncodingException {
if (!name.equals(URLEncoder.encode(name, UTF_8)))
throw new RuntimeException("<loginParameter /> may only take a name which is identical under URL encoding so far: " + name);
}

public static String copyLoginParameters(Exchange exc, List<LoginParameter> loginParameters) throws Exception {
StringBuilder sb = new StringBuilder();

Expand All @@ -46,23 +53,19 @@ public static String copyLoginParameters(Exchange exc, List<LoginParameter> logi

Map<String, String> params = URLParamUtil.getParams(new URIFactory(), exc, URLParamUtil.DuplicateKeyOrInvalidFormStrategy.ERROR);
loginParameters.forEach(lp -> {
try {
if (lp.getValue() != null) {
if (lp.getValue() != null) {
sb.append("&");
sb.append(lp.getName());
sb.append("=");
sb.append(URLEncoder.encode(lp.getValue(), UTF_8));
} else {
if (params.containsKey(lp.getName())) {
String encoded = URLEncoder.encode(params.get(lp.getName()), UTF_8);
sb.append("&");
sb.append(lp.getName());
sb.append("=");
sb.append(UriUtil.encode(lp.getValue()));
} else {
if (params.containsKey(lp.getName())) {
String encoded = UriUtil.encode(params.get(lp.getName()));
sb.append("&");
sb.append(lp.getName());
sb.append("=");
sb.append(encoded);
}
sb.append(encoded);
}
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ public void init(Router router) throws Exception {
oAuth2CallbackRequestHandler.init(uriFactory, auth, originalExchangeStore, accessTokenRevalidator,
sessionAuthorizer, publicUrlManager, callbackPath, onlyRefreshToken);
tokenAuthenticator.init(sessionAuthorizer, statistics, accessTokenRevalidator, auth);
for (LoginParameter loginParameter : loginParameters)
loginParameter.init();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -356,7 +356,7 @@ public void loginParams() throws Exception {

@Test
public void loginParamsPerFlow() throws Exception {
Exchange exc = new Request.Builder().get(getClientAddress() + "/pe/init?domain_hint=flow&illegal=true").buildExchange();
Exchange exc = new Request.Builder().get(getClientAddress() + "/pe/init?domain_hint=flow%c3%b6&illegal=true").buildExchange();
browser.applyWithoutRedirect(exc);

String location = exc.getResponse().getHeader().getFirstValue("Location");
Expand All @@ -369,7 +369,7 @@ public void loginParamsPerFlow() throws Exception {
assertTrue(params.containsKey("fooflow"));
assertEquals("bar", params.get("foo"));
assertTrue(params.containsKey("domain_hint"));
assertEquals("flow", params.get("domain_hint"));
assertEquals("flow\u00f6", params.get("domain_hint")); // 'c3 b6' in UTF-8 for unicode '00 f6': o umlaut
assertFalse(params.containsKey("illegal"));
}

Expand Down

0 comments on commit 8412f04

Please sign in to comment.