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

Use Pattern.compile in Netty RequestWrapper #1163

Merged
merged 2 commits into from
Mar 1, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class RequestWrapper extends ExtendedRequest {
private static final Pattern URL_REPLACEMENT_PATTERN = Pattern.compile("(?i)%(?![\\da-f]{2})");
private static CookieDecoder cookieDecoder = new CookieDecoder();
private final HttpRequest request;
private final Set<Cookie> cookies;
Expand All @@ -51,7 +53,7 @@ public RequestWrapper(DefaultHttpRequest request) {
Map<String, List<String>> params;
try {
String uri = request.getUri();
uri = uri.replaceAll("(?i)%(?![\\da-f]{2})", "%25"); // Escape any percent signs in the URI
uri = URL_REPLACEMENT_PATTERN.matcher(uri).replaceAll("%25"); // Escape any percent signs in the URI
QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
params = decoderQuery.getParameters();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package netty34;

import com.agent.instrumentation.netty34.RequestWrapper;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RequestWrapperTest {

@Test
public void testPercentageEscaping() {
Map<String, String> inputExpectedMap = new HashMap<>();
inputExpectedMap.put("http://example.com?asdf=%qwer", "%qwer");
inputExpectedMap.put("http://example.com?asdf=%20", " ");
inputExpectedMap.put("http://example.com?asdf=%2b", "+");
inputExpectedMap.put("http://example.com?asdf=qwer", "qwer");

for (Map.Entry<String, String> inputExpectedEntry : inputExpectedMap.entrySet()) {
String input = inputExpectedEntry.getKey();
String expected = inputExpectedEntry.getValue();
DefaultHttpRequest request = mock(DefaultHttpRequest.class);
when(request.getUri()).thenReturn(input);
RequestWrapper requestWrapper = new RequestWrapper(request);
Assert.assertEquals(expected, requestWrapper.getParameterValues("asdf")[0]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class RequestWrapper extends ExtendedRequest {
private static final Pattern URL_REPLACEMENT_PATTERN = Pattern.compile("(?i)%(?![\\da-f]{2})");
private static CookieDecoder cookieDecoder = new CookieDecoder();
private final HttpRequest request;
private final Set<Cookie> cookies;
Expand All @@ -51,7 +53,7 @@ public RequestWrapper(DefaultHttpRequest request) {
Map<String, List<String>> params;
try {
String uri = request.getUri();
uri = uri.replaceAll("(?i)%(?![\\da-f]{2})", "%25"); // Escape any percent signs in the URI
uri = URL_REPLACEMENT_PATTERN.matcher(uri).replaceAll("%25"); // Escape any percent signs in the URI
QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
params = decoderQuery.getParameters();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package netty38;

import com.agent.instrumentation.netty38.RequestWrapper;
import org.jboss.netty.handler.codec.http.DefaultHttpRequest;
import org.jboss.netty.handler.codec.http.HttpHeaders;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RequestWrapperTest {

@Test
public void testPercentageEscaping() {
Map<String, String> inputExpectedMap = new HashMap<>();
inputExpectedMap.put("http://example.com?asdf=%qwer", "%qwer");
inputExpectedMap.put("http://example.com?asdf=%20", " ");
inputExpectedMap.put("http://example.com?asdf=%2b", "+");
inputExpectedMap.put("http://example.com?asdf=qwer", "qwer");

for (Map.Entry<String, String> inputExpectedEntry : inputExpectedMap.entrySet()) {
String input = inputExpectedEntry.getKey();
String expected = inputExpectedEntry.getValue();
DefaultHttpRequest request = mock(DefaultHttpRequest.class);
when(request.headers()).thenReturn(HttpHeaders.EMPTY_HEADERS);
when(request.getUri()).thenReturn(input);
RequestWrapper requestWrapper = new RequestWrapper(request);
Assert.assertEquals(expected, requestWrapper.getParameterValues("asdf")[0]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class RequestWrapper extends ExtendedRequest {
private static final Pattern URL_REPLACEMENT_PATTERN = Pattern.compile("(?i)%(?![\\da-f]{2})");
private final HttpRequest request;
private final Set<Cookie> cookies;
private final Map<String, List<String>> parameters;
Expand All @@ -49,7 +51,7 @@ public RequestWrapper(DefaultHttpRequest request) {
Map<String, List<String>> params;
try {
String uri = request.getUri();
uri = uri.replaceAll("(?i)%(?![\\da-f]{2})", "%25"); // Escape any percent signs in the URI
uri = URL_REPLACEMENT_PATTERN.matcher(uri).replaceAll("%25"); // Escape any percent signs in the URI
QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
params = decoderQuery.parameters();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package netty40;

import com.agent.instrumentation.netty40.RequestWrapper;
import io.netty.handler.codec.http.DefaultHttpRequest;
import io.netty.handler.codec.http.HttpHeaders;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RequestWrapperTest {

@Test
public void testPercentageEscaping() {
Map<String, String> inputExpectedMap = new HashMap<>();
inputExpectedMap.put("http://example.com?asdf=%qwer", "%qwer");
inputExpectedMap.put("http://example.com?asdf=%20", " ");
inputExpectedMap.put("http://example.com?asdf=%2b", "+");
inputExpectedMap.put("http://example.com?asdf=qwer", "qwer");

for (Map.Entry<String, String> inputExpectedEntry : inputExpectedMap.entrySet()) {
String input = inputExpectedEntry.getKey();
String expected = inputExpectedEntry.getValue();
DefaultHttpRequest request = mock(DefaultHttpRequest.class);
when(request.headers()).thenReturn(HttpHeaders.EMPTY_HEADERS);
when(request.getUri()).thenReturn(input);
RequestWrapper requestWrapper = new RequestWrapper(request);
Assert.assertEquals(expected, requestWrapper.getParameterValues("asdf")[0]);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@
import java.util.Map;
import java.util.Set;
import java.util.logging.Level;
import java.util.regex.Pattern;

public class RequestWrapper extends ExtendedRequest {
private static final Pattern URL_REPLACEMENT_PATTERN = Pattern.compile("(?i)%(?![\\da-f]{2})");
private final HttpRequest request;
private final Set<Cookie> cookies;
private final Map<String, List<String>> parameters;
Expand All @@ -48,7 +50,7 @@ public RequestWrapper(HttpRequest request) {
Map<String, List<String>> params;
try {
String uri = request.getUri();
uri = uri.replaceAll("(?i)%(?![\\da-f]{2})", "%25"); // Escape any percent signs in the URI
uri = URL_REPLACEMENT_PATTERN.matcher(uri).replaceAll("%25"); // Escape any percent signs in the URI
QueryStringDecoder decoderQuery = new QueryStringDecoder(uri);
params = decoderQuery.parameters();
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package netty408;

import com.agent.instrumentation.netty40.RequestWrapper;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import org.junit.Assert;
import org.junit.Test;

import java.util.HashMap;
import java.util.Map;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public class RequestWrapperTest {

@Test
public void testPercentageEscaping() {
Map<String, String> inputExpectedMap = new HashMap<>();
inputExpectedMap.put("http://example.com?asdf=%qwer", "%qwer");
inputExpectedMap.put("http://example.com?asdf=%20", " ");
inputExpectedMap.put("http://example.com?asdf=%2b", "+");
inputExpectedMap.put("http://example.com?asdf=qwer", "qwer");

for (Map.Entry<String, String> inputExpectedEntry : inputExpectedMap.entrySet()) {
String input = inputExpectedEntry.getKey();
String expected = inputExpectedEntry.getValue();
HttpRequest request = mock(HttpRequest.class);
when(request.headers()).thenReturn(HttpHeaders.EMPTY_HEADERS);
when(request.getUri()).thenReturn(input);
RequestWrapper requestWrapper = new RequestWrapper(request);
Assert.assertEquals(expected, requestWrapper.getParameterValues("asdf")[0]);
}
}
}