|
| 1 | +/** |
| 2 | + * URL guard for the SKILL_API.fetch bridge. |
| 3 | + * |
| 4 | + * Skills are untrusted user-supplied code packages executed in a QuickJS VM. |
| 5 | + * The host-side fetch bridge runs in the extension's service worker context |
| 6 | + * and would otherwise allow a malicious skill to perform Server-Side Request |
| 7 | + * Forgery (SSRF) against private network resources reachable from the host |
| 8 | + * (e.g. cloud-metadata services at 169.254.169.254, services bound to |
| 9 | + * localhost, or RFC1918 ranges on the user's LAN). |
| 10 | + * |
| 11 | + * This module provides an allow-by-default-public-only policy: |
| 12 | + * - Only http(s) URLs are permitted. |
| 13 | + * - Hostnames that resolve to (or literally are) loopback, link-local, |
| 14 | + * unique-local, broadcast, multicast, or RFC1918 private ranges are |
| 15 | + * rejected. |
| 16 | + * - "localhost" and analogous reserved names are rejected. |
| 17 | + * |
| 18 | + * Note: Because hostname resolution happens inside fetch(), DNS-based |
| 19 | + * rebinding is a residual risk. Where strict isolation is required, callers |
| 20 | + * should additionally restrict to an explicit allowlist. |
| 21 | + */ |
| 22 | + |
| 23 | +/** Hostnames that always resolve to loopback / unsafe targets. */ |
| 24 | +const BLOCKED_HOSTNAMES = new Set<string>([ |
| 25 | + "localhost", |
| 26 | + "ip6-localhost", |
| 27 | + "ip6-loopback", |
| 28 | + "broadcasthost", |
| 29 | +]); |
| 30 | + |
| 31 | +/** Reserved hostname suffixes (mDNS / link-local naming). */ |
| 32 | +const BLOCKED_HOSTNAME_SUFFIXES = [".localhost", ".local", ".internal"]; |
| 33 | + |
| 34 | +function isIPv4(host: string): boolean { |
| 35 | + const parts = host.split("."); |
| 36 | + if (parts.length !== 4) return false; |
| 37 | + return parts.every( |
| 38 | + (p) => /^\d{1,3}$/.test(p) && Number(p) >= 0 && Number(p) <= 255, |
| 39 | + ); |
| 40 | +} |
| 41 | + |
| 42 | +function isPrivateIPv4(host: string): boolean { |
| 43 | + if (!isIPv4(host)) return false; |
| 44 | + const parts = host.split(".").map(Number); |
| 45 | + const a = parts[0] ?? -1; |
| 46 | + const b = parts[1] ?? -1; |
| 47 | + // 0.0.0.0/8 - "this" network |
| 48 | + if (a === 0) return true; |
| 49 | + // 10.0.0.0/8 - private |
| 50 | + if (a === 10) return true; |
| 51 | + // 127.0.0.0/8 - loopback |
| 52 | + if (a === 127) return true; |
| 53 | + // 169.254.0.0/16 - link-local (incl. 169.254.169.254 cloud metadata) |
| 54 | + if (a === 169 && b === 254) return true; |
| 55 | + // 172.16.0.0/12 - private |
| 56 | + if (a === 172 && b >= 16 && b <= 31) return true; |
| 57 | + // 192.0.0.0/24, 192.0.2.0/24, 192.88.99.0/24 - reserved/docs |
| 58 | + if (a === 192 && b === 0) return true; |
| 59 | + // 192.168.0.0/16 - private |
| 60 | + if (a === 192 && b === 168) return true; |
| 61 | + // 198.18.0.0/15 - benchmarking |
| 62 | + if (a === 198 && (b === 18 || b === 19)) return true; |
| 63 | + // 198.51.100.0/24, 203.0.113.0/24 - documentation |
| 64 | + if (a === 198 && b === 51) return true; |
| 65 | + if (a === 203 && b === 0) return true; |
| 66 | + // 224.0.0.0/4 - multicast |
| 67 | + if (a >= 224 && a <= 239) return true; |
| 68 | + // 240.0.0.0/4 - reserved (incl. 255.255.255.255 broadcast) |
| 69 | + if (a >= 240) return true; |
| 70 | + return false; |
| 71 | +} |
| 72 | + |
| 73 | +function normalizeIPv6(host: string): string { |
| 74 | + // Strip brackets if present (URL hostnames preserve brackets in some envs) |
| 75 | + return host.replace(/^\[/, "").replace(/\]$/, "").toLowerCase(); |
| 76 | +} |
| 77 | + |
| 78 | +function isPrivateIPv6(rawHost: string): boolean { |
| 79 | + const host = normalizeIPv6(rawHost); |
| 80 | + if (!host.includes(":")) return false; |
| 81 | + // Loopback ::1 |
| 82 | + if (host === "::1" || host === "0:0:0:0:0:0:0:1") return true; |
| 83 | + // Unspecified :: |
| 84 | + if (host === "::" || /^0+(:0+){0,7}$/.test(host)) return true; |
| 85 | + // IPv4-mapped dotted form (::ffff:a.b.c.d) |
| 86 | + const v4MappedMatch = host.match( |
| 87 | + /^::ffff:(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/, |
| 88 | + ); |
| 89 | + if (v4MappedMatch?.[1] && isPrivateIPv4(v4MappedMatch[1])) return true; |
| 90 | + // IPv4-mapped hex form (::ffff:HHHH:HHHH) — convert to dotted and re-check |
| 91 | + const v4MappedHex = host.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/); |
| 92 | + if (v4MappedHex) { |
| 93 | + const high = parseInt(v4MappedHex[1] ?? "0", 16); |
| 94 | + const low = parseInt(v4MappedHex[2] ?? "0", 16); |
| 95 | + const dotted = [ |
| 96 | + (high >> 8) & 0xff, |
| 97 | + high & 0xff, |
| 98 | + (low >> 8) & 0xff, |
| 99 | + low & 0xff, |
| 100 | + ].join("."); |
| 101 | + if (isPrivateIPv4(dotted)) return true; |
| 102 | + } |
| 103 | + // Link-local fe80::/10 |
| 104 | + if (/^fe[89ab][0-9a-f]?:/.test(host)) return true; |
| 105 | + // Unique local fc00::/7 |
| 106 | + if (/^f[cd][0-9a-f]{2}:/.test(host)) return true; |
| 107 | + // Multicast ff00::/8 |
| 108 | + if (/^ff[0-9a-f]{2}:/.test(host)) return true; |
| 109 | + return false; |
| 110 | +} |
| 111 | + |
| 112 | +export class SsrfBlockedError extends Error { |
| 113 | + constructor(message: string) { |
| 114 | + super(message); |
| 115 | + this.name = "SsrfBlockedError"; |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +/** |
| 120 | + * Validate that a URL is safe for the skill fetch bridge. |
| 121 | + * Throws SsrfBlockedError if the URL targets a private/internal resource |
| 122 | + * or uses a non-http(s) scheme. |
| 123 | + */ |
| 124 | +export function assertSkillFetchUrlAllowed(rawUrl: string): URL { |
| 125 | + let parsed: URL; |
| 126 | + try { |
| 127 | + parsed = new URL(rawUrl); |
| 128 | + } catch { |
| 129 | + throw new SsrfBlockedError(`Invalid URL: ${rawUrl}`); |
| 130 | + } |
| 131 | + |
| 132 | + if (parsed.protocol !== "http:" && parsed.protocol !== "https:") { |
| 133 | + throw new SsrfBlockedError( |
| 134 | + `Blocked URL scheme '${parsed.protocol}' (only http/https are allowed)`, |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + // Strip brackets that URL parsing leaves around IPv6 literals |
| 139 | + const hostname = parsed.hostname |
| 140 | + .toLowerCase() |
| 141 | + .replace(/^\[/, "") |
| 142 | + .replace(/\]$/, ""); |
| 143 | + |
| 144 | + if (!hostname) { |
| 145 | + throw new SsrfBlockedError("Blocked URL with empty hostname"); |
| 146 | + } |
| 147 | + |
| 148 | + if (BLOCKED_HOSTNAMES.has(hostname)) { |
| 149 | + throw new SsrfBlockedError(`Blocked private hostname: ${hostname}`); |
| 150 | + } |
| 151 | + |
| 152 | + for (const suffix of BLOCKED_HOSTNAME_SUFFIXES) { |
| 153 | + if (hostname === suffix.slice(1) || hostname.endsWith(suffix)) { |
| 154 | + throw new SsrfBlockedError( |
| 155 | + `Blocked private hostname suffix: ${hostname}`, |
| 156 | + ); |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + if (isIPv4(hostname) && isPrivateIPv4(hostname)) { |
| 161 | + throw new SsrfBlockedError(`Blocked private IPv4 address: ${hostname}`); |
| 162 | + } |
| 163 | + |
| 164 | + if (hostname.includes(":") && isPrivateIPv6(hostname)) { |
| 165 | + throw new SsrfBlockedError(`Blocked private IPv6 address: ${hostname}`); |
| 166 | + } |
| 167 | + |
| 168 | + return parsed; |
| 169 | +} |
0 commit comments