Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UNDERTOW-1565 Don't encode query strings and URI paths while redirecting #785

Merged
merged 1 commit into from Jul 29, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -43,10 +43,12 @@ protected URI getRedirectURI(HttpServerExchange exchange) throws URISyntaxExcept
return getRedirectURI(exchange, redirectPort);
}

protected URI getRedirectURI(HttpServerExchange exchange, int port) throws URISyntaxException {
String host = exchange.getHostName();

String queryString = exchange.getQueryString();
protected URI getRedirectURI(final HttpServerExchange exchange, final int port) throws URISyntaxException {
final StringBuilder uriBuilder = new StringBuilder();
uriBuilder.append("https://").append(exchange.getHostName());
if (port > 0) {
uriBuilder.append(":").append(port);
}
String uri = exchange.getRequestURI();
if(exchange.isHostIncludedInRequestURI()) {
int slashCount = 0;
Expand All @@ -60,8 +62,12 @@ protected URI getRedirectURI(HttpServerExchange exchange, int port) throws URISy
}
}
}
return new URI("https", null, host, port, uri,
queryString == null || queryString.length() == 0 ? null : queryString, null);
uriBuilder.append(uri);
final String queryString = exchange.getQueryString();
if (queryString != null && !queryString.isEmpty()) {
uriBuilder.append("?").append(queryString);
}
return new URI(uriBuilder.toString());
}

}
Expand Up @@ -49,6 +49,8 @@
public class SimpleConfidentialRedirectTestCase {


private static int redirectPort = -1;

@BeforeClass
public static void setup() throws IOException {
DefaultServer.startSSLServer();
Expand All @@ -58,11 +60,13 @@ public static void setup() throws IOException {
public void handleRequest(final HttpServerExchange exchange) throws Exception {
exchange.getResponseHeaders().put(HttpString.tryFromString("scheme"), exchange.getRequestScheme());
exchange.getResponseHeaders().put(HttpString.tryFromString("uri"), exchange.getRequestURI());
exchange.getResponseHeaders().put(HttpString.tryFromString("queryString"), exchange.getQueryString());
exchange.getResponseHeaders().put(HttpString.tryFromString("redirectedToPort"), exchange.getHostPort());
exchange.endExchange();
}
};

current = new SinglePortConfidentialityHandler(current, DefaultServer.getHostSSLPort("default"));
redirectPort = DefaultServer.getHostSSLPort("default");
current = new SinglePortConfidentialityHandler(current, redirectPort);

DefaultServer.setRootHandler(current);
}
Expand All @@ -77,11 +81,11 @@ public void simpleRedirectTestCase() throws IOException, GeneralSecurityExceptio
TestHttpClient client = new TestHttpClient();
client.setSSLContext(DefaultServer.getClientSSLContext());
try {
sendRequest(client, "/foo");
sendRequest(client, "/foo+bar");
sendRequest(client, "/foo+bar;aa");


sendRequest(client, "/foo", null);
sendRequest(client, "/foo+bar", null);
sendRequest(client, "/foo+bar;aa", null);
sendRequest(client, "/foo+bar;aa", "x=y");
sendRequest(client, "/foo+bar%3Aaa", "x=%3Ablah");
} finally {
client.getConnectionManager().shutdown();
}
Expand All @@ -99,12 +103,21 @@ public void testRedirectWithFullURLInPath() throws IOException {
}
}

private void sendRequest(TestHttpClient client, String uri) throws IOException {
HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + uri);
private void sendRequest(final TestHttpClient client, final String uri, final String queryString) throws IOException {
String targetURL = DefaultServer.getDefaultServerURL() + uri;
if (queryString != null) {
targetURL = targetURL + "?" + queryString;
}
final HttpGet get = new HttpGet(targetURL);
HttpResponse result = client.execute(get);
Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode());
Assert.assertEquals("https", result.getFirstHeader("scheme").getValue());
Assert.assertEquals(uri, result.getFirstHeader("uri").getValue());
Assert.assertEquals("Unexpected scheme in redirected URI", "https", result.getFirstHeader("scheme").getValue());
Assert.assertEquals("Unexpected port in redirected URI", String.valueOf(redirectPort), result.getFirstHeader("redirectedToPort").getValue());
Assert.assertEquals("Unexpected path in redirected URI", uri, result.getFirstHeader("uri").getValue());
if (queryString != null) {
Assert.assertEquals("Unexpected query string in redirected URI", queryString,
result.getFirstHeader("queryString").getValue());
}
HttpClientUtils.readResponse(result);
}

Expand Down