|
| 1 | +import { describe, it, expect, beforeEach, afterEach, spyOn } from "bun:test"; |
| 2 | +import { Client } from "@modelcontextprotocol/sdk/client/index.js"; |
| 3 | +import { InMemoryTransport } from "@modelcontextprotocol/sdk/inMemory.js"; |
| 4 | +import { createServer } from "./server"; |
| 5 | + |
| 6 | +function firstText(r: Awaited<ReturnType<Client["callTool"]>>): string { |
| 7 | + return (r.content as Array<{ type: string; text: string }>)[0].text; |
| 8 | +} |
| 9 | + |
| 10 | +describe("wiki-explorer URL validation", () => { |
| 11 | + let server: ReturnType<typeof createServer>; |
| 12 | + let client: Client; |
| 13 | + |
| 14 | + beforeEach(async () => { |
| 15 | + server = createServer(); |
| 16 | + client = new Client({ name: "test", version: "1" }); |
| 17 | + const [ct, st] = InMemoryTransport.createLinkedPair(); |
| 18 | + await Promise.all([server.connect(st), client.connect(ct)]); |
| 19 | + }); |
| 20 | + |
| 21 | + afterEach(async () => { |
| 22 | + await client.close(); |
| 23 | + await server.close(); |
| 24 | + }); |
| 25 | + |
| 26 | + it("rejects non-Wikipedia URLs", async () => { |
| 27 | + const r = await client.callTool({ |
| 28 | + name: "get-first-degree-links", |
| 29 | + arguments: { url: "https://evil.com/wiki/Test" }, |
| 30 | + }); |
| 31 | + const result = JSON.parse(firstText(r)); |
| 32 | + expect(result.error).toBe("Not a valid Wikipedia URL"); |
| 33 | + }); |
| 34 | + |
| 35 | + it("rejects path traversal that escapes /wiki/", async () => { |
| 36 | + // This URL passes the old regex but resolves to /w/api.php (outside /wiki/) |
| 37 | + const r = await client.callTool({ |
| 38 | + name: "get-first-degree-links", |
| 39 | + arguments: { |
| 40 | + url: "https://en.wikipedia.org/wiki/../../w/api.php?action=query&list=allusers", |
| 41 | + }, |
| 42 | + }); |
| 43 | + const result = JSON.parse(firstText(r)); |
| 44 | + expect(result.error).toBe("Not a valid Wikipedia URL"); |
| 45 | + }); |
| 46 | + |
| 47 | + it("rejects path traversal to API endpoints", async () => { |
| 48 | + const r = await client.callTool({ |
| 49 | + name: "get-first-degree-links", |
| 50 | + arguments: { |
| 51 | + url: "https://en.wikipedia.org/wiki/../../../api/rest_v1/feed/featured/2024/01/01", |
| 52 | + }, |
| 53 | + }); |
| 54 | + const result = JSON.parse(firstText(r)); |
| 55 | + expect(result.error).toBe("Not a valid Wikipedia URL"); |
| 56 | + }); |
| 57 | + |
| 58 | + it("accepts valid Wikipedia URLs", async () => { |
| 59 | + // Mock fetch to avoid real network requests |
| 60 | + const mockFetch = spyOn(globalThis, "fetch").mockResolvedValueOnce( |
| 61 | + new Response("<html><body><a href='/wiki/Test'>Test</a></body></html>", { |
| 62 | + status: 200, |
| 63 | + headers: { "Content-Type": "text/html" }, |
| 64 | + }), |
| 65 | + ); |
| 66 | + |
| 67 | + try { |
| 68 | + const r = await client.callTool({ |
| 69 | + name: "get-first-degree-links", |
| 70 | + arguments: { |
| 71 | + url: "https://en.wikipedia.org/wiki/Model_Context_Protocol", |
| 72 | + }, |
| 73 | + }); |
| 74 | + const result = JSON.parse(firstText(r)); |
| 75 | + expect(result.error).toBeNull(); |
| 76 | + expect(result.page.url).toBe( |
| 77 | + "https://en.wikipedia.org/wiki/Model_Context_Protocol", |
| 78 | + ); |
| 79 | + } finally { |
| 80 | + mockFetch.mockRestore(); |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + it("disables redirect following on fetch", async () => { |
| 85 | + // Ensure fetch is called with redirect: 'error' or 'manual' to prevent |
| 86 | + // following redirects to non-Wikipedia domains |
| 87 | + const mockFetch = spyOn(globalThis, "fetch").mockResolvedValueOnce( |
| 88 | + new Response("<html><body></body></html>", { |
| 89 | + status: 200, |
| 90 | + headers: { "Content-Type": "text/html" }, |
| 91 | + }), |
| 92 | + ); |
| 93 | + |
| 94 | + try { |
| 95 | + await client.callTool({ |
| 96 | + name: "get-first-degree-links", |
| 97 | + arguments: { |
| 98 | + url: "https://en.wikipedia.org/wiki/Test_Page", |
| 99 | + }, |
| 100 | + }); |
| 101 | + expect(mockFetch).toHaveBeenCalledTimes(1); |
| 102 | + const fetchArgs = mockFetch.mock.calls[0]; |
| 103 | + // Second argument should have redirect: "error" |
| 104 | + expect(fetchArgs[1]).toBeDefined(); |
| 105 | + expect((fetchArgs[1] as RequestInit).redirect).toBe("error"); |
| 106 | + } finally { |
| 107 | + mockFetch.mockRestore(); |
| 108 | + } |
| 109 | + }); |
| 110 | +}); |
0 commit comments