From 4990e46fc4a98226b065161d0fd06871bd6e6ed9 Mon Sep 17 00:00:00 2001 From: Anthony Ettinger Date: Wed, 11 Nov 2026 13:16:30 +0000 Subject: [PATCH] App grants: re-check the webhook host is public on every delivery Co-Authored-By: Claude Opus 5.5 --- apps/pwa/src/lib/app-grants.mjs | 11 ++++++++++- apps/pwa/src/routes/app-grants.mjs | 2 +- apps/pwa/test/app-grants.test.mjs | 8 ++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/apps/pwa/src/lib/app-grants.mjs b/apps/pwa/src/lib/app-grants.mjs index 7ed0ce24..7016ef63 100644 --- a/apps/pwa/src/lib/app-grants.mjs +++ b/apps/pwa/src/lib/app-grants.mjs @@ -150,7 +150,16 @@ export function signWebhook(secret, id, ts, body) { } /** POST one event to the app. Returns { ok, status?, error? }; never throws. */ -export async function sendWebhook(url, secret, payload, { fetchImpl = fetch } = {}) { +export async function sendWebhook(url, secret, payload, { fetchImpl = fetch, allowLocal = false } = {}) { + // Re-checked on every delivery: a host that resolved publicly at consent time + // may point inside our network by the time a rotation fires. + if (!allowLocal) { + try { + await assertPublicHost(new URL(url).hostname); + } catch (e) { + return { ok: false, error: e.message }; + } + } const body = JSON.stringify(payload); const id = `msg_${randomBytes(12).toString("hex")}`; const ts = Math.floor(Date.now() / 1000); diff --git a/apps/pwa/src/routes/app-grants.mjs b/apps/pwa/src/routes/app-grants.mjs index 9ef43473..e46a54dc 100644 --- a/apps/pwa/src/routes/app-grants.mjs +++ b/apps/pwa/src/routes/app-grants.mjs @@ -76,7 +76,7 @@ async function deliver(grants, type, sealedById = {}) { recipient_user_id: parseJson(g.recipient, {})?.user_id ?? null, ...(sealedById[g.id] ? { sealed: sealedById[g.id] } : {}), }; - const r = await sendWebhook(g.webhook_url, g.webhook_secret, payload, { fetchImpl }); + const r = await sendWebhook(g.webhook_url, g.webhook_secret, payload, { fetchImpl, allowLocal: ALLOW_LOCAL }); await run(`UPDATE app_grants SET last_delivered_at = ?, last_error = ? WHERE id = ?`, [r.ok ? Date.now() : g.last_delivered_at ?? null, r.ok ? null : r.error, g.id]); out.push({ grant_id: g.id, key_name: g.key_name, delivered: r.ok, ...(r.ok ? {} : { error: r.error }) }); } diff --git a/apps/pwa/test/app-grants.test.mjs b/apps/pwa/test/app-grants.test.mjs index 210ff855..772b6d3f 100644 --- a/apps/pwa/test/app-grants.test.mjs +++ b/apps/pwa/test/app-grants.test.mjs @@ -185,3 +185,11 @@ test("an unknown nonce or a bad manifest is refused", async () => { manifests.set("nonce-badhook-0001", { ...manifest(), webhook_url: "https://elsewhere.test/hook" }); assert.equal((await open("nonce-badhook-0001")).status, 502); }); + +test("a webhook host that now resolves privately gets no delivery", async () => { + const { sendWebhook } = await import("../src/lib/app-grants.mjs"); + let called = false; + const r = await sendWebhook("https://localhost.localdomain./hook", "s".repeat(40), {}, { fetchImpl: async () => ((called = true), new Response("ok")) }); + assert.equal(r.ok, false); + assert.equal(called, false); +});