Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): prevent SSRF in web_fetch (#1241)

Casey Gowrie83c753e6fcc7
diff --git a/.changeset/safe-web-fetch.md b/.changeset/safe-web-fetch.md
new file mode 100644
index 000000000..76362e91e
--- /dev/null
+++ b/.changeset/safe-web-fetch.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Harden `web_fetch` against SSRF by requiring HTTPS, rejecting non-public destinations during DNS resolution, and returning redirect targets without following them automatically.
diff --git a/e2e/fixtures/agent-tools/evals/static-tools/web-fetch.eval.ts b/e2e/fixtures/agent-tools/evals/static-tools/web-fetch.eval.ts
new file mode 100644
index 000000000..586702be8
--- /dev/null
+++ b/e2e/fixtures/agent-tools/evals/static-tools/web-fetch.eval.ts
@@ -0,0 +1,43 @@
+import { defineEval } from "eve/evals";
+
+const TOOL_NAME = "web_fetch";
+const TARGET_URL = "https://example.com/";
+const CONTENT_MARKER = "Example Domain";
+
+export default defineEval({
+ description: "Framework tools smoke: web_fetch retrieves an HTTPS page.",
+ async test(t) {
+ const turn = await t.send(
+ [
+ `Call \`${TOOL_NAME}\` exactly once with URL ${JSON.stringify(TARGET_URL)} and format "text".`,
+ "Do not call any other tools.",
+ `After the tool returns, reply with the page title: ${CONTENT_MARKER}.`,
+ ].join("\n"),
+ );
+
+ turn.expectOk();
+ turn.calledTool(TOOL_NAME, {
+ count: 1,
+ input: {
+ format: "text",
+ url: TARGET_URL,
+ },
+ output: (value) => {
+ if (typeof value !== "object" || value === null || Array.isArray(value)) {
+ return false;
+ }
+ const result = value as Record<string, unknown>;
+ return (
+ typeof result.content === "string" &&
+ result.content.includes(CONTENT_MARKER) &&
+ typeof result.contentType === "string" &&
+ result.contentType.includes("text/html") &&
+ result.truncated === false &&
+ result.url === TARGET_URL
+ );
+ },
+ });
+ turn.noFailedActions();
+ turn.messageIncludes(CONTENT_MARKER);
+ },
+});
diff --git a/packages/eve/package.json b/packages/eve/package.json
index 8e3a24cda..487bff7b3 100644
--- a/packages/eve/package.json
+++ b/packages/eve/package.json
@@ -299,7 +299,8 @@
"test:vercel": "vitest run --config vitest.vercel.config.ts"
},
"dependencies": {
- "nitro": "3.0.260610-beta"
+ "nitro": "3.0.260610-beta",
+ "undici": "7.28.0"
},
"devDependencies": {
"@ai-sdk/anthropic": "catalog:",
diff --git a/packages/eve/scripts/build-rolldown.mjs b/packages/eve/scripts/build-rolldown.mjs
index 30f10e597..764c4fbbd 100644
--- a/packages/eve/scripts/build-rolldown.mjs
+++ b/packages/eve/scripts/build-rolldown.mjs
@@ -127,7 +127,7 @@ const EXCLUDED_DIRECTORIES = new Set([join("internal", "testing")]);
*
* - Peer dependencies (`ai`, `next`, `react`, `@opentelemetry/api`,
* `braintrust`) — consumers provide the install.
- * - Runtime dependencies (`nitro`) — resolved at
+ * - Runtime dependencies (`nitro`, `undici`) — resolved at
* runtime against the eve installation.
* - Optional peer dependency (`just-bash`) — the opt-in local sandbox
* engine; resolved lazily against the consumer's install and never
@@ -149,6 +149,7 @@ const EXTERNAL_PACKAGES = new Set([
"nitro",
"react",
"svelte",
+ "undici",
"vite",
"vue",
]);
diff --git a/packages/eve/src/execution/web-fetch/request.test.ts b/packages/eve/src/execution/web-fetch/request.test.ts
new file mode 100644
index 000000000..ddf4539e0
--- /dev/null
+++ b/packages/eve/src/execution/web-fetch/request.test.ts
@@ -0,0 +1,286 @@
+import type { LookupAddress } from "node:dns";
+import { isIP, type LookupFunction } from "node:net";
+
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+import { requestPublicUrl } from "#execution/web-fetch/request.js";
+
+const networkMocks = vi.hoisted(() => ({
+ agentClose: vi.fn(),
+ agentOptions: [] as unknown[],
+ fetch: vi.fn(),
+ lookup: vi.fn(),
+ socketAddresses: [] as unknown[],
+}));
+
+vi.mock("node:dns", () => ({
+ lookup: networkMocks.lookup,
+}));
+
+vi.mock("undici", () => ({
+ Agent: class MockAgent {
+ readonly options: unknown;
+
+ constructor(options: unknown) {
+ this.options = options;
+ networkMocks.agentOptions.push(options);
+ }
+
+ close(): Promise<void> {
+ return networkMocks.agentClose();
+ }
+ },
+}));
+
+const REQUEST_OPTIONS = {
+ headers: { Accept: "text/plain" },
+ maxResponseSize: 100,
+ signal: new AbortController().signal,
+} as const;
+
+interface MockResponse {
+ readonly body?: string;
+ readonly headers?: Readonly<Record<string, string>>;
+ readonly status?: number;
+}
+
+beforeEach(() => {
+ networkMocks.agentClose.mockReset();
+ networkMocks.agentClose.mockResolvedValue(undefined);
+ networkMocks.agentOptions.length = 0;
+ networkMocks.fetch.mockReset();
+ vi.stubGlobal("fetch", networkMocks.fetch);
+ networkMocks.lookup.mockReset();
+ networkMocks.lookup.mockImplementation(
+ (
+ _hostname: string,
+ _options: unknown,
+ callback: (error: Error | null, addresses: LookupAddress[]) => void,
+ ) => callback(null, [{ address: "93.184.216.34", family: 4 }]),
+ );
+ networkMocks.socketAddresses.length = 0;
+});
+
+afterEach(() => {
+ vi.unstubAllGlobals();
+});
+
+describe("requestPublicUrl", () => {
+ it("rejects HTTP and non-network URLs", async () => {
+ for (const url of ["http://example.com", "file:///etc/passwd"]) {
+ await expect(requestPublicUrl(url, REQUEST_OPTIONS), url).rejects.toThrow(
+ "URL must start with https://",
+ );
+ }
+
+ expect(networkMocks.lookup).not.toHaveBeenCalled();
+ expect(networkMocks.fetch).not.toHaveBeenCalled();
+ });
+
+ it("rejects direct loopback, private, link-local, and address-translation targets", async () => {
+ for (const url of [
+ "https://127.0.0.1",
+ "https://2130706433",
+ "https://10.0.0.1",
+ "https://169.254.169.254/latest/meta-data",
+ "https://[::1]",
+ "https://[fc00::1]",
+ "https://[::ffff:7f00:1]",
+ "https://[::ffff:c0a8:1]",
+ "https://[64:ff9b::a9fe:a9fe]",
+ ]) {
+ await expect(requestPublicUrl(url, REQUEST_OPTIONS), url).rejects.toThrow(
+ "URL must not target localhost, private, link-local, or reserved IP addresses",
+ );
+ }
+
+ expect(networkMocks.lookup).not.toHaveBeenCalled();
+ expect(networkMocks.fetch).not.toHaveBeenCalled();
+ });
+
+ it("rejects localhost names before DNS resolution", async () => {
+ for (const url of [
+ "https://localhost",
+ "https://localhost.",
+ "https://service.localhost",
+ "https://service.localhost.",
+ ]) {
+ await expect(requestPublicUrl(url, REQUEST_OPTIONS), url).rejects.toThrow(
+ "URL must not target localhost",
+ );
+ }
+
+ expect(networkMocks.lookup).not.toHaveBeenCalled();
+ });
+
+ it("rejects a hostname when any resolved address is private", async () => {
+ queueLookup([
+ { address: "93.184.216.34", family: 4 },
+ { address: "10.0.0.8", family: 4 },
+ ]);
+ queueResponse({});
+
+ await expect(requestPublicUrl("https://example.com", REQUEST_OPTIONS)).rejects.toThrow(
+ "URL must not target localhost, private, link-local, or reserved IP addresses",
+ );
+
+ expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(1);
+ });
+
+ it("resolves and validates addresses inside the socket lookup", async () => {
+ queueLookup([
+ { address: "2606:2800:220:1:248:1893:25c8:1946", family: 6 },
+ { address: "93.184.216.34", family: 4 },
+ ]);
+ queueResponse({
+ body: "public content",
+ headers: { "content-type": "text/plain" },
+ });
+
+ const response = await requestPublicUrl("https://example.com/page", REQUEST_OPTIONS);
+
+ expect(await response.text()).toBe("public content");
+ expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(1);
+
+ const agentOptions = readAgentOptions(0);
+ expect(agentOptions.connect.lookup).toBeTypeOf("function");
+ expect(networkMocks.socketAddresses).toEqual([
+ [
+ { address: "2606:2800:220:1:248:1893:25c8:1946", family: 6 },
+ { address: "93.184.216.34", family: 4 },
+ ],
+ ]);
+ expect(networkMocks.lookup).toHaveBeenCalledWith(
+ "example.com",
+ {
+ all: true,
+ verbatim: true,
+ },
+ expect.any(Function),
+ );
+
+ const [, fetchOptions] = networkMocks.fetch.mock.calls[0]!;
+ expect(fetchOptions).toMatchObject({
+ dispatcher: expect.anything(),
+ redirect: "manual",
+ });
+ });
+
+ it("returns private redirect targets without requesting them", async () => {
+ queueResponse({
+ headers: { location: "https://169.254.169.254/latest/meta-data" },
+ status: 302,
+ });
+
+ await expect(requestPublicUrl("https://example.com", REQUEST_OPTIONS)).rejects.toThrow(
+ "Request redirected to https://169.254.169.254/latest/meta-data. Call web_fetch again",
+ );
+
+ expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(1);
+ expect(networkMocks.lookup).toHaveBeenCalledTimes(1);
+ });
+
+ it("rejects redirects that downgrade to HTTP", async () => {
+ queueResponse({
+ headers: { location: "http://example.com/insecure" },
+ status: 302,
+ });
+
+ await expect(requestPublicUrl("https://example.com", REQUEST_OPTIONS)).rejects.toThrow(
+ "URL must start with https://",
+ );
+
+ expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(1);
+ expect(networkMocks.lookup).toHaveBeenCalledTimes(1);
+ });
+
+ it("returns an absolute HTTPS redirect target for agent follow-up", async () => {
+ queueResponse({
+ headers: { location: "/article" },
+ status: 301,
+ });
+
+ await expect(requestPublicUrl("https://example.com/start", REQUEST_OPTIONS)).rejects.toThrow(
+ "Request redirected to https://example.com/article. Call web_fetch again with that URL.",
+ );
+
+ expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(1);
+ expect(networkMocks.lookup).toHaveBeenCalledTimes(1);
+ });
+
+ it("rejects declared and streamed bodies over the response limit", async () => {
+ queueResponse({
+ headers: { "content-length": "101" },
+ });
+
+ await expect(requestPublicUrl("https://example.com/declared", REQUEST_OPTIONS)).rejects.toThrow(
+ "Response too large",
+ );
+
+ queueResponse({
+ body: "x".repeat(101),
+ });
+
+ await expect(requestPublicUrl("https://example.com/streamed", REQUEST_OPTIONS)).rejects.toThrow(
+ "Response too large",
+ );
+
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(2);
+ });
+});
+
+function queueResponse(response: MockResponse): void {
+ networkMocks.fetch.mockImplementationOnce(async (input: URL, options: RequestInit) => {
+ await connectThroughDispatcher(input, options);
+ return new Response(response.body ?? "", {
+ headers: response.headers,
+ status: response.status ?? 200,
+ });
+ });
+}
+
+function queueLookup(addresses: LookupAddress[]): void {
+ networkMocks.lookup.mockImplementationOnce(
+ (
+ _hostname: string,
+ _options: unknown,
+ callback: (error: Error | null, addresses: LookupAddress[]) => void,
+ ) => callback(null, addresses),
+ );
+}
+
+function readAgentOptions(index: number): { connect: { lookup?: LookupFunction } } {
+ return networkMocks.agentOptions[index] as { connect: { lookup?: LookupFunction } };
+}
+
+function runLookup(publicLookup: LookupFunction | undefined, hostname: string): Promise<unknown> {
+ return new Promise((resolve, reject) => {
+ expect(publicLookup).toBeTypeOf("function");
+
+ publicLookup!(hostname, { all: true }, (error, addresses) => {
+ if (error !== null) {
+ reject(error);
+ return;
+ }
+
+ resolve(addresses);
+ });
+ });
+}
+
+async function connectThroughDispatcher(input: URL, options: RequestInit): Promise<void> {
+ const hostname = input.hostname.replace(/^\[(.*)\]$/u, "$1");
+ if (isIP(hostname) !== 0) {
+ return;
+ }
+
+ const dispatcher = (options as RequestInit & { dispatcher: { options: unknown } }).dispatcher;
+ const agentOptions = dispatcher.options as { connect: { lookup?: LookupFunction } };
+ const addresses = await runLookup(agentOptions.connect.lookup, hostname);
+ networkMocks.socketAddresses.push(addresses);
+}
diff --git a/packages/eve/src/execution/web-fetch/request.ts b/packages/eve/src/execution/web-fetch/request.ts
new file mode 100644
index 000000000..3794e5af0
--- /dev/null
+++ b/packages/eve/src/execution/web-fetch/request.ts
@@ -0,0 +1,229 @@
+import { lookup } from "node:dns";
+import { isIP } from "node:net";
+import type { LookupFunction } from "node:net";
+
+import { Agent } from "undici";
+
+import { isLoopbackHostname, isPrivateOrReservedIpAddress } from "#shared/network-address.js";
+
+const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
+const UNSAFE_DESTINATION_ERROR =
+ "URL must not target localhost, private, link-local, or reserved IP addresses.";
+
+/** Options for an SSRF-safe HTTPS request. */
+export interface PublicUrlRequestOptions {
+ readonly headers: Readonly<Record<string, string>>;
+ readonly maxResponseSize: number;
+ readonly signal: AbortSignal;
+}
+
+/**
+ * Requests an untrusted HTTPS URL while preventing access to non-public
+ * network destinations.
+ *
+ * The socket lookup rejects the hostname if any resolved address is non-public
+ * and connects using those exact results.
+ */
+export async function requestPublicUrl(
+ urlText: string,
+ options: PublicUrlRequestOptions,
+): Promise<Response> {
+ const url = parseHttpsUrl(urlText);
+ const { dispatcher, response } = await requestOnce(url, options);
+
+ try {
+ const location = response.headers.get("location");
+
+ if (location !== null && REDIRECT_STATUSES.has(response.status)) {
+ await cancelResponseBody(response);
+ const redirectUrl = parseHttpsUrl(new URL(location, url).toString());
+ throw new Error(
+ `Request redirected to ${redirectUrl.toString()}. Call web_fetch again with that URL.`,
+ );
+ }
+
+ return await consumeResponse(response, options.maxResponseSize);
+ } finally {
+ await dispatcher.close();
+ }
+}
+
+function parseHttpsUrl(urlText: string): URL {
+ let url: URL;
+
+ try {
+ url = new URL(urlText);
+ } catch {
+ throw new Error("URL must be a valid absolute https:// URL.");
+ }
+
+ if (url.protocol !== "https:") {
+ throw new Error("URL must start with https://");
+ }
+
+ return url;
+}
+
+async function requestOnce(
+ url: URL,
+ options: PublicUrlRequestOptions,
+): Promise<{ readonly dispatcher: Agent; readonly response: Response }> {
+ assertPublicHostname(url.hostname);
+ const dispatcher = new Agent({
+ connect: {
+ lookup: createPublicLookup(),
+ },
+ });
+
+ try {
+ const fetchOptions: RequestInit & { dispatcher: Agent } = {
+ dispatcher,
+ headers: options.headers,
+ redirect: "manual",
+ signal: options.signal,
+ };
+ const response = await fetch(url, fetchOptions);
+ return { dispatcher, response };
+ } catch (error) {
+ await dispatcher.close();
+ throw error;
+ }
+}
+
+function assertPublicHostname(hostname: string): void {
+ const normalizedHostname = hostname.replace(/^\[(.*)\]$/u, "$1");
+ const family = isIP(normalizedHostname);
+
+ if (family !== 0) {
+ assertPublicAddress(normalizedHostname);
+ return;
+ }
+
+ if (isLoopbackHostname(normalizedHostname)) {
+ throw new Error(UNSAFE_DESTINATION_ERROR);
+ }
+}
+
+function assertPublicAddress(address: string): void {
+ if (isPrivateOrReservedIpAddress(address)) {
+ throw new Error(UNSAFE_DESTINATION_ERROR);
+ }
+}
+
+function createPublicLookup(): LookupFunction {
+ return (hostname, lookupOptions, callback) => {
+ lookup(hostname, { all: true, verbatim: true }, (error, addresses) => {
+ if (error !== null) {
+ callback(error, "", 0);
+ return;
+ }
+
+ try {
+ for (const { address } of addresses) {
+ assertPublicAddress(address);
+ }
+ } catch (error) {
+ callback(error as Error, "", 0);
+ return;
+ }
+
+ const matchingAddresses =
+ lookupOptions.family === undefined || lookupOptions.family === 0
+ ? addresses
+ : addresses.filter(({ family }) => family === lookupOptions.family);
+
+ if (matchingAddresses.length === 0) {
+ const error = new Error(
+ `Unable to resolve URL hostname "${hostname}" in the requested IP family.`,
+ );
+ Object.assign(error, { code: "ENOTFOUND" });
+ callback(error, "", 0);
+ return;
+ }
+
+ if (lookupOptions.all === true) {
+ callback(null, matchingAddresses);
+ return;
+ }
+
+ const selected = matchingAddresses[0]!;
+ callback(null, selected.address, selected.family);
+ });
+ };
+}
+
+async function consumeResponse(response: Response, maxResponseSize: number): Promise<Response> {
+ const declaredLength = parseContentLength(response.headers.get("content-length"));
+
+ if (declaredLength !== undefined && declaredLength > maxResponseSize) {
+ await cancelResponseBody(response);
+ throw createResponseTooLargeError();
+ }
+
+ const body = await readBoundedBody(response, maxResponseSize);
+ const responseBody = body.byteLength === 0 ? null : Uint8Array.from(body).buffer;
+
+ return new Response(responseBody, {
+ headers: response.headers,
+ status: response.status,
+ statusText: response.statusText,
+ });
+}
+
+function parseContentLength(value: string | null): number | undefined {
+ if (value === null) {
+ return undefined;
+ }
+
+ const parsed = Number.parseInt(value, 10);
+ return Number.isFinite(parsed) && parsed >= 0 ? parsed : undefined;
+}
+
+async function readBoundedBody(response: Response, maxResponseSize: number): Promise<Uint8Array> {
+ if (response.body === null) {
+ return new Uint8Array();
+ }
+
+ const reader = response.body.getReader();
+ const chunks: Uint8Array[] = [];
+ let byteLength = 0;
+
+ try {
+ while (true) {
+ const { done, value } = await reader.read();
+ if (done) {
+ break;
+ }
+
+ byteLength += value.byteLength;
+ if (byteLength > maxResponseSize) {
+ await reader.cancel();
+ throw createResponseTooLargeError();
+ }
+
+ chunks.push(value);
+ }
+ } finally {
+ reader.releaseLock();
+ }
+
+ const body = new Uint8Array(byteLength);
+ let offset = 0;
+ for (const chunk of chunks) {
+ body.set(chunk, offset);
+ offset += chunk.byteLength;
+ }
+ return body;
+}
+
+async function cancelResponseBody(response: Response): Promise<void> {
+ try {
+ await response.body?.cancel();
+ } catch {
+ // The connection will still be closed with its per-request dispatcher.
+ }
+}
+
+function createResponseTooLargeError(): Error {
+ return new Error("Response too large (exceeds 5 MB limit).");
+}
diff --git a/packages/eve/src/execution/web-fetch/tool.test.ts b/packages/eve/src/execution/web-fetch/tool.test.ts
index be484d262..e5ffdf795 100644
--- a/packages/eve/src/execution/web-fetch/tool.test.ts
+++ b/packages/eve/src/execution/web-fetch/tool.test.ts
@@ -1,9 +1,16 @@
-import { afterEach, describe, expect, it, vi } from "vitest";
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { EVE_PACKAGE_NAME } from "#internal/package-name.js";
import { MAX_OUTPUT_BYTES, MAX_OUTPUT_LINES } from "#execution/sandbox/truncate-output.js";
import { convertHtmlToMarkdown, extractTextFromHtml } from "#execution/web-fetch/html.js";
+import { requestPublicUrl } from "#execution/web-fetch/request.js";
import { executeWebFetchTool } from "#execution/web-fetch/tool.js";
+vi.mock("#execution/web-fetch/request.js", () => ({
+ requestPublicUrl: vi.fn(),
+}));
+
+const requestPublicUrlMock = vi.mocked(requestPublicUrl);
+
describe("convertHtmlToMarkdown", () => {
it("converts headings to ATX-style markdown", () => {
const html = "<h1>Title</h1><h2>Subtitle</h2><p>Body text.</p>";
@@ -100,24 +107,18 @@ describe("extractTextFromHtml", () => {
});
describe("executeWebFetchTool", () => {
- afterEach(() => {
- vi.restoreAllMocks();
+ beforeEach(() => {
+ requestPublicUrlMock.mockReset();
});
- it("rejects URLs that do not start with http:// or https://", async () => {
- await expect(executeWebFetchTool({ url: "ftp://example.com" })).rejects.toThrow(
- "URL must start with http:// or https://",
- );
-
- await expect(executeWebFetchTool({ url: "file:///etc/passwd" })).rejects.toThrow(
- "URL must start with http:// or https://",
- );
+ afterEach(() => {
+ vi.restoreAllMocks();
});
it("fetches a URL and returns content in the default markdown format", async () => {
const html = "<html><body><h1>Hello</h1><p>World</p></body></html>";
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response(html, {
headers: { "Content-Type": "text/html; charset=utf-8" },
status: 200,
@@ -137,7 +138,7 @@ describe("executeWebFetchTool", () => {
it("returns raw HTML when format is html", async () => {
const html = "<html><body><h1>Hello</h1></body></html>";
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response(html, {
headers: { "Content-Type": "text/html" },
status: 200,
@@ -155,7 +156,7 @@ describe("executeWebFetchTool", () => {
it("extracts plain text from HTML when format is text", async () => {
const html = "<html><body><script>evil()</script><h1>Title</h1><p>Body text.</p></body></html>";
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response(html, {
headers: { "Content-Type": "text/html" },
status: 200,
@@ -175,7 +176,7 @@ describe("executeWebFetchTool", () => {
it("returns non-HTML content as-is regardless of format", async () => {
const json = '{"key": "value"}';
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response(json, {
headers: { "Content-Type": "application/json" },
status: 200,
@@ -190,36 +191,21 @@ describe("executeWebFetchTool", () => {
});
it("throws on non-ok responses", async () => {
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(new Response("Not Found", { status: 404 }));
+ requestPublicUrlMock.mockResolvedValueOnce(new Response("Not Found", { status: 404 }));
await expect(executeWebFetchTool({ url: "https://example.com/missing" })).rejects.toThrow(
"Request failed with status code: 404",
);
});
- it("throws when Content-Length exceeds the 5 MB limit", async () => {
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
- new Response("", {
- headers: { "Content-Length": String(6 * 1024 * 1024) },
- status: 200,
- }),
- );
-
- await expect(executeWebFetchTool({ url: "https://example.com/large" })).rejects.toThrow(
- "Response too large",
- );
- });
-
it("retries with honest user-agent on Cloudflare challenge", async () => {
- const fetchSpy = vi.spyOn(globalThis, "fetch");
-
- fetchSpy.mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response("Blocked", {
headers: { "cf-mitigated": "challenge" },
status: 403,
}),
);
- fetchSpy.mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response("OK content", {
headers: { "Content-Type": "text/plain" },
status: 200,
@@ -231,19 +217,17 @@ describe("executeWebFetchTool", () => {
})) as { content: string };
expect(result.content).toBe("OK content");
- expect(fetchSpy).toHaveBeenCalledTimes(2);
+ expect(requestPublicUrlMock).toHaveBeenCalledTimes(2);
- const retryCall = fetchSpy.mock.calls[1];
+ const retryCall = requestPublicUrlMock.mock.calls[1];
expect(retryCall).toBeDefined();
- const retryHeaders = (retryCall![1] as RequestInit).headers as Record<string, string>;
+ const retryHeaders = retryCall![1].headers;
expect(retryHeaders["User-Agent"]).toBe(EVE_PACKAGE_NAME);
});
it("sends format-aware Accept headers", async () => {
- const fetchSpy = vi.spyOn(globalThis, "fetch");
-
- fetchSpy.mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response("content", {
headers: { "Content-Type": "text/plain" },
status: 200,
@@ -255,9 +239,9 @@ describe("executeWebFetchTool", () => {
url: "https://example.com",
});
- const firstCall = fetchSpy.mock.calls[0];
+ const firstCall = requestPublicUrlMock.mock.calls[0];
expect(firstCall).toBeDefined();
- const headers = (firstCall![1] as RequestInit).headers as Record<string, string>;
+ const headers = firstCall![1].headers;
expect(headers.Accept).toContain("text/markdown");
});
@@ -265,7 +249,7 @@ describe("executeWebFetchTool", () => {
it("truncates large bodies to the shared tool-output budget", async () => {
const huge = "line of content\n".repeat(MAX_OUTPUT_LINES + 500);
- vi.spyOn(globalThis, "fetch").mockResolvedValueOnce(
+ requestPublicUrlMock.mockResolvedValueOnce(
new Response(huge, {
headers: { "Content-Type": "text/plain" },
status: 200,
diff --git a/packages/eve/src/execution/web-fetch/tool.ts b/packages/eve/src/execution/web-fetch/tool.ts
index 0a10928ed..ffd39668f 100644
--- a/packages/eve/src/execution/web-fetch/tool.ts
+++ b/packages/eve/src/execution/web-fetch/tool.ts
@@ -1,6 +1,7 @@
import { EVE_PACKAGE_NAME } from "#internal/package-name.js";
import { truncateHead } from "#execution/sandbox/truncate-output.js";
import { convertHtmlToMarkdown, extractTextFromHtml } from "#execution/web-fetch/html.js";
+import { requestPublicUrl } from "#execution/web-fetch/request.js";
const MAX_RESPONSE_SIZE = 5 * 1024 * 1024; // 5 MB
const DEFAULT_TIMEOUT_MS = 30_000; // 30 seconds
@@ -60,10 +61,6 @@ export async function executeWebFetchTool(
): Promise<WebFetchResult> {
const { url, format = "markdown", timeout } = args;
- if (!url.startsWith("http://") && !url.startsWith("https://")) {
- throw new Error("URL must start with http:// or https://");
- }
-
const timeoutMs = Math.min(
timeout !== undefined ? timeout * 1000 : DEFAULT_TIMEOUT_MS,
MAX_TIMEOUT_MS,
@@ -76,15 +73,20 @@ export async function executeWebFetchTool(
: AbortSignal.any([options.abortSignal, timeoutSignal]);
const headers = buildHeaders(format);
- const initial = await fetch(url, { headers, signal });
+ const initial = await requestPublicUrl(url, {
+ headers,
+ maxResponseSize: MAX_RESPONSE_SIZE,
+ signal,
+ });
// Cloudflare may reject browser-like UA strings from Node.js runtimes
// because the TLS fingerprint does not match a real browser. Retry with
// an honest UA to bypass the challenge.
const response =
initial.status === 403 && initial.headers.get("cf-mitigated") === "challenge"
- ? await fetch(url, {
+ ? await requestPublicUrl(url, {
headers: { ...headers, "User-Agent": EVE_PACKAGE_NAME },
+ maxResponseSize: MAX_RESPONSE_SIZE,
signal,
})
: initial;
@@ -93,18 +95,8 @@ export async function executeWebFetchTool(
throw new Error(`Request failed with status code: ${response.status}`);
}
- const declaredLength = response.headers.get("content-length");
-
- if (declaredLength !== null && parseInt(declaredLength, 10) > MAX_RESPONSE_SIZE) {
- throw new Error("Response too large (exceeds 5 MB limit).");
- }
-
const buffer = await response.arrayBuffer();
- if (buffer.byteLength > MAX_RESPONSE_SIZE) {
- throw new Error("Response too large (exceeds 5 MB limit).");
- }
-
const contentType = response.headers.get("content-type") ?? "";
const isHtml = contentType.includes("text/html");
const body = new TextDecoder().decode(buffer);
diff --git a/packages/eve/src/runtime/framework-tools/web-fetch.ts b/packages/eve/src/runtime/framework-tools/web-fetch.ts
index ab252db71..37fb1fa08 100644
--- a/packages/eve/src/runtime/framework-tools/web-fetch.ts
+++ b/packages/eve/src/runtime/framework-tools/web-fetch.ts
@@ -16,9 +16,7 @@ export const WEB_FETCH_INPUT_SCHEMA = z.strictObject({
)
.optional(),
timeout: z.number().describe("Optional timeout in seconds. Defaults to 30, max 120.").optional(),
- url: z
- .string()
- .describe("The fully-formed URL to fetch content from. Must start with http:// or https://."),
+ url: z.string().describe("The fully-formed URL to fetch content from. Must start with https://."),
});
export const WEB_FETCH_OUTPUT_SCHEMA = z.strictObject({
@@ -33,7 +31,7 @@ export const WEB_FETCH_TOOL_DEFINITION: ResolvedToolDefinition = {
"Fetch a webpage and return its content in the requested format. Use this to retrieve and analyze content from URLs.",
"",
"Usage notes:",
- "- The URL must be a fully-formed valid URL starting with http:// or https://",
+ "- The URL must be a fully-formed valid URL starting with https://",
"- HTML responses are automatically converted to markdown or plain text based on the requested format",
'- Format options: "markdown" (default), "text", or "html"',
"- Default timeout is 30 seconds (max 120 seconds)",
diff --git a/packages/eve/src/shared/network-address.test.ts b/packages/eve/src/shared/network-address.test.ts
index 5ad147eba..63bff9311 100644
--- a/packages/eve/src/shared/network-address.test.ts
+++ b/packages/eve/src/shared/network-address.test.ts
@@ -3,12 +3,23 @@ import { describe, expect, it } from "vitest";
import {
isLoopbackHostname,
isLoopbackServerUrl,
+ isPrivateOrReservedIpAddress,
isReservedIpAddress,
} from "#shared/network-address.js";
describe("isLoopbackHostname", () => {
it("accepts the IPv4 loopback block, IPv6 loopback, and the localhost namespace", () => {
- for (const host of ["localhost", "app.localhost", "127.0.0.1", "127.1.2.3", "::1", "[::1]"]) {
+ for (const host of [
+ "localhost",
+ "localhost.",
+ "app.localhost",
+ "app.localhost.",
+ "127.0.0.1",
+ "127.1.2.3",
+ "::1",
+ "[::1]",
+ "::ffff:7f00:1",
+ ]) {
expect(isLoopbackHostname(host), host).toBe(true);
}
});
@@ -41,7 +52,7 @@ describe("isLoopbackServerUrl", () => {
});
describe("isReservedIpAddress", () => {
- it("blocks link-local (cloud metadata), private, CGNAT, ULA, and unspecified addresses", () => {
+ it("blocks non-public IPv4 and IPv6 address ranges", () => {
for (const host of [
"169.254.169.254", // cloud metadata (link-local)
"10.0.0.1",
@@ -53,6 +64,12 @@ describe("isReservedIpAddress", () => {
"[fc00::1]", // IPv6 ULA
"[::]",
"::ffff:169.254.169.254", // IPv4-mapped IPv6 must not bypass the IPv4 ranges
+ "::ffff:a9fe:a9fe", // hexadecimal IPv4-mapped form
+ "64:ff9b::a9fe:a9fe", // NAT64 must not translate to cloud metadata
+ "192.0.2.1", // documentation
+ "224.0.0.1", // multicast
+ "2001:db8::1", // documentation
+ "ff02::1", // multicast
]) {
expect(isReservedIpAddress(host), host).toBe(true);
}
@@ -70,3 +87,17 @@ describe("isReservedIpAddress", () => {
}
});
});
+
+describe("isPrivateOrReservedIpAddress", () => {
+ it("also blocks the IPv4 and IPv6 loopback ranges", () => {
+ for (const host of ["127.0.0.1", "127.200.1.2", "::1", "[::1]", "::ffff:7f00:1"]) {
+ expect(isPrivateOrReservedIpAddress(host), host).toBe(true);
+ }
+ });
+
+ it("allows public addresses", () => {
+ for (const host of ["8.8.8.8", "1.1.1.1", "2606:4700:4700::1111"]) {
+ expect(isPrivateOrReservedIpAddress(host), host).toBe(false);
+ }
+ });
+});
diff --git a/packages/eve/src/shared/network-address.ts b/packages/eve/src/shared/network-address.ts
index 59c79ac5c..ed129898e 100644
--- a/packages/eve/src/shared/network-address.ts
+++ b/packages/eve/src/shared/network-address.ts
@@ -21,11 +21,30 @@ reservedRanges.addSubnet("10.0.0.0", 8, "ipv4"); // RFC1918 private
reservedRanges.addSubnet("100.64.0.0", 10, "ipv4"); // RFC6598 carrier-grade NAT
reservedRanges.addSubnet("169.254.0.0", 16, "ipv4"); // link-local incl. cloud metadata
reservedRanges.addSubnet("172.16.0.0", 12, "ipv4"); // RFC1918 private
+reservedRanges.addSubnet("192.0.0.0", 24, "ipv4"); // IETF protocol assignments
+reservedRanges.addSubnet("192.0.2.0", 24, "ipv4"); // documentation
+reservedRanges.addSubnet("192.88.99.0", 24, "ipv4"); // deprecated 6to4 relay anycast
reservedRanges.addSubnet("192.168.0.0", 16, "ipv4"); // RFC1918 private
reservedRanges.addSubnet("198.18.0.0", 15, "ipv4"); // RFC2544 benchmarking
+reservedRanges.addSubnet("198.51.100.0", 24, "ipv4"); // documentation
+reservedRanges.addSubnet("203.0.113.0", 24, "ipv4"); // documentation
+reservedRanges.addSubnet("224.0.0.0", 4, "ipv4"); // multicast
+reservedRanges.addSubnet("240.0.0.0", 4, "ipv4"); // reserved and limited broadcast
reservedRanges.addAddress("::", "ipv6"); // unspecified
+reservedRanges.addSubnet("::", 96, "ipv6"); // IPv4-compatible addresses
+reservedRanges.addSubnet("64:ff9b::", 96, "ipv6"); // well-known NAT64 prefix
+reservedRanges.addSubnet("64:ff9b:1::", 48, "ipv6"); // local-use NAT64 prefix
+reservedRanges.addSubnet("100::", 64, "ipv6"); // discard-only
+reservedRanges.addSubnet("2001::", 23, "ipv6"); // special-purpose
+reservedRanges.addSubnet("2001:db8::", 32, "ipv6"); // documentation
+reservedRanges.addSubnet("2002::", 16, "ipv6"); // 6to4
reservedRanges.addSubnet("fc00::", 7, "ipv6"); // unique-local
reservedRanges.addSubnet("fe80::", 10, "ipv6"); // link-local
+reservedRanges.addSubnet("ff00::", 8, "ipv6"); // multicast
+
+const loopbackRanges = new BlockList();
+loopbackRanges.addSubnet("127.0.0.0", 8, "ipv4");
+loopbackRanges.addAddress("::1", "ipv6");
function normalizeAddress(host: string): string {
const withoutBrackets = host.trim().replace(/^\[(.*)\]$/u, "$1");
@@ -50,18 +69,14 @@ function normalizeAddress(host: string): string {
* loopback connect targets.
*/
export function isLoopbackHostname(hostname: string): boolean {
- const normalized = normalizeAddress(hostname).toLowerCase();
+ const normalized = normalizeAddress(hostname).toLowerCase().replace(/\.$/u, "");
if (normalized === "localhost" || normalized.endsWith(".localhost")) {
return true;
}
const family = isIP(normalized);
- if (family === 4) {
- return normalized.startsWith("127.");
- }
-
- return family === 6 && normalized === "::1";
+ return family !== 0 && loopbackRanges.check(normalized, family === 4 ? "ipv4" : "ipv6");
}
/** Returns whether `urlText` is an HTTP(S) URL with a loopback hostname. */
@@ -82,9 +97,23 @@ export function isLoopbackServerUrl(urlText: string): boolean {
*/
export function isReservedIpAddress(host: string): boolean {
const normalized = normalizeAddress(host);
+
+ if (isLoopbackHostname(normalized)) {
+ return false;
+ }
+
const family = isIP(normalized);
if (family === 0) {
return false;
}
return reservedRanges.check(normalized, family === 4 ? "ipv4" : "ipv6");
}
+
+/**
+ * Whether `host` is an IP literal that must not be used as the destination of
+ * an untrusted outbound request. Unlike {@link isReservedIpAddress}, this also
+ * blocks loopback addresses.
+ */
+export function isPrivateOrReservedIpAddress(host: string): boolean {
+ return isLoopbackHostname(host) || isReservedIpAddress(host);
+}
diff --git a/packages/eve/test/runtime-agent-graph.test.ts b/packages/eve/test/runtime-agent-graph.test.ts
index 24913beaf..1f1f6c78e 100644
--- a/packages/eve/test/runtime-agent-graph.test.ts
+++ b/packages/eve/test/runtime-agent-graph.test.ts
@@ -320,7 +320,7 @@ describe("resolveRuntimeAgentGraph", () => {
"Fetch a webpage and return its content in the requested format. Use this to retrieve and analyze content from URLs.",
"",
"Usage notes:",
- "- The URL must be a fully-formed valid URL starting with http:// or https://",
+ "- The URL must be a fully-formed valid URL starting with https://",
"- HTML responses are automatically converted to markdown or plain text based on the requested format",
'- Format options: "markdown" (default), "text", or "html"',
"- Default timeout is 30 seconds (max 120 seconds)",
@@ -407,7 +407,7 @@ describe("resolveRuntimeAgentGraph", () => {
"Fetch a webpage and return its content in the requested format. Use this to retrieve and analyze content from URLs.",
"",
"Usage notes:",
- "- The URL must be a fully-formed valid URL starting with http:// or https://",
+ "- The URL must be a fully-formed valid URL starting with https://",
"- HTML responses are automatically converted to markdown or plain text based on the requested format",
'- Format options: "markdown" (default), "text", or "html"',
"- Default timeout is 30 seconds (max 120 seconds)",
diff --git a/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts b/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts
index 3b5958c25..4a0c634cf 100644
--- a/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts
+++ b/packages/eve/test/scenarios/app-runtime-dependencies.scenario.test.ts
@@ -723,7 +723,7 @@ describe("app runtime dependency tracing", () => {
"This tool requires sandbox access on the runtime context.",
);
expect(serverModuleSource).toContain("The dynamic skill");
- expect(serverModuleSource).toContain("URL must start with http:// or https://");
+ expect(serverModuleSource).toContain("URL must start with https://");
}, 30_000);
it("does not bundle local-only runtime infrastructure into hosted Vercel output", async () => {
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 2c51fbaae..928745fe0 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -1124,6 +1124,9 @@ importers:
nitro:
specifier: 3.0.260610-beta
+ undici:
+ specifier: 7.28.0
+ version: 7.28.0
devDependencies:
'@ai-sdk/anthropic':
specifier: 'catalog:'