| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073 |
- import { describe, expect, it, vi, beforeEach } from "vitest";
- import {
- createModelId,
- createModelProviderId,
- createHttpClientError,
- createSessionId,
- createTurnId,
- type HttpClientPort,
- type HttpClientResponse,
- type Model,
- type SessionEvent,
- } from "@zcode/contracts";
- import {
- clearWebFetchCacheForTests,
- webFetchHandler,
- webFetchToolEntry,
- } from "../src/tool/handlers/webfetch.js";
- import type { ToolExecutionContext } from "../src/tool/types.js";
- import { createTestModelFormatProperties } from "./test-runtime-model.js";
- const dnsLookupMock = vi.hoisted(() => vi.fn());
- vi.mock("node:dns/promises", () => ({
- lookup: dnsLookupMock,
- }));
- describe("WebFetch tool", () => {
- beforeEach(() => {
- clearWebFetchCacheForTests();
- dnsLookupMock.mockReset();
- dnsLookupMock.mockRejectedValue(new Error("DNS lookup should not run for WebFetch"));
- });
- it("upgrades explicit http URLs before fetching and answers with the active model", async () => {
- const httpClient = createHttpClient([
- response({
- body: "<html><body><h1>Install</h1><p>Run <strong>zcode</strong>.</p></body></html>",
- url: "https://example.com:12345/docs",
- }),
- ]);
- const model = createTestModel("Use zcode.");
- const output = await webFetchHandler(
- {
- url: "http://example.com:12345/docs",
- prompt: "What should I run?",
- },
- createContext({ httpClient, model }),
- );
- expect(httpClient.request).toHaveBeenCalledWith(
- expect.objectContaining({
- headers: {
- Accept: "text/markdown, text/html, */*",
- "User-Agent": expect.any(String),
- },
- method: "GET",
- redirect: "manual",
- timeoutMs: 60_000,
- url: "https://example.com:12345/docs",
- }),
- expect.any(Object),
- );
- expect(model.generateText).toHaveBeenCalledWith(
- expect.objectContaining({
- messages: expect.arrayContaining([
- expect.objectContaining({
- content: expect.stringContaining("# Install"),
- role: "user",
- }),
- ]),
- tools: [],
- }),
- );
- const processingRequest = vi.mocked(model.generateText).mock.calls[0]?.[0];
- const processingPrompt = processingRequest?.messages
- .map((message) => message.content)
- .join("\n");
- expect(processingPrompt).toContain("Web page content:");
- expect(processingPrompt).toContain("---\n# Install");
- expect(processingPrompt).toContain("\nWhat should I run?\n");
- expect(processingPrompt).toContain(
- "Provide a concise response based only on the content above.",
- );
- expect(processingPrompt).not.toContain(
- "IMPORTANT: WebFetch WILL FAIL for authenticated or private URLs.",
- );
- expect(processingRequest?.options).toEqual({
- maxOutputTokens: 4_096,
- reasoningLevel: "low",
- });
- expect(output).toMatchObject({
- cacheHit: false,
- finalUrl: "https://example.com:12345/docs",
- result: "Use zcode.",
- truncated: false,
- url: "http://example.com:12345/docs",
- });
- expect(webFetchToolEntry.formatModelContent?.(output)).toBe("Use zcode.");
- });
- it("rejects local IP literal URLs before any network request", async () => {
- const httpClient = createHttpClient([]);
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- {
- url: "http://127.0.0.1:12345/status",
- prompt: "Summarize",
- },
- createContext({ httpClient, model }),
- ),
- ).rejects.toMatchObject({
- context: {
- hostname: "127.0.0.1",
- webFetchCode: "webfetch_egress_blocked",
- },
- });
- expect(httpClient.request).not.toHaveBeenCalled();
- expect(dnsLookupMock).not.toHaveBeenCalled();
- expect(model.generateText).not.toHaveBeenCalled();
- });
- it("fetches hostname URLs without DNS preflight", async () => {
- const httpClient = createHttpClient([
- response({
- body: "hostname content",
- contentType: "text/plain",
- url: "https://internal.example.com/status",
- }),
- ]);
- const model = createTestModel("hostname answer");
- const output = await webFetchHandler(
- { url: "https://internal.example.com/status", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output.result).toBe("hostname answer");
- expect(dnsLookupMock).not.toHaveBeenCalled();
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- });
- it("rejects localhost-style hostnames as invalid URLs before fetching", async () => {
- const httpClient = createHttpClient([]);
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- { url: "https://api.localhost/status", prompt: "Summarize" },
- createContext({ httpClient, model }),
- ),
- ).rejects.toMatchObject({
- context: {
- hostname: "api.localhost",
- webFetchCode: "webfetch_invalid_url",
- },
- });
- expect(dnsLookupMock).not.toHaveBeenCalled();
- expect(httpClient.request).not.toHaveBeenCalled();
- expect(model.generateText).not.toHaveBeenCalled();
- });
- it.each([
- ["10.0.0.12", "private IPv4"],
- ["169.254.169.254", "link-local metadata IPv4"],
- ["198.18.0.1", "benchmark IPv4"],
- ])("rejects %s %s literal URLs before fetching", async (address) => {
- const httpClient = createHttpClient([]);
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- { url: `https://${address}/status`, prompt: "Summarize" },
- createContext({ httpClient, model }),
- ),
- ).rejects.toMatchObject({
- context: {
- address,
- hostname: address,
- webFetchCode: "webfetch_egress_blocked",
- },
- });
- expect(dnsLookupMock).not.toHaveBeenCalled();
- expect(httpClient.request).not.toHaveBeenCalled();
- expect(model.generateText).not.toHaveBeenCalled();
- });
- it.each([
- ["::1", "loopback IPv6"],
- ["64:ff9b::a9fe:a9fe", "DNS64 metadata IPv6"],
- ["64:ff9b:1::1", "local-use IPv4/IPv6 translation"],
- ["100::1", "IPv6 discard-only"],
- ["2001:2::1", "IPv6 benchmarking"],
- ["2001:10::1", "ORCHIDv1"],
- ["2001:20::1", "ORCHIDv2"],
- ])("rejects %s %s literal URLs before fetching", async (address) => {
- const httpClient = createHttpClient([]);
- const model = createTestModel("unused");
- const urlAddress = `[${address}]`;
- await expect(
- webFetchHandler(
- { url: `https://${urlAddress}/status`, prompt: "Summarize" },
- createContext({ httpClient, model }),
- ),
- ).rejects.toMatchObject({
- context: {
- address,
- hostname: address,
- webFetchCode: "webfetch_egress_blocked",
- },
- });
- expect(dnsLookupMock).not.toHaveBeenCalled();
- expect(httpClient.request).not.toHaveBeenCalled();
- expect(model.generateText).not.toHaveBeenCalled();
- });
- it("allows public IP literal URLs without DNS preflight", async () => {
- const httpClient = createHttpClient([
- response({
- body: "public literal content",
- contentType: "text/plain",
- url: "https://8.8.8.8/dns",
- }),
- ]);
- const model = createTestModel("public literal answer");
- const output = await webFetchHandler(
- { url: "https://8.8.8.8/dns", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output.result).toBe("public literal answer");
- expect(dnsLookupMock).not.toHaveBeenCalled();
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- });
- it("emits network request status events for outbound HTTP", async () => {
- const events: SessionEvent[] = [];
- const httpClient = createHttpClient([
- response({
- body: "network content",
- contentType: "text/plain",
- url: "https://example.com/network",
- }),
- ]);
- const model = createTestModel("network answer");
- await webFetchHandler(
- { url: "https://example.com/network", prompt: "Summarize" },
- createContext({
- emitEvent: async (event) => {
- events.push(event);
- },
- httpClient,
- model,
- }),
- );
- const payloads = events.map((event) => event.payload as Record<string, unknown>);
- expect(events.map((event) => event.type)).toEqual([
- "network_request_status",
- "network_request_status",
- ]);
- expect(payloads[0]).toMatchObject({
- method: "GET",
- source: "http_client",
- status: "pending",
- toolCallId: "webfetch-call",
- toolName: "WebFetch",
- url: "https://example.com/network",
- });
- expect(payloads[1]).toMatchObject({
- durationMs: 5,
- method: "GET",
- source: "http_client",
- status: "complete",
- statusCode: 200,
- toolCallId: "webfetch-call",
- toolName: "WebFetch",
- url: "https://example.com/network",
- });
- expect(payloads[0]?.requestId).toBe(payloads[1]?.requestId);
- });
- it("emits a network error event when WebFetch fails before a response", async () => {
- const events: SessionEvent[] = [];
- const httpClient: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async () => {
- throw new Error("socket down");
- }),
- };
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- { url: "https://example.com/error", prompt: "Summarize" },
- createContext({
- emitEvent: async (event) => {
- events.push(event);
- },
- httpClient,
- model,
- }),
- ),
- ).rejects.toThrow("socket down");
- const payloads = events.map((event) => event.payload as Record<string, unknown>);
- expect(payloads).toHaveLength(2);
- expect(payloads[0]).toMatchObject({
- status: "pending",
- url: "https://example.com/error",
- });
- expect(payloads[1]).toMatchObject({
- error: "socket down",
- status: "error",
- url: "https://example.com/error",
- });
- expect(payloads[0]?.requestId).toBe(payloads[1]?.requestId);
- });
- it("shows a compact connect timeout cause when WebFetch fails before a response", async () => {
- const events: SessionEvent[] = [];
- const concreteCause = new Error(
- "Connect Timeout Error (attempted address: www.bbc.com:443, timeout: 10000ms)",
- );
- concreteCause.name = "ConnectTimeoutError";
- const fetchCause = new TypeError("fetch failed");
- Object.defineProperty(fetchCause, "cause", {
- configurable: true,
- value: concreteCause,
- });
- const httpClient: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async (request) => {
- throw createHttpClientError({
- cause: fetchCause,
- code: "network_error",
- message: "fetch failed",
- url: request.url,
- });
- }),
- };
- const model = createTestModel("unused");
- let thrown: unknown;
- try {
- await webFetchHandler(
- { url: "https://www.bbc.com/sport/football/world-cup", prompt: "Summarize" },
- createContext({
- emitEvent: async (event) => {
- events.push(event);
- },
- httpClient,
- model,
- }),
- );
- } catch (error) {
- thrown = error;
- }
- expect(thrown).toBeInstanceOf(Error);
- expect((thrown as Error).message).toBe("Connect Timeout Error (443, timeout 10000ms)");
- const payloads = events.map((event) => event.payload as Record<string, unknown>);
- expect(payloads[1]).toMatchObject({
- error: "Connect Timeout Error (443, timeout 10000ms)",
- status: "error",
- url: "https://www.bbc.com/sport/football/world-cup",
- });
- });
- it("includes the deepest network error code when a WebFetch request fails", async () => {
- const events: SessionEvent[] = [];
- const resetCause = Object.assign(new Error("aborted"), { code: "ECONNRESET" });
- const httpClient: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async (request) => {
- throw createHttpClientError({
- cause: resetCause,
- code: "proxy_error",
- message: "aborted",
- url: request.url,
- });
- }),
- };
- const model = createTestModel("unused");
- let thrown: unknown;
- try {
- await webFetchHandler(
- { url: "https://www.goal.com/en/world-cup/schedule-results", prompt: "Summarize" },
- createContext({
- emitEvent: async (event) => {
- events.push(event);
- },
- httpClient,
- model,
- }),
- );
- } catch (error) {
- thrown = error;
- }
- expect(thrown).toBeInstanceOf(Error);
- expect((thrown as Error).message).toBe("aborted (ECONNRESET)");
- const payloads = events.map((event) => event.payload as Record<string, unknown>);
- expect(payloads[1]).toMatchObject({
- error: "aborted (ECONNRESET)",
- status: "error",
- url: "https://www.goal.com/en/world-cup/schedule-results",
- });
- });
- it("does not retry transient connection resets before failing WebFetch", async () => {
- const events: SessionEvent[] = [];
- const resetCause = Object.assign(new Error("aborted"), { code: "ECONNRESET" });
- const httpClient: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async (request) => {
- throw createHttpClientError({
- cause: resetCause,
- code: "proxy_error",
- message: "aborted",
- url: request.url,
- });
- }),
- };
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- {
- url: "https://www.flashscore.com/football/world/world-cup-2026/results/",
- prompt: "Summarize",
- },
- createContext({
- emitEvent: async (event) => {
- events.push(event);
- },
- httpClient,
- model,
- }),
- ),
- ).rejects.toThrow("aborted (ECONNRESET)");
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(events.map((event) => (event.payload as Record<string, unknown>).status)).toEqual([
- "pending",
- "error",
- ]);
- });
- it("reuses cached fetched content while still applying the caller prompt", async () => {
- const httpClient = createHttpClient([
- response({
- body: "# Cached\n\nThe answer is in the page.",
- contentType: "text/plain",
- url: "https://example.com/cache",
- }),
- ]);
- const model = createTestModel("cached answer");
- const context = createContext({ httpClient, model });
- const first = await webFetchHandler(
- { url: "https://example.com/cache", prompt: "First prompt" },
- context,
- );
- const second = await webFetchHandler(
- { url: "https://example.com/cache", prompt: "Second prompt" },
- context,
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).toHaveBeenCalledTimes(2);
- expect(first.cacheHit).toBe(false);
- expect(second.cacheHit).toBe(true);
- });
- it("processes non-preapproved markdown content with the lite model", async () => {
- const httpClient = createHttpClient([
- response({
- body: "# Markdown\n\nThe answer is already readable.",
- contentType: "text/markdown",
- url: "https://example.com/markdown",
- }),
- ]);
- const model = createTestModel("processed markdown");
- const output = await webFetchHandler(
- { url: "https://example.com/markdown", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).toHaveBeenCalledTimes(1);
- expect(output).toMatchObject({
- cacheHit: false,
- contentType: "text/markdown",
- finalUrl: "https://example.com/markdown",
- result: "processed markdown",
- truncated: false,
- });
- });
- it("returns preapproved markdown content directly without invoking the lite model", async () => {
- const httpClient = createHttpClient([
- response({
- body: "# Direct markdown\n\nThe answer is already readable.",
- contentType: "text/markdown",
- url: "https://docs.python.org/3/",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://docs.python.org/3/", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output).toMatchObject({
- cacheHit: false,
- contentType: "text/markdown",
- finalUrl: "https://docs.python.org/3/",
- result: "# Direct markdown\n\nThe answer is already readable.",
- truncated: false,
- });
- });
- it("uses the preapproved processing prompt branch when direct markdown return does not apply", async () => {
- const httpClient = createHttpClient([
- response({
- body: "<html><body><h1>Docs</h1><p>Use pathlib.</p></body></html>",
- contentType: "text/html",
- url: "https://docs.python.org/3/library/pathlib.html",
- }),
- ]);
- const model = createTestModel("pathlib answer");
- await webFetchHandler(
- { url: "https://docs.python.org/3/library/pathlib.html", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- const processingRequest = vi.mocked(model.generateText).mock.calls[0]?.[0];
- const processingPrompt = processingRequest?.messages
- .map((message) => message.content)
- .join("\n");
- expect(processingPrompt).toContain(
- "Provide a concise response based on the content above. Include relevant details, code examples, and documentation excerpts as needed.",
- );
- expect(processingPrompt).not.toContain("strict 125-character maximum");
- });
- it("allows same-host redirects modulo www", async () => {
- const httpClient = createHttpClient([
- response({
- headers: { location: "https://www.example.com/final" },
- status: 301,
- statusText: "Moved Permanently",
- url: "https://example.com/start",
- }),
- response({
- body: "final content",
- contentType: "text/plain",
- url: "https://www.example.com/final",
- }),
- ]);
- const model = createTestModel("final answer");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output.redirects).toEqual([
- {
- from: "https://example.com/start",
- status: 301,
- to: "https://www.example.com/final",
- },
- ]);
- expect(output.finalUrl).toBe("https://www.example.com/final");
- });
- it("returns cross-host redirects as a model-readable tool result", async () => {
- const httpClient = createHttpClient([
- response({
- headers: { location: "https://evil.example/final" },
- status: 302,
- statusText: "Found",
- url: "https://example.com/start",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output).toMatchObject({
- bytes: expect.any(Number),
- contentType: "text/plain",
- finalUrl: "https://example.com/start",
- redirects: [
- {
- from: "https://example.com/start",
- status: 302,
- to: "https://evil.example/final",
- },
- ],
- status: 302,
- statusText: "Found",
- truncated: false,
- url: "https://example.com/start",
- });
- expect(output.result).toBe(
- [
- "REDIRECT DETECTED: The URL redirects to a different host.",
- "",
- "Original URL: https://example.com/start",
- "Redirect URL: https://evil.example/final",
- "Status: 302 Found",
- "",
- "To complete your request, I need to fetch content from the redirected URL. Please use WebFetch again with these parameters:",
- '- url: "https://evil.example/final"',
- '- prompt: "Summarize"',
- ].join("\n"),
- );
- });
- it("uses status-code text for redirect outputs when response status text is empty", async () => {
- const httpClient = createHttpClient([
- response({
- headers: { location: "https://evil.example/final" },
- status: 302,
- statusText: "",
- url: "https://example.com/start",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output.statusText).toBe("Found");
- expect(output.result).toContain("Status: 302 Found");
- });
- it("returns credential redirects as a redacted model-readable tool result", async () => {
- const httpClient = createHttpClient([
- response({
- headers: { location: "https://user:secret@evil.example/final" },
- status: 302,
- statusText: "Found",
- url: "https://example.com/start",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output.result).toContain("REDIRECT DETECTED");
- expect(output.result).toContain('url: "https://evil.example/final"');
- expect(output.result).not.toContain("user:secret");
- expect(output.redirects).toEqual([
- {
- from: "https://example.com/start",
- status: 302,
- to: "https://evil.example/final",
- },
- ]);
- });
- it("returns private-host redirects as a model-readable tool result without fetching them", async () => {
- const httpClient = createHttpClient([
- response({
- headers: { location: "https://localhost/final" },
- status: 302,
- statusText: "Found",
- url: "https://example.com/start",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output.result).toContain("REDIRECT DETECTED");
- expect(output.result).toContain('url: "https://localhost/final"');
- });
- it("returns redirect responses with missing Location as HTTP errors", async () => {
- const httpClient = createHttpClient([
- response({
- body: "redirect error body",
- status: 302,
- statusText: "Found",
- url: "https://example.com/start",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output).toMatchObject({
- bytes: 0,
- contentType: "text/plain",
- finalUrl: "https://example.com/start",
- redirects: [],
- status: 302,
- statusText: "Found",
- truncated: false,
- url: "https://example.com/start",
- });
- expect(output.result).toContain("The server returned HTTP 302 Found.");
- expect(output.result).toContain("The response body was not retrieved.");
- expect(output.result).not.toContain("redirect error body");
- });
- it("does not treat non-followable 3xx responses as redirects", async () => {
- const httpClient = createHttpClient([
- response({
- body: "not modified body",
- headers: { location: "https://evil.example/final" },
- status: 304,
- statusText: "Not Modified",
- url: "https://example.com/start",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/start", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output).toMatchObject({
- bytes: 0,
- finalUrl: "https://example.com/start",
- redirects: [],
- status: 304,
- statusText: "Not Modified",
- });
- expect(output.result).toContain("The server returned HTTP 304 Not Modified.");
- expect(output.result).not.toContain("REDIRECT DETECTED");
- expect(output.result).not.toContain("not modified body");
- });
- it("returns non-2xx responses as HTTP errors without applying the lite model", async () => {
- const httpClient = createHttpClient([
- response({
- body: "not found body",
- status: 404,
- statusText: "Not Found",
- url: "https://example.com/missing",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/missing", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- expect(output).toMatchObject({
- bytes: 0,
- contentType: "text/plain",
- finalUrl: "https://example.com/missing",
- redirects: [],
- status: 404,
- statusText: "Not Found",
- truncated: false,
- url: "https://example.com/missing",
- });
- expect(output.result).toContain("The server returned HTTP 404 Not Found.");
- expect(output.result).toContain("The response body was not retrieved.");
- expect(output.result).not.toContain("not found body");
- });
- it("uses status-code text for HTTP error outputs when response status text is empty", async () => {
- const httpClient = createHttpClient([
- response({
- body: "not found body",
- status: 404,
- statusText: "",
- url: "https://example.com/missing",
- }),
- ]);
- const model = createTestModel("unused");
- const output = await webFetchHandler(
- { url: "https://example.com/missing", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output.statusText).toBe("Not Found");
- expect(output.result).toContain("The server returned HTTP 404 Not Found.");
- });
- it("raises an egress-blocked error before generic HTTP error formatting", async () => {
- const httpClient = createHttpClient([
- response({
- body: "blocked body",
- headers: { "x-proxy-error": "blocked-by-allowlist" },
- status: 403,
- statusText: "Forbidden",
- url: "https://blocked.example/docs",
- }),
- ]);
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- { url: "https://blocked.example/docs", prompt: "Summarize" },
- createContext({ httpClient, model }),
- ),
- ).rejects.toMatchObject({
- context: {
- domain: "blocked.example",
- error_type: "EGRESS_BLOCKED",
- webFetchCode: "webfetch_egress_blocked",
- },
- });
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(model.generateText).not.toHaveBeenCalled();
- });
- it("maps public egress policy blocks to WebFetch egress errors", async () => {
- const events: SessionEvent[] = [];
- const httpClient: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async (request) => {
- throw createHttpClientError({
- code: "egress_blocked",
- message:
- "HTTP public egress blocked metadata.example because it resolved to 169.254.169.254",
- url: request.url,
- });
- }),
- };
- const model = createTestModel("unused");
- const request = webFetchHandler(
- { url: "https://metadata.example/latest", prompt: "Summarize" },
- createContext({
- emitEvent: async (event) => {
- events.push(event);
- },
- httpClient,
- model,
- }),
- );
- await expect(request).rejects.toThrow(
- "HTTP public egress blocked metadata.example because it resolved to a non-public address",
- );
- await expect(request).rejects.toMatchObject({
- context: {
- domain: "metadata.example",
- error_type: "EGRESS_BLOCKED",
- webFetchCode: "webfetch_egress_blocked",
- },
- });
- expect(events.map((event) => event.payload as Record<string, unknown>)[1]).toMatchObject({
- error:
- "HTTP public egress blocked metadata.example because it resolved to a non-public address",
- status: "error",
- });
- expect(model.generateText).not.toHaveBeenCalled();
- });
- it("rejects local hostnames before any network request", async () => {
- const httpClient = createHttpClient([]);
- const model = createTestModel("unused");
- await expect(
- webFetchHandler(
- { url: "https://localhost/status", prompt: "Summarize" },
- createContext({ httpClient, model }),
- ),
- ).rejects.toThrow("public hostname");
- expect(httpClient.request).not.toHaveBeenCalled();
- });
- it("rejects URLs longer than the WebFetch runtime limit", async () => {
- const httpClient = createHttpClient([]);
- const model = createTestModel("unused");
- const longUrl = `https://example.com/${"a".repeat(2001)}`;
- await expect(
- webFetchHandler({ url: longUrl, prompt: "Summarize" }, createContext({ httpClient, model })),
- ).rejects.toThrow("URL is too long");
- expect(httpClient.request).not.toHaveBeenCalled();
- });
- it("skips external domain safety preflight and fetches the target URL directly", async () => {
- const httpClient = createHttpClient([
- response({
- body: "target content wins over domain safety service",
- contentType: "text/plain",
- url: "https://blocked.example/docs",
- }),
- ]);
- const model = createTestModel("target answer");
- const output = await webFetchHandler(
- { url: "https://blocked.example/docs", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output).toMatchObject({
- finalUrl: "https://blocked.example/docs",
- result: "target answer",
- });
- expect(httpClient.request).toHaveBeenCalledTimes(1);
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- expect(getContentRequestCalls(httpClient)[0]?.[0]).toMatchObject({
- method: "GET",
- url: "https://blocked.example/docs",
- });
- });
- it("does not fail when the old domain safety service is unreachable", async () => {
- const httpClient: HttpClientPort = {
- request: vi.fn<HttpClientPort["request"]>(async (request) => {
- if (request.url !== "https://example.com/docs") {
- throw new Error(`unexpected WebFetch request: ${request.url}`);
- }
- return response({
- body: "reachable target",
- contentType: "text/plain",
- url: request.url,
- });
- }),
- };
- const model = createTestModel("reachable answer");
- const output = await webFetchHandler(
- { url: "https://example.com/docs", prompt: "Summarize" },
- createContext({ httpClient, model }),
- );
- expect(output.result).toBe("reachable answer");
- expect(httpClient.request).toHaveBeenCalledTimes(1);
- expect(getContentRequestCalls(httpClient)).toHaveLength(1);
- });
- });
- function createContext(options: {
- emitEvent?: (event: SessionEvent) => Promise<void>;
- httpClient: HttpClientPort;
- model: Model;
- }): ToolExecutionContext {
- const modelSelection = {
- modelId: createModelId("test-model"),
- providerId: createModelProviderId("test"),
- };
- return {
- abortSignal: new AbortController().signal,
- emitEvent: options.emitEvent,
- httpClientPort: options.httpClient,
- model: options.model,
- modelSelection,
- sessionId: createSessionId("webfetch"),
- toolCallId: "webfetch-call",
- traceId: "trace-webfetch" as never,
- turnId: createTurnId("webfetch"),
- workingDirectory: "/tmp/zcode-webfetch-tool",
- workspaceRoot: "/tmp/zcode-webfetch-tool",
- };
- }
- function createHttpClient(responses: HttpClientResponse[]): HttpClientPort {
- return {
- request: vi.fn<HttpClientPort["request"]>(async () => {
- const next = responses.shift();
- if (!next) {
- throw new Error("No fake WebFetch response was configured");
- }
- return next;
- }),
- };
- }
- type HttpClientRequestCall = Parameters<HttpClientPort["request"]>;
- function getContentRequestCalls(httpClient: HttpClientPort): HttpClientRequestCall[] {
- return vi.mocked(httpClient.request).mock.calls;
- }
- function createTestModel(text: string): Model {
- const model: Model = {
- providerId: createModelProviderId("test"),
- modelId: createModelId("test-model"),
- properties: {
- contextWindow: 200_000,
- ...createTestModelFormatProperties(),
- supportsToolCall: true,
- supportsJsonSchemaOutput: true,
- supportsNativeWebSearch: false,
- supportsMidConversationSystem: true,
- },
- optionSpecs: {
- reasoningLevel: { values: ["low", "high"] },
- maxOutputTokens: { max: 32_000 },
- },
- options: { maxOutputTokens: 32_000, reasoningLevel: "high" },
- bind() {
- return this;
- },
- generateText: vi.fn<Model["generateText"]>(async () => ({
- finishReason: "stop",
- model: { providerId: model.providerId, modelId: model.modelId },
- text,
- usage: {},
- })),
- async *streamText() {
- yield { type: "start" };
- yield { finishReason: "stop", type: "finish", usage: {} };
- },
- };
- return model;
- }
- function response(options: {
- body?: string;
- contentType?: string;
- headers?: Record<string, string>;
- status?: number;
- statusText?: string;
- url: string;
- }): HttpClientResponse {
- const body = new TextEncoder().encode(options.body ?? "");
- const headers = {
- "content-type": options.contentType ?? "text/html; charset=utf-8",
- ...options.headers,
- };
- return {
- body,
- bytes: body.byteLength,
- durationMs: 5,
- headers,
- status: options.status ?? 200,
- statusText: options.statusText ?? "OK",
- url: options.url,
- };
- }
|