Skip to content

armgit / eve

Public Imported from GitHub
0

The Open Framework for Building Agents

fix(eve): web_fetch - follow safe redirects (#2510)

Andrew Barba94a09529f09b
diff --git a/.changeset/safe-web-fetch-redirects.md b/.changeset/safe-web-fetch-redirects.md
new file mode 100644
index 000000000..081a97917
--- /dev/null
+++ b/.changeset/safe-web-fetch-redirects.md
@@ -0,0 +1,5 @@
+---
+"eve": patch
+---
+
+Follow up to ten `web_fetch` redirects while rechecking each destination for SSRF safety. Non-success HTTP responses now return a plain-text failure result with the response body when available instead of failing the tool call.
diff --git a/docs/concepts/built-in-tools.md b/docs/concepts/built-in-tools.md
index 6f2a90582..6e79cdf72 100644
--- a/docs/concepts/built-in-tools.md
+++ b/docs/concepts/built-in-tools.md
@@ -32,6 +32,7 @@ Notes:
- **`load_skill`** only pulls instructions into context. It adds no new execution surface, because behavior still comes from the tools the agent already has.
- **`connection_search`** surfaces a connection's tools by their qualified name (e.g. `linear__list_issues`), which the model can then call directly. It's registered only when the agent has connections.
- **`web_search`** has no local executor; the provider runs it. AI Gateway models use Exa by default. To use Parallel instead, export `webSearch({ provider: "parallel" })` from `agent/tools/web_search.ts`. Direct provider models continue to use their native search implementation. To supply your own implementation, override it with `defineTool()`.
+- **`web_fetch`** follows up to ten redirects, rechecking every destination for SSRF safety. Non-success HTTP responses return a plain-text failure result with the response body when available instead of failing the tool call.
Review these default tools before production use. Disable, wrap, restrict, or require approval for any tool that can access the filesystem, network, shell, or sensitive data.
diff --git a/packages/eve/src/execution/web-fetch/request.test.ts b/packages/eve/src/execution/web-fetch/request.test.ts
index 74e0afa6d..f9aa24aa1 100644
--- a/packages/eve/src/execution/web-fetch/request.test.ts
+++ b/packages/eve/src/execution/web-fetch/request.test.ts
@@ -204,14 +204,14 @@ describe("requestPublicUrl", () => {
expect(fetchDispatcher).toBe(networkMocks.dispatcherInstances[0]);
});
- it("returns private redirect targets without requesting them", async () => {
+ it("rejects 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",
+ "URL must not target localhost, private, link-local, or reserved IP addresses",
);
expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
@@ -234,19 +234,23 @@ describe("requestPublicUrl", () => {
expect(networkMocks.lookup).toHaveBeenCalledTimes(1);
});
- it("returns an absolute HTTPS redirect target for agent follow-up", async () => {
- queueResponse({
- headers: { location: "/article" },
- status: 301,
- });
+ it("follows at most ten relative HTTPS redirects", async () => {
+ for (let index = 1; index <= 11; index++) {
+ queueResponse({
+ headers: { location: `/redirect-${index}` },
+ 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.",
- );
+ const response = await requestPublicUrl("https://example.com/start", REQUEST_OPTIONS);
- expect(networkMocks.fetch).toHaveBeenCalledTimes(1);
- expect(networkMocks.agentClose).toHaveBeenCalledTimes(1);
- expect(networkMocks.lookup).toHaveBeenCalledTimes(1);
+ expect(response.status).toBe(301);
+ expect(networkMocks.fetch).toHaveBeenCalledTimes(11);
+ expect(networkMocks.agentClose).toHaveBeenCalledTimes(11);
+ expect(networkMocks.lookup).toHaveBeenCalledTimes(11);
+ expect(networkMocks.fetch.mock.calls[10]![0]).toEqual(
+ new URL("https://example.com/redirect-10"),
+ );
});
it("rejects declared and streamed bodies over the response limit", async () => {
diff --git a/packages/eve/src/execution/web-fetch/request.ts b/packages/eve/src/execution/web-fetch/request.ts
index f58e74170..48616acd3 100644
--- a/packages/eve/src/execution/web-fetch/request.ts
+++ b/packages/eve/src/execution/web-fetch/request.ts
@@ -7,6 +7,7 @@ import type { Dispatcher1Wrapper } from "undici";
import { isLoopbackHostname, isPrivateOrReservedIpAddress } from "#shared/network-address.js";
const REDIRECT_STATUSES = new Set([301, 302, 303, 307, 308]);
+const MAX_REDIRECTS = 10;
const UNSAFE_DESTINATION_ERROR =
"URL must not target localhost, private, link-local, or reserved IP addresses.";
@@ -32,23 +33,28 @@ export async function requestPublicUrl(
urlText: string,
options: PublicUrlRequestOptions,
): Promise<Response> {
- const url = parseHttpsUrl(urlText);
- const { dispatcher, response } = await requestOnce(url, options);
+ let url = parseHttpsUrl(urlText);
+
+ for (let redirectCount = 0; ; redirectCount++) {
+ const { dispatcher, response } = await requestOnce(url, options);
+
+ try {
+ const location = response.headers.get("location");
+
+ if (
+ location !== null &&
+ REDIRECT_STATUSES.has(response.status) &&
+ redirectCount < MAX_REDIRECTS
+ ) {
+ await cancelResponseBody(response);
+ url = parseHttpsUrl(new URL(location, url).toString());
+ continue;
+ }
- 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();
}
-
- return await consumeResponse(response, options.maxResponseSize);
- } finally {
- await dispatcher.close();
}
}
diff --git a/packages/eve/src/execution/web-fetch/tool.test.ts b/packages/eve/src/execution/web-fetch/tool.test.ts
index e5ffdf795..b28e92ef6 100644
--- a/packages/eve/src/execution/web-fetch/tool.test.ts
+++ b/packages/eve/src/execution/web-fetch/tool.test.ts
@@ -190,12 +190,15 @@ describe("executeWebFetchTool", () => {
expect(result.content).toBe(json);
});
- it("throws on non-ok responses", async () => {
+ it("returns non-ok responses as plain-text failures", async () => {
requestPublicUrlMock.mockResolvedValueOnce(new Response("Not Found", { status: 404 }));
- await expect(executeWebFetchTool({ url: "https://example.com/missing" })).rejects.toThrow(
- "Request failed with status code: 404",
- );
+ await expect(executeWebFetchTool({ url: "https://example.com/missing" })).resolves.toEqual({
+ content: "Request failed with status code: 404\n\nNot Found",
+ contentType: expect.stringContaining("text/plain"),
+ truncated: false,
+ url: "https://example.com/missing",
+ });
});
it("retries with honest user-agent on Cloudflare challenge", async () => {
diff --git a/packages/eve/src/execution/web-fetch/tool.ts b/packages/eve/src/execution/web-fetch/tool.ts
index ffd39668f..234c8daf7 100644
--- a/packages/eve/src/execution/web-fetch/tool.ts
+++ b/packages/eve/src/execution/web-fetch/tool.ts
@@ -91,10 +91,6 @@ export async function executeWebFetchTool(
})
: initial;
- if (!response.ok) {
- throw new Error(`Request failed with status code: ${response.status}`);
- }
-
const buffer = await response.arrayBuffer();
const contentType = response.headers.get("content-type") ?? "";
@@ -111,6 +107,11 @@ export async function executeWebFetchTool(
rawContent = body;
}
+ if (!response.ok) {
+ const failure = `Request failed with status code: ${response.status}`;
+ rawContent = rawContent.length === 0 ? failure : `${failure}\n\n${rawContent}`;
+ }
+
const { output: content, truncated } = truncateHead(rawContent);
return {