fix(mesh): answer HEAD on a remote GET route (CARD-20, #125)
The teacup's router resolves HEAD to a remote buffered GET through the implicit-HEAD fallback, and OPTIONS advertises HEAD. But the remote route handler forwarded req.method, so the teapot got a HEAD envelope, found no HEAD plan in resolveRoute, and answered 404. Forward a HEAD that matched a remote GET as GET. dispatch already drops the body for HEAD, so the client gets the GET's status and headers and no body, the same as a local route. An explicit remote HEAD route is matched first and is still forwarded as HEAD. The fix is teacup-side (D16, accepted 2026-10-22): the teapot and the wire protocol are unchanged, so a new teacup works against an older teapot during a rolling deploy. Tests cover app.fetch and the Node listener (200 + GET headers + empty body, OPTIONS, explicit HEAD precedence, 404/500 kept with no body, the teapot handler called once as GET) and Deno and Bun. The frozen owner-mesh-allow.json capture changes in the next commit. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
5 files changed
+237
-1
No files matched your search
@@ -83,6 +83,14 @@ npm treats versions as semver and semver forbids leading zeros.
|
|||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- **Mesh (alpha): `HEAD` on a remote `GET` route now works** (#125). The teacup resolved `HEAD` to
|
||||||
|
the remote `GET` and `OPTIONS` advertised `HEAD`, but the request went to the teapot as `HEAD`,
|
||||||
|
which has no `HEAD` plan, so it answered `404`. The teacup now forwards it as a `GET` and drops
|
||||||
|
the body, so it answers like a local route: the `GET`'s status and headers, no body, and a
|
||||||
|
`404`/`500` from the route keeps its status. An explicit remote `HEAD` route still takes
|
||||||
|
precedence. The teapot is unchanged, so a new teacup works against an older teapot during a
|
||||||
|
rolling deploy.
|
||||||
|
|
||||||
- **`static` no longer serves files outside its root through a symlink** (#106). The traversal guard
|
- **`static` no longer serves files outside its root through a symlink** (#106). The traversal guard
|
||||||
compared paths lexically, and the file read then followed symlinks, so `public/leak -> /etc/hosts`
|
compared paths lexically, and the file read then followed symlinks, so `public/leak -> /etc/hosts`
|
||||||
was served. Containment now compares real paths: the requested file, and a directory's
|
was served. Containment now compares real paths: the requested file, and a directory's
|
||||||
|
|||||||
+5
-1
@@ -1064,7 +1064,11 @@ async function spliceRemoteScopes(
|
|||||||
// protocol never declared, so a teapot could come to rely on something we never promised.
|
// protocol never declared, so a teapot could come to rely on something we never promised.
|
||||||
handler: async (req) =>
|
handler: async (req) =>
|
||||||
route.handler({
|
route.handler({
|
||||||
method: req.method,
|
// HEAD reaches a remote GET through the implicit-HEAD fallback, and the teapot looks
|
||||||
|
// its plan up by method, so a forwarded HEAD finds none and answers 404. Send it as the
|
||||||
|
// GET it resolved to; dispatch drops the body for HEAD. Fixed teacup-side (D16) so an
|
||||||
|
// unchanged, older teapot still answers it during a rolling deploy.
|
||||||
|
method: req.method === 'HEAD' && route.method === 'GET' ? 'GET' : req.method,
|
||||||
params: req.params,
|
params: req.params,
|
||||||
query: req.query,
|
query: req.query,
|
||||||
body: req.body,
|
body: req.body,
|
||||||
|
|||||||
@@ -82,3 +82,39 @@ test('mesh on Bun: a teapot refuses a bad secret over its control channel', asyn
|
|||||||
tServer.stop(true);
|
tServer.stop(true);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// CARD-20 (#125, D16): HEAD on a remote GET answers like HEAD on a local one.
|
||||||
|
@Route('/remote')
|
||||||
|
class RemoteHeadCtl {
|
||||||
|
@Get('/x', { export: true })
|
||||||
|
x() {
|
||||||
|
return { a: 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [RemoteHeadCtl] })
|
||||||
|
class RemoteHeadTeapotModule {}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [] })
|
||||||
|
class EmptyTeacupModule {}
|
||||||
|
|
||||||
|
test('mesh on Bun: HEAD on a remote GET answers 200 with the GET headers and no body', async () => {
|
||||||
|
const teapot = createApp({ modules: [RemoteHeadTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||||
|
const tServer = await serveBun(teapot, { port: 0 });
|
||||||
|
|
||||||
|
const teacup = createApp({
|
||||||
|
modules: [EmptyTeacupModule],
|
||||||
|
experimental: true,
|
||||||
|
mesh: { teapots: [{ url: controlUrl(tServer.port), secret: SECRET }], reconnect: false },
|
||||||
|
});
|
||||||
|
const cServer = await serveBun(teacup, { port: 0 });
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`http://127.0.0.1:${cServer.port}/api/remote/x`, { method: 'HEAD' });
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
expect(res.headers.get('content-type') ?? '').toContain('application/json');
|
||||||
|
expect(await res.text()).toBe('');
|
||||||
|
} finally {
|
||||||
|
await teacup.close();
|
||||||
|
cServer.stop(true);
|
||||||
|
tServer.stop(true);
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -106,3 +106,39 @@ Deno.test('mesh on Deno: a teapot refuses a bad secret over its control channel'
|
|||||||
await t.server.shutdown();
|
await t.server.shutdown();
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// CARD-20 (#125, D16): HEAD on a remote GET answers like HEAD on a local one.
|
||||||
|
@Route('/remote')
|
||||||
|
class RemoteHeadCtl {
|
||||||
|
@Get('/x', { export: true })
|
||||||
|
x() {
|
||||||
|
return { a: 1 };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [RemoteHeadCtl] })
|
||||||
|
class RemoteHeadTeapotModule {}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [] })
|
||||||
|
class EmptyTeacupModule {}
|
||||||
|
|
||||||
|
Deno.test('mesh on Deno: HEAD on a remote GET answers 200 with the GET headers and no body', async () => {
|
||||||
|
const teapot = createApp({ modules: [RemoteHeadTeapotModule], experimental: true, mesh: { secret: SECRET } });
|
||||||
|
const t = await serve(teapot);
|
||||||
|
|
||||||
|
const teacup = createApp({
|
||||||
|
modules: [EmptyTeacupModule],
|
||||||
|
experimental: true,
|
||||||
|
mesh: { teapots: [{ url: controlUrl(t.port), secret: SECRET }], reconnect: false },
|
||||||
|
});
|
||||||
|
const c = await serve(teacup);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(`http://127.0.0.1:${c.port}/api/remote/x`, { method: 'HEAD' });
|
||||||
|
assertEquals(res.status, 200);
|
||||||
|
assertStringIncludes(res.headers.get('content-type') ?? '', 'application/json');
|
||||||
|
assertEquals(await res.text(), '');
|
||||||
|
} finally {
|
||||||
|
await teacup.close();
|
||||||
|
await c.server.shutdown();
|
||||||
|
await t.server.shutdown();
|
||||||
|
}
|
||||||
|
});
|
||||||
@@ -0,0 +1,152 @@
|
|||||||
|
/**
|
||||||
|
* CARD-20 (#125, D16): `HEAD` on a remote buffered `GET` behaves like `HEAD` on a local one.
|
||||||
|
*
|
||||||
|
* The teacup resolves `HEAD` to the remote `GET` (implicit HEAD) and forwards it to the teapot as a
|
||||||
|
* `GET`, then drops the body. The teapot is unchanged, so an older teapot works too.
|
||||||
|
*/
|
||||||
|
import 'reflect-metadata';
|
||||||
|
import http from 'http';
|
||||||
|
import type { AddressInfo } from 'net';
|
||||||
|
import { describe, it, expect, afterEach } from 'vitest';
|
||||||
|
import { createApp, Route, Get, Head, Module, NotFound, ctx } from '../../src/index';
|
||||||
|
import type { App } from '../../src/app/types';
|
||||||
|
import type { Logger } from '../../src/logger';
|
||||||
|
|
||||||
|
const SECRET = 'head-secret';
|
||||||
|
const quiet: Logger = { debug: () => {}, info: () => {}, warn: () => {}, error: () => {} };
|
||||||
|
|
||||||
|
const seen: string[] = [];
|
||||||
|
|
||||||
|
@Route('/')
|
||||||
|
class RemoteCtl {
|
||||||
|
@Get('/x', { export: true }) x(@ctx() c: { req: { method: string } }) {
|
||||||
|
seen.push(`x ${c.req.method}`);
|
||||||
|
return { a: 1 };
|
||||||
|
}
|
||||||
|
@Get('/missing', { export: true }) missing() {
|
||||||
|
seen.push('missing');
|
||||||
|
throw new NotFound();
|
||||||
|
}
|
||||||
|
@Get('/boom', { export: true }) boom() {
|
||||||
|
seen.push('boom');
|
||||||
|
throw new Error('kaboom');
|
||||||
|
}
|
||||||
|
@Get('/both', { export: true }) bothGet() {
|
||||||
|
seen.push('both GET');
|
||||||
|
return { via: 'get' };
|
||||||
|
}
|
||||||
|
@Head('/both', { export: true }) bothHead(@ctx() c: { req: { method: string } }) {
|
||||||
|
seen.push(`both HEAD as ${c.req.method}`);
|
||||||
|
return { via: 'head' };
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Module({ mountpoint: '/api', controllers: [RemoteCtl] })
|
||||||
|
class TeapotModule {}
|
||||||
|
|
||||||
|
@Module({ mountpoint: '/api', controllers: [] })
|
||||||
|
class TeacupModule {}
|
||||||
|
|
||||||
|
const open: App[] = [];
|
||||||
|
afterEach(async () => {
|
||||||
|
while (open.length) await open.pop()!.close();
|
||||||
|
seen.length = 0;
|
||||||
|
});
|
||||||
|
|
||||||
|
async function mesh(): Promise<{ teacup: App; port: number }> {
|
||||||
|
const teapot = createApp({ modules: [TeapotModule], experimental: true, logger: quiet, mesh: { secret: SECRET } });
|
||||||
|
open.push(teapot);
|
||||||
|
const tPort = ((await teapot.listen(0)).address() as AddressInfo).port;
|
||||||
|
const teacup = createApp({
|
||||||
|
modules: [TeacupModule],
|
||||||
|
experimental: true,
|
||||||
|
logger: quiet,
|
||||||
|
mesh: { teapots: [{ url: `ws://127.0.0.1:${tPort}/__mesh__/control`, secret: SECRET }], reconnect: false },
|
||||||
|
});
|
||||||
|
open.unshift(teacup); // closed first
|
||||||
|
const port = ((await teacup.listen(0)).address() as AddressInfo).port;
|
||||||
|
return { teacup, port };
|
||||||
|
}
|
||||||
|
|
||||||
|
function nodeRequest(port: number, method: string, path: string) {
|
||||||
|
return new Promise<{ status: number; headers: http.IncomingHttpHeaders; body: string }>((resolve, reject) => {
|
||||||
|
const req = http.request({ host: '127.0.0.1', port, method, path, agent: false }, (res) => {
|
||||||
|
let body = '';
|
||||||
|
res.setEncoding('utf8');
|
||||||
|
res.on('data', (chunk) => (body += chunk));
|
||||||
|
res.on('end', () => resolve({ status: res.statusCode ?? 0, headers: res.headers, body }));
|
||||||
|
});
|
||||||
|
req.on('error', reject);
|
||||||
|
req.end();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const fetchVia = (app: App, method: string, path: string) =>
|
||||||
|
app.fetch(new Request(`http://mesh.test${path}`, { method }));
|
||||||
|
|
||||||
|
describe('mesh: HEAD on a remote GET route (CARD-20, D16)', () => {
|
||||||
|
it('T1: HEAD via app.fetch answers 200 with the GET headers and no body', async () => {
|
||||||
|
const { teacup } = await mesh();
|
||||||
|
const get = await fetchVia(teacup, 'GET', '/api/x');
|
||||||
|
const head = await fetchVia(teacup, 'HEAD', '/api/x');
|
||||||
|
expect(head.status).toBe(200);
|
||||||
|
expect(head.headers.get('content-type')).toBe(get.headers.get('content-type'));
|
||||||
|
expect(head.headers.get('content-type')).toContain('application/json');
|
||||||
|
expect(await head.text()).toBe('');
|
||||||
|
expect(await get.json()).toEqual({ a: 1 });
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T2: HEAD via the Node listener answers 200 with the GET headers and no body', async () => {
|
||||||
|
const { port } = await mesh();
|
||||||
|
const get = await nodeRequest(port, 'GET', '/api/x');
|
||||||
|
const head = await nodeRequest(port, 'HEAD', '/api/x');
|
||||||
|
expect(head.status).toBe(200);
|
||||||
|
expect(head.headers['content-type']).toBe(get.headers['content-type']);
|
||||||
|
expect(head.body).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('OPTIONS still advertises GET, HEAD, OPTIONS', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
expect((await fetchVia(teacup, 'OPTIONS', '/api/x')).headers.get('allow')).toBe('GET, HEAD, OPTIONS');
|
||||||
|
expect((await nodeRequest(port, 'OPTIONS', '/api/x')).headers.allow).toBe('GET, HEAD, OPTIONS');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T3: an explicit remote HEAD route keeps precedence over the implicit fallback', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
const viaFetch = await fetchVia(teacup, 'HEAD', '/api/both');
|
||||||
|
expect(viaFetch.status).toBe(200);
|
||||||
|
expect(await viaFetch.text()).toBe('');
|
||||||
|
const viaNode = await nodeRequest(port, 'HEAD', '/api/both');
|
||||||
|
expect(viaNode.status).toBe(200);
|
||||||
|
expect(viaNode.body).toBe('');
|
||||||
|
// The explicit HEAD handler ran, reached as HEAD — the GET handler never did.
|
||||||
|
expect(seen).toEqual(['both HEAD as HEAD', 'both HEAD as HEAD']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T4: a remote GET that throws NotFound answers HEAD with 404 and no body', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
const viaFetch = await fetchVia(teacup, 'HEAD', '/api/missing');
|
||||||
|
expect(viaFetch.status).toBe(404);
|
||||||
|
expect(await viaFetch.text()).toBe('');
|
||||||
|
const viaNode = await nodeRequest(port, 'HEAD', '/api/missing');
|
||||||
|
expect(viaNode.status).toBe(404);
|
||||||
|
expect(viaNode.body).toBe('');
|
||||||
|
// The teapot handler ran: the 404 is the route's, not a missing HEAD plan on the teapot.
|
||||||
|
expect(seen).toEqual(['missing', 'missing']);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('a remote GET that fails answers HEAD with 500 and no body', async () => {
|
||||||
|
const { teacup, port } = await mesh();
|
||||||
|
const viaFetch = await fetchVia(teacup, 'HEAD', '/api/boom');
|
||||||
|
expect(viaFetch.status).toBe(500);
|
||||||
|
expect(await viaFetch.text()).toBe('');
|
||||||
|
const viaNode = await nodeRequest(port, 'HEAD', '/api/boom');
|
||||||
|
expect(viaNode.status).toBe(500);
|
||||||
|
expect(viaNode.body).toBe('');
|
||||||
|
});
|
||||||
|
|
||||||
|
it('T5: one HEAD calls the teapot handler once, as a GET', async () => {
|
||||||
|
const { teacup } = await mesh();
|
||||||
|
await fetchVia(teacup, 'HEAD', '/api/x');
|
||||||
|
expect(seen).toEqual(['x GET']);
|
||||||
|
});
|
||||||
|
});
|
||||||
Reference in new issue
Block a user