Skip to content

Fix ArrayIndexOutOfBoundsException in SearchQueryUtils.getFields() - #50409

Closed
Pons3006 wants to merge 9 commits into
keycloak:mainfrom
Pons3006:issue-50228
Closed

Fix ArrayIndexOutOfBoundsException in SearchQueryUtils.getFields()#50409
Pons3006 wants to merge 9 commits into
keycloak:mainfrom
Pons3006:issue-50228

Conversation

@Pons3006

@Pons3006 Pons3006 commented Jun 29, 2026

Copy link
Copy Markdown
Contributor

Closes #50228

… trailing backslash - Issue keycloak#50228

Signed-off-by: Ponshankar <mail4ponz@gmail.com>
@Pons3006
Pons3006 requested a review from a team as a code owner June 29, 2026 06:44
Copilot AI review requested due to automatic review settings June 29, 2026 06:44
@Pons3006
Pons3006 marked this pull request as draft June 29, 2026 06:45

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.

Pull request overview

This PR addresses uncaught ArrayIndexOutOfBoundsException risks in SearchQueryUtils.getFields() when parsing malformed search query input (notably trailing escape characters), improving robustness of Admin API-style search parsing.

Changes:

  • Add bounds checks before accessing chars[i + 1] while processing escape sequences in both field-name and value parsing loops.
  • Treat a trailing escape (\) as a terminator to avoid out-of-bounds access, rather than crashing.
  • Add a regression test case for a trailing backslash in a field value.

Reviewed changes

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

File Description
services/src/main/java/org/keycloak/utils/SearchQueryUtils.java Adds bounds checks and terminator handling in escape parsing to prevent ArrayIndexOutOfBoundsException.
services/src/test/java/org/keycloak/utils/SearchQueryUtilsTest.java Adds regression coverage for malformed input ending with a trailing escape in a value.

Comment thread services/src/test/java/org/keycloak/utils/SearchQueryUtilsTest.java Outdated
Signed-off-by: Ponshankar <mail4ponz@gmail.com>

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@ahus1 ahus1 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank you for the PR. As described in the connected issue, this should rather be a strict parser with a descriptive error message.

In general, it is usually a good idea to first agree on a possible solution in the issue (especially if there are multiple options, and the issue is still marked "triage").

@Pons3006
Pons3006 marked this pull request as draft June 30, 2026 17:34
@Pons3006
Pons3006 requested review from ahus1 and Copilot July 2, 2026 03:17
@Pons3006
Pons3006 marked this pull request as ready for review July 2, 2026 03:17
@Pons3006
Pons3006 marked this pull request as draft July 2, 2026 03:18

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@Pons3006
Pons3006 marked this pull request as ready for review July 2, 2026 07:20
Copilot AI review requested due to automatic review settings July 2, 2026 07:20

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.

Pull request overview

Copilot reviewed 2 out of 2 changed files in this pull request and generated no new comments.

@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.

Thanks for the PR @Pons3006 ! Only flagged a small opportunity to improve the tests that I think is worth pursuing given that the examples were described in the original issue.


assertInvalidQuery("key:val\\");
assertInvalidQuery("key\\");
assertInvalidQuery("key:\"val\\");

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.

The original issue lists additional malformed inputs beyond trailing backslashes: "key \", key ", "key \"1\"". It would be good to add test cases for these as well to confirm they're handled gracefully (some may already work via the existing loop bounds, but explicit coverage prevents future fixes on this).

@sguilhen

sguilhen commented Aug 7, 2026

Copy link
Copy Markdown
Contributor

Thanks for the contribution @Pons3006! Since this has been idle for a while, I went ahead and opened a new PR preserving your commit with the fix. I just added a few extra test cases on top:

  • lone \ as the minimal crash case
  • double-backslash at the value boundary (key:val\\, key:\\) to exercise the \\ escape path where i += 2 lands exactly at the array end
  • the malformed inputs from the original issue ("key \"1\", "key \", key ") to confirm they're handled gracefully

Closing in favor of #51543.

@sguilhen sguilhen closed this Aug 7, 2026
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.

SearchQuery.getFields() exceptions

4 participants