Skip to content

Commit 91c46a9

Browse files
authored
fix: UriTemplate reserved expansion does not escape reserved chars (#1844)
When using UriTemplate.expand with a reserved expansion "{+var}", a set of allowed characters must not be encoded. According to section of [3.2.3 Reserved Expansion: {+var}](https://www.rfc-editor.org/rfc/rfc6570#section-3.2.3), unreserved and reserved character should not be escaped. This fix adds the missing characters `#[]` that must not be percent encoded when using reserved expansion. - [x] Ensure the tests and linter pass - [x] Code coverage does not decrease (if any source code was changed) - [x] Appropriate docs were updated (if necessary) Fixes #1838
1 parent f1da9e7 commit 91c46a9

File tree

2 files changed

+34
-3
lines changed

2 files changed

+34
-3
lines changed

google-http-client/src/main/java/com/google/api/client/util/escape/PercentEscaper.java

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,15 @@ public class PercentEscaper extends UnicodeEscaper {
6464
public static final String SAFEPATHCHARS_URLENCODER = "-_.!~*'()@:$&,;=+";
6565

6666
/**
67-
* Contains the safe characters plus all reserved characters. This happens to be the safe path
68-
* characters plus those characters which are reserved for URI segments, namely '/' and '?'.
67+
* A string of characters that do not need to be encoded when used in URI Templates reserved
68+
* expansion, as specified in RFC 6570. This includes the safe characters plus all reserved
69+
* characters.
70+
*
71+
* <p>For details on escaping URI Templates using the reserved expansion, see <a
72+
* href="https://www.rfc-editor.org/rfc/rfc6570#section-3.2.3">RFC 6570 - section 3.2.3</a>.
6973
*/
70-
public static final String SAFE_PLUS_RESERVED_CHARS_URLENCODER = SAFEPATHCHARS_URLENCODER + "/?";
74+
public static final String SAFE_PLUS_RESERVED_CHARS_URLENCODER =
75+
SAFEPATHCHARS_URLENCODER + "/?#[]";
7176

7277
/**
7378
* A string of characters that do not need to be encoded when used in URI user info part, as

google-http-client/src/test/java/com/google/api/client/http/UriTemplateTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,4 +322,30 @@ public void testExpandSeveralTemplatesNoParametersUsed() {
322322
SortedMap<String, Object> map = Maps.newTreeMap();
323323
assertEquals("", UriTemplate.expand("{?id,uid}", map, false));
324324
}
325+
326+
public void testExpandTemplates_reservedExpansion_mustNotEscapeReservedCharSet() {
327+
328+
String reservedSet = ":/?#[]@!$&'()*+,;=";
329+
330+
SortedMap<String, Object> requestMap = Maps.newTreeMap();
331+
requestMap.put("var", reservedSet);
332+
333+
assertEquals(
334+
"Reserved expansion must not escape chars from reserved set according to rfc6570#section-3.2.3",
335+
reservedSet,
336+
UriTemplate.expand("{+var}", requestMap, false));
337+
}
338+
339+
public void testExpandTemplates_reservedExpansion_mustNotEscapeUnreservedCharSet() {
340+
341+
String unReservedSet = "-._~";
342+
343+
SortedMap<String, Object> requestMap = Maps.newTreeMap();
344+
requestMap.put("var", unReservedSet);
345+
346+
assertEquals(
347+
"Reserved expansion must not escape chars from unreserved set according to rfc6570#section-3.2.3",
348+
unReservedSet,
349+
UriTemplate.expand("{+var}", requestMap, false));
350+
}
325351
}

0 commit comments

Comments
 (0)