|
| 1 | +// SPDX-FileCopyrightText: 2026 Yvig Bidon |
| 2 | +// SPDX-License-Identifier: AGPL-3.0-or-later |
| 3 | + |
| 4 | +import { describe, expect, it, vi } from 'vitest'; |
| 5 | + |
| 6 | +// The assembled app without Redis, as in app.test.ts: every request below is answered before any |
| 7 | +// database or queue is reached. |
| 8 | +vi.mock('../lib/redis.js', async (importOriginal) => ({ |
| 9 | + ...(await importOriginal<typeof import('../lib/redis.js')>()), |
| 10 | + enableRedisTransport: vi.fn(), |
| 11 | +})); |
| 12 | + |
| 13 | +import express, { type Express } from 'express'; |
| 14 | +import request from 'supertest'; |
| 15 | +import { buildExpressApp } from '../app.js'; |
| 16 | +import { buildFastifyShell, shellRequestListener } from './fastify-shell.js'; |
| 17 | + |
| 18 | +const STREAM_BYTES = 20 * 1024 * 1024; |
| 19 | +const STREAM_CHUNK_BYTES = 64 * 1024; |
| 20 | +const BODY_LIMIT = '2mb'; |
| 21 | + |
| 22 | +const shelled = async (app: Express) => shellRequestListener(await buildFastifyShell(app)); |
| 23 | + |
| 24 | +const v2Alone = buildExpressApp({ rateLimit: false }); |
| 25 | +const v2Shelled = await shelled(buildExpressApp({ rateLimit: false })); |
| 26 | + |
| 27 | +/** A minimal Express app parsing bodies the way V2 does, to check what reaches a route. */ |
| 28 | +function echoApp(): Express { |
| 29 | + const app = express(); |
| 30 | + app.use(express.json({ limit: BODY_LIMIT })); |
| 31 | + app.use(express.urlencoded({ extended: false, limit: BODY_LIMIT })); |
| 32 | + app.post('/echo', (req, res) => { |
| 33 | + res.json({ body: req.body as unknown }); |
| 34 | + }); |
| 35 | + return app; |
| 36 | +} |
| 37 | + |
| 38 | +describe('Fastify shell around the V2 Express app', () => { |
| 39 | + it('answers like Express alone on a public route, security headers included', async () => { |
| 40 | + const [direct, shell] = await Promise.all([ |
| 41 | + request(v2Alone).get('/api/openapi.json'), |
| 42 | + request(v2Shelled).get('/api/openapi.json'), |
| 43 | + ]); |
| 44 | + expect(shell.status).toBe(direct.status); |
| 45 | + expect(shell.body).toEqual(direct.body); |
| 46 | + expect(shell.headers['x-content-type-options']).toBe('nosniff'); |
| 47 | + expect(shell.headers['content-security-policy']).toBe(direct.headers['content-security-policy']); |
| 48 | + }); |
| 49 | + |
| 50 | + it('keeps the error shape of a refused call', async () => { |
| 51 | + const [direct, shell] = await Promise.all([ |
| 52 | + request(v2Alone).get('/api/projects'), |
| 53 | + request(v2Shelled).get('/api/projects'), |
| 54 | + ]); |
| 55 | + expect(shell.status).toBe(401); |
| 56 | + expect(shell.body).toEqual(direct.body); |
| 57 | + }); |
| 58 | + |
| 59 | + it('answers a body sent to an unknown route with a 404 instead of hanging (probe finding 1)', async () => { |
| 60 | + // Express consumes the body, finds no route and hands over to Fastify, which would wait for a |
| 61 | + // body already read without the shell's pass-through parser. |
| 62 | + const res = await request(v2Shelled) |
| 63 | + .patch('/api/does-not-exist') |
| 64 | + .send({ trim: { inFrame: 1 } }) |
| 65 | + .timeout(5_000); |
| 66 | + expect(res.status).toBe(404); |
| 67 | + }); |
| 68 | + |
| 69 | + it('answers an unknown path with a JSON 404 where Express alone sent an HTML page', async () => { |
| 70 | + // V2 has no 404 handler of its own. The status is unchanged; the body is Fastify's. |
| 71 | + const [direct, shell] = await Promise.all([ |
| 72 | + request(v2Alone).get('/api/does-not-exist'), |
| 73 | + request(v2Shelled).get('/api/does-not-exist'), |
| 74 | + ]); |
| 75 | + expect(direct.status).toBe(404); |
| 76 | + expect(direct.headers['content-type']).toMatch(/text\/html/); |
| 77 | + expect(shell.status).toBe(404); |
| 78 | + expect(shell.body).toMatchObject({ statusCode: 404, error: 'Not Found' }); |
| 79 | + }); |
| 80 | + |
| 81 | + it('records what Express routes lose through the shell until step 3: ETag/304 and the request id', async () => { |
| 82 | + // Probe finding 3: the plugin swaps Express's res.send for Fastify's, skipping Express's ETag and |
| 83 | + // freshness check, and sets req.id first, so V2's logger neither generates nor echoes |
| 84 | + // x-request-id. Step 3 of the phase 2a plan restores both on the Fastify side and flips this test. |
| 85 | + const [direct, shell] = await Promise.all([ |
| 86 | + request(v2Alone).get('/api/openapi.json'), |
| 87 | + request(v2Shelled).get('/api/openapi.json'), |
| 88 | + ]); |
| 89 | + expect(direct.headers.etag).toBeDefined(); |
| 90 | + expect(direct.headers['x-request-id']).toBeDefined(); |
| 91 | + expect(shell.headers.etag).toBeUndefined(); |
| 92 | + expect(shell.headers['x-request-id']).toBeUndefined(); |
| 93 | + }); |
| 94 | +}); |
| 95 | + |
| 96 | +describe('Fastify shell mechanics (minimal Express apps)', () => { |
| 97 | + it('trusts exactly the proxies Express trusts: the client IP behind nginx (probe finding 2)', async () => { |
| 98 | + const ipApp = (hops: number) => { |
| 99 | + const app = express(); |
| 100 | + app.set('trust proxy', hops); |
| 101 | + app.get('/ip', (req, res) => { |
| 102 | + res.json({ ip: req.ip }); |
| 103 | + }); |
| 104 | + return app; |
| 105 | + }; |
| 106 | + const behindProxy = await request(await shelled(ipApp(1))) |
| 107 | + .get('/ip') |
| 108 | + .set('X-Forwarded-For', '203.0.113.7'); |
| 109 | + expect(behindProxy.body).toEqual({ ip: '203.0.113.7' }); |
| 110 | + // TRUST_PROXY=0: a spoofed header is ignored, as with Express alone. |
| 111 | + const direct = await request(ipApp(0)).get('/ip').set('X-Forwarded-For', '203.0.113.7'); |
| 112 | + const spoofed = await request(await shelled(ipApp(0))) |
| 113 | + .get('/ip') |
| 114 | + .set('X-Forwarded-For', '203.0.113.7'); |
| 115 | + expect(spoofed.body).toEqual(direct.body); |
| 116 | + expect(spoofed.body).not.toEqual({ ip: '203.0.113.7' }); |
| 117 | + }); |
| 118 | + |
| 119 | + it('lets JSON and urlencoded bodies reach the Express route', async () => { |
| 120 | + const shell = await shelled(echoApp()); |
| 121 | + const json = await request(shell).post('/echo').send({ name: 'shot 010' }).timeout(5_000); |
| 122 | + const form = await request(shell).post('/echo').type('form').send('name=shot+010').timeout(5_000); |
| 123 | + expect(json.body).toEqual({ body: { name: 'shot 010' } }); |
| 124 | + expect(form.body).toEqual({ body: { name: 'shot 010' } }); |
| 125 | + }); |
| 126 | + |
| 127 | + it('refuses a body over the limit the way Express alone does', async () => { |
| 128 | + const big = { blob: 'x'.repeat(2 * 1024 * 1024 + 10) }; |
| 129 | + const direct = await request(echoApp()).post('/echo').send(big).timeout(10_000); |
| 130 | + const shell = await request(await shelled(echoApp())) |
| 131 | + .post('/echo') |
| 132 | + .send(big) |
| 133 | + .timeout(10_000); |
| 134 | + expect(shell.status).toBe(direct.status); |
| 135 | + }); |
| 136 | + |
| 137 | + it('streams a large response and answers a Range request', async () => { |
| 138 | + const file = Buffer.alloc(STREAM_BYTES, 7); |
| 139 | + const app = express(); |
| 140 | + app.get('/file', (req, res) => { |
| 141 | + const range = /bytes=(\d+)-(\d+)/.exec(req.headers.range ?? ''); |
| 142 | + if (range) { |
| 143 | + const [start, end] = [Number(range[1]), Number(range[2])]; |
| 144 | + res |
| 145 | + .status(206) |
| 146 | + .set('Content-Range', `bytes ${start}-${end}/${file.length}`) |
| 147 | + .end(file.subarray(start, end + 1)); |
| 148 | + return; |
| 149 | + } |
| 150 | + res.set('Content-Length', String(file.length)); |
| 151 | + let offset = 0; |
| 152 | + const pump = (): void => { |
| 153 | + while (offset < file.length) { |
| 154 | + const chunk = file.subarray(offset, offset + STREAM_CHUNK_BYTES); |
| 155 | + offset += chunk.length; |
| 156 | + if (!res.write(chunk)) { |
| 157 | + res.once('drain', pump); |
| 158 | + return; |
| 159 | + } |
| 160 | + } |
| 161 | + res.end(); |
| 162 | + }; |
| 163 | + pump(); |
| 164 | + }); |
| 165 | + const shell = await shelled(app); |
| 166 | + const full = await request(shell) |
| 167 | + .get('/file') |
| 168 | + .buffer(true) |
| 169 | + .parse((res, done) => { |
| 170 | + const parts: Buffer[] = []; |
| 171 | + res.on('data', (chunk: Buffer) => parts.push(chunk)); |
| 172 | + res.on('end', () => { |
| 173 | + done(null, Buffer.concat(parts)); |
| 174 | + }); |
| 175 | + }); |
| 176 | + const part = await request(shell).get('/file').set('Range', 'bytes=0-1023'); |
| 177 | + expect((full.body as Buffer).length).toBe(file.length); |
| 178 | + expect(part.status).toBe(206); |
| 179 | + expect(part.headers['content-range']).toBe(`bytes 0-1023/${file.length}`); |
| 180 | + }); |
| 181 | +}); |
0 commit comments