import { describe, expect, test } from 'vitest' import { validateSubmission } from '../src/modules/forms/validateSubmission.js' import { fakePayload } from './helpers.js' const forms = { f: { fields: [ { name: 'name', blockType: 'text' }, { name: 'email', blockType: 'email' }, { name: 'topic', blockType: 'select', options: [{ value: 'offer' }, { value: 'support' }] }, { name: 'count', blockType: 'number' }, { name: 'message', blockType: 'textarea' }, { name: 'agree', blockType: 'checkbox' }, ], }, } const { payload } = fakePayload({ forms }) const check = (data: Record) => validateSubmission(payload, 'f', data) describe('form validation — injection and abuse', () => { test.each([ ['a@x.pl, victim@y.pl'], ['a@x.pl;victim@y.pl'], ['a@x.pl\r\nBcc: victim@y.pl'], ['Name '], ['not-an-email'], [`${'a'.repeat(250)}@x.pl`], ])('email %j is rejected — no extra recipients or headers', async (email) => { expect(await check({ email })).toMatchObject({ field: 'email', kind: 'invalid_format', ok: false }) }) test('a valid email is trimmed and kept', async () => { expect(await check({ email: ' jan.kowalski+oferta@firma.com.pl ' })).toMatchObject({ cleaned: { email: 'jan.kowalski+oferta@firma.com.pl' }, ok: true, }) }) test('select accepts only values configured in the panel', async () => { expect(await check({ topic: 'offer' })).toMatchObject({ ok: true }) expect(await check({ topic: '' })).toMatchObject({ field: 'topic', kind: 'invalid_format' }) }) test('objects, arrays and wrong types are rejected', async () => { expect(await check({ name: { $gt: '' } })).toMatchObject({ field: 'name', kind: 'invalid_format' }) expect(await check({ message: ['a', 'b'] })).toMatchObject({ field: 'message', kind: 'invalid_format' }) expect(await check({ count: '12abc' })).toMatchObject({ field: 'count', kind: 'invalid_format' }) expect(await check({ agree: 'true' })).toMatchObject({ field: 'agree', kind: 'invalid_format' }) }) test('control and bidi characters are stripped; single-line text loses line breaks', async () => { const res = await check({ name: 'Jan\r\nKowalski\u0007', message: 'Dzień\u0000 dobry‮\nlinia 2' }) expect(res).toMatchObject({ cleaned: { name: 'Jan Kowalski', message: 'Dzień dobry\nlinia 2' }, ok: true }) }) test('HTML is stored as text (the renderer and e-mails escape it)', async () => { expect(await check({ message: '' })).toMatchObject({ cleaned: { message: '' }, ok: true, }) }) test('numbers are normalized', async () => { expect(await check({ count: ' 42 ' })).toMatchObject({ cleaned: { count: 42 }, ok: true }) }) })