import fc from 'fast-check'; import { describe, expect, it } from 'vitest'; import { isSafeTarget } from '../target'; describe('isSafeTarget', () => { it.each([ '#pakketten', '#contact', '#a-b_c1', '/', '/over-mij', '/a?b=1&c=2', 'https://example.com', 'https://example.com/x?y=1#z', ])('accepts %s', (target) => { expect(isSafeTarget(target)).toBe(true); }); it.each([ '', '#', '# x', '#a b', '#a.b', '//evil.example/x', '/\\evil', '/a b', 'javascript:alert(1)', 'JAVASCRIPT:alert(1)', 'data:text/html,x', 'http://example.com', 'https://', 'https://user:pw@example.com', 'https://user@example.com', 'ftp://example.com', 'mailto:a@b.nl', 'pakketten', 'https://exa mple.com', '/a\nb', ])('refuses %j', (target) => { expect(isSafeTarget(target)).toBe(false); }); it('refuses anything that is not a string', () => { for (const value of [null, undefined, 1, {}, [], true]) { expect(isSafeTarget(value)).toBe(false); } }); it('property: whatever is accepted has one of the three allowed forms and no unsafe characters', () => { fc.assert( fc.property(fc.string(), (value) => { if (!isSafeTarget(value)) return true; const allowedForm = /^#[A-Za-z0-9_-]+$/.test(value) || (value.startsWith('/') && !value.startsWith('//')) || value.startsWith('https://'); // eslint-disable-next-line no-control-regex return allowedForm && !/[\u0000- \u007f\\]/.test(value); }), { numRuns: 500 }, ); }); });