Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

docs: constrain owner lookup to the workspace, index sha256

Dannyb27eca64bed0
diff --git a/docs/design/sheet-blob-scoping.md b/docs/design/sheet-blob-scoping.md
index e32d97b328..3897d7a43c 100644
--- a/docs/design/sheet-blob-scoping.md
+++ b/docs/design/sheet-blob-scoping.md
@@ -109,8 +109,26 @@ CREATE TABLE sheet_blob_ref (
sha256 bytea NOT NULL REFERENCES sheet_blob(sha256),
PRIMARY KEY (project, sha256)
);
+
+CREATE INDEX idx_sheet_blob_ref_sha256 ON sheet_blob_ref(sha256);
```
+`project` alone is a sufficient scope column: `project.resource_id` is a single-column primary key,
+so project IDs are one global namespace across every workspace, and a project resolves to exactly
+one workspace through `project.workspace`.
+
+The gate and `HasSheets` both query `WHERE project = ? AND sha256 IN (…)`, which the
+project-leading primary key serves directly. The secondary index exists for the two paths that
+start from a hash with no project in hand: the owner lookup for a withheld statement
+([naming the owner](sheet-history-on-database-transfer.md#naming-the-owner)) and the zero-ref
+verification query in [Rollout](#rollout). PostgreSQL can only full-scan a `(project, sha256)` btree
+for a `sha256`-only predicate, so both would degrade as the table grows.
+
+Those hash-first queries are a deliberate exception to the composite-PK predicate rule in
+`AGENTS.md`: their whole purpose is to discover *which* project holds a hash, so the scope column
+cannot be supplied. They are made safe by joining through `project` and constraining to the caller's
+workspace instead — never by omitting scope entirely.
+
Migration slot `backend/migrator/migration/3.22/0005##scope_sheet_blob.sql`; `TestLatestVersion` in
`backend/migrator/migrator_test.go` needs updating, and `LATEST.sql` mirrored.
diff --git a/docs/design/sheet-history-on-database-transfer.md b/docs/design/sheet-history-on-database-transfer.md
index ef3bb2d3fa..332179bd25 100644
--- a/docs/design/sheet-history-on-database-transfer.md
+++ b/docs/design/sheet-history-on-database-transfer.md
@@ -114,9 +114,27 @@ resource name; `backend/store/changelog.go:163-168` already parses a project out
1. The revision's `release`, else its `taskRun` — the authoring project, and correct even after the
database has moved or the project has been purged, because the string is immutable.
-2. `sheet_blob_ref` — which projects currently hold a ref for that hash. Useful when the revision
- carries no provenance, both fields being optional.
-3. Neither — report that the authoring project is unknown.
+2. `sheet_blob_ref` — which projects currently hold a ref for that hash, **joined through `project`
+ and constrained to the caller's workspace**, and only when that leaves exactly one candidate.
+ Useful when the revision carries no provenance, both fields being optional.
+3. Neither, or more than one candidate — report that the authoring project is unknown.
+
+Step 2 must not read the ref table unqualified. `sheet_blob_ref` has no workspace column, and
+content-addressed dedup means one blob is shared by every project that authored identical SQL —
+across tenants. Generated boilerplate makes that routine rather than rare: `getCreateDatabaseStatement`
+emits `CREATE DATABASE %s;` for several engines, so a common statement will carry refs from many
+workspaces. Naming an arbitrary one would leak a foreign tenant's project name from a design whose
+purpose is closing cross-tenant leakage.
+
+```sql
+SELECT r.project
+FROM sheet_blob_ref r
+JOIN project p ON p.resource_id = r.project
+WHERE r.sha256 = ? AND p.workspace = ?
+```
+
+Ambiguity resolves to unknown rather than to a guess. Two projects in the same workspace holding
+refs for one hash is honest dedup, and neither is more the author than the other.
Purge is why step 1 has to come first. `DeleteProject` removes `sheet_blob_ref WHERE project = A`
before deleting project A, while a workspace-instance database that belonged to A is reassigned to
@@ -192,6 +210,12 @@ Tests:
been deleted, so it exercises the provenance path specifically.
- A revision carrying neither `release` nor `taskRun` falls through to `sheet_blob_ref`, and to
unknown when that is empty too.
+- Cross-tenant leak guard: seed identical SQL in two workspaces so one blob carries refs from both,
+ then request a withheld statement from one of them and assert the response never names the other
+ workspace's project. Generated `CREATE DATABASE` boilerplate makes this collision realistic, so it
+ is a regression test rather than a contrived one.
+- Ambiguity: two projects in the caller's own workspace holding refs for one hash resolves to
+ unknown, not to whichever row comes back first.
## Open items