- 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
93 lines
3.2 KiB
TypeScript
93 lines
3.2 KiB
TypeScript
// The chat's link guard: URLs whose written host is not the host the
|
||
// browser opens stay text, in renderChatText, linkify and messageUrls;
|
||
// honest URLs (IDN hosts in Unicode, ports, queries, fragments) still link.
|
||
import { describe, expect, it } from 'vitest'
|
||
import { renderChatText } from './chatText'
|
||
import { linkify, messageUrls } from './linkEmbeds'
|
||
import { escapeHtml, safeUrl } from './markdown'
|
||
import { honestHttpUrl } from './urlGuard'
|
||
|
||
const DECEPTIVE = [
|
||
'https://bit%2Ely/x',
|
||
'https://bit.l%79/x',
|
||
'https://bit.ly/x',
|
||
'https://bit。ly/x',
|
||
'https://bit.ly/x',
|
||
'https://bit。ly/x',
|
||
'https://user@bit.ly/x',
|
||
'https://example.com@bit.ly/',
|
||
'https:/\\bit.ly/x',
|
||
'https://\\bit.ly/x',
|
||
'https://bit.ly./x',
|
||
'https:///bit.ly/x',
|
||
'https:bit.ly/x',
|
||
'https://bit.ly/x',
|
||
'https://bit.ly/x',
|
||
'https://0x7f.1/x',
|
||
]
|
||
|
||
const HONEST = [
|
||
'https://bit.ly/x',
|
||
'http://example.com:8080/a?b=c#d',
|
||
'https://żółw.example/',
|
||
'https://xn--w-uga1v8h.example/',
|
||
'https://sub.example.org/path',
|
||
'https://Example.COM/Path',
|
||
'https://medium.example/@ada/post',
|
||
'https://a.example/?next=https://b.example/%2E',
|
||
]
|
||
|
||
describe('honestHttpUrl', () => {
|
||
it.each(DECEPTIVE)('refuses %s', (raw) => {
|
||
expect(honestHttpUrl(raw)).toBeNull()
|
||
})
|
||
|
||
it('refuses other schemes and what the parser cannot read', () => {
|
||
for (const raw of ['javascript:alert(1)', 'data:text/html,hi', 'mailto:a@b.example', 'ftp://a.example/', 'https://', 'https://exa mple.com/', '//bit.ly/x', '/x', '']) {
|
||
expect(honestHttpUrl(raw)).toBeNull()
|
||
}
|
||
})
|
||
|
||
it.each(HONEST)('accepts %s', (raw) => {
|
||
const url = honestHttpUrl(raw)
|
||
expect(url).not.toBeNull()
|
||
expect(url!.href).toBe(new URL(raw).href)
|
||
})
|
||
|
||
it('answers the parsed URL, IDN hosts in their ASCII form', () => {
|
||
expect(honestHttpUrl('https://żółw.example/a')!.hostname).toBe('xn--w-uga1v8h.example')
|
||
expect(honestHttpUrl('http://example.com:8080/a?b=c#d')!.port).toBe('8080')
|
||
})
|
||
})
|
||
|
||
describe('the chat renderer with the guard', () => {
|
||
it.each(DECEPTIVE)('%s stays text, bare and as a [label](url) link', (raw) => {
|
||
const bare = renderChatText(`look ${raw} now`)
|
||
expect(bare).not.toContain('<a')
|
||
expect(bare).toContain(escapeHtml(raw))
|
||
const labelled = renderChatText(`[click](${raw})`)
|
||
expect(labelled).not.toContain('<a')
|
||
expect(labelled).toContain('click')
|
||
expect(safeUrl(raw)).toBeNull()
|
||
})
|
||
|
||
it.each(DECEPTIVE)('%s yields no card and no linkify link', (raw) => {
|
||
expect(messageUrls(`look ${raw} now`)).toEqual([])
|
||
expect(messageUrls(`[click](${raw})`)).toEqual([])
|
||
expect(linkify(escapeHtml(`look ${raw} now`))).not.toContain('<a')
|
||
})
|
||
|
||
it.each(HONEST)('%s still links and yields a card', (raw) => {
|
||
expect(renderChatText(`look ${raw} now`)).toContain(`<a href="${escapeHtml(raw)}"`)
|
||
expect(renderChatText(`[click](${raw})`)).toContain(`<a href="${escapeHtml(raw)}"`)
|
||
expect(messageUrls(`look ${raw} now`).map((u) => u.href)).toEqual([new URL(raw).href])
|
||
})
|
||
|
||
it('keeps mailto, site paths and anchors', () => {
|
||
expect(safeUrl('mailto:ada@example.com')).toBe('mailto:ada@example.com')
|
||
expect(safeUrl('/chat')).toBe('/chat')
|
||
expect(safeUrl('#top')).toBe('#top')
|
||
expect(safeUrl('/\\bit.ly')).toBeNull()
|
||
})
|
||
})
|