Files
ipal-kit/test/richTextLinks.test.ts
T
Radosław SmolińskiandClaude Opus 5.5 1beb0edb7f RenderRichText neutralises unsafe link URLs (safeHref)
Payload's link converter rendered fields.url as is; javascript:/data: URLs
stored via import or API reached the href. They now become '#'.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
2026-10-22 17:09:44 +02:00

31 lines
1.5 KiB
TypeScript

import { describe, expect, test } from 'vitest'
import { sanitizeRichTextLinks } from '../src/modules/richtext/sanitizeLinks.js'
const link = (url: string, type = 'link', linkType = 'custom') => ({ type, children: [{ type: 'text', text: 'x' }], fields: { linkType, url } })
const doc = (...nodes: object[]) => ({ root: { type: 'root', children: [{ type: 'paragraph', children: nodes }] } })
const urls = (d: ReturnType<typeof doc>) =>
(d.root.children[0].children as { fields: { url: string } }[]).map((n) => n.fields.url)
describe('rich text links', () => {
test.each(['javascript:alert(1)', ' JaVaScRiPt:alert(1)', 'java\tscript:alert(1)', 'data:text/html,<script>alert(1)</script>', 'vbscript:x'])(
'%j is neutralised to #',
(bad) => {
expect(urls(sanitizeRichTextLinks(doc(link(bad), link(bad, 'autolink'))))).toEqual(['#', '#'])
},
)
test('safe links are kept and an untouched document is returned as is', () => {
const d = doc(link('https://acme.pl'), link('/pl/kontakt'), link('mailto:[email protected]'), link('tel:+48123'), link('#faq'))
expect(sanitizeRichTextLinks(d)).toBe(d)
})
test('only the path to an unsafe link is copied; the input is not mutated', () => {
const d = doc(link('https://acme.pl'), link('javascript:alert(1)'))
const out = sanitizeRichTextLinks(d)
expect(urls(d)).toEqual(['https://acme.pl', 'javascript:alert(1)'])
expect(urls(out)).toEqual(['https://acme.pl', '#'])
expect(out.root.children[0].children[0]).toBe(d.root.children[0].children[0])
})
})