-
Notifications
You must be signed in to change notification settings - Fork 2.3k
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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; | ||
|
@@ -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, | ||
false).getValue() | ||
){ | ||
try { | ||
name = URLDecoder.decode(name, "UTF-8"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
@@ -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()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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