| 1 | // safeurl.mjs — markup.SafeURL: an author link Mapa makes clickable. |
| 2 | // http, https or mailto, with a host for the web ones, and nothing |
| 3 | // net/url.Parse (Go 1.26) would refuse. This ports the parts of Parse |
| 4 | // that decide those answers, in Parse's order; a bracketed IP literal is |
| 5 | // refused outright, on both sides (spec §6.4, "Parity hazards"). |
| 6 | |
| 7 | // hostAllowed is the ASCII net/url lets into a host unescaped |
| 8 | // (shouldEscape(c, encodeHost) is false): letters, digits, -_.~ and |
| 9 | // !$&'()*+,;=:[]<>". |
| 10 | const hostAllowed = /^[A-Za-z0-9\-_.~!$&'()*+,;=:[\]<>"]$/u |
| 11 | const userinfoAllowed = /^[A-Za-z0-9\-._:~!$&'()*+,;=%@]$/u |
| 12 | const hexDigit = /^[0-9A-Fa-f]$/u |
| 13 | |
| 14 | // MAX_ADDRESS and linkAddress are markup.MaxAddress and LinkAddress: a |
| 15 | // [label](address) link's address is at most 2,048 bytes, by its first |
| 16 | // bytes http, https or mailto, then safeURL. Without the bound, "[x(" |
| 17 | // repeated and closed once parsed the whole shared address per [. |
| 18 | export const MAX_ADDRESS = 2048 |
| 19 | |
| 20 | export function linkAddress (raw) { |
| 21 | if (byteLength(raw) > MAX_ADDRESS) return false |
| 22 | const head = raw.slice(0, 8).toLowerCase() |
| 23 | if (!head.startsWith('http://') && !head.startsWith('https://') && |
| 24 | !head.startsWith('mailto:')) return false |
| 25 | return safeURL(raw) |
| 26 | } |
| 27 | |
| 28 | // byteLength is UTF-8 length, as Go's len. |
| 29 | function byteLength (text) { |
| 30 | return new TextEncoder().encode(text).length |
| 31 | } |
| 32 | |
| 33 | export function safeURL (raw) { |
| 34 | const parsed = parseURL(raw) |
| 35 | if (parsed === null) return false |
| 36 | switch (parsed.scheme) { |
| 37 | case 'http': |
| 38 | case 'https': |
| 39 | return parsed.host !== '' && !parsed.host.startsWith('[') |
| 40 | case 'mailto': |
| 41 | return parsed.opaque !== '' |
| 42 | } |
| 43 | return false |
| 44 | } |
| 45 | |
| 46 | // parseURL is url.Parse, reduced to the scheme, host and opaque part, |
| 47 | // or null where Parse returns an error. |
| 48 | function parseURL (raw) { |
| 49 | const hash = raw.indexOf('#') |
| 50 | const head = hash < 0 ? raw : raw.slice(0, hash) |
| 51 | const fragment = hash < 0 ? '' : raw.slice(hash + 1) |
| 52 | const parsed = parse(head) |
| 53 | if (parsed === null) return null |
| 54 | if (fragment !== '' && !validEscapes(fragment)) return null |
| 55 | return parsed |
| 56 | } |
| 57 | |
| 58 | function parse (raw) { |
| 59 | if (/[\x00-\x1f\x7f]/u.test(raw)) return null |
| 60 | if (raw === '*') return { scheme: '', host: '', opaque: '' } |
| 61 | const scheme = getScheme(raw) |
| 62 | if (scheme === null) return null |
| 63 | let rest = scheme.rest |
| 64 | const parsed = { scheme: scheme.name.toLowerCase(), host: '', opaque: '' } |
| 65 | if (rest.endsWith('?') && rest.split('?').length === 2) { |
| 66 | rest = rest.slice(0, -1) |
| 67 | } else if (rest.includes('?')) { |
| 68 | rest = rest.slice(0, rest.indexOf('?')) |
| 69 | } |
| 70 | if (!rest.startsWith('/')) { |
| 71 | if (parsed.scheme !== '') { |
| 72 | parsed.opaque = rest |
| 73 | return parsed |
| 74 | } |
| 75 | const segment = rest.split('/')[0] |
| 76 | if (segment.includes(':')) return null |
| 77 | } |
| 78 | if ((parsed.scheme !== '' || !rest.startsWith('///')) && |
| 79 | rest.startsWith('//')) { |
| 80 | let authority = rest.slice(2) |
| 81 | rest = '' |
| 82 | const slash = authority.indexOf('/') |
| 83 | if (slash >= 0) { |
| 84 | rest = authority.slice(slash) |
| 85 | authority = authority.slice(0, slash) |
| 86 | } |
| 87 | const host = parseAuthority(parsed.scheme, authority) |
| 88 | if (host === null) return null |
| 89 | parsed.host = host |
| 90 | } |
| 91 | return validEscapes(rest) ? parsed : null |
| 92 | } |
| 93 | |
| 94 | function getScheme (raw) { |
| 95 | for (let index = 0; index < raw.length; index++) { |
| 96 | const char = raw[index] |
| 97 | if (/^[A-Za-z]$/u.test(char)) continue |
| 98 | if (/^[0-9+\-.]$/u.test(char)) { |
| 99 | if (index === 0) return { name: '', rest: raw } |
| 100 | continue |
| 101 | } |
| 102 | if (char === ':') { |
| 103 | if (index === 0) return null |
| 104 | return { name: raw.slice(0, index), rest: raw.slice(index + 1) } |
| 105 | } |
| 106 | return { name: '', rest: raw } |
| 107 | } |
| 108 | return { name: '', rest: raw } |
| 109 | } |
| 110 | |
| 111 | function parseAuthority (scheme, authority) { |
| 112 | const at = authority.lastIndexOf('@') |
| 113 | const host = parseHost(scheme, at < 0 ? authority : authority.slice(at + 1)) |
| 114 | if (host === null || at < 0) return host |
| 115 | const userinfo = authority.slice(0, at) |
| 116 | for (const char of userinfo) { |
| 117 | if (!userinfoAllowed.test(char)) return null |
| 118 | } |
| 119 | return validEscapes(userinfo) ? host : null |
| 120 | } |
| 121 | |
| 122 | // parseHost is url.parseHost, but for a bracketed literal, which |
| 123 | // SafeURL refuses whatever Parse makes of it. |
| 124 | function parseHost (scheme, host) { |
| 125 | const bracket = host.lastIndexOf('[') |
| 126 | if (bracket > 0) return null |
| 127 | if (bracket === 0) return '[' |
| 128 | let colon = host.indexOf(':') |
| 129 | if (colon !== -1) { |
| 130 | const last = host.lastIndexOf(':') |
| 131 | // urlstrictcolons=1, Go 1.26's default: http and https keep the |
| 132 | // first colon, so a second one makes the port invalid. |
| 133 | if (last !== colon && scheme !== 'http' && scheme !== 'https') { |
| 134 | colon = last |
| 135 | } |
| 136 | if (!/^:[0-9]*$/u.test(host.slice(colon))) return null |
| 137 | } |
| 138 | return validHost(host) ? host : null |
| 139 | } |
| 140 | |
| 141 | // validHost is unescape(host, encodeHost) succeeding. |
| 142 | function validHost (host) { |
| 143 | for (let index = 0; index < host.length; index++) { |
| 144 | const char = host[index] |
| 145 | if (char === '%') { |
| 146 | if (!escapeAt(host, index)) return false |
| 147 | if (parseInt(host[index + 1], 16) < 8 && |
| 148 | host.slice(index, index + 3) !== '%25') return false |
| 149 | index += 2 |
| 150 | } else if (char.charCodeAt(0) < 0x80 && !hostAllowed.test(char)) { |
| 151 | return false |
| 152 | } |
| 153 | } |
| 154 | return true |
| 155 | } |
| 156 | |
| 157 | // validEscapes is unescape succeeding in a mode with no other rule: every |
| 158 | // % starts %XX. |
| 159 | function validEscapes (text) { |
| 160 | for (let index = 0; index < text.length; index++) { |
| 161 | if (text[index] !== '%') continue |
| 162 | if (!escapeAt(text, index)) return false |
| 163 | index += 2 |
| 164 | } |
| 165 | return true |
| 166 | } |
| 167 | |
| 168 | function escapeAt (text, index) { |
| 169 | return index + 2 < text.length && |
| 170 | hexDigit.test(text[index + 1]) && hexDigit.test(text[index + 2]) |
| 171 | } |
| 172 | |