Form files: a dropped, stalled or endless upload is aborted and frees its slot, attempt key and buffers

Security review of 1.16.0 (blocking): a request body that errored mid-file never
resolved, leaking the concurrency slot and the attempt key. Also: idle and request
timeouts (submit.idleTimeoutMs / timeoutMs), ImmutableId preference on every Graph
draft call, the remaining total in the too-large message, files sent to a form
without a File field are refused and logged.

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 17:02:10 +01:00
co-authored by Claude Fable 5.2
parent cd74d7decb
commit e24c918d4b
13 changed files with 311 additions and 30 deletions
+27 -1
View File
@@ -8,14 +8,16 @@ import { fileFieldLimits, resolveFormAttachments } from '../src/modules/forms/at
import { canCarryFiles, emailAddresses, fixedRecipients, mayCarryFiles } from '../src/modules/forms/attachments/delivery.js'
import { buildFileBlock } from '../src/modules/forms/attachments/fileBlock.js'
import { checkFileContent, matchFileType, oleHasMacros, pdfHasActiveContent, zipEntries } from '../src/modules/forms/attachments/fileChecks.js'
import { limitInMB } from '../src/modules/forms/attachments/fileGate.js'
import { createFileGate, limitInMB } from '../src/modules/forms/attachments/fileGate.js'
import { describeFileTypes, FORM_FILE_TYPES, formatFileSize, RISKY_FILE_EXTENSIONS, safeFileName } from '../src/modules/forms/attachments/fileTypes.js'
import { BuilderForm } from '../src/modules/forms/BuilderForm.js'
import { fileFieldError, fileFieldRules, initialBuilderValues, validateBuilderFields } from '../src/modules/forms/builderLogic.js'
import { splitFormFiles } from '../src/modules/forms/createSubmitAction.js'
import { publicForm } from '../src/modules/forms/RenderForm.js'
import { submitForm } from '../src/modules/forms/submitForm.js'
import { NOTIFICATION_FALLBACK, NOTIFICATION_FALLBACK_PL } from '../src/modules/notifications/defaults.js'
import { resolveFormMessage } from '../src/modules/notifications/resolveFormMessage.js'
import { fakePayload } from './helpers.js'
vi.mock('server-only', () => ({}))
@@ -125,6 +127,22 @@ describe('limits', () => {
expect(limitInMB(2_999_999)).toBe(2.8) // rounded down — never promise more than can be sent
})
test('when earlier files used part of the submission total, the message names what is left', async () => {
const pdf = (size: number) => Buffer.concat([Buffer.from('%PDF-1.7\n'), Buffer.alloc(size - 9, 0x20)])
const gate = createFileGate({ fields: [{ name: 'files', allowedTypes: ['pdf'], maxFiles: 3, maxSizeMB: 10 }], options })
const first = gate.begin('files', 'a.pdf')
if ('failure' in first) {throw new Error('unexpected')}
expect(await gate.finish(first.token, pdf(6 * MB))).toBeNull()
const second = gate.begin('files', 'b.pdf')
if ('failure' in second) {throw new Error('unexpected')}
expect(second.slot.maxBytes).toBe(4 * MB)
expect(second.slot.tooLarge()).toMatchObject({ file: 'b.pdf', kind: 'file_too_large', max: 4 }) // not the per-file 10
expect(await gate.finish(second.token, pdf(4 * MB))).toBeNull()
const third = gate.begin('files', 'c.pdf')
if ('failure' in third) {throw new Error('unexpected')}
expect(third.slot.tooLarge()).toMatchObject({ max: 10 }) // nothing left: the submission's total
})
test('sizes read naturally', () => {
expect([formatFileSize(512), formatFileSize(64_000), formatFileSize(2.14 * MB), formatFileSize(10 * MB)]).toEqual(['512 B', '63 KB', '2.1 MB', '10 MB'])
})
@@ -272,4 +290,12 @@ describe('messages and helpers', () => {
expect(files?.single).toHaveLength(1)
expect(await splitFormFiles({ name: 'Jan', empty: [] })).toEqual({ fields: { name: 'Jan', empty: [] } })
})
test('submitForm({ files }) without forms.attachments: refused and logged, the submission is not stored', async () => {
const { create, payload } = fakePayload({ forms: { f: { fields: [{ name: 'name', blockType: 'text' }] } } })
const result = await submitForm({ data: { name: 'Jan' }, files: { cv: [{ name: 'cv.pdf', data: new Uint8Array([37, 80, 68, 70]) }] }, formId: 'f', maxPerMinute: 0, payload })
expect(result).toMatchObject({ kind: 'unknown_fields', reason: 'validation', success: false })
expect(payload.logger.warn).toHaveBeenCalledWith(expect.stringContaining('forms.attachments is off'))
expect(create).not.toHaveBeenCalled()
})
})