Repository navigation
🔒 fix(backend): prevent SQL injection in Postgres jobstore queries - #24037
undivisible wants to merge 1 commit into
Conversation
Replaced f-string formatting for table names in database queries with `psycopg.sql.SQL` and `psycopg.sql.Identifier` across `postgres_store.py` and `backfill.py` to ensure secure dynamic table querying. Failure-Class: none Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
👋 Jules, reporting for duty! I'm here to lend a hand with this pull request. When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down. I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job! For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with New to Jules? Learn more at jules.google/docs. For security, I will only act on instructions from the user who triggered this task. |
Git-on-my-level
left a comment
There was a problem hiding this comment.
The hardening approach is correct and consistently applied — but two concrete items need fixing before merge (one of them is the red Formatting check, caused by this diff).
The core fix is right. All five interpolated sites are converted correctly:
backend/database/jobstore/postgres_store.py—read_doc,write_doc,delete_doc, andpurge_namespacenow useSQL('... {} ...').format(Identifier(table)); the%splaceholders remain server-bound parameters. Correct psycopg3 composition.backend/database/jobstore/backfill.py—postgres_keysgets the same correct treatment.
One honest calibration note for the record: every one of these sites is gated by table in _TYPED (a fixed allowlist) or uses the already-parameterized docs branch, so the old f-strings were not reachable with externally controlled input. This is valuable defense-in-depth, not a fix for a live exploitable injection — worth merging for exactly that reason, and the framing in the PR description slightly overstates the risk.
Blocking items:
- Formatting check fails, and it's this diff's lines. Three added lines exceed the backend 120-char line limit:
backfill.py~L60 (126 chars),postgres_store.py~L294 (140),postgres_store.py~L318 (130). A formatter pass (wrap theSQL(...)call) turns the check green. .agent-brief/firestore-index-guard/tests.txtis unrelated to this PR. It creates a newfirestore-index-guardbrief directory listing seven backend firestore test files — nothing to do with the Postgres jobstore. Please drop it from this PR (or explain why it belongs here); an unexplained agent-brief artifact in a security PR is exactly the kind of thing reviewers need to be able to ignore.
Security/blast radius: no new query surface or behavior; identifier quoting strictly narrows what the SQL can be. If wrong, the affected surface is jobstore read/write/delete/purge — but backend unit suite, Hermetic Backend E2E, and CodeQL all pass on this head.
USER IMPACT: none — internal hardening of how database queries are built; users will notice no difference.
Nice-to-have, not blocking: a small negative test asserting a table outside _TYPED is rejected would lock in the allowlist assumption this fix leans on.
Given this touches the Postgres jobstore persistence layer, it should get a maintainer's sign-off on that surface once the two items above are addressed.
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with need human response.
## Summary Canonical re-cut of #24042 on current `main` (`72d5dfdc671a2ccb0ffbb983785e995d8bdaa507`). Head: `6a634c9706cd4f84d20cc492be8662f8ef5dc1b4`. This replaces f-string interpolation at all 13 assigned composition sites with `psycopg.sql` identifiers, literals, and typed literal SQL fragments. Runtime values remain query parameters. A single public `database.jobstore.connection.sql()` accessor preserves the lazy-postgres-driver invariant, and `_fenced_update` now types its SQL fragments as `LiteralString`. This is defense in depth, not a claim of a live exploit: the existing call paths already gate table/column choices through fixed allowlists, validated priorities/pools, or internal constants, as the reviewers of the overlapping PRs noted. Failure-Class: none ## Sites fixed - `backend/database/jobstore/postgres_store.py:261` — `due()`: quote the selected due column in both predicate and ordering. - `backend/database/jobstore/postgres_store.py:404` — `control_candidates()`: quote every priority/pool-derived column reference. - `backend/database/jobstore/postgres_store.py:508` — typed `read_doc()`: compose the table identifier and lock suffix. - `backend/database/jobstore/postgres_store.py:541` — `write_doc()`: quote the typed table identifier. - `backend/database/jobstore/postgres_store.py:556` — `delete_doc()`: quote the typed table identifier. - `backend/database/jobstore/postgres_store.py:569` — `service_frontier_in()`: quote the priority-derived column. - `backend/database/jobstore/postgres_store.py:593` — `purge_namespace()`: quote every cleanup table identifier. - `backend/database/jobstore/backfill.py:60` — `postgres_keys()`: quote the typed table identifier. - `backend/database/jobstore/outbox_pg.py:169` — `claim()`: compose the fixed `RETURNING` column list. - `backend/database/jobstore/outbox_pg.py:202` — `claim_key()`: compose the fixed `RETURNING` column list. - `backend/database/jobstore/outbox_pg.py:255` — `_fenced_update()`: compose only `LiteralString` set/guard fragments; keep claim data bound. - `backend/testing/pipeline_sim/postgres_store.py:35` — `PgRows._all()`: compose each typed table as `Identifier` and its projected name as `Literal` across the `UNION ALL`. - `backend/testing/pipeline_sim/postgres_store.py:210` — simulator `delete()`: quote the typed table identifier. `backend/database/jobstore/connection.py:28` adds the single lazy `psycopg.sql` accessor. The two simulator hunks are folded in from #24015. None of #24042's unrelated `commit_message.txt`, `test_sql.py`, or `.agent-brief/firestore-index-guard/tests.txt` files are carried over. ## Tests New hermetic behavioral coverage exercises every site above with hostile identifiers/values, verifies parameter tuples never enter rendered statement text, checks the outbox produces real `psycopg.sql.Composable` statements, and imports every changed module in clean subprocesses where `psycopg` is explicitly unavailable. Focused pytest runs (one process per file): | Test file | Result | | --- | ---: | | `test_jobstore_sql_composition.py` | 18 passed | | `test_jobstore_backfill.py` | 11 passed | | `test_jobstore_dual_store.py` | 68 passed | | `test_jobstore_migrations.py` | 7 passed | | `test_jobstore_postgres_locking.py` | 10 passed | | `test_jobstore_projection_mirror.py` | 8 passed | | `test_jobstore_protocol_parity.py` | 69 passed | | `test_jobstore_shadow_seams.py` | 23 passed | | `test_jobstore_codec.py` | 2 passed | | `test_batch_pipeline_lock_jobstore.py` | 2 passed | Total: **218 passed**. No full-suite or live-service run was used. - `bash backend/scripts/typecheck.sh`: **0 errors**. - `scripts/backend-python-format --check ...`: **7 files unchanged**. ## Supersedes and follow-up - Supersedes the original #24042 and overlapping #24015, #24023, and #24037; #24015's unique simulator coverage is retained here. - #24037's module-level psycopg import and #24015's private `_psycopg()` access are both avoided by the public lazy accessor. - #23806 must rebase after this merges and obtain fresh exact-head review; its recovery work is otherwise orthogonal. <!-- This is an auto-generated description by cubic. --> <a href="https://cubic.dev/pr/BasedHardware/omi/pull/25116?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="View guided diff" src="https://www.cubic.dev/buttons/review-in-cubic-light.svg"></picture></a> <a href="https://www.cubic.dev/action/auto-fix/pr/BasedHardware/omi/25116?returnTo=https%3A%2F%2Fgithub.com%2FBasedHardware%2Fomi%2Fpull%2F25116&source=description" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"><img alt="Turn on auto-fix" src="https://www.cubic.dev/buttons/turn-on-auto-fix-light.svg"></picture></a> <!-- End of auto-generated description by cubic. -->
🎯 What: Fixed potential SQL injection vulnerabilities in
⚠️ Risk: If the
backend/database/jobstore/postgres_store.pyandbackend/database/jobstore/backfill.pywhere dynamic table names were interpolated using f-strings into SQL queries.tablevariable was somehow supplied or tampered with by an external actor, it could lead to SQL injection attacks, putting the integrity and confidentiality of the database at risk.🛡️ Solution: Replaced f-string interpolation with
psycopg.sql.SQLandpsycopg.sql.Identifierformatting to properly quote and validate identifiers before passing them to the database engine.Failure-Class: none
PR created automatically by Jules for task 16573884054984747543 started by @undivisible