Files
vue-chat-layer/app/lib/linkEmbeds.test.ts
Jakub Zych a798bb1acb fix: deceptive link hosts render as text, not links or cards
- urlGuard: honestHttpUrl answers an http(s) URL only when its written
  host is the host the browser opens: no percent-escape, userinfo or
  backslash in the authority, no ideographic, fullwidth or halfwidth dot,
  fullwidth letter, invisible character or trailing dot in the host, and
  exactly two slashes after the scheme; IDN hosts in Unicode pass
- safeUrl allows an http(s) target only through honestHttpUrl, so
  renderChatText leaves a deceptive URL as text, bare or labelled
- linkify and messageUrls parse through it, so no card shows a host the
  text does not
- tests: urlGuard.test.ts and linkEmbeds cases; the SPA's copied tests
  are unchanged
2026-11-02 18:35:52 +01:00

119 lines
5.3 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { afterEach, describe, expect, it } from 'vitest'
import { embedRendererFor, linkify, messageUrls, registerEmbedRenderer } from './linkEmbeds'
import { escapeHtml } from './markdown'
const EXTERNAL = 'target="_blank" rel="noopener noreferrer nofollow"'
describe('linkify', () => {
it('links a bare https URL with target and rel set', () => {
expect(linkify('see https://summercms.io/docs now')).toBe(
`see <a href="https://summercms.io/docs" ${EXTERNAL}>https://summercms.io/docs</a> now`,
)
})
it('links http as well, and only http(s)', () => {
expect(linkify('http://a.example')).toContain('<a href="http://a.example"')
for (const s of ['javascript:alert(1)', 'ftp://a.example/x', 'mailto:a@b.example', 'data:text/html,hi', '//a.example/x']) {
expect(linkify(escapeHtml(s))).toBe(escapeHtml(s))
}
})
it('leaves deceptive hosts as text and links honest ones', () => {
for (const s of ['https://example.com@bit.ly/', 'https://bit。ly/x']) {
expect(linkify(escapeHtml(s))).toBe(escapeHtml(s))
}
expect(linkify('https://ok.example:8443/a?b=c#d')).toContain('<a href="https://ok.example:8443/a?b=c#d"')
expect(linkify('https://żółw.example/')).toContain('<a href="https://żółw.example/"')
})
it('keeps trailing punctuation outside the link', () => {
expect(linkify('go to https://a.example/x.')).toBe(`go to <a href="https://a.example/x" ${EXTERNAL}>https://a.example/x</a>.`)
expect(linkify('(https://a.example/x)')).toBe(`(<a href="https://a.example/x" ${EXTERNAL}>https://a.example/x</a>)`)
expect(linkify('https://en.wikipedia.org/wiki/Go_(game)')).toContain('href="https://en.wikipedia.org/wiki/Go_(game)"')
expect(linkify('https://a.example/?q=1, then')).toContain('href="https://a.example/?q=1"')
})
it('works on escaped text and adds no markup of the input', () => {
const input = '<script>alert(1)</script> https://a.example/?a=1&b=<x>'
const out = linkify(escapeHtml(input))
expect(out).not.toContain('<script')
expect(out).toContain('&lt;script&gt;')
// The URL's & and <> stay escaped once in the href and the text.
expect(out).toContain('href="https://a.example/?a=1&amp;b=&lt;x&gt;"')
expect(out).not.toMatch(/<(?!a |\/a>)/)
})
it('cannot break out of the href attribute', () => {
const out = linkify(escapeHtml('https://a.example/"onmouseover="alert(1)'))
expect(out).not.toContain('" onmouseover')
expect(out).not.toMatch(/href="[^"]*"[^>]*onmouseover/)
})
it('leaves text without URLs alone', () => {
expect(linkify('no links here')).toBe('no links here')
expect(linkify('')).toBe('')
})
})
describe('messageUrls', () => {
it('lists distinct http(s) URLs in order', () => {
const urls = messageUrls('a https://x.example/1 b http://y.example c https://x.example/1 d https://z.example/2.')
expect(urls.map((u) => u.href)).toEqual(['https://x.example/1', 'http://y.example/', 'https://z.example/2'])
})
it('skips code spans, other schemes and empty text', () => {
expect(messageUrls('`https://code.example` and ftp://f.example and https://ok.example').map((u) => u.host)).toEqual(['ok.example'])
expect(messageUrls('')).toEqual([])
expect(messageUrls(null)).toEqual([])
expect(messageUrls(undefined)).toEqual([])
})
it('counts the URL of a [label](url) link and ignores a URL glued to a word', () => {
expect(messageUrls('[docs](https://docs.example/a)').map((u) => u.href)).toEqual(['https://docs.example/a'])
expect(messageUrls('xhttps://a.example user@https://b.example')).toEqual([])
})
it('skips URLs whose written host is not the host they open', () => {
const text = 'a https://bit%2Ely/x b https://ok.example/1 c https://bit.ly/x d https://żółw.example/'
expect(messageUrls(text).map((u) => u.host)).toEqual(['ok.example', 'xn--w-uga1v8h.example'])
})
it('gives the cards limits.max_embeds takes the first of', () => {
const urls = messageUrls('https://1.example https://2.example https://3.example https://4.example')
expect(urls.slice(0, 3).map((u) => u.host)).toEqual(['1.example', '2.example', '3.example'])
})
})
describe('registerEmbedRenderer', () => {
const cleanups: Array<() => void> = []
afterEach(() => {
while (cleanups.length) cleanups.pop()?.()
})
it('returns null without a matching renderer (the generic card)', () => {
expect(embedRendererFor(new URL('https://a.example/'))).toBeNull()
})
it('picks the first registered renderer that matches', () => {
const blog = { name: 'BlogCard' }
const any = { name: 'AnyCard' }
cleanups.push(registerEmbedRenderer((u) => u.pathname.startsWith('/blog/'), blog))
cleanups.push(registerEmbedRenderer(() => true, any))
expect(embedRendererFor(new URL('https://a.example/blog/post'))).toBe(blog)
expect(embedRendererFor(new URL('https://a.example/other'))).toBe(any)
})
it('removes a registration with the returned function, and a throwing matcher matches nothing', () => {
const card = { name: 'Card' }
const off = registerEmbedRenderer(() => {
throw new Error('broken matcher')
}, { name: 'Broken' })
cleanups.push(off)
const remove = registerEmbedRenderer((u) => u.host === 'a.example', card)
expect(embedRendererFor(new URL('https://a.example/'))).toBe(card)
remove()
remove()
expect(embedRendererFor(new URL('https://a.example/'))).toBeNull()
})
})