mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-12-05 01:29:32 +01:00
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:
1 parent
a117955490
commit
4be36b0404
6 files changed
+113
-8
No files matched your search
@@ -255,19 +255,57 @@ export interface PresentedProxyHeaders {
|
||||
user: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* The two other views Node keeps of a request's headers, beside `headers`: the flat
|
||||
* `[name, value, ...]` list as received and the per-name arrays. Both are optional
|
||||
* because a light-my-request message (`app.inject`) may lack `headersDistinct`.
|
||||
*/
|
||||
export interface RawHeaderViews {
|
||||
headers?: IncomingHttpHeaders;
|
||||
rawHeaders?: string[];
|
||||
headersDistinct?: NodeJS.Dict<string[]>;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove `names` (lowercase) from `raw.rawHeaders` and `raw.headersDistinct`, in place.
|
||||
*
|
||||
* ⚠️ Order matters on a real `http.IncomingMessage`: its `headers` and `headersDistinct`
|
||||
* getters are LAZY and walk `rawHeaders` up to a header count stored when the request was
|
||||
* parsed, so a getter that first runs after a splice reads past the shortened array and
|
||||
* throws. Both are therefore read (and so cached) BEFORE `rawHeaders` shrinks, and later
|
||||
* reads return the cached, already stripped objects.
|
||||
*/
|
||||
function stripRawHeaderViews(raw: RawHeaderViews, names: readonly string[]): void {
|
||||
void raw.headers;
|
||||
const distinct = raw.headersDistinct;
|
||||
if (distinct && typeof distinct === 'object') {
|
||||
for (const name of names) delete distinct[name];
|
||||
}
|
||||
const list = raw.rawHeaders;
|
||||
if (!Array.isArray(list)) return;
|
||||
for (let i = list.length - 2; i >= 0; i -= 2) {
|
||||
const name = list[i];
|
||||
if (typeof name === 'string' && names.includes(name.toLowerCase())) list.splice(i, 2);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Read the trusted-proxy headers and DELETE them from `headers`, so nothing that later
|
||||
* copies request headers (the web-tab relay, logging) ever sees the secret. Removes the
|
||||
* secret header, the default username header and `userHeader` (from
|
||||
* `resolveTrustedProxyUserHeader`, so always a usable name), whether or not the feature is
|
||||
* on: a proxy may send them to an instance that has it off.
|
||||
* on: a proxy may send them to an instance that has it off. Given the raw message (`raw`,
|
||||
* the auth hook passes `req.raw`), it removes them from `rawHeaders` and `headersDistinct`
|
||||
* too, so no view of the request still holds the secret.
|
||||
*/
|
||||
export function takeTrustedProxyHeaders(
|
||||
headers: IncomingHttpHeaders,
|
||||
userHeader: string = DEFAULT_TRUSTED_PROXY_USER_HEADER
|
||||
userHeader: string = DEFAULT_TRUSTED_PROXY_USER_HEADER,
|
||||
raw?: RawHeaderViews
|
||||
): PresentedProxyHeaders {
|
||||
const rawSecret = headers[TRUSTED_PROXY_SECRET_HEADER];
|
||||
const rawUser = headers[userHeader];
|
||||
if (raw) stripRawHeaderViews(raw, [TRUSTED_PROXY_SECRET_HEADER, DEFAULT_TRUSTED_PROXY_USER_HEADER, userHeader]);
|
||||
delete headers[TRUSTED_PROXY_SECRET_HEADER];
|
||||
delete headers[DEFAULT_TRUSTED_PROXY_USER_HEADER];
|
||||
delete headers[userHeader];
|
||||
|
||||
@@ -181,7 +181,7 @@ const PROXY_LAST_LOGIN_INTERVAL_MS = 15 * 60 * 1000;
|
||||
function registerTrustedProxyHeaderStrip(app: FastifyInstance): void {
|
||||
const userHeader = resolveTrustedProxyUserHeader();
|
||||
app.addHook('onRequest', (req, _reply, done) => {
|
||||
presentedProxyHeaders.set(req, takeTrustedProxyHeaders(req.headers, userHeader));
|
||||
presentedProxyHeaders.set(req, takeTrustedProxyHeaders(req.headers, userHeader, req.raw));
|
||||
done();
|
||||
});
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user