8000 Decode \uXXXX escapes in SCIM filter string values by amdadulbari · Pull Request #52298 · keycloak/keycloak · GitHub
[go: up one dir, main page]

Skip to content
8000
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,10 @@ public Void visitComparisonExpression(ScimFilterParser.ComparisonExpressionConte
}

/**
* Unescapes a JSON string value (without surrounding quotes) per RFC 8259.
* Unicode escape sequences are handled by the ANTLR lexer.
* Unescapes a JSON string value (without surrounding quotes) per RFC 8259,
* including {@code \\uXXXX} Unicode escape sequences. The ANTLR lexer only
* recognizes these escapes; it does not decode them, so {@code getText()}
* returns them verbatim and they must be decoded here like every other escape.
*/
public static String unescapeJsonString(String s) {
if (s.indexOf('\\') == -1) {
Expand All @@ -145,6 +147,17 @@ public static String unescapeJsonString(String s) {
case 'n' -> sb.append('\n');
case 'r' -> sb.append('\r');
case 't' -> sb.append('\t');
case 'u' -> {
if (isHexEscape(s, i + 1)) {
// a single UTF-16 code unit; surrogate pairs arrive as two escapes
char codeUnit = (char) Integer.parseInt(s, i + 1, i + 5, 16);
sb.append(codeUnit);
i += 4;
} else {
// not four hex digits - leave the escape literal
sb.append('\\').append(next);
}
}
Comment on lines +150 to +160
default -> sb.append('\\').append(next);
}
} else {
Expand All @@ -153,4 +166,24 @@ public static String unescapeJsonString(String s) {
}
return sb.toString();
}

/**
* Returns whether the four characters starting at {@code start} are all ASCII
* hex digits ({@code [0-9A-Fa-f]}), as required by RFC 8259 for {@code \\uXXXX}.
* {@link Integer#parseInt} alone is more permissive (it accepts a leading sign
* and non-ASCII Unicode digits), so it must not be relied on for validation.
*/
private static boolean isHexEscape(String s, int start) {
if (start + 4 > s.length()) {
return false;
}
for (int j = start; j < start + 4; j++) {
char h = s.charAt(j);
boolean hex = (h >= '0' && h <= '9') || (h >= 'a' && h <= 'f') || (h >= 'A' && h <= 'F');
if (!hex) {
return false;
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,29 @@ public void testUnescapeJsonStringDoesNotDoubleDecode() {
assertEquals("\\r", FilterUtils.unescapeJsonString("\\\\r"));
}

@Test
public void testUnescapeUnicodeEscape() {
// \\uXXXX escapes are only recognized - not decoded - by the ANTLR lexer, so
// unescapeJsonString must decode them like every other JSON escape.
assertEquals("A", FilterUtils.unescapeJsonString("\\u0041"));
assertEquals("ABC", FilterUtils.unescapeJsonString("\\u0041BC"));
assertEquals("caf\u00e9", FilterUtils.unescapeJsonString("caf\\u00e9"));
// interleaved with other escapes
assertEquals("A\tB", FilterUtils.unescapeJsonString("\\u0041\\t\\u0042"));
// a malformed \\u (too few hex digits) is left literal rather than throwing
assertEquals("\\u12", FilterUtils.unescapeJsonString("\\u12"));
// hex digits are case-insensitive
assertEquals("\u00e9\u00E9", FilterUtils.unescapeJsonString("\\u00e9\\u00E9"));
// only ASCII hex digits are accepted, unlike Integer.parseInt which also takes
// a leading sign or non-ASCII Unicode digits - such escapes are left literal
assertEquals("\\u+041", FilterUtils.unescapeJsonString("\\u+041"));
assertEquals("\\u-041", FilterUtils.unescapeJsonString("\\u-041"));
assertEquals("\\u\u0660\u0660\u0664\u0661", FilterUtils.unescapeJsonString("\\u\u0660\u0660\u0664\u0661"));
assertEquals("\\u004G", FilterUtils.unescapeJsonString("\\u004G"));
// end-to-end: a filter comparison value carrying a \\uXXXX escape
assertDoesNotThrow(() -> FilterUtils.parseFilter("userName eq \"\\u0041\""));
}

@Test
public void testInvalidSyntax() {
// Missing value
Expand Down
Loading
0