Skip to content

Open-source AI browser agent for Chrome and Firefox (monorepo) 🧠

Merge main into claude/sec-lmstudio-dns-pinning; keep literal-IP check in safeFetch

Emre Sokullu5259c91ff1ab
diff --cc lmstudio-plugin/README.md
index 6b81e81b0,0dcea5f92..aad5301a4
--- a/lmstudio-plugin/README.md
+++ b/lmstudio-plugin/README.md
@@@ -213,18 -213,18 +213,22 @@@ Everything below concerns `fetch_url`
- **URL guard, structural (sync).** Requests to RFC1918 (`10.*`,
`172.16-31.*`, `192.168.*`), loopback (`127.*`, `::1`), link-local
(`169.254.*`, `fe80::/10`), unique-local IPv6 (`fc00::/7`),
- cloud-metadata IPs, and `*.local` / `*.internal` / `*.lan` /
+ carrier-grade NAT (`100.64.0.0/10`), unspecified (`0.0.0.0/8`, `::`),
+ multicast, cloud-metadata IPs, IPv6 addresses that embed any of
+ those IPv4 ranges (IPv4-mapped `::ffff:7f00:1` in any spelling,
+ IPv4-compatible, NAT64 `64:ff9b::/96`, 6to4 `2002::/16`, Teredo),
+ and `*.local` / `*.internal` / `*.lan` /
`*.home` / `*.corp` / `*.intranet` are blocked. Non-`http(s)`
protocols (`file://`, `ftp://`, `gopher://`, `javascript:`) too.
-- **DNS resolution check.** Before each hop, the hostname is
- resolved via `dns.lookup({all: true})` and every returned A/AAAA
- address is checked against the same private ranges. Closes the
- bypass where `attacker.example A 127.0.0.1` would otherwise pass
- the syntactic check.
+- **DNS resolution check, pinned to the connection.** On each hop the
+ hostname is resolved by the socket's own `lookup` hook via
+ `dns.lookup({all: true})`; every returned A/AAAA address is checked
+ against the same private ranges, and only those checked addresses
+ are dialled. Closes the bypass where `attacker.example A 127.0.0.1`
+ would otherwise pass the syntactic check, and DNS rebinding β€” a
+ TTL=0 record cannot answer public to the check and private to the
+ connection, because they are the same lookup. Requests use a fresh
+ connection each time, never a pooled socket resolved earlier.
- **Per-redirect re-validation.** Redirects are followed manually
(5 hops max). Each `Location` runs through both checks before we
follow it β€” a public URL cannot 302 to `http://169.254.169.254/...`.
diff --cc lmstudio-plugin/package.json
index 37caa5349,1df09a80e..98adaa2f9
--- a/lmstudio-plugin/package.json
+++ b/lmstudio-plugin/package.json
@@@ -15,7 -15,7 +15,7 @@@
"scripts": {
"build": "tsc",
"dev": "tsc --watch",
- "test": "npm run build && node --test test/browserTask.test.mjs test/safeFetch.test.mjs",
- "test": "npm run build && node --test test/browserTask.test.mjs test/urlGuard.test.mjs test/toolParameters.test.mjs",
++ "test": "npm run build && node --test test/browserTask.test.mjs test/urlGuard.test.mjs test/toolParameters.test.mjs test/safeFetch.test.mjs",
"clean": "rm -rf dist"
},
"keywords": [
diff --cc lmstudio-plugin/src/util/safeFetch.ts
index 2b421d124,ae06ed83f..e3379b16c
--- a/lmstudio-plugin/src/util/safeFetch.ts
+++ b/lmstudio-plugin/src/util/safeFetch.ts
@@@ -31,12 -29,8 +31,12 @@@
* open-redirect chain.
*/
-import { lookup as dnsLookup } from "node:dns/promises";
-import { isIP } from "node:net";
+import { lookup as dnsLookup, type LookupAddress } from "node:dns";
+import * as http from "node:http";
+import * as https from "node:https";
- import type { LookupFunction } from "node:net";
++import { isIP, type LookupFunction } from "node:net";
+import { pipeline, Readable } from "node:stream";
+import * as zlib from "node:zlib";
import {
assertSafeUrl,
isPrivateIpv4,
@@@ -95,200 -123,6 +95,220 @@@ function assertAddressesArePublic(host
}
}
++/**
++ * Literal-IP hosts never reach `guardedLookup`: Node connects to them
++ * directly without calling the socket's `lookup` hook. Classify them here
++ * on every hop so this layer does not depend on `assertSafeUrl` alone.
++ */
++function assertLiteralIpIsPublic(url: URL): void {
++ let host = url.hostname;
++ if (host.startsWith("[") && host.endsWith("]")) host = host.slice(1, -1);
++ const family = isIP(host);
++ if (family === 4 ? isPrivateIpv4(host) : family === 6 && isPrivateIpv6(host)) {
++ throw new Error(
++ `Refusing private IPv${family} ${host} β€” set allowPrivate to opt in.`,
++ );
++ }
++}
++
+/**
+ * `lookup` hook for the request socket. Node calls it when connecting
+ * and dials only the addresses it hands back, so the check and the use
+ * are the same resolution β€” resolving first and then letting the HTTP
+ * client resolve the name again would let a TTL=0 record answer public
+ * to the check and private to the connection. Literal-IP hosts never
- * reach a lookup; the sync URL guard covers them.
++ * reach a lookup; `assertLiteralIpIsPublic` covers them.
+ */
+const guardedLookup: LookupFunction = (hostname, options, callback) => {
+ dnsLookup(hostname, { ...options, all: true }, (err, addresses) => {
+ if (err) {
+ callback(err, "");
+ return;
+ }
+ try {
+ if (addresses.length === 0) throw new Error(`No addresses found for ${hostname}`);
+ assertAddressesArePublic(hostname, addresses);
+ } catch (e) {
+ callback(e as NodeJS.ErrnoException, "");
+ return;
+ }
+ if (options.all) callback(null, addresses);
+ else callback(null, addresses[0]!.address, addresses[0]!.family);
+ });
+};
+
+/** Statuses whose Response must have a null body (Fetch standard). */
+const NULL_BODY_STATUSES = new Set([101, 103, 204, 205, 304]);
+/** Lenient flushing, as fetch uses, so a truncated stream still yields what decoded. */
+const ZLIB_OPTIONS = {
+ flush: zlib.constants.Z_SYNC_FLUSH,
+ finishFlush: zlib.constants.Z_SYNC_FLUSH,
+};
+const BROTLI_OPTIONS = {
+ flush: zlib.constants.BROTLI_OPERATION_FLUSH,
+ finishFlush: zlib.constants.BROTLI_OPERATION_FLUSH,
+};
+
+function abortReason(signal: AbortSignal): Error {
+ return signal.reason ??
+ Object.assign(new Error("This operation was aborted"), { name: "AbortError" });
+}
+
+interface HopInit {
+ method: string;
+ headers: Record<string, string>;
+ body: string | undefined;
+ signal?: AbortSignal;
+}
+
+/**
+ * Headers fetch adds on its own. Without them some servers refuse the
+ * request (no User-Agent) or skip compression. Caller values win.
+ */
+function withFetchDefaultHeaders(
+ headers: Record<string, string>,
+ body: string | undefined,
+): Record<string, string> {
+ const out = { ...headers };
+ const present = new Set(Object.keys(out).map((k) => k.toLowerCase()));
+ const defaults: Array<[string, string]> = [
+ ["accept", "*/*"],
+ ["accept-encoding", "gzip, deflate, br"],
+ ["user-agent", "node"],
+ ];
+ if (body !== undefined) defaults.push(["content-type", "text/plain;charset=UTF-8"]);
+ for (const [k, v] of defaults) {
+ if (!present.has(k)) out[k] = v;
+ }
+ return out;
+}
+
+/**
+ * Undo Content-Encoding like fetch: gzip / deflate / br, last-applied
+ * first. An unknown coding leaves the body as it came.
+ */
+function decodeBody(incoming: http.IncomingMessage): Readable {
+ const codings = (incoming.headers["content-encoding"] || "")
+ .toLowerCase()
+ .split(",")
+ .map((c) => c.trim())
+ .filter(Boolean)
+ .reverse();
+ if (codings.length === 0) return incoming;
+ const decoders: NodeJS.ReadWriteStream[] = [];
+ for (const coding of codings) {
+ if (coding === "gzip" || coding === "x-gzip") decoders.push(zlib.createGunzip(ZLIB_OPTIONS));
+ else if (coding === "deflate") decoders.push(zlib.createInflate(ZLIB_OPTIONS));
+ else if (coding === "br") decoders.push(zlib.createBrotliDecompress(BROTLI_OPTIONS));
+ else return incoming;
+ }
+ // pipeline destroys every stage when one fails or the last is
+ // destroyed (e.g. readBodyCapped cancelling), which closes the socket.
+ return pipeline([incoming, ...decoders], () => {}) as unknown as Readable;
+}
+
+/**
+ * Send one request (no redirect following) over a connection whose
+ * address went through `guardedLookup`, and wrap the reply in a
+ * standard `Response` with `url` set, as fetch would return it.
+ */
+function pinnedFetch(url: URL, init: HopInit, opts: UrlGuardOptions): Promise<Response> {
+ return new Promise((resolve, reject) => {
+ const { signal } = init;
+ if (url.username || url.password) {
+ reject(new TypeError(
+ `Request cannot be constructed from a URL that includes credentials: ${url}`,
+ ));
+ return;
+ }
+ if (signal?.aborted) {
+ reject(abortReason(signal));
+ return;
+ }
+
++ if (!opts.allowPrivate) {
++ try { assertLiteralIpIsPublic(url); } catch (e) { reject(e); return; }
++ }
++
+ let req: http.ClientRequest;
+ try {
+ req = (url.protocol === "https:" ? https : http).request(url, {
+ method: init.method,
+ headers: withFetchDefaultHeaders(init.headers, init.body),
+ // A fresh connection per request: a pooled keep-alive socket was
+ // resolved for whichever request opened it (possibly one with
+ // allowPrivate), so reusing it would skip this request's lookup.
+ agent: false,
+ ...(opts.allowPrivate ? {} : { lookup: guardedLookup }),
+ });
+ } catch (e) {
+ reject(e);
+ return;
+ }
+
+ const onAbort = () => req.destroy(abortReason(signal!));
+ signal?.addEventListener("abort", onAbort, { once: true });
+ // Kept for the request's lifetime: a late socket error with no
+ // listener would crash the plugin process.
+ req.on("error", (e) => {
+ signal?.removeEventListener("abort", onAbort);
+ reject(e);
+ });
+ req.once("response", (incoming) => {
+ signal?.removeEventListener("abort", onAbort);
+ try {
+ resolve(toFetchResponse(incoming, url, init.method, signal));
+ } catch (e) {
+ incoming.destroy();
+ reject(e);
+ }
+ });
+ req.end(init.body);
+ });
+}
+
+function toFetchResponse(
+ incoming: http.IncomingMessage,
+ url: URL,
+ method: string,
+ signal: AbortSignal | undefined,
+): Response {
+ const status = incoming.statusCode ?? 0;
+ if (status < 200 || status > 599) {
+ throw new Error(`Unsupported HTTP status ${status} from ${url.origin}`);
+ }
+ const headers = new Headers();
+ for (let i = 0; i + 1 < incoming.rawHeaders.length; i += 2) {
+ headers.append(incoming.rawHeaders[i]!, incoming.rawHeaders[i + 1]!);
+ }
+
+ let body: ReadableStream<Uint8Array> | null = null;
+ if (method === "HEAD" || NULL_BODY_STATUSES.has(status)) {
+ incoming.on("error", () => {});
+ incoming.resume();
+ } else {
+ const decoded = decodeBody(incoming);
+ if (signal) {
+ // Aborting mid-body errors the stream with the abort reason, as fetch does.
+ const onAbort = () => decoded.destroy(abortReason(signal));
+ signal.addEventListener("abort", onAbort, { once: true });
+ decoded.once("close", () => signal.removeEventListener("abort", onAbort));
+ }
+ body = Readable.toWeb(decoded) as unknown as ReadableStream<Uint8Array>;
+ }
+
+ // Response's constructor rejects reason phrases fetch would accept;
+ // the phrase is cosmetic.
+ const statusMessage = incoming.statusMessage ?? "";
+ const statusText = /^[\t\x20-\x7e\x80-\xff]*$/.test(statusMessage) ? statusMessage : "";
+ const res = new Response(body, { status, statusText, headers });
+ // Only fetch() can set Response.url; callers report it as the final URL.
+ const finalUrl = new URL(url);
+ finalUrl.hash = "";
+ Object.defineProperty(res, "url", { value: finalUrl.toString() });
+ return res;
+}
+
export interface SafeFetchInit {
method?: string;
headers?: Record<string, string>;
diff --cc lmstudio-plugin/src/util/urlGuard.ts
index d2773d386,26fca7d4e..c5ec22ceb
--- a/lmstudio-plugin/src/util/urlGuard.ts
+++ b/lmstudio-plugin/src/util/urlGuard.ts
@@@ -12,23 -12,28 +12,25 @@@
*
* `assertSafeUrl(url)` (sync) rejects:
* - non-http(s) protocols (file://, ftp://, gopher://, javascript:)
- * - literal RFC1918 / loopback / link-local / ULA IPv6 hostnames
+ * - literal RFC1918 / loopback / link-local / ULA IPv6 hostnames,
+ * including IPv6 forms that embed a private IPv4 (IPv4-mapped,
+ * NAT64, 6to4, Teredo)
* - common private DNS spellings (localhost, *.local, *.internal, *.lan)
*
- * The DNS-resolution-aware check
- * (`assertHostnameDoesNotResolveToPrivate`) lives in `safeFetch.ts`
- * because it requires `node:dns` and is awaited once per redirect hop
- * inside the `safeFetch` wrapper. That additional layer closes the case
+ * The DNS-resolution-aware check (`guardedLookup`) lives in
+ * `safeFetch.ts` because it requires `node:dns`. It runs as the request
+ * socket's `lookup` hook on every hop, so the connection can only use
+ * addresses that passed it. That additional layer closes the case
* where a public-looking hostname has an A record pointing into
* RFC1918 / loopback / link-local space, e.g. `attacker.example A
- * 127.0.0.1`. `isPrivateIpv4` / `isPrivateIpv6` are exported so the
- * resolver-side check reuses the same range definitions.
+ * 127.0.0.1`, and DNS rebinding (a TTL=0 record answering public to a
+ * check and private to the connection), because there is no separate
+ * check to answer. `isPrivateIpv4` / `isPrivateIpv6` are exported so
+ * the resolver-side check reuses the same range definitions.
*
- * Residual gap: DNS rebinding. An attacker controlling a domain with
- * TTL=0 can return a public IP at the time of the guard check and a
- * private IP at the time `fetch` connects. Closing this fully requires
- * pinning the resolved IP via a custom undici Agent + `lookup` hook β€”
- * tracked as a follow-up. The current layered defense (sync check +
- * DNS check + per-redirect re-validation) raises the bar enough to
- * stop drive-by SSRF from open redirects and naive hostname tricks
- * but is NOT a hardened sandbox.
+ * The layered defense (sync check + connect-time DNS check +
+ * per-redirect re-validation) stops drive-by SSRF from open redirects,
+ * naive hostname tricks and rebinding, but is NOT a hardened sandbox.
*
* To opt-out (e.g. you really do want to point this at your intranet)
* call assertSafeUrl(url, { allowPrivate: true }) and pass the same
diff --cc lmstudio-plugin/test/safeFetch.test.mjs
index 92c4fce9c,000000000..a8f0cbf65
mode 100644,000000..100644
--- a/lmstudio-plugin/test/safeFetch.test.mjs
+++ b/lmstudio-plugin/test/safeFetch.test.mjs
@@@ -1,141 -1,0 +1,158 @@@
+import assert from "node:assert/strict";
+import dns from "node:dns";
+import { once } from "node:events";
+import { createServer } from "node:http";
+import { gzipSync } from "node:zlib";
+import { after, afterEach, before, test } from "node:test";
+
+import { readBodyCapped, safeFetch } from "../dist/util/safeFetch.js";
+
+const originalLookup = dns.lookup;
+const originalPromisesLookup = dns.promises.lookup;
+
+let server;
+let port;
+const hits = [];
+
+before(async () => {
+ server = createServer((req, res) => {
+ let body = "";
+ req.on("data", (chunk) => { body += chunk; });
+ req.on("end", () => {
+ hits.push({ method: req.method, url: req.url, host: req.headers.host, body });
+ if (req.url === "/start") {
+ res.writeHead(302, { location: "/final" });
+ res.end();
+ } else if (req.url === "/final") {
+ res.writeHead(200, { "content-type": "text/plain", "content-encoding": "gzip" });
+ res.end(gzipSync("hello pinned"));
+ } else if (req.url === "/echo") {
+ res.writeHead(200, { "content-type": "text/plain" });
+ res.end(`${req.method} ${req.headers["content-type"]} ${body}`);
+ } else if (req.url === "/slow") {
+ // Never answers; the caller's signal has to end the request.
+ } else {
+ res.writeHead(200, { "content-type": "text/plain" });
+ res.end("internal secret");
+ }
+ });
+ });
+ server.listen(0, "127.0.0.1");
+ await once(server, "listening");
+ port = server.address().port;
+});
+
+after(() => {
+ server.closeAllConnections();
+ server.close();
+});
+
+afterEach(() => {
+ dns.lookup = originalLookup;
+ dns.promises.lookup = originalPromisesLookup;
+ hits.length = 0;
+});
+
+/**
+ * Answer each resolution of `hostname` with the next entry of `answers`
+ * (the last one repeats), through both the callback and promise DNS
+ * APIs β€” like an attacker's TTL=0 record that changes between queries.
+ */
+function fakeDns(hostname, answers) {
+ let calls = 0;
+ const next = () => answers[Math.min(calls++, answers.length - 1)];
+ dns.lookup = function (host, options, callback) {
+ if (typeof options === "function") {
+ callback = options;
+ options = {};
+ }
+ if (host !== hostname) return originalLookup.call(this, host, options, callback);
+ const addresses = next();
+ process.nextTick(() => {
+ if (options?.all) callback(null, addresses);
+ else callback(null, addresses[0].address, addresses[0].family);
+ });
+ };
+ dns.promises.lookup = async (host, options) => {
+ if (host !== hostname) return originalPromisesLookup(host, options);
+ const addresses = next();
+ return options?.all ? addresses : addresses[0];
+ };
+}
+
+const PUBLIC = { address: "192.0.2.10", family: 4 }; // TEST-NET-1: public to the guard, never routed
+const LOOPBACK = { address: "127.0.0.1", family: 4 };
+
+test("a DNS answer that changes after the check cannot move the connection to a private address", async () => {
+ fakeDns("rebind.example", [[PUBLIC], [LOOPBACK]]);
+
+ // The connection may only use the answer that was validated (PUBLIC),
+ // which is unroutable, so the request fails or times out. Before the
+ // fix, fetch resolved the name again and reached the loopback server.
+ await assert.rejects(
+ safeFetch(`http://rebind.example:${port}/secret`, { signal: AbortSignal.timeout(1500) }),
+ );
+ assert.deepEqual(hits, []);
+});
+
+test("every address the connection could use is checked before connecting", async () => {
+ fakeDns("mixed.example", [[PUBLIC, LOOPBACK]]);
+
+ await assert.rejects(
+ safeFetch(`http://mixed.example:${port}/secret`),
+ /resolves to private IPv4 127\.0\.0\.1/,
+ );
+ assert.deepEqual(hits, []);
+});
+
+test("allowPrivate requests follow redirects and keep the fetch response shape", async () => {
+ fakeDns("intranet.example", [[LOOPBACK]]);
+
+ const res = await safeFetch(`http://intranet.example:${port}/start`, { allowPrivate: true });
+ assert.equal(res.status, 200);
+ assert.equal(res.ok, true);
+ assert.equal(res.url, `http://intranet.example:${port}/final`);
+ assert.equal(res.headers.get("content-type"), "text/plain");
+ const { text, truncated } = await readBodyCapped(res, 1024);
+ assert.equal(text, "hello pinned");
+ assert.equal(truncated, false);
+ assert.deepEqual(hits.map((hit) => [hit.url, hit.host]), [
+ ["/start", `intranet.example:${port}`],
+ ["/final", `intranet.example:${port}`],
+ ]);
+
+ const echo = await safeFetch(`http://intranet.example:${port}/echo`, {
+ method: "post",
+ body: "a=1",
+ allowPrivate: true,
+ });
+ assert.equal(await echo.text(), "POST text/plain;charset=UTF-8 a=1");
+});
+
+test("aborting a pending request rejects with an AbortError", async () => {
+ fakeDns("intranet.example", [[LOOPBACK]]);
+ const controller = new AbortController();
+ setTimeout(() => controller.abort(), 100);
+
+ await assert.rejects(
+ safeFetch(`http://intranet.example:${port}/slow`, { allowPrivate: true, signal: controller.signal }),
+ { name: "AbortError" },
+ );
+});
++
++test("literal private IPs are refused by safeFetch itself, since they never reach the lookup hook", async () => {
++ // Disable the sync URL guard so only safeFetch's own literal-IP check is left.
++ const { createRequire } = await import("node:module");
++ const guard = createRequire(import.meta.url)("../dist/util/urlGuard.js");
++ const original = guard.assertSafeUrl;
++ guard.assertSafeUrl = (url) => new URL(url);
++ const before = hits.length;
++ try {
++ for (const host of ["127.0.0.1", "[::1]", "[::ffff:7f00:1]", "0.0.0.0", "169.254.169.254"]) {
++ await assert.rejects(safeFetch(`http://${host}:${port}/`), /Refusing private/, host);
++ }
++ assert.equal(hits.length, before, "no request may reach the loopback server");
++ } finally {
++ guard.assertSafeUrl = original;
++ }
++});