Open History to see the last commit
1import { NodeHttpServer } from "@effect/platform-node";
2import { expect, it } from "@effect/vitest";
3import * as Context from "effect/Context";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import { HttpClient, HttpRouter, HttpServer, HttpServerResponse } from "effect/http";
7
8import * as McpWebhookTransport from "./McpWebhookTransport.ts";
9
10it.each([
11 ["127.0.0.1", false],
12 ["10.1.2.3", false],
13 ["172.16.0.1", false],
14 ["192.168.1.1", false],
15 ["169.254.169.254", false],
16 ["100.64.0.1", false],
17 ["0.0.0.0", false],
18 ["::1", false],
19 ["fc00::1", false],
20 ["fe80::1", false],
21 ["::ffff:127.0.0.1", false],
22 ["fec0::1", false],
23 ["5f00::1", false],
24 ["::ffff:0:102:304", false],
25 ["8.8.8.8", true],
26 ["2606:4700:4700::1111", true],
27])("treats %s as globally routable: %s", (address, routable) => {
28 expect(McpWebhookTransport.isGloballyRoutable(address)).toBe(routable);
29});
30
31it.effect("refuses private callbacks unless allowlisted, and the client never reaches them", () =>
32 Effect.gen(function* () {
33 yield* HttpRouter.serve(
34 Layer.mergeAll(
35 HttpRouter.add("POST", "/redirect", HttpServerResponse.redirect("/target")),
36 HttpRouter.add("POST", "/target", HttpServerResponse.text("reached")),
37 ),
38 { disableListenLog: true, disableLogger: true },
39 ).pipe(Layer.build);
40 const port = new URL(HttpServer.formatAddress((yield* HttpServer.HttpServer).address)).port;
41 const guard = McpWebhookTransport.make(["127.0.0.1"]);
42 const client = Context.get(yield* Layer.build(guard.layerClient), HttpClient.HttpClient);
43
44 const refused = (url: string) =>
45 guard.check(new URL(url)).pipe(
46 Effect.flip,
47 Effect.map((error) => error._tag),
48 );
49 expect(yield* refused("https://10.0.0.1/hook")).toBe("WebhookAddressRejected");
50 expect(yield* refused("https://[::1]/hook")).toBe("WebhookAddressRejected");
51 expect(yield* refused("https://localhost/hook")).toBe("WebhookAddressRejected");
52 yield* guard.check(new URL(`http://127.0.0.1:${port}/hook`));
53 expect(guard.allowsPlaintext(new URL("http://127.0.0.1/hook"))).toBe(true);
54 expect(guard.allowsPlaintext(new URL("http://localhost/hook"))).toBe(false);
55
56 // A redirect comes back as it is, never followed.
57 const redirected = yield* client.post(`http://127.0.0.1:${port}/redirect`);
58 expect(redirected.status).toBe(302);
59 // The client resolves again when it connects, so a name for a private address fails there.
60 const pinned = yield* Effect.flip(client.post(`http://localhost:${port}/target`));
61 expect(pinned.reason._tag).toBe("TransportError");
62 expect(String((pinned.reason as { readonly cause?: unknown }).cause)).toContain(
63 "localhost is not a public address",
64 );
65 }).pipe(Effect.scoped, Effect.provide(NodeHttpServer.layerTest)),
66);