commit
asdas
diff --git a/src/urls.ts b/src/urls.ts
index c81a495..ef76b49 100644
--- a/src/urls.ts
+++ b/src/urls.ts
@@ -24,7 +24,16 @@ const MAX_URL = 400;
const HOSTILE = /[\s\\<>"'`{}|^\x00-\x1f\x7f]/;
-function parseStrict(raw: string, allowedHosts: readonly string[]): URL | null {
+// allowedParams names the query parameters a caller is willing to consider.
+// It is deliberately opt-in and empty by default, so every existing caller
+// keeps the blanket "no query string" rule. Naming a parameter here only gets
+// it past this gate — the caller still has to validate the value and rebuild
+// the query itself, so nothing user-controlled is passed through verbatim.
+function parseStrict(
+ raw: string,
+ allowedHosts: readonly string[],
+ allowedParams: readonly string[] = [],
+): URL | null {
const value = raw.trim();
if (!value || value.length > MAX_URL) return null;
if (HOSTILE.test(value)) return null;
@@ -41,8 +50,20 @@ function parseStrict(raw: string, allowedHosts: readonly string[]): URL | null {
if (url.protocol !== "https:") return null;
if (url.username || url.password) return null;
if (url.port) return null;
- if (url.search || url.hash) return null;
+ if (url.hash) return null;
if (!allowedHosts.includes(url.hostname)) return null;
+
+ if (url.search) {
+ const seen = new Set<string>();
+ for (const key of url.searchParams.keys()) {
+ if (!allowedParams.includes(key)) return null;
+ // A repeated key is ambiguous rather than useful; refuse instead of
+ // silently picking one of the values.
+ if (seen.has(key)) return null;
+ seen.add(key);
+ }
+ }
+
return url;
}
@@ -91,11 +112,37 @@ const DISCORD_HOSTS = [
const WEBHOOK_PATH = /^\/api(?:\/v\d{1,3})?\/webhooks\/\d{1,32}\/[A-Za-z0-9._-]{1,200}$/;
+// The two Discord webhook parameters worth honouring, each pinned to the exact
+// shape its value may take. thread_id posts into an existing thread rather than
+// the parent channel; wait=true makes Discord answer 200 + the created message
+// instead of a bare 204, so a delivery that Discord rejects is reported as a
+// failure rather than passing silently. Both stay inside notify.ts's
+// `response.ok` check, which spans the whole 2xx range.
+const WEBHOOK_PARAMS: Record<string, RegExp> = {
+ thread_id: /^\d{1,32}$/,
+ wait: /^(?:true|false)$/,
+};
+
+const WEBHOOK_PARAM_NAMES = Object.keys(WEBHOOK_PARAMS);
+
export function discordWebhookUrl(raw: string): string | null {
- const url = parseStrict(raw, DISCORD_HOSTS);
+ const url = parseStrict(raw, DISCORD_HOSTS, WEBHOOK_PARAM_NAMES);
if (!url) return null;
if (!WEBHOOK_PATH.test(url.pathname)) return null;
- return `https://${url.hostname}${url.pathname}`;
+
+ // Rebuild the query from validated values in a fixed order rather than
+ // reusing url.search, so the stored URL is canonical and nothing the user
+ // typed reaches fetch() unexamined.
+ const params = new URLSearchParams();
+ for (const name of WEBHOOK_PARAM_NAMES) {
+ const value = url.searchParams.get(name);
+ if (value === null) continue;
+ if (!WEBHOOK_PARAMS[name]!.test(value)) return null;
+ params.set(name, value);
+ }
+
+ const query = params.toString();
+ return `https://${url.hostname}${url.pathname}${query ? `?${query}` : ""}`;
}
export function maskWebhook(url: string): string {
diff --git a/test/urls.test.mjs b/test/urls.test.mjs
index b5a4bbe..89a6e48 100644
--- a/test/urls.test.mjs
+++ b/test/urls.test.mjs
@@ -80,6 +80,10 @@ console.log("\n-- port / query / fragment --");
no("github", "https://github.com:8080/user/repo", "explicit port");
no("github", "https://github.com:22/user/repo", "explicit ssh port");
no("github", "https://github.com/user/repo?x=1", "query string");
+// The webhook validator opts into thread_id and wait; mirrors opt into
+// nothing, so those names must be just as dead here as any other.
+no("github", "https://github.com/user/repo?thread_id=17", "an opted-in webhook parameter");
+no("github", "https://github.com/user/repo?wait=true", "a webhook boolean");
no("github", "https://github.com/user/repo#frag", "fragment");
console.log("\n-- path shape --");
@@ -124,12 +128,44 @@ check(
"accepts canary",
discordWebhookUrl("https://canary.discord.com/api/webhooks/1/tok") !== null,
);
+check(
+ "accepts ptb",
+ discordWebhookUrl("https://ptb.discord.com/api/webhooks/1/tok") !== null,
+);
+
+// thread_id and wait are the only query parameters allowed through, and only
+// with these exact value shapes. Anything else must still be refused outright.
+check("accepts thread_id", discordWebhookUrl(`${W}?thread_id=17`) === `${W}?thread_id=17`);
+check("accepts wait=true", discordWebhookUrl(`${W}?wait=true`) === `${W}?wait=true`);
+check("accepts wait=false", discordWebhookUrl(`${W}?wait=false`) === `${W}?wait=false`);
+check(
+ "accepts both together",
+ discordWebhookUrl(`${W}?thread_id=17&wait=true`) === `${W}?thread_id=17&wait=true`,
+);
+check(
+ "canonicalises parameter order",
+ discordWebhookUrl(`${W}?wait=true&thread_id=17`) === `${W}?thread_id=17&wait=true`,
+);
+check(
+ "keeps parameters on canary",
+ discordWebhookUrl("https://canary.discord.com/api/webhooks/1/tok?thread_id=17") ===
+ "https://canary.discord.com/api/webhooks/1/tok?thread_id=17",
+);
+
const wno = (url, why) =>
check(`refuses webhook ${why}`, discordWebhookUrl(url) === null);
wno("https://evil.test/api/webhooks/1/tok", "wrong host");
wno("https://discord.com.evil.test/api/webhooks/1/tok", "suffixed host");
wno("http://discord.com/api/webhooks/1/tok", "plain http");
-wno("https://discord.com/api/webhooks/1/tok?wait=true", "query string");
+wno(`${W}?username=admin`, "an unlisted parameter");
+wno(`${W}?thread_id=17&evil=1`, "an unlisted parameter alongside a good one");
+wno(`${W}?thread_id=abc`, "non-numeric thread_id");
+wno(`${W}?thread_id=`, "empty thread_id");
+wno(`${W}?thread_id=${"9".repeat(33)}`, "over-long thread_id");
+wno(`${W}?thread_id=17&thread_id=18`, "a repeated parameter");
+wno(`${W}?wait=1`, "wait that is not a boolean");
+wno(`${W}?wait=TRUE`, "wait in the wrong case");
+wno(`${W}#fragment`, "a fragment");
wno("https://discord.com/", "no webhook path");
wno("https://discord.com/api/webhooks/", "incomplete path");
wno("https://discord.com/api/webhooks/notanid/tok", "non-numeric id");