Skip to content

Commit 17d9f9e

Browse files
cliffhallclaude
andcommitted
fix: survive a malformed request target; publish legacy-era graph writes
A target such as `//[` made `new URL()` throw inside the Node request listener, outside any handler, ending the process. The HTTP endpoints now compare the path without parsing the target, so it is answered 404. Over HTTP, memory now publishes every graph change on the handler's bus whatever era wrote it, so a 2026-07-28 listener hears of a 2025-era client's writes too. Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: cliffhall <[email protected]>
1 parent 311c0fd commit 17d9f9e

7 files changed

Lines changed: 92 additions & 30 deletions

File tree

‎src/filesystem/__tests__/http.test.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// get, and main() refuses to start without one.
88

99
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
10+
import { connect as connectTcp } from "node:net";
1011
import { mkdtemp, realpath, rm, writeFile } from "node:fs/promises";
1112
import { tmpdir } from "node:os";
1213
import path from "node:path";
@@ -138,6 +139,23 @@ describe("startHttpServer", () => {
138139
return running;
139140
}
140141

142+
it("answers a malformed request target 404 and keeps serving", async () => {
143+
const url = new URL((await start()).url);
144+
const reply = await new Promise<string>((resolve, reject) => {
145+
const socket = connectTcp(Number(url.port), url.hostname, () =>
146+
socket.write("GET //[ HTTP/1.1\r\nHost: localhost\r\n\r\n"),
147+
);
148+
socket.setEncoding("utf8");
149+
socket.once("data", (chunk: string) => {
150+
socket.destroy();
151+
resolve(chunk);
152+
});
153+
socket.once("error", reject);
154+
});
155+
expect(reply).toMatch(/^HTTP\/1\.1 404/);
156+
expect((await fetch(new URL("/other", url))).status).toBe(404);
157+
});
158+
141159
it("answers 404 off the MCP path and 403 to a foreign Origin", async () => {
142160
const base = new URL((await start()).url);
143161
expect((await fetch(new URL("/other", base))).status).toBe(404);

‎src/filesystem/http.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ export async function startHttpServer(
8282
const allowOrigin = localhostOriginValidation();
8383

8484
const httpServer = createNodeServer((req, res) => {
85-
// A request a Node server hands its listener always carries a URL.
86-
const path = new URL(req.url!, "http://localhost").pathname;
85+
// The path, without parsing the target: a malformed one (`//[`) would
86+
// make URL parsing throw here, outside any handler, and end the process.
87+
const path = String(req.url).split("?")[0];
8788
if (path !== MCP_PATH) {
8889
res.writeHead(404, { "content-type": "text/plain" }).end("Not Found");
8990
return;

‎src/memory/__tests__/http.test.ts‎

Lines changed: 41 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// across requests, and a 2026-07-28 listen stream across clients.
66

77
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
8+
import { connect as connectTcp } from "node:net";
89
import {
910
Client,
1011
StreamableHTTPClientTransport,
@@ -83,27 +84,47 @@ describe("Streamable HTTP", () => {
8384
},
8485
);
8586

86-
it("sends a 2026-07-28 listener the updates another client's write makes", async () => {
87-
const url = (await start()).url;
88-
const listener = await connect(url, ERAS[0][1]);
89-
const writer = await connect(url, ERAS[0][1]);
90-
try {
91-
const updated = new Promise<string>((resolve) => {
92-
listener.setNotificationHandler(
93-
"notifications/resources/updated",
94-
(notification) => resolve(notification.params.uri),
95-
);
96-
});
97-
const subscription = await listener.listen({
98-
resourceSubscriptions: [RESOURCE_URI],
87+
it.each(ERAS)(
88+
"sends a 2026-07-28 listener the updates a %s writer makes",
89+
async (_, negotiation) => {
90+
const url = (await start()).url;
91+
const listener = await connect(url, ERAS[0][1]);
92+
const writer = await connect(url, negotiation);
93+
try {
94+
const updated = new Promise<string>((resolve) => {
95+
listener.setNotificationHandler(
96+
"notifications/resources/updated",
97+
(notification) => resolve(notification.params.uri),
98+
);
99+
});
100+
const subscription = await listener.listen({
101+
resourceSubscriptions: [RESOURCE_URI],
102+
});
103+
await call(writer, "create_entities", { entities: [alice] });
104+
expect(await updated).toBe(RESOURCE_URI);
105+
await subscription.close();
106+
} finally {
107+
await listener.close();
108+
await writer.close();
109+
}
110+
},
111+
);
112+
113+
it("answers a malformed request target 404 and keeps serving", async () => {
114+
const url = new URL((await start()).url);
115+
const reply = await new Promise<string>((resolve, reject) => {
116+
const socket = connectTcp(Number(url.port), url.hostname, () =>
117+
socket.write("GET //[ HTTP/1.1\r\nHost: localhost\r\n\r\n"),
118+
);
119+
socket.setEncoding("utf8");
120+
socket.once("data", (chunk: string) => {
121+
socket.destroy();
122+
resolve(chunk);
99123
});
100-
await call(writer, "create_entities", { entities: [alice] });
101-
expect(await updated).toBe(RESOURCE_URI);
102-
await subscription.close();
103-
} finally {
104-
await listener.close();
105-
await writer.close();
106-
}
124+
socket.once("error", reject);
125+
});
126+
expect(reply).toMatch(/^HTTP\/1\.1 404/);
127+
expect((await fetch(new URL("/other", url))).status).toBe(404);
107128
});
108129

109130
it("answers 404 off the MCP path and 403 to a foreign Origin", async () => {

‎src/memory/http.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ export async function startHttpServer(
9090
const allowOrigin = localhostOriginValidation();
9191

9292
const httpServer = createNodeServer((req, res) => {
93-
// A request a Node server hands its listener always carries a URL.
94-
const path = new URL(req.url!, "http://localhost").pathname;
93+
// The path, without parsing the target: a malformed one (`//[`) would
94+
// make URL parsing throw here, outside any handler, and end the process.
95+
const path = String(req.url).split("?")[0];
9596
if (path !== MCP_PATH) {
9697
res.writeHead(404, { "content-type": "text/plain" }).end("Not Found");
9798
return;

‎src/memory/index.ts‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -960,9 +960,10 @@ const ReadEntitySchema = EntitySchema.extend({
960960
// returns an independent server with its own subscriber set. `era` is the
961961
// protocol era the instance will serve, which decides how graph changes are
962962
// announced (see notifyGraphUpdated). `publishGraphUpdated`, when given,
963-
// announces a 2026-07-28 change instead of the instance itself: over HTTP each
964-
// request gets a fresh server that holds no listen stream, so http.ts hands in
965-
// the handler's subscription bus, which every open stream listens on.
963+
// announces every change instead of the instance itself, whatever era wrote
964+
// it: over HTTP each request gets a fresh server that holds no listen stream
965+
// and no subscriber, so http.ts hands in the handler's subscription bus, which
966+
// every open 2026-07-28 stream listens on.
966967
export function createServer(
967968
memoryFilePath: string,
968969
era: "legacy" | "modern" = "legacy",
@@ -986,7 +987,7 @@ export function createServer(
986987
// the notification only to a listen stream that named the URI, so it is
987988
// always handed over.
988989
function notifyGraphUpdated() {
989-
if (era === "modern" && publishGraphUpdated) {
990+
if (publishGraphUpdated) {
990991
publishGraphUpdated();
991992
} else if (era === "modern" || resourceSubscribers.has(RESOURCE_URI)) {
992993
// Fire-and-forget: the tool result must not wait on (or fail because of)

‎src/sequentialthinking/__tests__/http.test.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// history is asserted across calls and across clients.
66

77
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
8+
import { connect as connectTcp } from "node:net";
89
import {
910
Client,
1011
StreamableHTTPClientTransport,
@@ -91,6 +92,24 @@ describe("Streamable HTTP", () => {
9192
}
9293
});
9394

95+
it("answers a malformed request target 404 and keeps serving", async () => {
96+
running = await startHttpServer({ host: "127.0.0.1", port: 0 });
97+
const url = new URL(running.url);
98+
const reply = await new Promise<string>((resolve, reject) => {
99+
const socket = connectTcp(Number(url.port), url.hostname, () =>
100+
socket.write("GET //[ HTTP/1.1\r\nHost: localhost\r\n\r\n"),
101+
);
102+
socket.setEncoding("utf8");
103+
socket.once("data", (chunk: string) => {
104+
socket.destroy();
105+
resolve(chunk);
106+
});
107+
socket.once("error", reject);
108+
});
109+
expect(reply).toMatch(/^HTTP\/1\.1 404/);
110+
expect((await fetch(new URL("/other", url))).status).toBe(404);
111+
});
112+
94113
it("answers 404 off the MCP path and 403 to a foreign Origin", async () => {
95114
running = await startHttpServer({ host: "127.0.0.1", port: 0 });
96115
const base = new URL(running.url);

‎src/sequentialthinking/http.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,9 @@ export async function startHttpServer({
8585
const allowOrigin = localhostOriginValidation();
8686

8787
const httpServer = createNodeServer((req, res) => {
88-
// A request a Node server hands its listener always carries a URL.
89-
const path = new URL(req.url!, "http://localhost").pathname;
88+
// The path, without parsing the target: a malformed one (`//[`) would
89+
// make URL parsing throw here, outside any handler, and end the process.
90+
const path = String(req.url).split("?")[0];
9091
if (path !== MCP_PATH) {
9192
res.writeHead(404, { "content-type": "text/plain" }).end("Not Found");
9293
return;

0 commit comments

Comments
 (0)