Skip to content

Commit 0f6342f

Browse files
committed
fix(everything): reject HTTP error responses in gzip-file-as-resource
fetchSafely only checked that a body was present, so a 404 or 500 page was compressed and returned as if it were the requested file.
1 parent f46d957 commit 0f6342f

2 files changed

Lines changed: 66 additions & 0 deletions

File tree

‎src/everything/__tests__/tools.test.ts‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import http from 'node:http';
12
import { describe, it, expect, vi, beforeEach } from 'vitest';
23
import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
34
import { registerEchoTool, EchoSchema } from '../tools/echo.js';
@@ -1217,5 +1218,66 @@ describe('Tools', () => {
12171218
handler!({ name: 'test.gz', data: 'ftp://example.com/file.txt', outputType: 'resource' })
12181219
).rejects.toThrow('Unsupported URL protocol');
12191220
});
1221+
1222+
it('should reject HTTP error responses instead of compressing the error page', async () => {
1223+
const mockServer = {
1224+
registerTool: vi.fn(),
1225+
registerResource: vi.fn(),
1226+
} as unknown as McpServer;
1227+
1228+
let handler: Function | null = null;
1229+
(mockServer.registerTool as any).mockImplementation(
1230+
(name: string, config: any, h: Function) => {
1231+
handler = h;
1232+
}
1233+
);
1234+
1235+
registerGZipFileAsResourceTool(mockServer);
1236+
1237+
const httpServer = http.createServer((req, res) => {
1238+
if (req.url === '/ok.txt') {
1239+
res.writeHead(200, { 'content-type': 'text/plain' });
1240+
res.end('real file contents');
1241+
return;
1242+
}
1243+
const status = req.url === '/boom.txt' ? 500 : 404;
1244+
res.writeHead(status, { 'content-type': 'text/plain' });
1245+
res.end(`error body for ${status}`);
1246+
});
1247+
await new Promise<void>((resolve) => httpServer.listen(0, '127.0.0.1', resolve));
1248+
const address = httpServer.address();
1249+
if (!address || typeof address === 'string') {
1250+
throw new Error('expected a TCP address');
1251+
}
1252+
1253+
try {
1254+
const ok = await handler!({
1255+
name: 'ok.txt.gz',
1256+
data: `http://127.0.0.1:${address.port}/ok.txt`,
1257+
outputType: 'resource',
1258+
});
1259+
expect(ok.content[0].type).toBe('resource');
1260+
1261+
await expect(
1262+
handler!({
1263+
name: 'missing.txt.gz',
1264+
data: `http://127.0.0.1:${address.port}/missing.txt`,
1265+
outputType: 'resource',
1266+
})
1267+
).rejects.toThrow('status 404');
1268+
1269+
await expect(
1270+
handler!({
1271+
name: 'boom.txt.gz',
1272+
data: `http://127.0.0.1:${address.port}/boom.txt`,
1273+
outputType: 'resource',
1274+
})
1275+
).rejects.toThrow('status 500');
1276+
} finally {
1277+
await new Promise<void>((resolve, reject) =>
1278+
httpServer.close((err) => (err ? reject(err) : resolve()))
1279+
);
1280+
}
1281+
});
12201282
});
12211283
});

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ async function fetchSafely(
193193
try {
194194
// Fetch the data
195195
const response = await fetch(url, { signal: controller.signal });
196+
if (!response.ok) {
197+
await response.body?.cancel().catch(() => {});
198+
throw new Error(`Failed to fetch ${url}: status ${response.status}`);
199+
}
196200
if (!response.body) {
197201
throw new Error("No response body");
198202
}

0 commit comments

Comments
 (0)