Skip to content

Potential fix for code scanning alert no. 27: Query built from user-controlled sources#3930

Open
strehle wants to merge 12 commits into
developfrom
Copilot-generated-fix-for-SQL-validation
Open

Potential fix for code scanning alert no. 27: Query built from user-controlled sources#3930
strehle wants to merge 12 commits into
developfrom
Copilot-generated-fix-for-SQL-validation

Conversation

@strehle
Copy link
Copy Markdown
Member

@strehle strehle commented Jun 3, 2026

Potential fix for https://github.com/cloudfoundry/uaa/security/code-scanning/27

General fix approach: keep using named parameters for values, but ensure any dynamically generated SQL fragment is validated against a strict allowlist/pattern before execution. In this code path, completeSql is unavoidable dynamic SQL due to SCIM filter translation, so the best practical fix is to reject suspicious tokens/separators and only permit a conservative SQL subset for generated WHERE/ORDER BY clauses.

Best single fix here without changing functionality: in AbstractQueryable, validate the generated fragment from where.getSql() before composing completeSql. Add a private method that rejects dangerous constructs (;, SQL comments, block comments) and enforces expected clause shape (must not start with top-level DML/DDL keywords). Call this validator inside getQuerySQL(...) so both paging and non-paging executions are protected in one place. This avoids changing endpoint/controller behavior and avoids changing converter interfaces.

Files/regions to change:

  • server/src/main/java/org/cloudfoundry/identity/uaa/resources/jdbc/AbstractQueryable.java
    • Add Locale import.
    • Update getQuerySQL(...) to call a new assertSafeGeneratedSql(...).
    • Add assertSafeGeneratedSql(...) private method near getQuerySQL(...).

Suggested fixes powered by Copilot Autofix. Review carefully before merging.

…ontrolled sources

Co-authored-by: Copilot Autofix powered by AI <62310815+github-advanced-security[bot]@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 aims to mitigate code scanning alert #27 (“Query built from user-controlled sources”) by adding a defensive validation step for the dynamically generated SQL fragment (where.getSql()) before composing and executing the final query in AbstractQueryable.

Changes:

  • Add a centralized validation step in getQuerySQL(...) to reject suspicious SQL fragment tokens before query execution.
  • Introduce assertSafeGeneratedSql(...) to block statement separators/comment tokens and reject fragments that begin with unexpected SQL clauses.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 1 out of 1 changed files in this pull request and generated 1 comment.

strehle added 2 commits June 4, 2026 01:01
The SQL fragment passed into getQuerySQL is generated by
SimpleSearchQueryConverter, which validates attribute names against
a hardcoded allow-list and binds every user-supplied value as a
named parameter via NamedParameterJdbcTemplate. assertSafeGeneratedSql
remains as defense-in-depth.

Add a justification comment and lgtm[java/sql-injection] suppressions
at the concatenation sites to address CodeQL alert #27.
…imFilterOnlyActive

Address Copilot review on PR #3930: the previous validator only protected
the inherited AbstractQueryable#query path. retrieveByScimFilterOnlyActive
also concatenates ProcessedFilter#getSql() into a SQL string and was
missing the same defense-in-depth check.

- Promote assertSafeGeneratedSql from private to protected static so
  subclasses that build SQL directly can reuse the same guard.
- Call it from JdbcScimUserProvisioning#retrieveByScimFilterOnlyActive
  before composing the where clause.
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated no new comments.

@strehle strehle marked this pull request as ready for review June 4, 2026 14:29
@strehle strehle requested a review from Copilot June 4, 2026 14:29
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated no new comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 2 comments.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated 1 comment.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 3 out of 3 changed files in this pull request and generated no new comments.

@strehle strehle requested review from duanemay and fhanik June 5, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

2 participants