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

Decode \uXXXX escapes in SCIM filter string values - #52298

Open
amdadulbari wants to merge 1 commit into
keycloak:mainfrom
amdadulbari:fix/scim-filter-unicode-escape
Open

amdadulbari wants to merge 1 commit into
keycloak:mainfrom
amdadulbari:fix/scim-filter-unicode-escape

Conversation

@amdadulbari
Copy link
Copy Markdown
Contributor

Closes #52295

FilterUtils.unescapeJsonString decoded every JSON escape except the \uXXXX form, whose default arm left it literal - so the escape for U+0041 stayed as its six characters instead of becoming A. The Javadoc claimed the ANTLR lexer handled Unicode escapes, but the lexer only recognizes \uXXXX (a grammar fragment) and returns the raw text via getText().

Decode \uXXXX like the other escapes (a malformed escape without four hex digits is left literal), and correct the Javadoc.

Added regression coverage in FilterUtilsTest, verified fails-before / passes-after.

@amdadulbari
amdadulbari requested a review from a team as a code owner September 1, 2026 18:08
Copilot AI balanced review requested due to automatic review settings September 1, 2026 18:08
Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Warning

Copilot couldn't run its full agentic review because it didn't start before the timeout. Make sure your repository has a runner available, or add a copilot-code-review.yml file specifying one with the runs-on attribute. See the docs for more details.

Pull request overview

Adds correct decoding of \uXXXX Unicode escapes in SCIM filter string values by updating FilterUtils.unescapeJsonString, correcting its Javadoc, and adding regression tests to prevent reintroducing the issue.

Changes:

  • Decode \uXXXX escapes in FilterUtils.unescapeJsonString while leaving malformed sequences literal.
  • Update Javadoc to reflect that ANTLR recognizes but does not decode Unicode escapes.
  • Add regression tests covering valid, mixed, and malformed Unicode escape cases.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
scim/core/src/main/java/org/keycloak/scim/filter/FilterUtils.java Implements \uXXXX decoding in unescapeJsonString and updates Javadoc to match actual lexer behavior.
scim/core/src/test/java/org/keycloak/scim/filter/FilterUtilsTest.java Adds regression tests ensuring \uXXXX escapes are decoded (and malformed ones remain literal).

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +150 to +165
case 'u' -> {
int cp = -1;
if (i + 4 < s.length()) {
try {
cp = Integer.parseInt(s, i + 1, i + 5, 16);
} catch (NumberFormatException ignored) {
// not four hex digits - leave the escape literal
}
}
if (cp >= 0) {
sb.append((char) cp);
i += 4;
} else {
sb.append('\\').append(next);
}
}
sguilhen
sguilhen previously approved these changes Sep 24, 2026
@sguilhen sguilhen left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FIx makes sense to me, CI failures are not related

Copilot AI review requested due to automatic review settings September 28, 2026 06:15
@ahus1
ahus1 force-pushed the fix/scim-filter-unicode-escape branch from 4e7acc4 to 4d9c541 Compare September 28, 2026 06:15
@ahus1
ahus1 commented Sep 28, 2026
Copy link
Copy Markdown
Member

Rebased to re-trigger CI that got stalled.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot review overview

🟡 Changes recommended

Hex parsing currently accepts malformed non-JSON escape forms.

Review effort: Balanced
Findings: 1 Medium severity · 1 Low severity

Open (2)

@ahus1
ahus1 dismissed sguilhen’s stale review September 28, 2026 06:38

New input from copilot for parser problem

@ahus1
ahus1 commented Sep 28, 2026
Copy link
Copy Markdown
Member

@amdadulbari / @sguilhen - the copilot comment seems valid; the parser should be strict. Please verify.

@amdadulbari
amdadulbari force-pushed the fix/scim-filter-unicode-escape branch from 4d9c541 to b987ec9 Compare November 28, 2026 05:05
Copilot AI balanced review requested due to automatic review settings November 28, 2026 05:05
@amdadulbari
Copy link
Copy Markdown
Contributor Author

@ahus1 Thanks, agreed - the parser should be strict. Integer.parseInt accepted inputs like \u+041 or non-ASCII Unicode digits and decoded them. The four characters after \u are now validated against ASCII [0-9A-Fa-f] before parsing; anything else is left literal. I also renamed cp to codeUnit as suggested, and added tests for mixed-case hex, a leading sign, non-ASCII digits and non-hex characters (verified failing before / passing after). Squashed into the single commit.

Copilot AI left a comment
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The test only verifies syntax acceptance and needs an assertion that extractCompValue decodes \u0041 to A to cover the lexer-to-decoder path.

2 open issues
1 resolved since last review

Review effort: Balanced

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\""));
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems legit and worth fixing, IMO: parseFilter never invokes the decoder, so this only proves the grammar accepts the escape, not that it's actually decoded through extractCompValue.

FilterUtils.unescapeJsonString decoded every JSON escape except the
\uXXXX form, whose default switch arm left it untouched. So the escape
for U+0041 stayed as its six literal characters (backslash, u, 0, 0, 4,
1) instead of becoming "A".

The Javadoc claimed Unicode escapes were handled by the ANTLR lexer, but
the lexer only recognizes \uXXXX (a grammar fragment) and does not decode
it - getText() returns the raw source. As a result a filter comparing
against a \uXXXX escape compared against those literal characters and
never matched the intended value.

Decode \uXXXX like the other escapes, and correct the Javadoc. The four
hex digits are validated strictly as ASCII [0-9A-Fa-f] before parsing,
since Integer.parseInt also accepts a leading sign and non-ASCII Unicode
digits; any escape that is not followed by four ASCII hex digits is left
literal.

Add regression coverage for Unicode escapes, including interleaving with
other escapes, mixed-case hex digits and malformed escapes (too few
digits, a sign, non-ASCII digits, non-hex characters).

Closes keycloak#52295

Signed-off-by: Md. Amdadul Bari Imad <amdadulbari@gmail.com>
@amdadulbari
amdadulbari force-pushed the fix/scim-filter-unicode-escape branch from b987ec9 to b75d41e Compare November 28, 2026 05:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SCIM filter string values: \uXXXX escape sequences are not decoded

5 participants

0