Repository navigation
🔒 Fix potential SQL injection in jobstore backfill postgres_keys - #24023
undivisible wants to merge 2 commits into
Conversation
Replaced the dynamic string construction of the SQL table name parameter in `postgres_keys` within `backend/database/jobstore/backfill.py`. The f-string logic has been updated to use `psycopg.sql.SQL` and `sql.Identifier` to safely and correctly format the table identifier into the query. 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. |
Replaced the dynamic string construction of the SQL table name parameter in `postgres_keys` within `backend/database/jobstore/backfill.py`. The f-string logic has been updated to use `psycopg.sql.SQL` and `sql.Identifier` to safely and correctly format the table identifier into the query. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
|
Verdict: the production change here is correct and nicely scoped; two non-blocking items before merge (a green CI run and one unrelated file). backend/database/jobstore/backfill.py — Related follow-up (not blocking): the same pattern still exists in
CI: Backend unit suite (shard 3/6) failed on Security: net hardening, no new surface. Performance: none — same single query per call. Still wanted before merge: (1) a green Backend unit suite run on this head, (2) drop or justify by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
|
The core fix is correct and verified; one unrelated file should be dropped, and the red Backend unit suite is pre-existing main flakiness, not from this change.
Security: strictly strengthens SQL construction; blast radius is the offline A6 backfill census path only. Performance/cost: unchanged — same single query per census. Related follow-up (not blocking): the same f-string USER IMPACT: none — internal hardening of an offline backfill tool; users will notice no difference. What I'd still want as a reviewer:
by AI on behalf of David — if you need David’s attention urgently, please @Git-on-my-level and escalate with |
## 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 a potential SQL injection vulnerability in
backend/database/jobstore/backfill.pywhere a table name was dynamically inserted into a raw SQL query string using f-strings (f'SELECT key FROM {table} ...').tableargument inpostgres_keys, it could allow arbitrary SQL execution. Whiletablemight currently be constrained by the_TYPEDallowlist earlier in the code block, using unescaped f-strings for SQL construction is an insecure pattern susceptible to injection if logic changes or constraints fail.🛡️ Solution: Updated the unparameterized query inside
postgres_keysto safely escape the table name by utilizing thepsycopg.sqlmodule (sql.SQLandsql.Identifier). This acts as robust defense-in-depth and eliminates the insecure pattern.Failure-Class: none
PR created automatically by Jules for task 7165385646914771110 started by @undivisible