Skip to content

Commit 81a1e63

Browse files
cliffhallclaude
andcommitted
fix(everything): fail gzip-file-as-resource on an HTTP error response
fetchSafely never checked the response status, so a 404 or 500 error page was gzipped and returned as the requested file. A non-2xx response now cancels the body and fails the call with an error naming the URL and the status, as the tool already does for size and timeout failures. Closes #5207 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: cliffhall <cliff@futurescale.com>
1 parent 846cc6c commit 81a1e63

3 files changed

Lines changed: 76 additions & 0 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@modelcontextprotocol/server-everything": patch
3+
---
4+
5+
`gzip-file-as-resource` now fails with an error naming the URL and HTTP status when the fetch returns a non-2xx response, instead of gzipping the error page as the file.

‎src/everything/__tests__/gzip-file-as-resource.test.ts‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,3 +221,61 @@ describe("gzip-file-as-resource", () => {
221221
});
222222
});
223223
});
224+
225+
describe("gzip-file-as-resource with an HTTP error response (#5207)", () => {
226+
// A server of its own, so these routes stay apart from the shared one above.
227+
let errors: Server;
228+
let errorBase: string;
229+
230+
beforeAll(async () => {
231+
errors = createHttpServer((req, res) => {
232+
if (req.url === "/server-error") {
233+
res.writeHead(500, { "content-type": "text/plain" });
234+
res.end("internal failure");
235+
return;
236+
}
237+
res.writeHead(404, { "content-type": "text/html" });
238+
res.end("<h1>Not Found</h1>");
239+
});
240+
errors.listen(0, "127.0.0.1");
241+
await once(errors, "listening");
242+
const address = errors.address();
243+
if (address === null || typeof address === "string")
244+
throw new Error("no port");
245+
errorBase = `http://127.0.0.1:${address.port}`;
246+
});
247+
248+
afterAll(async () => {
249+
errors.closeAllConnections();
250+
await new Promise((resolve) => errors.close(resolve));
251+
});
252+
253+
it.each([
254+
["/missing", "404 Not Found"],
255+
["/server-error", "500 Internal Server Error"],
256+
])(
257+
"fails on %s with HTTP %s and registers no resource",
258+
async (path, status) => {
259+
const s = await open();
260+
const url = `${errorBase}${path}`;
261+
const result = await gzip(s, {
262+
name: "error.gz",
263+
data: url,
264+
outputType: "resource",
265+
});
266+
expect(result).toEqual({
267+
isError: true,
268+
content: [
269+
{
270+
type: "text",
271+
text: `Fetching ${url} failed with HTTP ${status}`,
272+
},
273+
],
274+
});
275+
const { resources } = await s.client.listResources();
276+
expect(resources.map((r) => r.uri)).not.toContain(
277+
"demo://resource/session/error.gz",
278+
);
279+
},
280+
);
281+
});

‎src/everything/tools/gzip-file-as-resource.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,19 @@ async function fetchSafely(
217217
try {
218218
// Fetch the data
219219
const response = await fetch(url, { signal: controller.signal });
220+
if (!response.ok) {
221+
// An error page is not the requested file (#5207). Release the
222+
// connection without reading the body; as with the size check below,
223+
// the cancel is not awaited and its rejection is swallowed, since the
224+
// thrown status error is the failure this path reports.
225+
response.body?.cancel().catch(
226+
/* v8 ignore next -- only runs if cancelling the source fails, which nothing here can provoke */
227+
() => {},
228+
);
229+
throw new Error(
230+
`Fetching ${url} failed with HTTP ${response.status} ${response.statusText}`,
231+
);
232+
}
220233
if (!response.body) {
221234
throw new Error("No response body");
222235
}

0 commit comments

Comments
 (0)