// safeurl.mjs — markup.SafeURL: an author link Mapa makes clickable. // http, https or mailto, with a host for the web ones, and nothing // net/url.Parse (Go 1.26) would refuse. This ports the parts of Parse // that decide those answers, in Parse's order; a bracketed IP literal is // refused outright, on both sides (spec §6.4, "Parity hazards"). // hostAllowed is the ASCII net/url lets into a host unescaped // (shouldEscape(c, encodeHost) is false): letters, digits, -_.~ and // !$&'()*+,;=:[]<>". const hostAllowed = /^[A-Za-z0-9\-_.~!$&'()*+,;=:[\]<>"]$/u const userinfoAllowed = /^[A-Za-z0-9\-._:~!$&'()*+,;=%@]$/u const hexDigit = /^[0-9A-Fa-f]$/u // MAX_ADDRESS and linkAddress are markup.MaxAddress and LinkAddress: a // [label](address) link's address is at most 2,048 bytes, by its first // bytes http, https or mailto, then safeURL. Without the bound, "[x(" // repeated and closed once parsed the whole shared address per [. export const MAX_ADDRESS = 2048 export function linkAddress (raw) { if (byteLength(raw) > MAX_ADDRESS) return false const head = raw.slice(0, 8).toLowerCase() if (!head.startsWith('http://') && !head.startsWith('https://') && !head.startsWith('mailto:')) return false return safeURL(raw) } // byteLength is UTF-8 length, as Go's len. function byteLength (text) { return new TextEncoder().encode(text).length } export function safeURL (raw) { const parsed = parseURL(raw) if (parsed === null) return false switch (parsed.scheme) { case 'http': case 'https': return parsed.host !== '' && !parsed.host.startsWith('[') case 'mailto': return parsed.opaque !== '' } return false } // parseURL is url.Parse, reduced to the scheme, host and opaque part, // or null where Parse returns an error. function parseURL (raw) { const hash = raw.indexOf('#') const head = hash < 0 ? raw : raw.slice(0, hash) const fragment = hash < 0 ? '' : raw.slice(hash + 1) const parsed = parse(head) if (parsed === null) return null if (fragment !== '' && !validEscapes(fragment)) return null return parsed } function parse (raw) { if (/[\x00-\x1f\x7f]/u.test(raw)) return null if (raw === '*') return { scheme: '', host: '', opaque: '' } const scheme = getScheme(raw) if (scheme === null) return null let rest = scheme.rest const parsed = { scheme: scheme.name.toLowerCase(), host: '', opaque: '' } if (rest.endsWith('?') && rest.split('?').length === 2) { rest = rest.slice(0, -1) } else if (rest.includes('?')) { rest = rest.slice(0, rest.indexOf('?')) } if (!rest.startsWith('/')) { if (parsed.scheme !== '') { parsed.opaque = rest return parsed } const segment = rest.split('/')[0] if (segment.includes(':')) return null } if ((parsed.scheme !== '' || !rest.startsWith('///')) && rest.startsWith('//')) { let authority = rest.slice(2) rest = '' const slash = authority.indexOf('/') if (slash >= 0) { rest = authority.slice(slash) authority = authority.slice(0, slash) } const host = parseAuthority(parsed.scheme, authority) if (host === null) return null parsed.host = host } return validEscapes(rest) ? parsed : null } function getScheme (raw) { for (let index = 0; index < raw.length; index++) { const char = raw[index] if (/^[A-Za-z]$/u.test(char)) continue if (/^[0-9+\-.]$/u.test(char)) { if (index === 0) return { name: '', rest: raw } continue } if (char === ':') { if (index === 0) return null return { name: raw.slice(0, index), rest: raw.slice(index + 1) } } return { name: '', rest: raw } } return { name: '', rest: raw } } function parseAuthority (scheme, authority) { const at = authority.lastIndexOf('@') const host = parseHost(scheme, at < 0 ? authority : authority.slice(at + 1)) if (host === null || at < 0) return host const userinfo = authority.slice(0, at) for (const char of userinfo) { if (!userinfoAllowed.test(char)) return null } return validEscapes(userinfo) ? host : null } // parseHost is url.parseHost, but for a bracketed literal, which // SafeURL refuses whatever Parse makes of it. function parseHost (scheme, host) { const bracket = host.lastIndexOf('[') if (bracket > 0) return null if (bracket === 0) return '[' let colon = host.indexOf(':') if (colon !== -1) { const last = host.lastIndexOf(':') // urlstrictcolons=1, Go 1.26's default: http and https keep the // first colon, so a second one makes the port invalid. if (last !== colon && scheme !== 'http' && scheme !== 'https') { colon = last } if (!/^:[0-9]*$/u.test(host.slice(colon))) return null } return validHost(host) ? host : null } // validHost is unescape(host, encodeHost) succeeding. function validHost (host) { for (let index = 0; index < host.length; index++) { const char = host[index] if (char === '%') { if (!escapeAt(host, index)) return false if (parseInt(host[index + 1], 16) < 8 && host.slice(index, index + 3) !== '%25') return false index += 2 } else if (char.charCodeAt(0) < 0x80 && !hostAllowed.test(char)) { return false } } return true } // validEscapes is unescape succeeding in a mode with no other rule: every // % starts %XX. function validEscapes (text) { for (let index = 0; index < text.length; index++) { if (text[index] !== '%') continue if (!escapeAt(text, index)) return false index += 2 } return true } function escapeAt (text, index) { return index + 2 < text.length && hexDigit.test(text[index + 1]) && hexDigit.test(text[index + 2]) }