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: "
Run zcode.
", 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 RecordUse pathlib.
", 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