Skip to content

Commit

Permalink
Fixed #1259 - exception thrown when characters not considered valid b…
Browse files Browse the repository at this point in the history
…y java.net.URI are present unescaped in request URL
  • Loading branch information
tomakehurst committed Mar 5, 2020
1 parent 21b8a4f commit 214a3a8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,9 @@ public static Socket getTlsSocket(Response response) {
}
}

public static URI getUri(Request request) {
HttpURI httpUri = getHttpUri(request);

URI uri;
try {
Method getUriMethod = HttpURI.class.getDeclaredMethod("toURI");
uri = (URI) getUriMethod.invoke(httpUri);
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | InvocationTargetException e) {
uri = URI.create(httpUri.toString());
}

return uri;
public static boolean uriIsAbsolute(Request request) {
HttpURI uri = getHttpUri(request);
return uri.getScheme() != null;
}

private static HttpURI getHttpUri(Request request) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
package com.github.tomakehurst.wiremock.matching;

import java.net.URI;
import com.github.tomakehurst.wiremock.common.Urls;

public class UrlPathPattern extends UrlPattern {

Expand All @@ -29,7 +29,7 @@ public MatchResult match(String url) {
return MatchResult.noMatch();
}

String path = URI.create(url).getRawPath();
String path = Urls.getPath(url);
return super.match(path);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ public boolean isBrowserProxyRequest() {
}
if (request instanceof org.eclipse.jetty.server.Request) {
org.eclipse.jetty.server.Request jettyRequest = (org.eclipse.jetty.server.Request) request;
return JettyUtils.getUri(jettyRequest).isAbsolute();
return JettyUtils.uriIsAbsolute(jettyRequest);
}

return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.SocketException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
Expand Down Expand Up @@ -685,7 +689,37 @@ public void matchingOnMultipartRequestBodyWithAdvancedXPath() {
assertThat(response.statusCode(), is(HTTP_OK));
}

private Matcher<StubMapping> named(final String name) {
@Test
public void copesWithRequestCharactersThatReallyShouldBeEscapedWhenMatchingOnWholeUrlRegex() throws Exception {
stubFor(get(urlMatching("/dodgy-chars.*")).willReturn(ok()));

String url = "http://localhost:" + wireMockServer.port() + "/dodgy-chars?filter={\"accountid\":\"1\"}";
int code = getStatusCodeUsingJavaUrlConnection(url);

assertThat(code, is(200));
}

@Test
public void copesWithRequestCharactersThatReallyShouldBeEscapedWhenMatchingOnExactUrlPath() throws Exception {
stubFor(get(urlPathEqualTo("/dodgy-chars")).willReturn(ok()));

String url = "http://localhost:" + wireMockServer.port() + "/dodgy-chars?filter={\"accountid\":\"1\"}";
int code = getStatusCodeUsingJavaUrlConnection(url);

assertThat(code, is(200));
}

private int getStatusCodeUsingJavaUrlConnection(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
connection.disconnect();
return code;
}


private Matcher<StubMapping> named(final String name) {
return new TypeSafeMatcher<StubMapping>() {
@Override
public void describeTo(Description description) {
Expand Down

0 comments on commit 214a3a8

Please sign in to comment.