feat(backend): per-tenant isolation, my-cases library and delete-all - #22
Merged
Conversation
Ports Cinemory's reviewed multitenancy design onto ClaimScene's case routes: an optional Authorization: Bearer <Firebase ID token> resolves a verified uid via claimscene.auth; a _TenantScopedStorage wrapper prefixes every storage call with tenants/<uid>/ so isolation holds by construction, not by a checkable-and-forgettable comparison. Guest (no header, or FIREBASE_PROJECT_ID unset) is byte-identical to before this change. Wires the tenant dependency through case creation/render (sync and the async job submit + poll), and case fetch/schematic/illustration/verify/ receipt. Adds GET /me/library (list an authed tenant's own cases) and DELETE /me/data (erase everything under their prefix), both 401 for guest. Prerequisite fix: B2Storage._persist_index() blindly overwrote the remote index from its local snapshot, so a concurrent writer's rows (a background render-job thread vs a request thread) could be lost. Ported Cinemory's merge-on-write behaviour (re-read remote, union by key, drop a removed= key before the union so a delete cannot be resurrected) and added delete() to both the fake and B2 storage adapters.
6 tasks
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
Ports Cinemory's reviewed multitenancy design (this repo's other MIT entry) onto ClaimScene's case routes.
tenants/<uid>/storage prefix. Isolation is structural:_TenantScopedStorageprefixes everyput/get/exists/get_url/delete, and itsindexproperty returns only rows already starting with that prefix, with the prefix stripped — a cross-tenant or guest lookup 404s because the row is physically absent from the list being scanned, not because a comparison rejected it.uid(claimscene.auth.uid_from_authorization) — never a query param, header, or body field._tenant_storage(None)/_tenant_job_storage(None)return the exact module-level objects (no wrapper), and whenFIREBASE_PROJECT_IDis unset (today's production default)get_tenantreturnsNonewithout inspecting the header at all.GET /me/library(an authed tenant's own cases) andDELETE /me/data(erase everything under their prefix) — both 401 for guest.DELETE /me/datais structurally incapable of touching guest or another tenant's data: it enumerates the already-prefix-filtered index and deletes through the wrapper, so every key it can construct istenants/<verified-uid>/....Prerequisite fix —
B2Storage._persist_index(): previously overwrote the remoteindex.jsonlfrom the local in-memory snapshot, so a concurrent writer's rows (e.g. a background render-job thread vs. a request thread) could be silently lost. Ported Cinemory's merge-on-write behaviour: re-read the remote index, union by key, and (new) drop aremoved=<key>from that remote snapshot before the union so a just-deleted row is not resurrected. Addeddelete()to bothInMemoryStorageandB2Storage(no-op on an absent key, matching S3 semantics). A residual, documented limitation carries over honestly from Cinemory: a second already-constructedB2Storageinstance holding a stale in-memory index can still resurrect a just-deleted row if itputs before ever callingreload_index()— distributed locking over B2 remains out of scope by design.Test plan
ruff check src tests scripts— cleanpytest --cov=src --cov-report=term-missing --cov-fail-under=90 tests/— 415 passed, 1 skipped, exit 0, 97.07% coverage (up from 94.27% before this change)pytest tests/security -v— 75 passed (includes the 18 new adversarial multitenancy tests)python scripts/readiness.py --min 95— 100.0% automatable (unaffected; only pre-existing user-gated live-deploy items remain)git diff --name-only --diff-filter=M -- tests/— empty (every existing test file is untouched; all new coverage lives in 5 new files)/me/libraryshape + scoping + unreadable-manifest degrade;/me/datascope proof (other tenant's and guest data survive) + idempotency; tenant-scoped async job submit/poll incl. a different tenant polling a known job id → 404; spoofing via query param / header / body field (both unauthenticated and authenticated-as-someone-else); forged/expired/wrong-key tokens → 401 across every wired route; direct unit tests of_TenantScopedStorage(index filtering, hostile-uid sanitisation) and the guest byte-identity (is, not==) proof with multitenancy actually configured;B2Storage/InMemoryStoragedelete()+ merge-on-write unit tests (concurrent-writer union, corrupt-line self-heal, delete-resurrection regression).Notes on adaptation (not a blind port)
/me/libraryand/me/dataare kept identical to Cinemory's for surface parity between the two MIT entries.api._prepare_render), so it wouldn't actually exercise per-tenant namespacing here (documented in the new e2e file's module docstring).test_malformed_bearer_scheme_is_401_not_guestexplicitly setsFIREBASE_PROJECT_ID(Cinemory's version doesn't) so it genuinely exercises the malformed-schemeAuthError→ 401 path rather than incidentally passing via/me/library's own guest-401.