Skip to content

Commit c5c1d70

Browse files
committed
SPR-6291 - UrlPathHelper is too aggressive decoding URLs
1 parent 043ec2c commit c5c1d70

File tree

2 files changed

+5
-1
lines changed

2 files changed

+5
-1
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ public static String decode(String source, String encoding) throws UnsupportedEn
462462
Assert.hasLength(encoding, "'encoding' must not be empty");
463463
int length = source.length();
464464
ByteArrayOutputStream bos = new ByteArrayOutputStream(length);
465+
boolean changed = false;
465466
for (int i = 0; i < length; i++) {
466467
int ch = source.charAt(i);
467468
if (ch == '%') {
@@ -472,6 +473,7 @@ public static String decode(String source, String encoding) throws UnsupportedEn
472473
int l = Character.digit(hex2, 16);
473474
bos.write((char) ((u << 4) + l));
474475
i += 2;
476+
changed = true;
475477
}
476478
else {
477479
throw new IllegalArgumentException("Invalid encoded sequence \"" + source.substring(i) + "\"");
@@ -481,7 +483,7 @@ public static String decode(String source, String encoding) throws UnsupportedEn
481483
bos.write(ch);
482484
}
483485
}
484-
return new String(bos.toByteArray(), encoding);
486+
return changed ? new String(bos.toByteArray(), encoding) : source;
485487
}
486488

487489
}

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ public void encodeFragment() throws UnsupportedEncodingException {
8888

8989
@Test
9090
public void decode() throws UnsupportedEncodingException {
91+
assertEquals("Invalid encoded URI", "", UriUtils.decode("", ENC));
9192
assertEquals("Invalid encoded URI", "foobar", UriUtils.decode("foobar", ENC));
9293
assertEquals("Invalid encoded URI", "foo bar", UriUtils.decode("foo%20bar", ENC));
9394
assertEquals("Invalid encoded URI", "foo+bar", UriUtils.decode("foo%2bbar", ENC));
9495
assertEquals("Invalid encoded result", "T\u014dky\u014d", UriUtils.decode("T%C5%8Dky%C5%8D", ENC));
9596
assertEquals("Invalid encoded result", "/Z\u00fcrich", UriUtils.decode("/Z%C3%BCrich", ENC));
97+
assertEquals("Invalid encoded result", "T\u014dky\u014d", UriUtils.decode("T\u014dky\u014d", ENC));
9698
}
9799

98100
@Test(expected = IllegalArgumentException.class)

0 commit comments

Comments
 (0)