Zuhal Karadavut
Theme

LLM inference, evaluation and retrieval

tenant-guard

Can one tenant of a multi-tenant API read or change another tenant's data, even through an endpoint that forgets the tenant filter?

Answer

Not with row-level security underneath. I attacked my own API 252 times, through the real endpoints and through deliberately buggy ones. With only the app's SQLAlchemy filter, 108 attempts got through, all of them through the buggy endpoints. With PostgreSQL row-level security, none did. Against a real Keycloak, 13 of 13 bad tokens were rejected, and the whole guard adds 0.61 ms per request at p50.

Cross-tenant attempts that got through (attempts)
filter + RLS0results/isolation_matrix.jsonRLS only0results/isolation_matrix.jsonfilter only108results/isolation_matrix.jsonneither198results/isolation_matrix.json

How it was measured

  • 3 tenants with 3 roles each. Every tenant attacks every other tenant's records: list, get, change, delete, and a chat request with a spoofed tenant header. The buggy endpoints run raw SQL or an ORM query that opts out of the filter.
  • Four configurations: filter plus row-level security (shipped), row-level security only, filter only, and neither. Neither is the harness check: there, 72 of 72 reads must get through, and they do.
  • The application connects as a PostgreSQL role that is not a superuser and cannot bypass row-level security. Each transaction sets the tenant with SET LOCAL.
  • Bad tokens sent to a real Keycloak in CI: expired, wrong issuer, wrong audience, alg=none, a user without a tenant, a payload with the tenant switched, signed with an old key after rotation, and more.
  • Overhead: the same endpoint with and without the guard, 3,000 requests each.

Proof

Every file is pinned to the commit the numbers come from, so the links cannot drift.

Reproduce it

pip install -e ".[dev]"
pytest -m integration
python -m scripts.isolation_matrix
python -m scripts.token_rejection keycloak
python -m scripts.overhead

The integration tests and the experiments need PostgreSQL, Redis and Keycloak. CI starts all three as containers and runs the same commands on every push.

The repository on GitHub

Limits

  • One tenant per token. Users in several Keycloak organisations would need an explicit tenant selection step.
  • Token revocation is not checked, so access tokens should stay short-lived.
  • The Argo CD application is schema-checked, never synced to a real cluster.

Built with

  • Python
  • FastAPI
  • SQLAlchemy
  • PostgreSQL
  • Keycloak
  • Redis
  • OpenTelemetry
  • Helm
  • Argo CD