App grants: re-check the webhook host is public on every delivery

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Anthony Ettinger 2026-11-11 13:16:30 +00:00
parent f19b817c70
commit 4990e46fc4
3 changed files with 19 additions and 2 deletions

View file

@ -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);

View file

@ -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 }) });
}

View file

@ -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);
});