fix(api): scope /hunts queries by tenant_id (C-2)#117
Merged
Conversation
…ccess (C-2) The /hunts API previously executed every SELECT/UPDATE against aisoc_hunts and aisoc_hunt_runs without a tenant_id filter, so any authenticated caller could read or mutate any other tenant's hunt hypotheses, runs, and findings simply by guessing or enumerating UUIDs. The aisoc_hunt_runs table didn't even carry a tenant_id column. Changes: - services/api/app/api/v1/endpoints/hunts.py: every list/get/update/create/run/ add-findings path now binds `:tenant_id` from the calling CurrentUser and filters on `tenant_id = :tenant_id` (or includes it in INSERTs). Cross-tenant reads return 404 instead of leaking data; cross-tenant writes return 404 via RETURNING-checks. Also replace PG-specific cast syntax (`:tags::text[]`, `:sample::jsonb`, `:findings::jsonb`) with `CAST(... AS ...)` so SQLAlchemy's text() can bind the parameters reliably. `created_by` now records the user's email instead of the str(CurrentUser) repr. - services/api/migrations/043_hunts_tenant_isolation.sql: add tenant_id column to aisoc_hunt_runs, backfill from parent aisoc_hunts row, and add covering indexes on (tenant_id, created_at DESC) and (tenant_id, hunt_id, run_at DESC). - services/api/tests/test_hunts_tenant_isolation.py: regression tests that mock DBSession, capture every executed SQL statement, and assert tenant_id is in the WHERE clause and bound params on every touch of aisoc_hunts* tables. Cross-tenant reads/updates/runs return 404. Full api suite passes (1212/1212). Refs: BUGHUNT.md C-2
5 tasks
Restores 'Python — Lint & Type-check' to green by satisfying the repo-wide ruff format gate that runs across all services in CI.
This was referenced May 14, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes the cross-tenant data-access vulnerability in the
/huntsAPI(BUGHUNT.md C-2). Previously every SELECT/UPDATE against
aisoc_huntsandaisoc_hunt_runsran without atenant_idfilter, so any authenticated callercould read or mutate any other tenant's hunt hypotheses, runs, and findings
just by guessing UUIDs.
aisoc_hunt_runsdidn't even carry atenant_idcolumn.
Changes
services/api/app/api/v1/endpoints/hunts.py— every list/get/update/create/run/add-findings path now binds
:tenant_idfrom the callingCurrentUserand filters ontenant_id = :tenant_id(or includes it inINSERTs). Cross-tenant reads now return 404 instead of leaking data;
cross-tenant writes return 404 via
RETURNING-checks. Also replacesPostgres-specific cast syntax (
:tags::text[],:sample::jsonb,:findings::jsonb) withCAST(... AS ...)so SQLAlchemy'stext()canbind the parameters reliably (previous form silently broke parameter
binding).
created_bynow records the user's email instead of thestr(CurrentUser)repr.services/api/migrations/043_hunts_tenant_isolation.sql— addtenant_idcolumn toaisoc_hunt_runs, backfill from the parentaisoc_huntsrow, and add covering indexes on(tenant_id, created_at DESC)and
(tenant_id, hunt_id, run_at DESC).services/api/tests/test_hunts_tenant_isolation.py— regression teststhat mock
DBSession, capture every executed SQL statement, and asserttenant_idis in the WHERE clause and bound params on every touch ofaisoc_hunts*tables. Cross-tenant reads/updates/runs return 404.Verification
python -m pytest tests/test_hunts_tenant_isolation.py -x -q→ 9 passedpython -m pytest -x -q(full api suite) → 1212 passedRefs
/huntsendpointsMade with Cursor