Skip to content

fix: inject tenant scope into ORM queries - #817

Merged
yaojin3616 merged 1 commit into
mainfrom
agent/debugger/89b072a2
Aug 5, 2026
Merged

fix: inject tenant scope into ORM queries#817
yaojin3616 merged 1 commit into
mainfrom
agent/debugger/89b072a2

Conversation

@yaojin3616

Copy link
Copy Markdown
Collaborator

Summary

Moves tenant filtering from individual DAO and business queries into SQLAlchemy's ORM session execution layer.

Root cause

Tenant isolation depended on each API, service, and DAO remembering to add a tenant_id predicate. More than a thousand direct API/service ORM queries bypass the DAO layer, so any missed predicate could expose another tenant's records.

Changes

  • Inject the active ContextVar tenant predicate for tenant-owned ORM SELECTs, including direct API/service queries.
  • Treat models with non-null tenant_id columns as tenant-owned by default and explicitly mark legacy nullable tenant-owned models.
  • Add a regression test proving a direct ORM query without a tenant filter returns only the active tenant's rows.

Validation

  • PYTHONPATH=. uv run pytest tests/test_base_dao.py
  • PYTHONPATH=. uv run ruff check app/dao/base.py tests/test_base_dao.py
  • bash scripts/arch-guard.sh

@yaojin3616
yaojin3616 merged commit 5288d3b into main Aug 5, 2026
1 check was pending
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