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

feature: Control request parameter encoding with configuration #563

Closed
wants to merge 2 commits into from
Closed
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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public class ZuulConstants {
public static final String ZUUL_DEBUG_HOST = "zuul.debug.host";
public static final String ZUUL_REQUEST_BODY_MAX_SIZE = "zuul.body.request.max.size";
public static final String ZUUL_RESPONSE_BODY_MAX_SIZE = "zuul.body.response.max.size";
public static final String ZUUL_KEEP_ORIGINAL_QUERY_STRING_ENCODING = "zuul.keepOriginalQueryStringEncoding";

// Prevent instantiation
private ZuulConstants() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import com.google.common.collect.ImmutableListMultimap;
import com.google.common.collect.Iterables;
import com.google.common.collect.ListMultimap;
import com.netflix.config.DynamicPropertyFactory;
import com.netflix.zuul.constants.ZuulConstants;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
Expand All @@ -33,6 +35,7 @@
*/
public class HttpQueryParams implements Cloneable
{
private static final DynamicPropertyFactory propertyFactory = DynamicPropertyFactory.getInstance();
private final ListMultimap<String, String> delegate;
private final boolean immutable;
private final HashMap<String, Boolean> trailingEquals;
Expand All @@ -52,6 +55,7 @@ private HttpQueryParams(ListMultimap<String, String> delegate)
}

public static HttpQueryParams parse(String queryString) {

HttpQueryParams queryParams = new HttpQueryParams();
if (queryString == null) {
return queryParams;
Expand All @@ -67,12 +71,17 @@ public static HttpQueryParams parse(String queryString) {
String name = s.substring(0, i);
String value = s.substring(i + 1);

try {
name = URLDecoder.decode(name, "UTF-8");
value = URLDecoder.decode(value, "UTF-8");
}
catch (Exception e) {
// do nothing
if (!propertyFactory.getBooleanProperty(
ZuulConstants.ZUUL_KEEP_ORIGINAL_QUERY_STRING_ENCODING,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this is the correct thing to do. Preserving the encoding is fine, but the name and value still need to be validated. If this flag is off, all that needs to happen is the return value of URLDecoder should be discarded.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the validation really be the responsibility of a proxy by default? especially when you are using OData style queries it's hard for a proxy to do the validation. I believe there should be an option for the service providers to not have the proxy/gateway validate the query string

false).getValue()
){
try {
name = URLDecoder.decode(name, "UTF-8");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, I was going to comment on this, but it seems the issue is pre-existing. The name / value decoding should be done as all or nothing, because otherwise an exception in the value decoder could cause tearing.

Optional if you want to fix this.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based on what has been currently implemented I think it should be a per key/value. Cause in cases where the query string has only key the encoding decoding of the query string varies. I think we should leave it as is.

value = URLDecoder.decode(value, "UTF-8");
}
catch (Exception e) {
// do nothing
}
}

queryParams.add(name, value);
Expand Down Expand Up @@ -171,30 +180,34 @@ public Set<String> keySet() {

public String toEncodedString()
{
StringBuilder sb = new StringBuilder();
try {
for (Map.Entry<String, String> entry : entries()) {
sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
if (StringUtils.isNotEmpty(entry.getValue())) {
sb.append('=');
sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
if (propertyFactory.getBooleanProperty(ZuulConstants.ZUUL_KEEP_ORIGINAL_QUERY_STRING_ENCODING,false).getValue()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this should not be dynamically changeable. You should capture the value when the query string is parsed originally, and use that to decide what the string value should be.

return toString();
} else {
StringBuilder sb = new StringBuilder();
try {
for (Map.Entry<String, String> entry : entries()) {
sb.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
if (StringUtils.isNotEmpty(entry.getValue())) {
sb.append('=');
sb.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
else if (isTrailingEquals(entry.getKey())) {
sb.append('=');
}
sb.append('&');
}
else if (isTrailingEquals(entry.getKey())) {
sb.append('=');

// Remove trailing '&'.
if (sb.length() > 0 && '&' == sb.charAt(sb.length() - 1)) {
sb.deleteCharAt(sb.length() - 1);
}
sb.append('&');
}

// Remove trailing '&'.
if (sb.length() > 0 && '&' == sb.charAt(sb.length() - 1)) {
sb.deleteCharAt(sb.length() - 1);
catch (UnsupportedEncodingException e) {
// Won't happen.
e.printStackTrace();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, I know it's not your fault, but since you are touching this code: This should be throwing an AssertionError(e), rather than printing to stderr.

Copy link
Author

@sandy-adi sandy-adi Sep 9, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but IMO it might be a breaking change for some of the users.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMHO a proxy shouldn't be doing anything with the query parameters. It's arguable whether or not it should be a proxy concern. It's ok to have the ability to do that but it should definitely not be the default behavior. Especially for an open source implementation.

}
return sb.toString();
}
catch (UnsupportedEncodingException e) {
// Won't happen.
e.printStackTrace();
}
return sb.toString();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ private static String pathAndQueryString(HttpRequestMessage request) {
return request.getPath();
}
else {
return request.getPath() + "?" + cleanParams.toEncodedString();
return request.getPath() + "?" + cleanQueryStr;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,23 @@

import static junit.framework.Assert.assertEquals;


import com.netflix.config.ConfigurationManager;
import com.netflix.zuul.constants.ZuulConstants;
import java.util.Properties;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.runners.MockitoJUnitRunner;

@RunWith(MockitoJUnitRunner.class)
public class HttpQueryParamsTest {
Properties testProperties = new Properties();

@Before
public void setupDefaultResource(){
setKeepOriginalEncoding(false);
}

@Test
public void testMultiples() {
Expand Down Expand Up @@ -133,4 +144,30 @@ public void testParseKeysWithoutValuesMixedTrailers() {

assertEquals("k1=&k2=v2&k3&k4=v4", actual.toEncodedString());
}



@Test
public void testParseKeysWithoutValuesWithOriginalEncoding()
{
setKeepOriginalEncoding(true);
HttpQueryParams expected = new HttpQueryParams();
expected.add("k1", "");
expected.add("k2", "v2%20eq%20value_2");
expected.add("k3", "");
expected.add("k4", "v4%20eq%20%27quoted_string%27");
expected.add("$k5", "key%20eq%20%27quotedString%27");

HttpQueryParams actual = HttpQueryParams.parse("k1=&k2=v2%20eq%20value_2&k3&k4=v4%20eq%20%27quoted_string%27&$k5=key%20eq%20%27quotedString%27");

assertEquals(expected, actual);

assertEquals("k1&k2=v2%20eq%20value_2&k3&k4=v4%20eq%20%27quoted_string%27&$k5=key%20eq%20%27quotedString%27", actual.toEncodedString());
assertEquals("k1&k2=v2%20eq%20value_2&k3&k4=v4%20eq%20%27quoted_string%27&$k5=key%20eq%20%27quotedString%27", actual.toString());
}

private void setKeepOriginalEncoding(boolean keepOriginalEncoding) {
testProperties.setProperty(ZuulConstants.ZUUL_KEEP_ORIGINAL_QUERY_STRING_ENCODING, String.valueOf(keepOriginalEncoding));
ConfigurationManager.loadProperties(testProperties);
}
}