fix(fetch): security headers on early failures, live mesh boot gate
app.fetch's 413/400/501 body-acquisition failures were returned without the injected security/CORS headers Node's writeHead patch always applies, breaking byte-identical parity with the listen() path. Extract computeInjected() (shared by handle() and buildFetch()) and merge it into every error response, not just handle()'s outcomes. Also, buildAppFetch captured `booted` by value at createApp() time, so mesh apps (whose finalize() is deferred into listen()) had app.fetch permanently throw even after a successful listen(). Read booted live via a getter, mirroring explain()/graph()/inspect(), and lazily build/cache the fetch handler on first call after boot. Signed-off-by: Diego Resendez <diego.resendez@zero-oneit.com>
This commit is contained in:
4 files changed
+108
-18
No files matched your search
@@ -15,11 +15,23 @@ class Ctl {
|
||||
@Post('/echo') echo(@body() b: any) {
|
||||
return { got: b };
|
||||
}
|
||||
@Post('/tiny', { maxBodyBytes: 10 }) tiny(@body() b: any) {
|
||||
return { got: b };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/', providers: [Store], controllers: [Ctl] })
|
||||
class M {}
|
||||
const app = createApp({ modules: [M] });
|
||||
|
||||
@Route('/mesh')
|
||||
class MeshCtl {
|
||||
@Get('/ping') ping() {
|
||||
return { pong: true };
|
||||
}
|
||||
}
|
||||
@Module({ mountpoint: '/api', controllers: [MeshCtl] })
|
||||
class MeshModule {}
|
||||
|
||||
describe('app.fetch', () => {
|
||||
it('handles GET and returns JSON 200', async () => {
|
||||
const res = await app.fetch(new Request('http://x/api/hello'));
|
||||
@@ -44,4 +56,46 @@ describe('app.fetch', () => {
|
||||
const res = await app.fetch(new Request('http://x/api/hello'));
|
||||
expect(res.headers.get('x-content-type-options')).toBe('nosniff');
|
||||
});
|
||||
|
||||
it('a 413 (oversized body) still carries the injected security headers', async () => {
|
||||
const res = await app.fetch(
|
||||
new Request('http://x/api/tiny', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: JSON.stringify({ x: 'well over ten bytes' }),
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(413);
|
||||
expect(res.headers.get('x-content-type-options')).toBe('nosniff');
|
||||
expect(res.headers.get('x-frame-options')).toBe('SAMEORIGIN');
|
||||
});
|
||||
|
||||
it('a 400 (invalid JSON) still carries the injected security headers', async () => {
|
||||
const res = await app.fetch(
|
||||
new Request('http://x/api/echo', {
|
||||
method: 'POST',
|
||||
headers: { 'content-type': 'application/json' },
|
||||
body: '{not json',
|
||||
}),
|
||||
);
|
||||
expect(res.status).toBe(400);
|
||||
expect(res.headers.get('x-content-type-options')).toBe('nosniff');
|
||||
});
|
||||
});
|
||||
|
||||
describe('app.fetch mesh gating (live `booted` read)', () => {
|
||||
it('throws before listen() and starts working after listen() finalizes', async () => {
|
||||
const meshApp = createApp({ modules: [MeshModule], experimental: true, mesh: { secret: 's3cr3t' } });
|
||||
|
||||
await expect(meshApp.fetch(new Request('http://x/api/mesh/ping'))).rejects.toThrow(/unavailable before listen/);
|
||||
|
||||
const server = await meshApp.listen(0);
|
||||
try {
|
||||
const res = await meshApp.fetch(new Request('http://x/api/mesh/ping'));
|
||||
expect(res.status).toBe(200);
|
||||
expect(await res.json()).toEqual({ pong: true });
|
||||
} finally {
|
||||
server.close();
|
||||
}
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user