fix(server): validate X-Request-Id charset, not just length (#314)#315
Merged
Conversation
The `genReqId` in server.js + the duplicated test scaffolding in
tests/api/request-id.test.js previously trusted any incoming
`X-Request-Id` header whose length was 1..128 chars. That includes:
- Whitespace (`"has spaces"`) — accepted by Node's setHeader but
invalid per RFC 7230 token grammar and a real foothold for
header-continuation smuggling through proxies that don't
sanitize.
- Tabs / control chars / CR / LF — these cause Node's
`res.setHeader` to throw `ERR_INVALID_CHAR`. Inside the
pino-http request hook, that throw surfaces as an unhandled
500 from a header the client controlled. Worse, the absence
of a proper request id in the response makes the resulting
server log line harder to correlate.
Tighten the guard to the W3C trace-id alphabet (printable ASCII
[0x21..0x7e], no spaces, no controls). Anything outside that range
falls through to the UUID fallback the same way an empty or
oversized incoming value already does.
Tests:
- Replace the inline `genReqId` in tests/api/request-id.test.js
to mirror the new logic (the duplication is documented in the
file — it stands up its own app harness to isolate the hook).
- Add a test for the whitespace + tab cases that drive the new
regex branch.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #314.
Summary
Tightens
genReqIdin server.js (and the duplicated logic intests/api/request-id.test.js) to validate the incomingX-Request-Idagainst the W3C trace-id alphabet — printable ASCII[0x21..0x7e], no spaces, no controls. Anything outside falls back to a UUID.Old behavior accepted whitespace (smuggling foothold) and control chars (Node's
setHeaderthrows → unhandled 500).Test plan
npm run lint && npm test— 780 passing (was 778). 1 new case covering the whitespace/tab path; the existing oversized + missing tests still cover length boundaries.Proudly Made in Nebraska. Go Big Red! 🌽 https://xkcd.com/2347/