|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | + |
| 3 | +/** |
| 4 | + * #22046 (ruling B and its addendum) — the storage upload doors' ownership |
| 5 | + * rule, measured on a BOOTED organization wall. |
| 6 | + * |
| 7 | + * `@objectstack/service-storage` declares one ownership rule (`isFileUploader`) |
| 8 | + * that three upload doors consult after their by-id read and before any write |
| 9 | + * or disclosure: the commit door, the chunked-completion door and the progress |
| 10 | + * door. Its own suite pins the rule per door. What that suite cannot boot is |
| 11 | + * the organization wall, and the ruling's addendum asks for exactly that: per |
| 12 | + * door, a caller acting in ANOTHER organization, naming an upload it did not |
| 13 | + * start, is answered `403 PERMISSION_DENIED` with the same body as a caller in |
| 14 | + * the uploader's own organization — and the by-id read that precedes the rule |
| 15 | + * discloses nothing and writes nothing across the wall. |
| 16 | + * |
| 17 | + * ## Why this file lives in THIS package |
| 18 | + * |
| 19 | + * The wall is raised by this package's `OrganizationsPlugin` (the `org-scoping` |
| 20 | + * registrar the `tenancy` service probes). ADR-0132's entitlement boundary |
| 21 | + * forbids every other workspace package from declaring |
| 22 | + * `@objectstack/organizations` (`no-framework-dependents.pin.test.ts`), so the |
| 23 | + * storage package — and the dogfood package, whose multi-organization blocks |
| 24 | + * skip for the same reason — cannot mount it. The boundary is asymmetric: this |
| 25 | + * package may depend on framework packages freely, so the measurement comes |
| 26 | + * here rather than the plugin going there. It is the smallest package that |
| 27 | + * boots the wall. |
| 28 | + * |
| 29 | + * ## What is real |
| 30 | + * |
| 31 | + * `bootStack` (`@objectstack/verify`): an `ObjectKernel` over in-process |
| 32 | + * SQLite, the Hono app every request below is injected through, the real |
| 33 | + * `AuthPlugin` (better-auth sessions, organizations, invitations) and |
| 34 | + * `SecurityPlugin`. THIS package's `OrganizationsPlugin` and the real |
| 35 | + * `StorageServicePlugin` are mounted the way a host mounts them. The walled |
| 36 | + * posture is REQUESTED through `OS_TENANCY_POSTURE=isolated` and read back off |
| 37 | + * the `tenancy` service the boot resolved — not assumed. The three callers are |
| 38 | + * made through the product's own doors: the seeded platform owner, a colleague |
| 39 | + * invited into the owner's organization who accepted, and an outsider who |
| 40 | + * created an organization of their own. Nothing is stubbed. |
| 41 | + * |
| 42 | + * The rows each door names are read back through the engine as SYSTEM, whole, |
| 43 | + * before and after every refused call, and compared column for column — that is |
| 44 | + * "writes nothing". "Discloses nothing" is the refusal body: one code, one |
| 45 | + * status, and nothing about the row. |
| 46 | + */ |
| 47 | + |
| 48 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 49 | +import { mkdtempSync, rmSync } from 'node:fs'; |
| 50 | +import { tmpdir } from 'node:os'; |
| 51 | +import { join } from 'node:path'; |
| 52 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 53 | +import { StorageServicePlugin } from '@objectstack/service-storage'; |
| 54 | +import { OrganizationsPlugin } from './organizations-plugin.js'; |
| 55 | + |
| 56 | +const APP = { |
| 57 | + manifest: { |
| 58 | + id: 'com.example.uploadwall', |
| 59 | + namespace: 'uploadwall', |
| 60 | + version: '0.0.1', |
| 61 | + type: 'app', |
| 62 | + name: 'Upload Door Wall Fixture', |
| 63 | + }, |
| 64 | + objects: [], |
| 65 | +}; |
| 66 | + |
| 67 | +const SYS = { context: { isSystem: true } } as const; |
| 68 | + |
| 69 | +const COLLEAGUE_EMAIL = 'upload-wall-colleague@verify.test'; |
| 70 | +const OUTSIDER_EMAIL = 'upload-wall-outsider@verify.test'; |
| 71 | + |
| 72 | +interface Answer { |
| 73 | + status: number; |
| 74 | + body: any; |
| 75 | +} |
| 76 | + |
| 77 | +describe('the upload doors on a booted organization wall (#22046)', () => { |
| 78 | + const restoreEnv: Array<[string, string | undefined]> = []; |
| 79 | + let rootDir: string; |
| 80 | + let stack: VerifyStack; |
| 81 | + let ql: any; |
| 82 | + /** The seeded platform owner — every upload below is theirs. */ |
| 83 | + let uploader: string; |
| 84 | + /** Invited into the uploader's organization; not the uploader. */ |
| 85 | + let colleague: string; |
| 86 | + /** Acting in an organization of their own. */ |
| 87 | + let outsider: string; |
| 88 | + let uploaderOrg: string; |
| 89 | + let outsiderOrg: string; |
| 90 | + |
| 91 | + const answer = async (res: Response): Promise<Answer> => ({ |
| 92 | + status: res.status, |
| 93 | + body: await res.json().catch(() => undefined), |
| 94 | + }); |
| 95 | + const activeOrganization = async (token: string): Promise<string | null> => { |
| 96 | + const res = await stack.apiAs(token, 'GET', '/auth/get-session'); |
| 97 | + return ((await res.json()) as any)?.session?.activeOrganizationId ?? null; |
| 98 | + }; |
| 99 | + const stored = (object: string, id: string) => ql.findOne(object, { where: { id } }, SYS); |
| 100 | + |
| 101 | + const presign = async (): Promise<string> => { |
| 102 | + const res = await answer( |
| 103 | + await stack.apiAs(uploader, 'POST', '/storage/upload/presigned', { |
| 104 | + filename: 'wall-plan.txt', |
| 105 | + mimeType: 'text/plain', |
| 106 | + size: 5, |
| 107 | + }), |
| 108 | + ); |
| 109 | + expect(res.status).toBe(200); |
| 110 | + return res.body.data.fileId; |
| 111 | + }; |
| 112 | + const startChunked = async (): Promise<{ uploadId: string; fileId: string }> => { |
| 113 | + const res = await answer( |
| 114 | + await stack.apiAs(uploader, 'POST', '/storage/upload/chunked', { |
| 115 | + filename: 'wall-chunked.bin', |
| 116 | + mimeType: 'application/octet-stream', |
| 117 | + totalSize: 10, |
| 118 | + }), |
| 119 | + ); |
| 120 | + expect(res.status).toBe(200); |
| 121 | + return { uploadId: res.body.data.uploadId, fileId: res.body.data.fileId }; |
| 122 | + }; |
| 123 | + |
| 124 | + /** The one refusal: code and status, and nothing about the row. */ |
| 125 | + const expectRefused = (res: Answer, secrets: string[], label: string) => { |
| 126 | + expect(res.status, label).toBe(403); |
| 127 | + expect(res.body?.success, label).toBe(false); |
| 128 | + expect(res.body?.error?.code, label).toBe('PERMISSION_DENIED'); |
| 129 | + expect(res.body?.data, label).toBeUndefined(); |
| 130 | + const text = JSON.stringify(res.body); |
| 131 | + for (const s of secrets) expect(text.includes(s), `${label}: ${s}`).toBe(false); |
| 132 | + }; |
| 133 | + |
| 134 | + beforeAll(async () => { |
| 135 | + for (const [name, value] of [ |
| 136 | + ['OS_TENANCY_POSTURE', 'isolated'], |
| 137 | + // A walled deployment must declare what a new user joins, or this |
| 138 | + // package's own boot gate refuses it. |
| 139 | + ['OS_AUTH_MEMBERSHIP_POLICY', 'invite-only'], |
| 140 | + ] as const) { |
| 141 | + restoreEnv.push([name, process.env[name]]); |
| 142 | + process.env[name] = value; |
| 143 | + } |
| 144 | + rootDir = mkdtempSync(join(tmpdir(), 'upload-wall-')); |
| 145 | + stack = await bootStack(APP as never, { |
| 146 | + extraPlugins: [ |
| 147 | + new OrganizationsPlugin(), |
| 148 | + new StorageServicePlugin({ adapter: 'local', local: { rootDir }, bindToSettings: false }), |
| 149 | + ], |
| 150 | + }); |
| 151 | + ql = await stack.kernel.getServiceAsync('objectql'); |
| 152 | + |
| 153 | + uploader = await stack.signIn(); |
| 154 | + uploaderOrg = (await activeOrganization(uploader))!; |
| 155 | + |
| 156 | + colleague = await stack.signUp(COLLEAGUE_EMAIL); |
| 157 | + const invited = await answer( |
| 158 | + await stack.apiAs(uploader, 'POST', '/auth/organization/invite-member', { |
| 159 | + email: COLLEAGUE_EMAIL, |
| 160 | + role: 'member', |
| 161 | + organizationId: uploaderOrg, |
| 162 | + }), |
| 163 | + ); |
| 164 | + expect(invited.status).toBe(200); |
| 165 | + const accepted = await stack.apiAs(colleague, 'POST', '/auth/organization/accept-invitation', { |
| 166 | + invitationId: invited.body.id, |
| 167 | + }); |
| 168 | + expect(accepted.status).toBe(200); |
| 169 | + expect((await stack.apiAs(colleague, 'POST', '/auth/organization/set-active', { organizationId: uploaderOrg })).status).toBe(200); |
| 170 | + |
| 171 | + outsider = await stack.signUp(OUTSIDER_EMAIL); |
| 172 | + const created = await answer( |
| 173 | + await stack.apiAs(outsider, 'POST', '/auth/organization/create', { name: 'Upload Wall Other', slug: 'upload-wall-other' }), |
| 174 | + ); |
| 175 | + expect(created.status).toBe(200); |
| 176 | + outsiderOrg = created.body.id; |
| 177 | + }, 120_000); |
| 178 | + |
| 179 | + afterAll(async () => { |
| 180 | + await stack?.stop(); |
| 181 | + if (rootDir) rmSync(rootDir, { recursive: true, force: true }); |
| 182 | + for (const [name, value] of restoreEnv.reverse()) { |
| 183 | + if (value === undefined) delete process.env[name]; |
| 184 | + else process.env[name] = value; |
| 185 | + } |
| 186 | + }); |
| 187 | + |
| 188 | + it('PREMISE: the wall is up, and the three callers sit where the pins say they do', async () => { |
| 189 | + const tenancy: any = await stack.kernel.getServiceAsync('tenancy'); |
| 190 | + expect(tenancy.posture).toBe('isolated'); |
| 191 | + expect(tenancy.isolationActive).toBe(true); |
| 192 | + expect(tenancy.degraded).toBe(false); |
| 193 | + |
| 194 | + expect(uploaderOrg).toBeTruthy(); |
| 195 | + expect(await activeOrganization(colleague)).toBe(uploaderOrg); |
| 196 | + expect(await activeOrganization(outsider)).toBe(outsiderOrg); |
| 197 | + expect(outsiderOrg).not.toBe(uploaderOrg); |
| 198 | + |
| 199 | + // The rows the doors act on are stamped into the uploader's organization — |
| 200 | + // the wall the outsider is on the far side of. |
| 201 | + const fileId = await presign(); |
| 202 | + const row = await stored('sys_file', fileId); |
| 203 | + expect(row.organization_id).toBe(uploaderOrg); |
| 204 | + expect(row.owner_id).toBeTruthy(); |
| 205 | + const { uploadId } = await startChunked(); |
| 206 | + expect((await stored('sys_upload_session', uploadId)).organization_id).toBe(uploaderOrg); |
| 207 | + }); |
| 208 | + |
| 209 | + describe('the commit door', () => { |
| 210 | + const commit = (token: string, fileId: string, eTag: string) => |
| 211 | + stack.apiAs(token, 'POST', '/storage/upload/complete', { fileId, eTag }); |
| 212 | + |
| 213 | + it('SAME ORGANIZATION: a colleague is refused 403 PERMISSION_DENIED, and the row is unchanged', async () => { |
| 214 | + const fileId = await presign(); |
| 215 | + const before = await stored('sys_file', fileId); |
| 216 | + expectRefused(await answer(await commit(colleague, fileId, 'etag-colleague')), ['wall-plan', fileId, uploaderOrg], 'colleague'); |
| 217 | + expect(await stored('sys_file', fileId)).toEqual(before); |
| 218 | + }); |
| 219 | + |
| 220 | + it('ANOTHER ORGANIZATION: an outsider gets the colleague’s body, and the row is unchanged', async () => { |
| 221 | + const fileId = await presign(); |
| 222 | + const before = await stored('sys_file', fileId); |
| 223 | + const cross = await answer(await commit(outsider, fileId, 'etag-outsider')); |
| 224 | + expectRefused(cross, ['wall-plan', fileId, uploaderOrg], 'outsider'); |
| 225 | + expect(await stored('sys_file', fileId)).toEqual(before); |
| 226 | + expect(cross).toEqual(await answer(await commit(colleague, fileId, 'etag-colleague'))); |
| 227 | + }); |
| 228 | + |
| 229 | + it('POSITIVE: the uploader commits', async () => { |
| 230 | + const fileId = await presign(); |
| 231 | + const own = await answer(await stack.apiAs(uploader, 'POST', '/storage/upload/complete', { fileId, eTag: 'etag-uploader' })); |
| 232 | + expect(own.status).toBe(200); |
| 233 | + expect(own.body.data.fileId).toBe(fileId); |
| 234 | + const row = await stored('sys_file', fileId); |
| 235 | + expect(row.status).toBe('committed'); |
| 236 | + expect(row.etag).toBe('etag-uploader'); |
| 237 | + }); |
| 238 | + }); |
| 239 | + |
| 240 | + describe('the chunked-completion door', () => { |
| 241 | + const complete = (token: string, uploadId: string) => |
| 242 | + stack.apiAs(token, 'POST', `/storage/upload/chunked/${uploadId}/complete`, { parts: [] }); |
| 243 | + |
| 244 | + it('SAME ORGANIZATION: a colleague is refused 403 PERMISSION_DENIED, and neither row moves', async () => { |
| 245 | + const { uploadId, fileId } = await startChunked(); |
| 246 | + const sessionBefore = await stored('sys_upload_session', uploadId); |
| 247 | + const fileBefore = await stored('sys_file', fileId); |
| 248 | + expectRefused(await answer(await complete(colleague, uploadId)), ['wall-chunked', fileId, uploaderOrg], 'colleague'); |
| 249 | + expect(await stored('sys_upload_session', uploadId)).toEqual(sessionBefore); |
| 250 | + expect(await stored('sys_file', fileId)).toEqual(fileBefore); |
| 251 | + }); |
| 252 | + |
| 253 | + it('ANOTHER ORGANIZATION: an outsider gets the colleague’s body, and neither row moves', async () => { |
| 254 | + const { uploadId, fileId } = await startChunked(); |
| 255 | + const sessionBefore = await stored('sys_upload_session', uploadId); |
| 256 | + const fileBefore = await stored('sys_file', fileId); |
| 257 | + const cross = await answer(await complete(outsider, uploadId)); |
| 258 | + expectRefused(cross, ['wall-chunked', fileId, uploaderOrg], 'outsider'); |
| 259 | + expect(await stored('sys_upload_session', uploadId)).toEqual(sessionBefore); |
| 260 | + expect(await stored('sys_file', fileId)).toEqual(fileBefore); |
| 261 | + expect(cross).toEqual(await answer(await complete(colleague, uploadId))); |
| 262 | + }); |
| 263 | + |
| 264 | + it('POSITIVE: the uploader completes', async () => { |
| 265 | + const { uploadId, fileId } = await startChunked(); |
| 266 | + const own = await answer(await stack.apiAs(uploader, 'POST', `/storage/upload/chunked/${uploadId}/complete`, { parts: [] })); |
| 267 | + expect(own.status).toBe(200); |
| 268 | + expect(own.body.data.fileId).toBe(fileId); |
| 269 | + expect((await stored('sys_upload_session', uploadId)).status).toBe('completed'); |
| 270 | + expect((await stored('sys_file', fileId)).status).toBe('committed'); |
| 271 | + }); |
| 272 | + }); |
| 273 | + |
| 274 | + describe('the progress door', () => { |
| 275 | + const progress = (token: string, uploadId: string) => |
| 276 | + stack.apiAs(token, 'GET', `/storage/upload/chunked/${uploadId}/progress`); |
| 277 | + |
| 278 | + it('SAME ORGANIZATION: a colleague is refused 403 PERMISSION_DENIED, shown nothing, and the row is unchanged', async () => { |
| 279 | + const { uploadId, fileId } = await startChunked(); |
| 280 | + const before = await stored('sys_upload_session', uploadId); |
| 281 | + expectRefused(await answer(await progress(colleague, uploadId)), ['wall-chunked', fileId, uploaderOrg], 'colleague'); |
| 282 | + expect(await stored('sys_upload_session', uploadId)).toEqual(before); |
| 283 | + }); |
| 284 | + |
| 285 | + it('ANOTHER ORGANIZATION: an outsider gets the colleague’s body, shown nothing, and the row is unchanged', async () => { |
| 286 | + const { uploadId, fileId } = await startChunked(); |
| 287 | + const before = await stored('sys_upload_session', uploadId); |
| 288 | + const cross = await answer(await progress(outsider, uploadId)); |
| 289 | + expectRefused(cross, ['wall-chunked', fileId, uploaderOrg], 'outsider'); |
| 290 | + expect(await stored('sys_upload_session', uploadId)).toEqual(before); |
| 291 | + expect(cross).toEqual(await answer(await progress(colleague, uploadId))); |
| 292 | + }); |
| 293 | + |
| 294 | + it('POSITIVE: the uploader reads its progress', async () => { |
| 295 | + const { uploadId, fileId } = await startChunked(); |
| 296 | + const own = await answer(await stack.apiAs(uploader, 'GET', `/storage/upload/chunked/${uploadId}/progress`)); |
| 297 | + expect(own.status).toBe(200); |
| 298 | + expect(own.body.data.fileId).toBe(fileId); |
| 299 | + expect(own.body.data.filename).toBe('wall-chunked.bin'); |
| 300 | + }); |
| 301 | + }); |
| 302 | +}); |
0 commit comments