import { describe, expect, it } from 'vitest'; import { bodyLimitFor, DEFAULT_BODY_BYTES, IMPORT_BODY_BYTES, PROSE_BODY_BYTES } from '../../../scripts/request-limits.mjs'; import { MAX_IMPORT_BYTES } from '#lib/import-limits.ts'; import { MAX_SAVE_CHARS } from '#lib/body-limits.ts'; import { MAX_IMAGE_BYTES } from '#lib/server/uploads.ts'; /** * The request ceiling is deny-by-default, because the adapter sets one limit for * every route and applies it while the body is read. A route therefore carries a * larger allowance only when it is named, and an unnamed path must fall back to * the small one: a mistake here gives a route less room than it needs, never a * public route a hundred megabytes. * * The limits a route enforces for itself are the trap. Raising `MAX_IMPORT_BYTES` * or `MAX_IMAGE_BYTES` without moving the matching allowance turns a valid * upload into a 413 from outside the app, where no route can explain it. The * last three tests hold the pairs together. */ describe('bodyLimitFor', () => { it('names the three allowances', () => { expect(DEFAULT_BODY_BYTES).toBe(2 * 1024 * 1024); expect(PROSE_BODY_BYTES).toBe(12 * 1024 * 1024); expect(IMPORT_BODY_BYTES).toBe(101 * 1024 * 1024); }); it('gives an import and its preview room for the file limit', () => { expect(bodyLimitFor('/api/import')).toBe(IMPORT_BODY_BYTES); expect(bodyLimitFor('/api/import/preview')).toBe(IMPORT_BODY_BYTES); }); it('gives a route that carries a document the prose allowance', () => { const paths = [ '/api/scenes/scene-1', '/api/scenes/scene-1/split', '/api/notes/note-1', '/api/notes/note-1/image', '/api/note-types/type-1', '/s/share-1', '/s/share-1/scenes/scene-1' ]; for (const path of paths) expect(bodyLimitFor(path), path).toBe(PROSE_BODY_BYTES); }); it('falls back to the small allowance for everything else', () => { const paths = [ '/sign-in', '/signup', '/forgot-password', '/reset-password', '/admin', '/admin/accounts', '/account', '/library', '/p/project-1', '/p/project-1/grid', '/api/health', '/api/search', '/api/auth/passkey/verify-authentication', '/api/auth/passkey/verify-registration' ]; for (const path of paths) expect(bodyLimitFor(path), path).toBe(DEFAULT_BODY_BYTES); }); it('refuses a path that only almost names a rule', () => { // A rule is one path and everything below it, never a prefix of a word. expect(bodyLimitFor('/api/import-draft')).toBe(DEFAULT_BODY_BYTES); expect(bodyLimitFor('/api/notes-book')).toBe(DEFAULT_BODY_BYTES); expect(bodyLimitFor('/api/scenester')).toBe(DEFAULT_BODY_BYTES); expect(bodyLimitFor('/ss/share-1')).toBe(DEFAULT_BODY_BYTES); }); it('reads the path and ignores the query string', () => { expect(bodyLimitFor('/api/import?title=x')).toBe(IMPORT_BODY_BYTES); expect(bodyLimitFor('/api/scenes/scene-1?before=2')).toBe(PROSE_BODY_BYTES); expect(bodyLimitFor('/sign-in?redirectTo=/library')).toBe(DEFAULT_BODY_BYTES); }); it('treats a missing target as an unnamed path', () => { expect(bodyLimitFor(undefined)).toBe(DEFAULT_BODY_BYTES); expect(bodyLimitFor(null)).toBe(DEFAULT_BODY_BYTES); expect(bodyLimitFor('')).toBe(DEFAULT_BODY_BYTES); }); }); describe('the allowances above the app’s own file limits', () => { it('leaves room for an import to reach its own check', () => { expect(IMPORT_BODY_BYTES).toBeGreaterThan(MAX_IMPORT_BYTES); }); it('leaves room for a note image to reach its own check', () => { expect(bodyLimitFor('/api/notes/note-1/image')).toBeGreaterThan(MAX_IMAGE_BYTES); }); it('leaves room for a save to reach its own check', () => { // A UTF-16 unit takes at most four bytes of UTF-8. expect(PROSE_BODY_BYTES).toBeGreaterThan(MAX_SAVE_CHARS * 4); expect(DEFAULT_BODY_BYTES).toBeLessThan(PROSE_BODY_BYTES); }); });