Files
ipal-kit/test/formSecurity.test.ts
Radosław SmolińskiandClaude Opus 5.5 faa4ad679c Theme switcher, table of contents, missing-page redirect, form placeholders and time field; audit and revalidate fixes
- buildRevalidateHook skips outside Next (no NEXT_RUNTIME) and with context.disableRevalidate
- BuilderForm focuses only on a real step change (no scroll to the form on remount)
- mediaImageProps: placeholder colour only for JPEG (placeholderColor); hook stores no colour for transparent images
- buildLinkField: interface name per shape (CmsLink, CmsLinkPlain, CmsLinkNoLabel) + interfaceName option
- Cookie Settings: buttons.save -> buttons.savePreferences (Mongoose reserved name)
- ThemeScript, ThemeSwitcher, useTheme; Site Settings defaultTheme 'system' + themeToggleLabel; Turnstile follows page theme
- extractHeadings, RenderRichText headingIds, TableOfContents, buildTableOfContentsField
- createPageRoute onMissing: 'redirectHome'
- Form builder: localized placeholder on text/email/textarea/number, new time field
- ipal-audit: no false positive for object keys between JSX elements; /md route not reported as dynamic

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-19 21:32:40 +02:00

110 lines
5.1 KiB
TypeScript
Raw Permalink Blame History

This file contains invisible Unicode characters
This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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<string, unknown>) => validateSubmission(payload, 'f', data)
describe('form validation — injection and abuse', () => {
test.each([
['[email protected], [email protected]'],
['[email protected];[email protected]'],
['[email protected]\r\nBcc: [email protected]'],
['Name <[email protected]>'],
['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: ' [email protected] ' })).toMatchObject({
cleaned: { email: '[email protected]' },
ok: true,
})
})
test('select accepts only values configured in the panel', async () => {
expect(await check({ topic: 'offer' })).toMatchObject({ ok: true })
expect(await check({ topic: '<script>alert(1)</script>' })).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: '<img src=x onerror=alert(1)>' })).toMatchObject({
cleaned: { message: '<img src=x onerror=alert(1)>' },
ok: true,
})
})
test('numbers are normalized', async () => {
expect(await check({ count: ' 42 ' })).toMatchObject({ cleaned: { count: 42 }, ok: true })
})
})
describe('form validation — choice lists and dates', () => {
const { payload: p } = fakePayload({
forms: {
g: {
fields: [
{ name: 'services', blockType: 'checkboxGroup', maxSelected: 2, minSelected: 1, options: [{ value: 'www' }, { value: 'seo' }, { value: 'ads' }] },
{ name: 'size', blockType: 'radio', options: [{ value: 's' }, { value: 'l' }] },
{ name: 'deadline', blockType: 'date' },
{ name: 'hour', blockType: 'time' },
{ blockType: 'step' },
],
},
},
})
const v = (data: Record<string, unknown>) => validateSubmission(p, 'g', data)
test('checkbox group: only configured values, deduplicated, within min/max', async () => {
expect(await v({ services: ['www', 'seo', 'www'] })).toMatchObject({ cleaned: { services: ['www', 'seo'] }, ok: true })
expect(await v({ services: ['www', '<b>x</b>'] })).toMatchObject({ field: 'services', kind: 'invalid_format' })
expect(await v({ services: ['www', 'seo', 'ads'] })).toMatchObject({ field: 'services', kind: 'invalid_format' })
expect(await v({ services: 'www' })).toMatchObject({ field: 'services', kind: 'invalid_format' })
expect(await v({ services: [{ $ne: 1 }] })).toMatchObject({ field: 'services', kind: 'invalid_format' })
})
test('radio accepts only its options; date must be a real date', async () => {
expect(await v({ deadline: '2026-11-30', size: 'l' })).toMatchObject({ cleaned: { deadline: '2026-11-30', size: 'l' }, ok: true })
expect(await v({ size: 'xl' })).toMatchObject({ field: 'size', kind: 'invalid_format' })
expect(await v({ deadline: 'jutro' })).toMatchObject({ field: 'deadline', kind: 'invalid_format' })
expect(await v({ deadline: '2026-13-45' })).toMatchObject({ field: 'deadline', kind: 'invalid_format' })
})
test('time: HH:MM within a day', async () => {
expect(await v({ hour: ' 09:30 ' })).toMatchObject({ cleaned: { hour: '09:30' }, ok: true })
expect(await v({ hour: '24:00' })).toMatchObject({ field: 'hour', kind: 'invalid_format' })
expect(await v({ hour: 'rano' })).toMatchObject({ field: 'hour', kind: 'invalid_format' })
})
test('a Step block carries no value and is not an unknown field', async () => {
expect(await v({})).toMatchObject({ ok: true })
})
})