Skip to content

Commit a4484bb

Browse files
committed
Fix UriComponentsBuilder.fromUriString parsing error
This commit fixes cases where part of the URI was mistaken for the userinfo when: * the URI did not contain any path * the query string contained the "@" Issue: SPR-11964
1 parent bc62d63 commit a4484bb

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public class UriComponentsBuilder {
6262

6363
private static final String HTTP_PATTERN = "(?i)(http|https):";
6464

65-
private static final String USERINFO_PATTERN = "([^@/]*)";
65+
private static final String USERINFO_PATTERN = "([^@\\[/?#]*)";
6666

6767
private static final String HOST_IPV4_PATTERN = "[^\\[/?#:]*";
6868

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
import org.springframework.util.LinkedMultiValueMap;
2828
import org.springframework.util.MultiValueMap;
29+
import org.springframework.util.StringUtils;
2930

3031
import static org.hamcrest.Matchers.*;
3132
import static org.junit.Assert.*;
@@ -216,6 +217,17 @@ public void fromUriStringIPv6Host() throws URISyntaxException {
216217
assertEquals("[::192.168.1.1]", resultIPv4compatible.getHost());
217218
}
218219

220+
// SPR-11970
221+
222+
@Test
223+
public void fromUriStringNoPathWithReservedCharInQuery() {
224+
UriComponents result = UriComponentsBuilder.fromUriString("http://example.com?foo=bar@baz").build();
225+
assertTrue(StringUtils.isEmpty(result.getUserInfo()));
226+
assertEquals("example.com", result.getHost());
227+
assertTrue(result.getQueryParams().containsKey("foo"));
228+
assertEquals("bar@baz", result.getQueryParams().getFirst("foo"));
229+
}
230+
219231
@Test
220232
public void path() throws URISyntaxException {
221233
UriComponentsBuilder builder = UriComponentsBuilder.fromPath("/foo/bar");

0 commit comments

Comments
 (0)