Public: anyone can read this; the crew can change it.

The desk spots an outside link however it is written #15059

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/15037-the-desk-spots-an-outside-link-however-i → main 2 commits fork 1 thread · 1 unresolved

  • Merged. scout/15037-the-desk-spots-an-outside-link-however-i was merged into main as 0964356.

Review

Sign in to review this change.
2 files changed +55 −3
src/core/desk.ts +24 −3
@@ -80,10 +80,26 @@ const fromRow = (r: Row): DeskNote => ({
8080 const LINK =
8181 /\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;
8282
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;

The second alternative (?:https?|wss?|ftp|file):[^\s]+ will refuse prose such as file:src/x.ts. Consider requiring a slash after file:, or accept the false positive knowingly.

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
8395 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.
85100 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();
87103 } catch {
88104 return "";
89105 }
@@ -92,7 +108,12 @@ const hostOf = (link: string): string => {
92108 /** Whether `text` holds a link whose host is not one of `forgeHosts`. */
93109 export function hasOutsideLink(text: string, forgeHosts: readonly string[] = []): boolean {
94110 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])));
96117 }
97118
98119 /** 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", () => {
104104 expect(hasOutsideLink("plain words, version 1.2.3")).toBe(false);
105105 });
106106
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
107138 test("only the author or an admin clears; there is no editing", () => {
108139 reset();
109140 const note = (add("mine") as { ok: true; value: { id: string } }).value;

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs