Skip to content

Commit 88906d2

Browse files
committed
Improve regex for parsing query params
Previously UriComponentsBuilder used a regular expression for parsing query name-value pairs where both name and value were expected to not contain neither '&', not '='. The idea is that the presence of reserved characters makes it impossible to guess correctly how to parse the query string (e.g. a=b&c). This change relaxes the constraint on query param values, allowing them to contain '='. In effect '&' is the ultimate separator of name-value pairs, and any '=' in values is ignored. For example "q=1USD=?EUR" is interpreted as "q equals '1USD=?EUR'". Issue: SPR-9832
1 parent d8166d6 commit 88906d2

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

org.springframework.web/src/main/java/org/springframework/web/util/UriComponentsBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@
5353
*/
5454
public class UriComponentsBuilder {
5555

56-
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)=?([^&=]+)?");
56+
private static final Pattern QUERY_PARAM_PATTERN = Pattern.compile("([^&=]+)=?([^&]+)?");
5757

5858
private static final String SCHEME_PATTERN = "([^:/?#]+):";
5959

org.springframework.web/src/test/java/org/springframework/web/util/UriComponentsBuilderTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,16 @@ public void fromUriString() {
146146
assertEquals("28", result.getFragment());
147147
}
148148

149+
// SPR-9832
150+
151+
@Test
152+
public void fromUriStringQueryParamWithReservedCharInValue() throws URISyntaxException {
153+
String uri = "http://www.google.com/ig/calculator?q=1USD=?EUR";
154+
UriComponents result = UriComponentsBuilder.fromUriString(uri).build();
155+
156+
assertEquals("q=1USD=?EUR", result.getQuery());
157+
assertEquals("1USD=?EUR", result.getQueryParams().getFirst("q"));
158+
}
149159

150160
@Test
151161
public void path() throws URISyntaxException {

0 commit comments

Comments
 (0)