Skip to content

Open-source Grok Bot alternative with a virtual machine that bots can use

fix(images): refuse metadata and unique-local custom image URLs

Sebastien Tardif4ff2df269dec
diff --git a/shared/image-generation.test.ts b/shared/image-generation.test.ts
index 8c3bd5d1ca..c57424905e 100644
--- a/shared/image-generation.test.ts
+++ b/shared/image-generation.test.ts
@@ -14,7 +14,11 @@ describe("avatar image endpoint", () => {
it.each([
"file:///etc/passwd", "http://router.example/v1", "https://user:[email protected]/v1",
"https://router.example/v1?key=secret", "https://router.example/v1#secret",
- "http://169.254.169.254/latest", "https://169.254.169.254/latest", "http://0.0.0.0/v1", "not a URL",
+ "http://169.254.169.254/latest", "https://169.254.169.254/latest", "http://0.0.0.0/v1", "https://0.0.0.0/v1",
+ "http://[fd00:ec2::254]/latest", "https://[fd00:ec2::254]/latest", "http://[fc00::1]/v1", "https://[fdaa::3]/v1",
+ "https://[::ffff:169.254.169.254]/latest", "https://[::]/v1", "https://[::ffff:0.0.0.0]/",
+ "https://100.100.100.200/latest", "https://[64:ff9b::a9fe:a9fe]/", "https://[2002:a9fe:a9fe::]/", "https://[fe80::1]/v1",
+ "not a URL",
])("rejects unsafe or secret-bearing URLs %s", (input) => {
expect(() => normalizeImageGenerationUrl(input)).toThrow("API base URL");
});
diff --git a/shared/image-generation.ts b/shared/image-generation.ts
index bf9da56853..2195d04fcd 100644
--- a/shared/image-generation.ts
+++ b/shared/image-generation.ts
@@ -59,9 +59,67 @@ export interface ImageGenerationConfig {
}
/** A deliberately configured Images API endpoint, not a provider-returned URL.
- * Local HTTP is useful for self-hosted routers; internet endpoints require TLS. */
+ * Local HTTP is useful for self-hosted routers; internet endpoints require TLS.
+ * Unique-local, link-local, metadata, and unspecified addresses are refused on either scheme. */
const API_KEY_SHAPE = /^(sk-|sk_|xai-|gsk_|AIza)[A-Za-z0-9_-]{12,}$/;
+function bareHost(hostname: string): string {
+ return hostname.trim().toLowerCase().replace(/^\[|\]$/g, "").split("%", 1)[0] ?? "";
+}
+
+function ipv4Octets(host: string): number[] | null {
+ const match = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/.exec(host);
+ if (!match) return null;
+ const octets = match.slice(1).map(Number);
+ return octets.every((octet) => octet <= 255) ? octets : null;
+}
+
+/** Eight groups, with a trailing dotted IPv4 folded into the last two. */
+function ipv6Groups(host: string): number[] | null {
+ const dotted = /^(.*:)(\d{1,3}(?:\.\d{1,3}){3})$/.exec(host);
+ let text = host;
+ if (dotted) {
+ const octets = ipv4Octets(dotted[2] ?? "");
+ if (!octets) return null;
+ text = `${dotted[1]}${((octets[0]! << 8) | octets[1]!).toString(16)}:${((octets[2]! << 8) | octets[3]!).toString(16)}`;
+ }
+ const halves = text.split("::");
+ if (halves.length > 2) return null;
+ const head = halves[0] ? halves[0].split(":") : [];
+ const tail = halves.length === 2 && halves[1] ? halves[1].split(":") : [];
+ const missing = 8 - head.length - tail.length;
+ if (halves.length === 1 ? missing !== 0 : missing < 1) return null;
+ const groups = [...head, ...Array<string>(missing).fill("0"), ...tail].map((part) => /^[0-9a-f]{1,4}$/.test(part) ? Number.parseInt(part, 16) : Number.NaN);
+ return groups.length === 8 && groups.every((part) => Number.isInteger(part)) ? groups : null;
+}
+
+function octetsFromGroups(high: number, low: number): number[] {
+ return [high >> 8, high & 0xff, low >> 8, low & 0xff];
+}
+
+/** IPv4 carried in a mapped, NAT64, or 6to4 address. A translator sends that IPv4 on. */
+function embeddedOctets(groups: number[]): number[] | null {
+ const prefix = (count: number) => groups.slice(0, count).every((part) => part === 0);
+ if (prefix(5) && groups[5] === 0xffff) return octetsFromGroups(groups[6]!, groups[7]!);
+ if (groups[0] === 0x64 && groups[1] === 0xff9b && groups.slice(2, 6).every((part) => part === 0)) return octetsFromGroups(groups[6]!, groups[7]!);
+ if (groups[0] === 0x2002) return octetsFromGroups(groups[1]!, groups[2]!);
+ return null;
+}
+
+/** Cloud metadata, link-local, unique-local, and the unspecified address, including the IPv4 a translator would send.
+ * The settings page imports this file, so this stays free of node:net. */
+function refusedImageHost(host: string): boolean {
+ const bare = bareHost(host);
+ const ipv4 = ipv4Octets(bare);
+ if (ipv4) return ipv4[0] === 0 || (ipv4[0] === 169 && ipv4[1] === 254) || (ipv4[0] === 100 && ipv4[1] === 100 && ipv4[2] === 100 && ipv4[3] === 200);
+ const groups = ipv6Groups(bare);
+ if (!groups) return false;
+ const first = groups[0]!;
+ if (groups.every((part) => part === 0) || (first >= 0xfc00 && first <= 0xfdff) || (first >= 0xfe80 && first <= 0xfebf)) return true;
+ const embedded = embeddedOctets(groups);
+ return embedded !== null && refusedImageHost(embedded.join("."));
+}
+
/** A value pasted into a model field that has the shape of a provider's API key. */
export function looksLikeApiKey(value: string): boolean {
return API_KEY_SHAPE.test(value.trim());
@@ -73,15 +131,15 @@ export function normalizeImageGenerationUrl(value: string): string {
try { url = new URL(value.trim()); } catch { throw new Error(message); }
if (value.length > 2048 || url.username || url.password || url.search || url.hash) throw new Error(message);
const host = url.hostname.toLowerCase();
+ if (refusedImageHost(host)) throw new Error(message);
const ipv4 = /^(\d+)\.(\d+)\.(\d+)\.(\d+)$/.exec(host);
const local = host === "localhost" || host.endsWith(".localhost") || host.endsWith(".local")
|| host === "host.docker.internal" || host === "host.containers.internal"
- || host === "[::1]" || /^\[f[cd][0-9a-f]{2}:/.test(host)
+ || host === "[::1]"
|| (ipv4 && (Number(ipv4[1]) === 127 || Number(ipv4[1]) === 10
|| (Number(ipv4[1]) === 192 && Number(ipv4[2]) === 168)
|| (Number(ipv4[1]) === 172 && Number(ipv4[2]) >= 16 && Number(ipv4[2]) <= 31)));
if (url.protocol !== "https:" && !(url.protocol === "http:" && local)) throw new Error(message);
- if (ipv4 && (Number(ipv4[1]) === 0 || (Number(ipv4[1]) === 169 && Number(ipv4[2]) === 254))) throw new Error(message);
url.pathname = url.pathname.replace(/\/+$/, "").replace(/\/images\/generations$/, "");
return url.toString().replace(/\/+$/, "");
}