Public: anyone can read this; the crew can change it.
The desk spots an outside link however it is written #15059
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/15037-the-desk-spots-an-outside-link-however-i → main 2 commits fork 1 thread · 1 unresolved
2 files
changed +55
−3
src/core/desk.ts +24 −3
| @@ -80,10 +80,26 @@ const fromRow = (r: Row): DeskNote => ({ | ||
| 80 | 80 | const LINK = |
| 81 | 81 | /\b[a-z][a-z0-9+.-]*:\/\/[^\s]*|\bwww\.[^\s]+|\b(?:\d{1,3}(?:\.\d{1,3}){3}|[a-z0-9-]+(?:\.[a-z0-9-]+)*\.[a-z]{2,})(?![a-z0-9-])(?::\d+)?[^\s]*/gi; |
| 82 | 82 | |
| 83 | /** | |
| 84 | * The links a URL parser reads that LINK misses because their host has no | |
| 85 | * dot: any scheme then two slashes of either kind (`https:\\h`), a special | |
| 86 | * scheme with fewer (`HTTP:h`, `https:/h`), and scheme-relative `//h` or | |
| 87 | * `\\h` at the start of a word (#15037). | |
| 88 | */ | |
| 89 | const SCHEMED = | |
| 90 | /\b[a-z][a-z0-9+.-]*:[\\/]{2}[^\s]*|\b(?:https?|wss?|ftp|file):[^\s]+|(?<![\w\\/.:-])[\\/]{2,}[^\s\\/][^\s]*/gi; | |
| 91 | ||
| 92 | /** The parser drops tab, CR and LF anywhere in a URL, so `ht<tab>tps:` is `https:`. */ | |
| 93 | const PARSER_DROPS = /[\t\r\n]+/g; | |
| 94 | ||
| 83 | 95 | const hostOf = (link: string): string => { |
| 84 | const bare = /^[a-z][a-z0-9+.-]*:\/\//i.test(link) ? link : `https://${link}`; | |
| 96 | // `host.tld:8080/x` is a bare host with a port, not a scheme named `host.tld`. | |
| 97 | const led = /^(?:[a-z][a-z0-9+.-]*:[\\/]|(?:https?|wss?|ftp|file):)/i.test(link); | |
| 98 | // A base is lent only to `//h`: with one, `https:/h` would read as a path, | |
| 99 | // but copied anywhere else it is a link to `h`, so it is read without. | |
| 85 | 100 | try { |
| 86 | return new URL(bare).hostname.toLowerCase(); | |
| 101 | const url = /^[\\/]{2}/.test(link) ? new URL(link, "https://desk.invalid") : new URL(led ? link : `https://${link}`); | |
| 102 | return url.hostname.toLowerCase(); | |
| 87 | 103 | } catch { |
| 88 | 104 | return ""; |
| 89 | 105 | } |
| @@ -92,7 +108,12 @@ const hostOf = (link: string): string => { | ||
| 92 | 108 | /** Whether `text` holds a link whose host is not one of `forgeHosts`. */ |
| 93 | 109 | export function hasOutsideLink(text: string, forgeHosts: readonly string[] = []): boolean { |
| 94 | 110 | const own = new Set(forgeHosts.map((h) => h.toLowerCase())); |
| 95 | return [...text.matchAll(LINK)].some((m) => !own.has(hostOf(m[0]))); | |
| 111 | const links = [ | |
| 112 | ...text.matchAll(LINK), | |
| 113 | ...text.matchAll(SCHEMED), | |
| 114 | ...text.replace(PARSER_DROPS, "").matchAll(SCHEMED), | |
| 115 | ]; | |
| 116 | return links.some((m) => !own.has(hostOf(m[0]))); | |
| 96 | 117 | } |
| 97 | 118 | |
| 98 | 119 | /** Whether `text` holds a string shaped like a credential (the code scanner's rules). */ |
test/desk.test.ts +31
| @@ -104,6 +104,37 @@ describe("the desk's notes", () => { | ||
| 104 | 104 | expect(hasOutsideLink("plain words, version 1.2.3")).toBe(false); |
| 105 | 105 | }); |
| 106 | 106 | |
| 107 | // #15037: each form a URL parser reads as a link to `localhost`, which has no dot. | |
| 108 | describe("reads a link the way a URL parser does", () => { | |
| 109 | const own = ["forge.example"]; | |
| 110 | const cases: [string, string][] = [ | |
| 111 | ["scheme-relative //host", "see //localhost/x"], | |
| 112 | ["scheme-relative \\\\host", "see \\\\localhost\\x"], | |
| 113 | ["scheme-relative ///host", "see ///localhost/x"], | |
| 114 | ["backslashes after the scheme", "see https:\\\\localhost"], | |
| 115 | ["mixed slashes after the scheme", "see https:/\\localhost"], | |
| 116 | ["one slash after a special scheme", "see https:/localhost"], | |
| 117 | ["no slash after a mixed-case special scheme", "see hTtP:localhost"], | |
| 118 | ["mixed-case scheme with backslashes", "see HtTpS:\\\\localhost"], | |
| 119 | ["a tab inside the scheme", "see ht\ttps:localhost"], | |
| 120 | ["a newline inside the scheme", "see ht\ntps:\\\\localhost"], | |
| 121 | ["an IPv6 literal, scheme-relative", "see //[::1]/x"], | |
| 122 | ["a decimal IPv4, scheme-relative", "see //2130706433/x"], | |
| 123 | ]; | |
| 124 | for (const [form, text] of cases) | |
| 125 | test(form, () => expect(hasOutsideLink(text, own)).toBe(true)); | |
| 126 | ||
| 127 | test("the forge's own host, however written, still passes", () => { | |
| 128 | for (const text of ["//forge.example/x", "HTTPS:\\\\forge.example\\x", "https:/forge.example", "Http:forge.example", "forge.example:8080/x", "ht\ttps://forge.example/x"]) | |
| 129 | expect(hasOutsideLink(text, own)).toBe(false); | |
| 130 | }); | |
| 131 | ||
| 132 | test("words that are not links still pass", () => { | |
| 133 | for (const text of ["and/or", "a // comment", "src//x", "status: ok", "ratio 3:2", "C:\\Users is a path"]) | |
| 134 | expect(hasOutsideLink(text, own)).toBe(false); | |
| 135 | }); | |
| 136 | }); | |
| 137 | ||
| 107 | 138 | test("only the author or an admin clears; there is no editing", () => { |
| 108 | 139 | reset(); |
| 109 | 140 | const note = (add("mine") as { ok: true; value: { id: string } }).value; |
new above
offline
The second alternative
(?:https?|wss?|ftp|file):[^\s]+will refuse prose such asfile:src/x.ts. Consider requiring a slash afterfile:, or accept the false positive knowingly.