fix(auth): the review items of #854 that go on top

- The strip removed the proxy headers from req.headers only, while
  req.raw.rawHeaders and req.raw.headersDistinct still held the secret.
  takeTrustedProxyHeaders() now takes the raw message and removes them
  from both. A plain splice is not enough on a real IncomingMessage: its
  headers and headersDistinct getters are lazy and walk rawHeaders up to
  the count stored at parse time, so one that first runs after the splice
  throws. Both are read (cached) first. Two tests, each caught by its
  mutation: the stripping test checks the raw list, and a real http
  request checks all three views and that the getters still answer.
- CLAUDE.md's Rate limit row names the trusted-proxy secret's own bucket.
- The wiki lists the loopback addresses the check accepts, as the
  contributor offered.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Codeman maintainer committed 2026-12-03 10:22:23 +01:00
1 parent a117955490
commit 4be36b0404
6 files changed
+113 -8

No files matched your search

+65 -1
View File
@@ -310,6 +310,61 @@ describe('takeTrustedProxyHeaders', () => {
expect(takeTrustedProxyHeaders({ 'x-codeman-user': ['alice', 'bob'] }).user).toBe('alice, bob');
expect(takeTrustedProxyHeaders({ accept: '*/*' })).toEqual({ secretPresent: false, secret: '', user: '' });
});
it('removes them from the raw message too: rawHeaders and headersDistinct, on a real request', async () => {
let seen: { raw: string[]; distinct: Record<string, string[]>; headers: Record<string, unknown> } | null = null;
let failure: unknown = null;
const server = http.createServer((req, res) => {
try {
takeTrustedProxyHeaders(req.headers, 'x-forwarded-user', req);
// Read the lazy getters AFTER the splice: they must answer from their cache, not
// walk the shortened rawHeaders with the count stored at parse time (that throws).
seen = { raw: [...req.rawHeaders], distinct: { ...req.headersDistinct }, headers: { ...req.headers } };
} catch (err) {
failure = err;
}
res.end();
});
await new Promise<void>((resolve) => server.listen(0, '127.0.0.1', resolve));
try {
const { port } = server.address() as AddressInfo;
await new Promise<void>((resolve, reject) => {
const req = http.get(
{
host: '127.0.0.1',
port,
path: '/',
headers: {
'X-Codeman-Proxy-Secret': SECRET,
'X-Codeman-User': 'alice',
'X-Forwarded-User': 'bob',
Accept: '*/*',
},
},
(res) => {
res.resume();
res.on('end', resolve);
}
);
req.on('error', reject);
});
} finally {
await new Promise<void>((resolve) => server.close(() => resolve()));
}
expect(failure).toBeNull();
const result = seen!;
const rawNames = result.raw.filter((_, i) => i % 2 === 0).map((name) => name.toLowerCase());
for (const name of ['x-codeman-proxy-secret', 'x-codeman-user', 'x-forwarded-user']) {
expect(rawNames).not.toContain(name);
expect(result.distinct[name]).toBeUndefined();
expect(result.headers[name]).toBeUndefined();
}
expect(result.raw).not.toContain(SECRET);
// Everything else is left as it was, in all three views.
expect(rawNames).toContain('accept');
expect(result.distinct.accept).toEqual(['*/*']);
expect(result.headers.accept).toBe('*/*');
});
});
describe('capabilityPrefixOf judges a path the way a proxy matcher does', () => {
@@ -413,7 +468,11 @@ describe('the auth middleware strips the headers before any route sees them', ()
const app = Fastify();
await app.register(fastifyCookie);
registerAuthMiddleware(app, false);
app.get('/echo', async (req) => ({ headers: req.headers, user: req.authUser?.username ?? null }));
app.get('/echo', async (req) => ({
headers: req.headers,
rawHeaders: req.raw.rawHeaders,
user: req.authUser?.username ?? null,
}));
app.post('/echo', async (req) => ({ headers: req.headers, body: req.body }));
await app.ready();
return app;
@@ -434,6 +493,11 @@ describe('the auth middleware strips the headers before any route sees them', ()
expect(body.user).toBe('bob');
expect(body.headers['x-codeman-proxy-secret']).toBeUndefined();
expect(body.headers['x-codeman-user']).toBeUndefined();
// Not in the raw list either (section 10a: the headers travel no further).
const rawNames = (body.rawHeaders as string[]).filter((_, i) => i % 2 === 0).map((n) => n.toLowerCase());
expect(rawNames).not.toContain('x-codeman-proxy-secret');
expect(rawNames).not.toContain('x-codeman-user');
expect(body.rawHeaders).not.toContain(SECRET);
} finally {
await app.close();
}