import fs from 'node:fs/promises' import os from 'node:os' import path from 'node:path' import Fastify from 'fastify' import type { FastifyInstance } from 'fastify' import { afterEach, beforeEach, describe, expect, test } from 'vitest' import { cedarUploadsPlugin } from '../fastify/plugin.js' import { createDbProvider } from '../providers/db.js' import { createFsProvider } from '../providers/fs.js' import { storeFile } from '../storeFile.js' import { defineStorageTargets } from '../targets.js' import { db, prisma, resetTestDb } from './helpers/testDb.js' import { SECRET, tokenFor } from './helpers/tokens.js' interface Part { field?: string filename: string type: string content: string | Buffer } function multipart(parts: Part[]) { const boundary = '----CedarTestBoundary' const chunks: Buffer[] = [] for (const part of parts) { chunks.push( Buffer.from( `--${boundary}\r\n` + `Content-Disposition: form-data; name="${part.field ?? 'file'}"; ` + `filename="${part.filename}"\r\n` + `Content-Type: ${part.type}\r\n\r\n`, ), Buffer.from(part.content), Buffer.from('\r\n'), ) } chunks.push(Buffer.from(`--${boundary}--\r\n`)) const payload = Buffer.concat(chunks) return { payload, headers: { 'content-type': `multipart/form-data; boundary=${boundary}`, 'content-length': String(payload.byteLength), }, } } describe('cedarUploadsPlugin', () => { let uploadDir: string let app: FastifyInstance let targets: ReturnType function makeTargets(dir: string) { return defineStorageTargets({ local: createFsProvider({ uploadDir: dir, serveBaseUrl: 'http://localhost:8911', signSecret: SECRET, }), thumbs: createDbProvider(), }) } beforeEach(async () => { await resetTestDb() uploadDir = await fs.mkdtemp( path.join(os.tmpdir(), 'cedar-uploads-plugin-'), ) targets = makeTargets(uploadDir) app = Fastify() await app.register(cedarUploadsPlugin, { tokenSecret: SECRET, targets, db, // `anon` simulates a request with no session; anything else names the // requesting user, defaulting to the token's owner authenticate: async (req) => { const id = req.headers['x-test-user'] if (id === 'anon') { return null } return { id: typeof id === 'string' ? id : 'user_1' } }, }) await app.ready() }) afterEach(async () => { await app.close() await fs.rm(uploadDir, { recursive: true, force: true }) }) test('GET /upload/health answers liveness without configuration details', async () => { const res = await app.inject({ method: 'GET', url: '/upload/health' }) expect(res.statusCode).toBe(200) expect(res.json()).toEqual({ ok: true }) }) describe('POST /upload/fs', () => { test('stores each file and returns completed uploads', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'hello' }, { filename: 'b.txt', type: 'text/plain', content: 'world!' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor() }, payload: body.payload, }) expect(res.statusCode).toBe(201) const { uploads } = res.json() expect(uploads).toHaveLength(2) expect(uploads[0]).toMatchObject({ target: 'local', status: 'completed', filename: 'a.txt', mimeType: 'text/plain', size: '5', userId: 'user_1', }) expect(uploads[0]).not.toHaveProperty('data') expect(uploads[0]).not.toHaveProperty('tokenId') const onDisk = await fs.readFile( path.join(uploadDir, uploads[1].storageKey), ) expect(onDisk.toString()).toBe('world!') const rows = await prisma.upload.findMany() expect(rows.every((r) => r.status === 'completed')).toBe(true) }) test('rejects a missing or invalid token', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const missing = await app.inject({ method: 'POST', url: '/upload/fs', headers: body.headers, payload: body.payload, }) expect(missing.statusCode).toBe(401) expect(missing.json().error.code).toBe('INVALID_TOKEN') const bad = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': 'nope' }, payload: body.payload, }) expect(bad.statusCode).toBe(401) }) test('rejects a token issued to a different user', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor(), 'x-test-user': 'someone_else', }, payload: body.payload, }) expect(res.statusCode).toBe(403) expect(res.json().error.code).toBe('FORBIDDEN') expect(await prisma.upload.count()).toBe(0) }) test('rejects an unauthenticated request when an authenticator is configured', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor(), 'x-test-user': 'anon', }, payload: body.payload, }) expect(res.statusCode).toBe(401) expect(res.json().error.code).toBe('UNAUTHENTICATED') expect(await prisma.upload.count()).toBe(0) }) test('accepts the token when the requester matches', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor(), 'x-test-user': 'user_1', }, payload: body.payload, }) expect(res.statusCode).toBe(201) }) test('rejects disallowed MIME types', async () => { const body = multipart([ { filename: 'a.zip', type: 'application/zip', content: 'x' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor() }, payload: body.payload, }) expect(res.statusCode).toBe(415) expect(await prisma.upload.count()).toBe(0) }) test('rejects a request whose Content-Length exceeds the token bound', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'content-length': String(10 * 1024 * 1024), 'x-upload-token': tokenFor(), }, payload: body.payload, }) expect(res.statusCode).toBe(413) }) test('rejects a file larger than maxFileSize mid-stream', async () => { const body = multipart([ { filename: 'big.txt', type: 'text/plain', content: Buffer.alloc(2048, 'a'), }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor({ maxFileSize: 1024 }), }, payload: body.payload, }) expect(res.statusCode).toBe(413) expect(res.json().error.code).toBe('FILE_TOO_LARGE') expect(await prisma.upload.count()).toBe(0) }) test('rolls back every file of a batch when a later part is rejected', async () => { const body = multipart([ { filename: 'ok.txt', type: 'text/plain', content: 'fine' }, { filename: 'bad.zip', type: 'application/zip', content: 'nope' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor() }, payload: body.payload, }) expect(res.statusCode).toBe(415) expect(await prisma.upload.count()).toBe(0) expect(await fs.readdir(uploadDir)).toEqual([]) }) test('enforces maxFiles across requests sharing a token', async () => { const token = tokenFor({ maxFiles: 1 }) const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const first = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': token }, payload: body.payload, }) expect(first.statusCode).toBe(201) const second = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': token }, payload: body.payload, }) expect(second.statusCode).toBe(400) expect(second.json().error.code).toBe('TOO_MANY_FILES') }) test('refuses DB targets', async () => { const body = multipart([ { filename: 'a.txt', type: 'text/plain', content: 'x' }, ]) const res = await app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor({ target: 'thumbs' }), }, payload: body.payload, }) expect(res.statusCode).toBe(400) expect(res.json().error.code).toBe('NOT_SUPPORTED') }) test('trusts the claimed type with verifyContentType: false', async () => { const trusting = Fastify() await trusting.register(cedarUploadsPlugin, { tokenSecret: SECRET, targets, db, verifyContentType: false, }) await trusting.ready() const body = multipart([ { filename: 'a.png', type: 'image/png', content: '' }, ]) const res = await trusting.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': tokenFor() }, payload: body.payload, }) expect(res.statusCode).toBe(201) expect(res.json().uploads[0].mimeType).toBe('image/png') await trusting.close() }) describe('checking content types (the default)', () => { const PNG = Buffer.from([ 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0x0d, ]) const upload = (parts: Part[], token = tokenFor()) => { const body = multipart(parts) return app.inject({ method: 'POST', url: '/upload/fs', headers: { ...body.headers, 'x-upload-token': token }, payload: body.payload, }) } test('stores a file under the type its bytes are, whatever was claimed', async () => { const res = await upload([ { filename: 'photo', type: 'application/octet-stream', content: PNG, }, ]) expect(res.statusCode).toBe(201) const [stored] = res.json().uploads expect(stored.mimeType).toBe('image/png') expect(stored.storageKey).toMatch(/\.png$/) expect(await prisma.upload.findFirst()).toMatchObject({ mimeType: 'image/png', }) }) test('stores the canonical spelling of an aliased type', async () => { const res = await upload( [ { filename: 'a.jpg', type: 'image/jpg', content: Buffer.from([0xff, 0xd8, 0xff, 0xe0, 0, 0x10]), }, ], tokenFor({ allowedMimeTypes: ['image/jpg'] }), ) expect(res.statusCode).toBe(201) expect(res.json().uploads[0].mimeType).toBe('image/jpeg') }) test('rejects a file whose bytes are not an allowed type', async () => { const res = await upload([ { filename: 'a.png', type: 'image/png', content: '', }, ]) expect(res.statusCode).toBe(415) expect(res.json().error.code).toBe('MIME_TYPE_NOT_ALLOWED') expect(await prisma.upload.count()).toBe(0) }) test('rejects a recognised type the profile does not allow', async () => { const res = await upload([ { filename: 'a.pdf', type: 'image/png', content: '%PDF-1.7\n' }, ]) expect(res.statusCode).toBe(415) expect(res.json().error.message).toContain("'application/pdf'") }) test('says which type was sent and which the content is', async () => { // An iPhone photo whose major brand is `hevc`: a HEIC sequence const hevc = Buffer.concat([ Buffer.from([0, 0, 0, 0x18]), Buffer.from('ftyphevc\0\0\0\0mif1msf1'), ]) const res = await upload( [{ filename: 'photo.heic', type: 'image/heic', content: hevc }], tokenFor({ allowedMimeTypes: ['image/heic'] }), ) expect(res.statusCode).toBe(415) expect(res.json().error).toEqual({ code: 'MIME_TYPE_NOT_ALLOWED', message: "File 'photo.heic' was sent as 'image/heic', but its content is " + "'image/heic-sequence', which is not in the allowedMimeTypes of " + "upload profile 'avatar'. Add 'image/heic-sequence' to accept it.", }) }) test('explains a refused file that was not sent as another type', async () => { const message = async (part: Part, allowedMimeTypes?: string[]) => { const res = await upload( [part], tokenFor(allowedMimeTypes ? { allowedMimeTypes } : {}), ) expect(res.statusCode).toBe(415) return res.json().error.message } expect( await message({ filename: 'a.pdf', type: 'application/pdf', content: '%PDF-1.7\n', }), ).toBe( "File 'a.pdf' is 'application/pdf', which is not in the " + "allowedMimeTypes of upload profile 'avatar'. Add " + "'application/pdf' to accept it.", ) expect( await message({ filename: 'a.png', type: 'image/png', content: '', }), ).toBe( "File 'a.png' was sent as 'image/png', but its content isn't " + "'image/png' or any other type that could be recognised.", ) expect( await message({ filename: 'a.html', type: 'text/html', content: '

hi

', }), ).toBe( "File 'a.html' was sent as 'text/html', which upload profile " + "'avatar' only accepts when its allowedMimeTypes lists it by name.", ) expect( await message( { filename: 'a.csv', type: 'text/csv', content: 'a,b\n' }, ['image/*'], ), ).toBe( "File 'a.csv' was sent as 'text/csv', which is not in the " + "allowedMimeTypes of upload profile 'avatar'.", ) }) test('admits an unrecognisable type through a wildcard, except types that can carry script', async () => { // The default token allows `text/*` const text = await upload([ { filename: 'a.txt', type: 'text/plain', content: 'hi' }, ]) expect(text.statusCode).toBe(201) expect(text.json().uploads[0].mimeType).toBe('text/plain') const html = { filename: 'a.html', type: 'text/html', content: '

hi

', } expect((await upload([html])).statusCode).toBe(415) const listed = await upload( [html], tokenFor({ allowedMimeTypes: ['text/*', 'text/html'] }), ) expect(listed.statusCode).toBe(201) expect(listed.json().uploads[0].mimeType).toBe('text/html') }) }) }) describe('GET /upload/serve', () => { test('serves a completed file with safe headers', async () => { const upload = await storeFile(targets.local, { db, filename: 'räport "final".txt', mimeType: 'text/plain', data: Buffer.from('served'), }) const url = await targets.local.getSignedReadUrl(upload.storageKey!) const res = await app.inject({ method: 'GET', url }) expect(res.statusCode).toBe(200) expect(res.body).toBe('served') expect(res.headers['content-type']).toBe('text/plain') expect(res.headers['content-length']).toBe('6') expect(res.headers['x-content-type-options']).toBe('nosniff') expect(res.headers['cache-control']).toBe('private, max-age=3600') expect(res.headers['content-disposition']).toBe( `attachment; filename="r_port _final_.txt"; filename*=UTF-8''r%C3%A4port%20%22final%22.txt`, ) expect(res.headers.etag).toMatch(/^".+"$/) const cached = await app.inject({ method: 'GET', url, headers: { 'if-none-match': String(res.headers.etag) }, }) expect(cached.statusCode).toBe(304) }) test('honors inline disposition signed into the URL', async () => { const upload = await storeFile(targets.local, { db, filename: 'a.png', mimeType: 'image/png', data: Buffer.from('png'), }) const url = await targets.local.getSignedReadUrl(upload.storageKey!, { disposition: 'inline', }) const res = await app.inject({ method: 'GET', url }) expect(res.headers['content-disposition']).toMatch(/^inline;/) }) test('serves active content as an attachment even when inline was signed', async () => { const upload = await storeFile(targets.local, { db, filename: 'sneaky.svg', mimeType: 'image/svg+xml', data: Buffer.from(''), }) const url = await targets.local.getSignedReadUrl(upload.storageKey!, { disposition: 'inline', }) const res = await app.inject({ method: 'GET', url }) expect(res.statusCode).toBe(200) expect(res.headers['content-disposition']).toMatch(/^attachment;/) }) describe('with inlineTypes', () => { let inlineApp: FastifyInstance beforeEach(async () => { inlineApp = Fastify() await inlineApp.register(cedarUploadsPlugin, { tokenSecret: SECRET, targets, db, inlineTypes: ['Application/PDF'], }) await inlineApp.ready() }) afterEach(async () => { await inlineApp.close() }) const servePdf = async ( server: FastifyInstance, disposition: 'attachment' | 'inline', ) => { const upload = await storeFile(targets.local, { db, filename: 'report.pdf', mimeType: 'application/pdf', data: Buffer.from('%PDF'), }) const url = await targets.local.getSignedReadUrl(upload.storageKey!, { disposition, }) return server.inject({ method: 'GET', url }) } test('serves a listed active type inline when inline was signed', async () => { const res = await servePdf(inlineApp, 'inline') expect(res.statusCode).toBe(200) expect(res.headers['content-disposition']).toMatch(/^inline;/) }) test('keeps attachment when the signed URL asks for it', async () => { const res = await servePdf(inlineApp, 'attachment') expect(res.headers['content-disposition']).toMatch(/^attachment;/) }) test('still forces attachment for unlisted active types', async () => { const upload = await storeFile(targets.local, { db, filename: 'sneaky.svg', mimeType: 'image/svg+xml', data: Buffer.from(''), }) const url = await targets.local.getSignedReadUrl(upload.storageKey!, { disposition: 'inline', }) const res = await inlineApp.inject({ method: 'GET', url }) expect(res.headers['content-disposition']).toMatch(/^attachment;/) }) test('forces attachment for PDFs when they are not listed', async () => { const res = await servePdf(app, 'inline') expect(res.headers['content-disposition']).toMatch(/^attachment;/) }) }) test('answers 404 for forged tokens, missing rows, and missing files', async () => { const forged = await app.inject({ method: 'GET', url: '/upload/serve?token=not-a-token', }) expect(forged.statusCode).toBe(404) const noToken = await app.inject({ method: 'GET', url: '/upload/serve' }) expect(noToken.statusCode).toBe(404) const noRow = await app.inject({ method: 'GET', url: await targets.local.getSignedReadUrl('ghost.txt'), }) expect(noRow.statusCode).toBe(404) const upload = await storeFile(targets.local, { db, filename: 'a.txt', mimeType: 'text/plain', data: Buffer.from('x'), }) await fs.rm(path.join(uploadDir, upload.storageKey!)) const noFile = await app.inject({ method: 'GET', url: await targets.local.getSignedReadUrl(upload.storageKey!), }) expect(noFile.statusCode).toBe(404) }) test('does not serve pending rows', async () => { await prisma.upload.create({ data: { target: 'local', status: 'pending', filename: 'a.txt', mimeType: 'text/plain', size: 1n, storageKey: 'pending.txt', }, }) await fs.writeFile(path.join(uploadDir, 'pending.txt'), 'x') const res = await app.inject({ method: 'GET', url: await targets.local.getSignedReadUrl('pending.txt'), }) expect(res.statusCode).toBe(404) }) }) test('refuses to register without a token secret', async () => { const bare = Fastify() await expect( bare.register(cedarUploadsPlugin, { tokenSecret: '', targets, db }), ).rejects.toMatchObject({ code: 'CONFIGURATION' }) }) })