Skip to content

fix: escape the project database name and bind it in the terminate query - #293

Open
HuiJun wants to merge 1 commit into
developfrom
fix/project-database-name-quoting
Open

HuiJun wants to merge 1 commit into
developfrom
fix/project-database-name-quoting

Conversation

@HuiJun

@HuiJun HuiJun commented Nov 17, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

DatabaseDefinitionService built SQL from the project id by concatenation: databaseProjectString wrapped prefix_projectId in double quotes without escaping quotes inside it, and deleteProjectDatabase also spliced that into a string literal for the pg_stat_activity query. The REST controllers only let ^[\w-]+$ ids through, but nothing in this class enforced it.

  • databaseProjectString now doubles any " in the name, so the value can never close the quoted identifier (used by CREATE DATABASE and DROP DATABASE, which cannot take bind parameters).
  • The pg_terminate_backend query is a PreparedStatement with the name bound as a parameter.

SonarCloud issues (java:S2077): AaHWUyNv45Pg1SGp66jt (:95) is removed by the bind parameter. AaHWUyNv45Pg1SGp66ju (:98, DROP DATABASE) is still dynamically built, since an identifier cannot be bound, so Sonar may keep reporting it; with the escaping in place it is a candidate to mark as reviewed/safe.

Why it is safe

  • For every id the API accepts (^[\w-]+$, no quotes) the generated SQL and the bound value are identical to before; the 60 s query timeout is kept on both statements.
  • New DatabaseDefinitionServiceTest (first test in rdb, hence the spring-boot-starter-test test dependency other modules already use) checks: the DROP DATABASE text is unchanged for a valid id, the Postgres path binds the name instead of concatenating it, and an id containing " stays inside the identifier. ./gradlew :rdb:test passes (3 tests).
  • Not run against a live Postgres locally; the CI Postman run exercises project create and hard delete.

Noticed, not changed

The terminate query compares datname with the name including its double quotes ('"mms_p1"'), before and after this PR, so it looks like it can never match a database and never terminates anything. I kept the value as is to leave behaviour unchanged here; fixing it would start terminating open connections before the drop and deserves its own change.


Quality Assurance opened this PR for Jason
Implemented in session Get SonarCloud analysis landing for exec-mms
Review with Devin · Open in Devin Desktop


The project database name was concatenated into SQL as a quoted identifier
without escaping embedded quotes, and into a string literal. Double embedded
quotes and pass the literal as a bind parameter. Addresses Sonar java:S2077.

Co-Authored-By: jason.han <hanhuijun@gmail.com>
@devin-ai-integration

Copy link
Copy Markdown
Contributor

I'll fix CI failures and address comments from users with write access. I'll skip comments containing "(aside)".

  • Disable automatic comment, CI, and merge conflict monitoring

@devin-ai-integration devin-ai-integration Bot 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.

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no bugs or issues to report.

Devin Review

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant