import { describe, it, expect } from '@jest/globals'; import { asUuidOrNull, normalizeEmail, toPlainMultiline, toPlainText } from '../../sanitizeContactText.js'; describe('toPlainText', () => { it('removes tags and script bodies', () => { expect(toPlainText('Jane', 120)).toBe('Jane'); expect(toPlainText('Jane', 120)).toBe('Jane'); }); it('decodes entities so the value is not double-escaped downstream', () => { expect(toPlainText('Tom & Jerry', 120)).toBe('Tom & Jerry'); }); it('does not leave a tag behind after entity decoding', () => { // A tag smuggled as entities must not reassemble into markup. expect(toPlainText('<script>x</script>', 200)).not.toMatch( /]/i ); }); // CR/LF in a single-line value reaches the notification email's subject — // header injection if it survives. it('collapses CR and LF so they cannot reach an email header', () => { const out = toPlainText('Jane\r\nBcc: attacker@evil.test', 200); expect(out).not.toContain('\n'); expect(out).not.toContain('\r'); expect(out).toBe('Jane Bcc: attacker@evil.test'); }); it('strips NUL, which PostgreSQL cannot store in a text column', () => { expect(toPlainText('Jane', 120)).toBe('Jane'); }); it('strips bidi override characters (Trojan Source display spoofing)', () => { expect(toPlainText('Jane‮enaj', 120)).toBe('Janeenaj'); expect(toPlainText('⁦a⁩b', 120)).toBe('ab'); }); it('strips zero-width characters used to evade keyword filters', () => { expect(toPlainText('sp​am', 120)).toBe('spam'); expect(toPlainText('hello', 120)).toBe('hello'); }); it('caps length', () => { expect(toPlainText('a'.repeat(500), 120)).toHaveLength(120); }); it('returns an empty string for null and undefined', () => { expect(toPlainText(null, 120)).toBe(''); expect(toPlainText(undefined, 120)).toBe(''); }); }); describe('toPlainMultiline', () => { it('keeps line breaks', () => { expect(toPlainMultiline('one\ntwo', 5000)).toBe('one\ntwo'); }); it('normalizes CRLF to LF', () => { expect(toPlainMultiline('one\r\ntwo', 5000)).toBe('one\ntwo'); }); it('caps runs of blank lines so content cannot be pushed out of view', () => { expect(toPlainMultiline('one\n\n\n\n\n\ntwo', 5000)).toBe('one\n\ntwo'); }); it('still strips tags and control characters', () => { expect(toPlainMultiline('Hi', 5000)).toBe('Hi'); }); }); describe('normalizeEmail', () => { it('lowercases and trims so the customer-account join is a plain match', () => { expect(normalizeEmail(' Jane@Example.COM ')).toBe('jane@example.com'); }); it('removes all whitespace so CR/LF cannot reach the Reply-To header', () => { const out = normalizeEmail('jane@example.com\r\nBcc: x@y.z'); expect(out).not.toMatch(/\s/); expect(out).toBe('jane@example.combcc:x@y.z'); // And the collapsed result is no longer a valid address, so validation // rejects it rather than letting it through as something sendable. expect(out).not.toMatch(/^[^@\s]+@[^@\s]+\.[^@\s]+$/); }); it('caps length', () => { expect(normalizeEmail(`${'a'.repeat(300)}@x.com`)).toHaveLength(255); }); }); describe('asUuidOrNull', () => { // Without this guard a malformed value reaches a UUID column comparison and // PostgreSQL 500s a public, unauthenticated endpoint. it('rejects anything that is not a well-formed UUID', () => { expect(asUuidOrNull('x')).toBeNull(); expect(asUuidOrNull("' OR 1=1--")).toBeNull(); expect(asUuidOrNull('')).toBeNull(); expect(asUuidOrNull(null)).toBeNull(); expect(asUuidOrNull(undefined)).toBeNull(); expect(asUuidOrNull(123)).toBeNull(); expect(asUuidOrNull('123e4567-e89b-12d3-a456')).toBeNull(); }); it('accepts a well-formed UUID in either case', () => { expect(asUuidOrNull('123e4567-e89b-12d3-a456-426614174000')).toBe( '123e4567-e89b-12d3-a456-426614174000' ); expect(asUuidOrNull('123E4567-E89B-12D3-A456-426614174000')).toBe( '123E4567-E89B-12D3-A456-426614174000' ); }); });