Files
vue-chat-layer/app/lib/urlGuard.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

47 lines
2.3 KiB
TypeScript
Raw 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.
// The chat's guard for http(s) link targets.
//
// The chat links and embeds only URLs whose host, as written, is the host
// the browser opens. The URL parser percent-decodes the host, maps
// fullwidth letters and ideographic dots to ASCII, drops userinfo and reads
// a backslash as a slash, so "https://bit%2Ely/x" or "https://bit.ly/x"
// opens bit.ly although the text shows something else. Such a URL stays
// text: no anchor, and no link card, so a card never shows a host the text
// does not. Honest URLs, IDN hosts written in Unicode, ports, queries and
// fragments pass.
/** The written scheme: http or https, followed by exactly two slashes. */
const SCHEME_RE = /^https?:\/\/(?![/\\])/i
/** Ideographic, fullwidth and halfwidth full stops (URL parsers read them as "."). */
const ODD_DOTS_RE = /[。.。]/
/** Characters the URL parser drops from a host (soft hyphen, zero-width space and the like). */
const INVISIBLE_RE = /[\p{Default_Ignorable_Code_Point}\p{Cf}]/u
// eslint-disable-next-line no-control-regex
const NON_ASCII_RE = /[^\u0000-\u007f]/
/**
* honestHttpUrl answers the parsed URL when `raw` is an http(s) URL whose
* written host is the host it opens, else null. The authority as written
* (up to the first "/", "?" or "#") holds no "%", "@" or "\"; the host holds
* no ideographic, fullwidth or halfwidth dot and no invisible character the
* parser drops, is unchanged by NFKC and does not end with a dot; and its lower-cased form is the parsed hostname, or,
* for a host with non-ASCII letters, it is in NFC.
*/
export function honestHttpUrl(raw: string): URL | null {
if (!SCHEME_RE.test(raw)) return null
let url: URL
try {
url = new URL(raw)
} catch {
return null
}
if (url.protocol !== 'http:' && url.protocol !== 'https:') return null
const rest = raw.slice(raw.indexOf('//') + 2)
const authority = /^[^/?#]*/.exec(rest)?.[0] ?? ''
if (/[%@\\]/.test(authority)) return null
const host = authority.startsWith('[') ? authority.slice(0, authority.indexOf(']') + 1) : authority.replace(/:\d*$/, '')
if (!host || ODD_DOTS_RE.test(host) || INVISIBLE_RE.test(host) || host.normalize('NFKC') !== host || host.endsWith('.')) return null
if (host.toLowerCase() === url.hostname) return url
if (NON_ASCII_RE.test(host) && host.normalize('NFC') === host) return url
return null
}