fix: sanitize invalid characters in proxied response headers#472
Merged
mcollina merged 1 commit intoJul 12, 2026
Merged
Conversation
An upstream reply can contain header values with characters that are invalid in an outgoing HTTP header (control characters, or code points above 0xFF). Copying such a value onto the downstream reply throws 'Invalid character in header content' and fails the entire request. Strip those characters from response header values (strings and arrays such as set-cookie) in rewriteHeaders, so proxying degrades gracefully instead of crashing. Closes fastify#343
There was a problem hiding this comment.
Pull request overview
This PR prevents proxied requests from failing when an upstream response contains header values with characters that Node.js rejects (e.g., control chars or code points > 0xFF). It adds header-value sanitization utilities and applies them in the proxy response header rewrite path so invalid characters are stripped instead of crashing the entire request.
Changes:
- Added
sanitizeHeaderValueandsanitizeHeadershelpers to strip invalid HTTP header value characters (per Node/RFC7230-compatible ranges). - Applied
sanitizeHeadersinside the proxyrewriteHeadershook so downstream header setting won’t throw on invalid upstream values. - Added unit tests covering string and array header values (including control chars and high Unicode).
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| utils.js | Adds header-value sanitization helpers and exports them. |
| index.js | Applies sanitization to rewritten proxied response headers before returning them to reply-from. |
| test/utils.js | Adds unit tests validating sanitization behavior for string and array header values. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Description
When proxying, an upstream response can include a header whose value contains characters that are invalid in an outgoing HTTP header — control characters, or code points above
0xFF(e.g. an emoji or certain Unicode characters a service put in a header). Copying such a value onto the downstream reply makes Node throwInvalid character in header content [...], which fails the entire proxied request rather than just the offending header.This sanitizes response header values inside
rewriteHeadersbefore they are set downstream. Values outside the set allowed by RFC 7230 field-content — HTAB (0x09), visible ASCII/space (0x20–0x7E), and obs-text (0x80–0xFF) — are stripped, so a bad upstream header degrades gracefully instead of taking down the request. Both string values and array values (e.g.set-cookie) are handled.Closes #343
How Has This Been Tested?
utilshelpers:sanitizeHeaderValue— keeps normal values, tab, and obs-text; strips control characters, high-Unicode characters, and code points above0xFF.sanitizeHeaders— sanitizes string and array header values, leaving non-string values untouched.npm test(lint + tests + 100% coverage + TypeScript).Checklist
npm testpasses (lint, tests, coverage, types)