Form files: the visitor is told when the problem is the file — why and what to do; UTF-8 file names

Co-Authored-By: Claude Fable 5.2 <[email protected]>
This commit is contained in:
Radosław Smoliński
2026-11-13 16:51:05 +01:00
co-authored by Claude Fable 5.2
parent b6f9f34705
commit 97202ee03e
18 changed files with 335 additions and 128 deletions
+92 -33
View File
@@ -294,7 +294,7 @@ describe('2 · each allowed format: one submission, the file in the staff e-mail
const attachment = staff[0].attachments![0]
expect(Buffer.compare(attachment.content, data)).toBe(0)
expect(attachment.contentType).toBe(mime)
expect(attachment.filename.endsWith(`.${type}`)).toBe(true)
expect(attachment.filename).toBe(name.replace(/\.[^.]+$/, `.${type}`)) // the visitor's name (also with Polish letters), the detected extension
expect(staff[0].html).toContain(attachment.filename) // {{attachment}} → the readable name
expect(staff[0].html).toContain('not checked for viruses')
const customer = mails.filter((m) => m.to === '[email protected]')
@@ -313,45 +313,62 @@ describe('2 · each allowed format: one submission, the file in the staff e-mail
})
describe('3 · refused files leave no submission and send no e-mail', () => {
const refused = async (files: Upload[], expected: RegExp, extra: Post = {}) => {
// Every refusal is answered AT the File field, says it is the file, why, and what to do next.
const TYPES = 'PDF, DOC, DOCX'
const wrongFormat = (name: string) => `The file "${name}" is in a format we do not accept. Choose one of: ${TYPES}. You can also send the form without the file.`
const notAcceptable = (name: string) =>
`The file "${name}" was refused: it is a program or an archive, it holds macros, or its content does not match its type. Choose another file (${TYPES}). You can also send the form without the file.`
const refused = async (files: Upload[], expected: string, extra: Post = {}) => {
const before = await count()
const { body, status } = await post({ files, ...extra })
expect(body.success).toBe(false)
expect(status).toBe(422)
expect(body.message).toMatch(expected)
expect(body.message).toBe(expected)
expect(body).toMatchObject({ field: 'attachment', reason: 'validation' })
expect(await count()).toBe(before)
expect(mails).toEqual([])
}
test('oversized: stopped while reading', async () => {
await refused([{ name: 'big.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 200_000) }], /"big\.pdf" is too large\. The limit is 1 MB\./)
await refused(
[{ name: 'big.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 200_000) }],
'The file "big.pdf" is too large. Choose a file of up to 1 MB. You can also send the form without the file.',
)
expect(scanned).toEqual([])
})
test('mismatched: a program named .pdf, a PDF named .docx, an empty file', async () => {
await refused([{ name: 'cv.pdf', data: EXE }], /"cv\.pdf" is not an accepted file type\. Accepted: PDF, DOC, DOCX\./)
await refused([{ name: 'cv.docx', data: PDF }], /not an accepted file type/)
await refused([{ name: 'cv.pdf', data: Buffer.alloc(0) }], /not an accepted file type|too large/)
await refused([{ name: 'cv.pdf', data: EXE }], notAcceptable('cv.pdf'))
await refused([{ name: 'cv.docx', data: PDF }], notAcceptable('cv.docx'))
await refused([{ name: 'cv.pdf', data: Buffer.alloc(0) }], notAcceptable('cv.pdf'))
})
test('unsupported and dangerous names: other type, program, double extension, archive, macro-enabled Office', async () => {
await refused([{ name: 'photo.png', data: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }], /not an accepted file type/)
await refused([{ name: 'cv.exe', data: EXE }], /not an accepted file type/)
await refused([{ name: 'cv.pdf.exe', data: EXE }], /not an accepted file type/)
await refused([{ name: 'cv.exe.pdf', data: PDF }], /not an accepted file type/)
await refused([{ name: 'cv.pdf.docx', data: DOCX }], /not an accepted file type/)
await refused([{ name: 'cv.zip', data: ARCHIVE }], /not an accepted file type/)
await refused([{ name: 'cv.docm', data: DOCX_MACRO }], /not an accepted file type/)
test('another format: the message names the formats the field takes', async () => {
await refused([{ name: 'photo.png', data: Buffer.from([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]) }], wrongFormat('photo.png'))
await refused([{ name: 'notes.txt', data: Buffer.from('hello') }], wrongFormat('notes.txt'))
await refused([{ name: 'cv', data: PDF }], wrongFormat('cv'))
})
test('refused files: program, double extension, archive, macro-enabled Office — the message says the file was refused', async () => {
await refused([{ name: 'cv.exe', data: EXE }], notAcceptable('cv.exe'))
await refused([{ name: 'cv.pdf.exe', data: EXE }], notAcceptable('cv.pdf.exe'))
await refused([{ name: 'cv.exe.pdf', data: PDF }], notAcceptable('cv.exe.pdf'))
await refused([{ name: 'cv.pdf.docx', data: DOCX }], notAcceptable('cv.pdf.docx'))
await refused([{ name: 'cv.zip', data: ARCHIVE }], notAcceptable('cv.zip'))
await refused([{ name: 'cv.docm', data: DOCX_MACRO }], notAcceptable('cv.docm'))
})
test('the right container with macros inside: DOCX with a macro project, DOC with a macro storage', async () => {
await refused([{ name: 'cv.docx', data: DOCX_MACRO }], /not an accepted file type/)
await refused([{ name: 'cv.doc', data: DOC_MACRO }], /not an accepted file type/)
await refused([{ name: 'cv.docx', data: ARCHIVE }], /not an accepted file type/) // a ZIP that is not a Word file
await refused([{ name: 'cv.docx', data: DOCX_MACRO }], notAcceptable('cv.docx'))
await refused([{ name: 'cv.doc', data: DOC_MACRO }], notAcceptable('cv.doc'))
await refused([{ name: 'cv.docx', data: ARCHIVE }], notAcceptable('cv.docx')) // a ZIP that is not a Word file
})
test('extra files: more than the field allows, a file under a key that is no File field', async () => {
await refused([{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }], /Attach at most 1 files in "Attachment \(optional\)"\./)
await refused(
[{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }],
'Too many files in "Attachment (optional)". Remove some: the limit is 1. You can also send the form without the file.',
)
const before = await count()
const { body } = await post({ files: [{ name: 'a.pdf', data: PDF, field: 'name' }], values: { consent: true, email: '[email protected]' } })
expect(body.success).toBe(false)
@@ -370,7 +387,7 @@ describe('3 · refused files leave no submission and send no e-mail', () => {
test('the antivirus hook can refuse a file that passed everything else', async () => {
scanVerdict = false
await refused([{ name: 'cv.pdf', data: PDF }], /not an accepted file type/)
await refused([{ name: 'cv.pdf', data: PDF }], notAcceptable('cv.pdf'))
expect(scanned).toHaveLength(1)
})
@@ -501,7 +518,14 @@ describe('6 · failed send, retry, repeated request', () => {
const before = await count()
const { body, status } = await post({ files: [{ name: 'cv.pdf', data: PDF }] })
expect(status).toBe(502)
expect(body).toMatchObject({ message: 'We could not send your file. Try again, or send it by e-mail.', reason: 'delivery', success: false })
// The visitor is told it is the FILE, at the File field, and what to do — never the mail server's words.
expect(body).toEqual({
field: 'attachment',
message: 'We could not send the file "cv.pdf". Choose a smaller file or another format and try again. You can also send the form without the file.',
reason: 'delivery',
success: false,
})
expect(JSON.stringify(body)).not.toMatch(/552|size exceeds|smtp|ECONN/i)
const stored = (await submissions()).at(-1)!
expect(await count()).toBe(before + 1)
@@ -527,7 +551,8 @@ describe('6 · failed send, retry, repeated request', () => {
test('an adapter that throws is a failed send too', async () => {
mailMode = 'throw'
const { body } = await post({ files: [{ name: 'cv.pdf', data: PDF }] })
expect(body.reason).toBe('delivery')
expect(body).toMatchObject({ field: 'attachment', reason: 'delivery' })
expect(body.message).not.toMatch(/ECONNREFUSED/)
expect((await submissions()).at(-1)!.filesDelivery).toBe('failed')
})
@@ -564,7 +589,8 @@ describe('6 · failed send, retry, repeated request', () => {
mails.length = 0
mailMode = 'ok'
const again = await post({ attempt, files: [{ name: 'cv.pdf', data: PDF }] })
expect(again.body.reason).toBe('delivery')
expect(again.body).toMatchObject({ field: 'attachment', reason: 'delivery' })
expect(again.body.message).toContain('We could not send the file "cv.pdf".')
expect(await count()).toBe(before)
expect(mails).toEqual([])
})
@@ -572,16 +598,35 @@ describe('6 · failed send, retry, repeated request', () => {
describe('7 · languages, the panel, limits', () => {
test('messages follow the page language: Polish and English defaults, German and Dutch from the panel', async () => {
await payload.updateGlobal({ slug: 'notifications' as never, data: { form: { fileTooLarge: 'Die Datei „{file}“ ist zu groß. Maximal {max} MB.' } } as never, locale: 'de' })
await payload.updateGlobal({ slug: 'notifications' as never, data: { form: { fileTooLarge: 'Het bestand "{file}" is te groot. Maximaal {max} MB.' } } as never, locale: 'nl' })
await payload.updateGlobal({
slug: 'notifications' as never,
data: { form: { fileOptional: 'Sie können das Formular auch ohne Datei senden.', fileTooLarge: 'Die Datei „{file}“ ist zu groß. Wählen Sie eine Datei bis {max} MB.' } } as never,
locale: 'de',
})
await payload.updateGlobal({ slug: 'notifications' as never, data: { form: { fileOptional: 'U kunt het formulier ook zonder bestand versturen.', fileTooLarge: 'Het bestand "{file}" is te groot. Kies een bestand tot {max} MB.' } } as never, locale: 'nl' })
const big = [{ name: 'big.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 10) }]
expect((await post({ files: big, locale: 'pl' })).body.message).toBe('Plik „big.pdf” jest za duży. Limit to 1 MB.')
expect((await post({ files: big, locale: 'en' })).body.message).toBe('"big.pdf" is too large. The limit is 1 MB.')
expect((await post({ files: big, locale: 'de' })).body.message).toBe('Die Datei „big.pdf“ ist zu groß. Maximal 1 MB.')
expect((await post({ files: big, locale: 'nl' })).body.message).toBe('Het bestand "big.pdf" is te groot. Maximaal 1 MB.')
// The field's label comes from the panel in the page language.
const two = [{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }]
expect((await post({ files: two, locale: 'pl' })).body.message).toBe('W polu „Załącznik (opcjonalnie)” można dodać najwyżej 1 plików.')
expect((await post({ files: big, locale: 'pl' })).body.message).toBe('Plik „big.pdf” jest za duży. Wybierz plik do 1 MB. Możesz też wysłać formularz bez pliku.')
expect((await post({ files: big, locale: 'en' })).body.message).toBe('The file "big.pdf" is too large. Choose a file of up to 1 MB. You can also send the form without the file.')
expect((await post({ files: big, locale: 'de' })).body.message).toBe('Die Datei „big.pdf“ ist zu groß. Wählen Sie eine Datei bis 1 MB. Sie können das Formular auch ohne Datei senden.')
expect((await post({ files: big, locale: 'nl' })).body.message).toBe('Het bestand "big.pdf" is te groot. Kies een bestand tot 1 MB. U kunt het formulier ook zonder bestand versturen.')
// Every file message in Polish too; the field's label comes from the panel in the page language.
const pl = async (files: Upload[]) => (await post({ files, locale: 'pl' })).body.message
expect(await pl([{ name: 'a.pdf', data: PDF }, { name: 'b.pdf', data: PDF }])).toBe('Za dużo plików w polu „Załącznik (opcjonalnie)”. Usuń część: limit to 1. Możesz też wysłać formularz bez pliku.')
expect(await pl([{ name: 'zdjęcie.png', data: Buffer.alloc(32, 1) }])).toBe('Plik „zdjęcie.png” ma format, którego nie przyjmujemy. Wybierz jeden z: PDF, DOC, DOCX. Możesz też wysłać formularz bez pliku.')
expect(await pl([{ name: 'cv.pdf', data: EXE }])).toBe(
'Plik „cv.pdf” został odrzucony: to program lub archiwum, zawiera makra albo jego zawartość nie odpowiada typowi. Wybierz inny plik (PDF, DOC, DOCX). Możesz też wysłać formularz bez pliku.',
)
mailMode = 'refuse'
expect(await pl([{ name: 'cv.pdf', data: PDF }])).toBe('Nie udało się wysłać pliku „cv.pdf”. Wybierz mniejszy plik lub inny format i spróbuj ponownie. Możesz też wysłać formularz bez pliku.')
mailMode = 'ok'
// A failure that is not about the file keeps its own message — no file wording.
const consent = (await post({ files: [{ name: 'cv.pdf', data: PDF }], locale: 'en', values: { ...answers, consent: false } })).body
expect(consent).toMatchObject({ field: 'consent', reason: 'consent' })
expect(consent.message).not.toMatch(/file/i)
const missing = (await post({ files: [{ name: 'cv.pdf', data: PDF }], locale: 'en', values: { consent: true, email: '[email protected]' } })).body
expect(missing).toMatchObject({ field: 'name', message: 'Check the field "Name".' })
})
test('the form the browser gets carries the limits that really apply, and where to post', async () => {
@@ -632,6 +677,13 @@ describe('7 · languages, the panel, limits', () => {
expect(none.body).toMatchObject({ field: 'cv', reason: 'validation', success: false })
expect(await submitForm({ data: {}, formId: String(required.id), maxPerMinute: 0, payload })).toMatchObject({ field: 'cv', kind: 'required' })
expect(await count()).toBe(before)
// A required field: the same messages, without "send the form without the file".
const big = await post({ files: [{ name: 'cv.pdf', data: pad(Buffer.from('%PDF-1.7\n'), MB + 10), field: 'cv' }], form: String(required.id), values: {} })
expect(big.body).toMatchObject({ field: 'cv', message: 'The file "cv.pdf" is too large. Choose a file of up to 1 MB.' })
mailMode = 'refuse'
const unsent = await post({ files: [{ name: 'cv.pdf', data: PDF, field: 'cv' }], form: String(required.id), values: {} })
expect(unsent.body).toMatchObject({ field: 'cv', message: 'We could not send the file "cv.pdf". Choose a smaller file or another format and try again.' })
mailMode = 'ok'
const ok = await post({ files: [{ name: 'cv.pdf', data: PDF, field: 'cv' }], form: String(required.id), values: {} })
expect(ok.body.success).toBe(true)
})
@@ -640,7 +692,14 @@ describe('7 · languages, the panel, limits', () => {
const good = await submitForm({ data: answers, files: { attachment: [{ name: 'cv.pdf', data: PDF }] }, formId, locale: 'en', maxPerMinute: 0, payload })
expect(good.success).toBe(true)
expect(withFile()).toHaveLength(1)
expect(await submitForm({ data: answers, files: { attachment: [{ name: 'cv.pdf', data: EXE }] }, formId, maxPerMinute: 0, payload })).toMatchObject({ kind: 'file_type' })
expect(await submitForm({ data: answers, files: { attachment: [{ name: 'cv.pdf', data: EXE }] }, formId, maxPerMinute: 0, payload })).toMatchObject({
field: 'attachment',
file: 'cv.pdf',
kind: 'file_refused',
optional: true,
types: 'PDF, DOC, DOCX',
})
expect(await submitForm({ data: answers, files: { attachment: [{ name: 'cv.png', data: EXE }] }, formId, maxPerMinute: 0, payload })).toMatchObject({ kind: 'file_type' })
})
})