|
| 1 | +// Copyright (c) 2026 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Stored bytes are answered under one rule on a REAL boot with the local |
| 4 | +// storage adapter: a type outside the inline allow-list is served as an |
| 5 | +// `attachment`, every answer carries `nosniff` and the sandbox CSP, and an |
| 6 | +// allow-listed type is still served `inline`. |
| 7 | +// |
| 8 | +// The package suite (`service-storage/src/served-bytes-policy.test.ts`) pins |
| 9 | +// the rule and the S3 signed-URL parameters; this file runs the whole path a |
| 10 | +// browser takes — presign, the byte PUT, commit, the download door's signed |
| 11 | +// URL, and the raw byte route that answers it — through the real Hono app. |
| 12 | + |
| 13 | +import { describe, it, expect, beforeAll, afterAll } from 'vitest'; |
| 14 | +import { mkdtempSync } from 'node:fs'; |
| 15 | +import { join } from 'node:path'; |
| 16 | +import { tmpdir } from 'node:os'; |
| 17 | +import { bootStack, type VerifyStack } from '@objectstack/verify'; |
| 18 | +import { StorageServicePlugin } from '@objectstack/service-storage'; |
| 19 | +import { attachmentsFixtureStack, attachmentsFixtureSecurity } from './fixtures/attachments-fixture.js'; |
| 20 | + |
| 21 | +const PNG = Buffer.from('89504e470d0a1a0a0000000d49484452000000010000000108060000001f15c4890000000d4944415478da63f8ffff3f0005fe02fea7d6a6a30000000049454e44ae426082', 'hex'); |
| 22 | +const PDF = Buffer.from('%PDF-1.4\n1 0 obj<<>>endobj\ntrailer<<>>\n%%EOF\n'); |
| 23 | +const HTML = Buffer.from('<!doctype html><title>t</title><p>stored page</p>'); |
| 24 | +const SVG = Buffer.from('<svg xmlns="http://www.w3.org/2000/svg" width="1" height="1"><rect width="1" height="1"/></svg>'); |
| 25 | +const TEXT = Buffer.from('plain words\n'); |
| 26 | + |
| 27 | +const SANDBOX_CSP = "sandbox; default-src 'none'; style-src 'unsafe-inline'"; |
| 28 | +const MEDIA_CSP = "sandbox allow-same-origin; default-src 'none'; media-src 'self'; style-src 'unsafe-inline'"; |
| 29 | +/** A WAV of `samples` silent 8 kHz mono 8-bit PCM samples. */ |
| 30 | +function wav(samples: number): Buffer { |
| 31 | + const out = Buffer.alloc(44 + samples, 0x80); |
| 32 | + out.write('RIFF', 0, 'ascii'); |
| 33 | + out.writeUInt32LE(36 + samples, 4); |
| 34 | + out.write('WAVEfmt ', 8, 'ascii'); |
| 35 | + out.writeUInt32LE(16, 16); |
| 36 | + out.writeUInt16LE(1, 20); // PCM |
| 37 | + out.writeUInt16LE(1, 22); // mono |
| 38 | + out.writeUInt32LE(8000, 24); |
| 39 | + out.writeUInt32LE(8000, 28); |
| 40 | + out.writeUInt16LE(1, 32); |
| 41 | + out.writeUInt16LE(8, 34); |
| 42 | + out.write('data', 36, 'ascii'); |
| 43 | + out.writeUInt32LE(samples, 40); |
| 44 | + return out; |
| 45 | +} |
| 46 | +const WAV = wav(800); |
| 47 | + |
| 48 | +describe('stored bytes are served by the inline allow-list rule on a real boot', () => { |
| 49 | + let stack: VerifyStack; |
| 50 | + let token: string; |
| 51 | + |
| 52 | + const authed = (extra: Record<string, string> = {}) => ({ Authorization: `Bearer ${token}`, ...extra }); |
| 53 | + |
| 54 | + /** Upload through the presigned door and answer the raw byte route's response for the file. */ |
| 55 | + async function uploadAndFetch(filename: string, mimeType: string, bytes: Buffer): Promise<Response> { |
| 56 | + const presign = await stack.api('/storage/upload/presigned', { |
| 57 | + method: 'POST', |
| 58 | + headers: authed({ 'Content-Type': 'application/json' }), |
| 59 | + body: JSON.stringify({ filename, mimeType, size: bytes.byteLength, scope: 'user' }), |
| 60 | + }); |
| 61 | + expect(presign.status, await presign.clone().text()).toBe(200); |
| 62 | + const { data } = (await presign.json()) as any; |
| 63 | + |
| 64 | + const put = await stack.raw(String(data.uploadUrl).replace(/^https?:\/\/[^/]+/, ''), { |
| 65 | + method: 'PUT', |
| 66 | + headers: { 'content-type': mimeType }, |
| 67 | + body: new Uint8Array(bytes), |
| 68 | + }); |
| 69 | + expect(put.status, await put.clone().text()).toBeLessThan(300); |
| 70 | + |
| 71 | + const complete = await stack.api('/storage/upload/complete', { |
| 72 | + method: 'POST', |
| 73 | + headers: authed({ 'Content-Type': 'application/json' }), |
| 74 | + body: JSON.stringify({ fileId: data.fileId }), |
| 75 | + }); |
| 76 | + expect(complete.status, await complete.clone().text()).toBe(200); |
| 77 | + |
| 78 | + const link = await stack.api(`/storage/files/${data.fileId}/url`, { headers: authed() }); |
| 79 | + expect(link.status, await link.clone().text()).toBe(200); |
| 80 | + const url = String(((await link.json()) as any).data.url); |
| 81 | + // The signed link is the app's own raw byte route: it is answered on the app origin. |
| 82 | + expect(url).toMatch(/\/api\/v1\/storage\/_local\/raw\//); |
| 83 | + const served = await stack.raw(url.replace(/^https?:\/\/[^/]+/, '')); |
| 84 | + expect(served.status).toBe(200); |
| 85 | + return served; |
| 86 | + } |
| 87 | + |
| 88 | + beforeAll(async () => { |
| 89 | + const rootDir = mkdtempSync(join(tmpdir(), 'served-bytes-dogfood-')); |
| 90 | + stack = await bootStack(attachmentsFixtureStack as never, { |
| 91 | + security: attachmentsFixtureSecurity(), |
| 92 | + extraPlugins: [new StorageServicePlugin({ adapter: 'local', local: { rootDir } })], |
| 93 | + }); |
| 94 | + token = await stack.signIn(); |
| 95 | + }, 120_000); |
| 96 | + |
| 97 | + afterAll(async () => { |
| 98 | + await stack?.stop(); |
| 99 | + }); |
| 100 | + |
| 101 | + it('a text/html upload is served as an attachment, with nosniff and the sandbox CSP', async () => { |
| 102 | + const res = await uploadAndFetch('page.html', 'text/html', HTML); |
| 103 | + expect(res.headers.get('content-disposition')).toMatch(/^attachment;/); |
| 104 | + expect(res.headers.get('x-content-type-options')).toBe('nosniff'); |
| 105 | + expect(res.headers.get('content-security-policy')).toBe(SANDBOX_CSP); |
| 106 | + }); |
| 107 | + |
| 108 | + it('an image/svg+xml upload is served as an attachment, with nosniff', async () => { |
| 109 | + const res = await uploadAndFetch('drawing.svg', 'image/svg+xml', SVG); |
| 110 | + expect(res.headers.get('content-disposition')).toMatch(/^attachment;/); |
| 111 | + expect(res.headers.get('x-content-type-options')).toBe('nosniff'); |
| 112 | + }); |
| 113 | + |
| 114 | + it('a declared type is answered by its essence, so no spelling of it reaches inline', async () => { |
| 115 | + const res = await uploadAndFetch('odd.html', 'Text/HTML ; charset=utf-8', HTML); |
| 116 | + expect(res.headers.get('content-type')).toBe('text/html'); |
| 117 | + expect(res.headers.get('content-disposition')).toMatch(/^attachment;/); |
| 118 | + }); |
| 119 | + |
| 120 | + it('a declared type that is not a media type is served as opaque bytes, as an attachment', async () => { |
| 121 | + const res = await uploadAndFetch('odd.bin', 'not a type', HTML); |
| 122 | + expect(res.headers.get('content-type')).toBe('application/octet-stream'); |
| 123 | + expect(res.headers.get('content-disposition')).toMatch(/^attachment;/); |
| 124 | + }); |
| 125 | + |
| 126 | + it('control: an ordinary PNG still previews, inline under its own type and bytes', async () => { |
| 127 | + const res = await uploadAndFetch('pixel.png', 'image/png', PNG); |
| 128 | + expect(res.headers.get('content-type')).toBe('image/png'); |
| 129 | + expect(res.headers.get('content-disposition')).toMatch(/^inline;/); |
| 130 | + expect(Buffer.from(await res.arrayBuffer()).equals(PNG), 'the stored bytes, unchanged').toBe(true); |
| 131 | + }); |
| 132 | + |
| 133 | + it('an allow-listed audio file is served inline, with nosniff and the media policy', async () => { |
| 134 | + const res = await uploadAndFetch('tone.wav', 'audio/wav', WAV); |
| 135 | + expect(res.headers.get('content-type')).toBe('audio/wav'); |
| 136 | + expect(res.headers.get('content-disposition')).toMatch(/^inline;/); |
| 137 | + expect(res.headers.get('x-content-type-options')).toBe('nosniff'); |
| 138 | + expect(res.headers.get('content-security-policy')).toBe(MEDIA_CSP); |
| 139 | + }); |
| 140 | + |
| 141 | + it('an allow-listed PNG, PDF or plain text is served inline, with nosniff and the sandbox CSP', async () => { |
| 142 | + for (const [name, type, bytes] of [['pixel.png', 'image/png', PNG], ['doc.pdf', 'application/pdf', PDF], ['notes.txt', 'text/plain', TEXT]] as const) { |
| 143 | + const res = await uploadAndFetch(name, type, bytes); |
| 144 | + expect(res.headers.get('content-type'), name).toBe(type); |
| 145 | + expect(res.headers.get('content-disposition'), name).toMatch(/^inline;/); |
| 146 | + expect(res.headers.get('x-content-type-options'), name).toBe('nosniff'); |
| 147 | + expect(res.headers.get('content-security-policy'), name).toBe(SANDBOX_CSP); |
| 148 | + } |
| 149 | + }); |
| 150 | +}); |
0 commit comments