// The tenant proof against planted holes on the memory driver: each way past the repository guard // — a child with no tenant, `crossTenant()`, a service actor, a hand-built cache key, a mail, a // write by input id — reads crossed with its primitive named; a scoped read is proven, a public // read is public with its write half kept, and a handler that never answers proves nothing. import { afterAll, beforeAll, beforeEach, describe, expect, test } from 'bun:test'; import { type PgliteClient, pgliteClient } from '@ultimat3/db'; import type { ExerciseRecord, TenantFinding } from './exercise-report'; import { proveOne, resetTenantApp, tenantlessRows, tpFiles } from './tenant-proof-fixture'; import { tenantProofFindings } from './tenant-proof-report'; import type * as SqlFixture from './tenant-proof-sql-fixture'; import { testName } from './test-types'; /** The findings of a crossed record — an empty list for any other verdict, which fails the caller. */ const findingsOf = (record: ExerciseRecord): readonly TenantFinding[] => record.tenant?.verdict === 'crossed' ? record.tenant.findings : []; /** A crossed record whose positive control answered: the hole is real, not a broken fixture. */ const expectCrossed = (record: ExerciseRecord, name: string, kind: ExerciseRecord['kind']) => { expect({ name: record.name, kind: record.kind, outcome: record.outcome }).toEqual({ name, kind, outcome: 'reached', }); expect(record.tenant?.verdict).toBe('crossed'); return findingsOf(record); }; /** The ids of tp_files rows a write moved to `url` — the rows the finding must name. */ const renamedTo = async (url: string): Promise => (await tenantlessRows(tpFiles)).filter((row) => row['url'] === url).map((row) => row['id']); // The planted-hole fixtures declare `tp:read`/`tp:write` as they load, so they load HERE, never as // a static import: the registry guard keeps what a file's module graph declared for every later // file on a shared worker, and these names turned strict checking on for files that declare none // (X_PERMISSION_UNKNOWN). Loaded in `beforeAll`, the declarations are this file's own. let sql: typeof SqlFixture; beforeAll(async () => { sql = await import('./tenant-proof-sql-fixture'); await import('./tenant-proof-holes-fixture'); await import('./tenant-proof-sweep-fixture'); }); beforeEach(resetTenantApp); describe(testName('unit', 'tenant proof: every planted hole is crossed'), () => { test('a child with no tenant column, read by id, leaks its url into the output', async () => { const findings = expectCrossed(await proveOne('tpChildRead'), 'tpChildRead', 'action'); expect(findings).toContainEqual( expect.objectContaining({ code: 'X_TENANT_LEAK', leg: 'before', path: 'output.url' }), ); }); test('the same child read through a query is crossed, named as the query', async () => { const findings = expectCrossed(await proveOne('tpChildQuery'), 'tpChildQuery', 'query'); const leak = findings.find((finding) => finding.code === 'X_TENANT_LEAK'); expect(leak?.path).toMatch(/^output\b.*url$/); }); test('crossTenant() under a policy that is not public leaks', async () => { const findings = expectCrossed(await proveOne('tpCrossRead'), 'tpCrossRead', 'action'); expect(findings).toContainEqual( expect.objectContaining({ code: 'X_TENANT_LEAK', path: 'output.label' }), ); }); test('a service actor minted from the input org leaks', async () => { const findings = expectCrossed(await proveOne('tpServiceRead'), 'tpServiceRead', 'action'); expect(findings).toContainEqual( expect.objectContaining({ code: 'X_TENANT_LEAK', path: 'output.label' }), ); }); test('a cache key without the tenant leaks on the leg after A filled it, never before', async () => { const findings = expectCrossed(await proveOne('tpCachedRead'), 'tpCachedRead', 'action'); expect(findings.map((finding) => [finding.code, finding.leg])).toEqual([ ['X_TENANT_LEAK', 'after'], ]); }); test('a mail to A’s owner is a leak in an effect, with nothing in the output', async () => { const findings = expectCrossed(await proveOne('tpMailOwner'), 'tpMailOwner', 'action'); const leaks = findings.filter((finding) => finding.code === 'X_TENANT_LEAK'); expect(leaks.length).toBeGreaterThan(0); for (const leak of leaks) expect(leak.path).toStartWith('effect.'); }); test('a write by input id is X_TENANT_WRITE_CROSSED naming the table and A’s row', async () => { const findings = expectCrossed(await proveOne('tpRenameFile'), 'tpRenameFile', 'action'); // The scale sweep calls each reached primitive again in tenants of its own, so their own rows // are renamed too: A's row is the one the finding names, and it is among them. const renamed = (await renamedTo('https://renamed.example.test/by-caller')).map(String); const crossed = findings.find((one) => one.code === 'X_TENANT_WRITE_CROSSED'); expect(crossed).toMatchObject({ leg: 'before', table: 'tp_files' }); expect(renamed).toContain(String(crossed?.id)); }); test('the same write from a job body is crossed, named as the job', async () => { const findings = expectCrossed(await proveOne('tpRenameFileJob'), 'tpRenameFileJob', 'job'); // The scale sweep calls each reached primitive again in tenants of its own, so their own rows // are renamed too: A's row is the one the finding names, and it is among them. const renamed = (await renamedTo('https://renamed.example.test/by-worker')).map(String); const crossed = findings.find((one) => one.code === 'X_TENANT_WRITE_CROSSED'); expect(crossed).toMatchObject({ table: 'tp_files' }); expect(renamed).toContain(String(crossed?.id)); // Handed A's id, it is B choosing A's row: never a sweep, whatever the job declares. for (const finding of findings) expect(finding.sweep).toBeUndefined(); }); }); describe(testName('unit', 'tenant proof: what must stay green'), () => { test('a read the guard scopes is proven', async () => { const record = await proveOne('tpScopedRead'); expect(record.outcome).toBe('reached'); expect(record.tenant).toEqual({ verdict: 'proven' }); }); test('a public read is public with a reason, though the same read under a role leaks', async () => { const record = await proveOne('tpPublicFile'); expect(record.outcome).toBe('reached'); expect(record.tenant?.verdict).toBe('public'); expect(record.tenant?.verdict === 'public' && record.tenant.reason.trim()).not.toBe(''); }); test('a public write by input id is still crossed: the write half is never skipped', async () => { const findings = expectCrossed(await proveOne('tpPublicRename'), 'tpPublicRename', 'action'); expect(findings.map((finding) => finding.code)).toContain('X_TENANT_WRITE_CROSSED'); expect(findings.map((finding) => finding.code)).not.toContain('X_TENANT_LEAK'); }); test('a child declared shared belongs to no tenant: reading it by id is proven', async () => { const record = await proveOne('tpLinkRead'); expect(record.outcome).toBe('reached'); expect(record.tenant).toEqual({ verdict: 'proven' }); }); test('a job sweeping every org under crossTenant() is public, naming its reason', async () => { const record = await proveOne('tpVaultSweep'); expect(record.outcome).toBe('reached'); expect(record.tenant).toEqual({ verdict: 'public', reason: expect.stringContaining('the nightly vault report covers every org'), }); }); test('the same sweep with no crossTenant() declared is crossed', async () => { const record = await proveOne('tpFileSweep'); const findings = expectCrossed(record, 'tpFileSweep', 'job'); expect(findings).toContainEqual(expect.objectContaining({ code: 'X_TENANT_LEAK' })); // `tenant: 'none'`, handed nothing of A's: a sweep that declared no scope, so its fix is one. for (const finding of findings) expect(finding.sweep).toBe(true); for (const coded of tenantProofFindings([record])) { expect(coded.fix).toContain("crossTenant(''"); } }); test("a tenant: 'none' purge of every org's rows, handed no id, is public: no B chose A's row", async () => { const record = await proveOne('tpFilePurge'); expect(record.outcome).toBe('reached'); expect(record.tenant).toEqual({ verdict: 'public', reason: expect.stringContaining("tenant: 'none'"), }); }); test('a handler that always throws is unreached and unproven, never proven', async () => { const record = await proveOne('tpAlwaysThrows'); expect(record.outcome).toBe('unreached'); expect(record.tenant?.verdict).toBe('unproven'); expect(record.tenant?.verdict === 'unproven' && record.tenant.reason.trim()).not.toBe(''); }); }); /** A cold PGlite boot, the migration and two passes. */ const PGLITE_MS = 60_000; describe(testName('unit', 'tenant proof: raw SQL, on PGlite'), () => { let pglite: PgliteClient | undefined; beforeAll(async () => { pglite = pgliteClient(); await sql.useSql(pglite); }, PGLITE_MS); afterAll(async () => { sql.releaseSql(); await pglite?.close(); }); test( 'a raw-SQL read by input id leaks', async () => { const findings = expectCrossed(await sql.proveSql('tpsRawRead', 1), 'tpsRawRead', 'action'); expect(findings).toContainEqual( expect.objectContaining({ code: 'X_TENANT_LEAK', path: 'output.body' }), ); }, PGLITE_MS, ); test( 'a raw-SQL update by input id is crossed, naming the table and the row', async () => { const findings = expectCrossed(await sql.proveSql('tpsRawWrite', 2), 'tpsRawWrite', 'action'); // The scale sweep calls each reached primitive again in tenants of its own, so their own rows // are renamed too: A's row is the one the finding names, and it is among them. const overwritten = (await sql.overwrittenDocs()).map(String); const crossed = findings.find((one) => one.code === 'X_TENANT_WRITE_CROSSED'); expect(crossed).toMatchObject({ table: 'tps_docs' }); expect(overwritten).toContain(String(crossed?.id)); }, PGLITE_MS, ); test( 'the same row through the scoped repository is proven', async () => { const record = await sql.proveSql('tpsScopedRead', 3); expect(record.outcome).toBe('reached'); expect(record.tenant).toEqual({ verdict: 'proven' }); }, PGLITE_MS, ); });