Skip to content

Commit b16ea0f

Browse files
committed
Tighten up header validation to match Node
In future, if we successfully ship Node lenient mode, this could be relaxed in turn, but for now this is what stops us forwarding content Node will reject, so we do need to handle it identically.
1 parent 16747d3 commit b16ea0f

1 file changed

Lines changed: 5 additions & 1 deletion

File tree

src/util/header-utils.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,15 @@ export function dropDefaultHeaders(response: OngoingResponse) {
286286
// RFC 7230 token char set — anything outside this in a name is invalid.
287287
const VALID_TOKEN_CHARS = /^[!#$%&'*+\-.^_`|~0-9A-Za-z]+$/;
288288

289+
// RFC 7230 field-value chars: HTAB / SP / VCHAR / obs-text. I.e. tab, plus
290+
// 0x20–0x7E, plus 0x80–0xFF. Matches Node's checkInvalidHeaderChar.
291+
const INVALID_VALUE_CHAR = /[^\t\x20-\x7E\x80-\xFF]/;
292+
289293
export function validateHeader(name: string, value: string | string[]): boolean {
290294
if (typeof name !== 'string' || !VALID_TOKEN_CHARS.test(name)) return false;
291295
const values = Array.isArray(value) ? value : [value];
292296
for (const v of values) {
293-
if (typeof v !== 'string' || /[\r\n]/.test(v)) return false;
297+
if (typeof v !== 'string' || INVALID_VALUE_CHAR.test(v)) return false;
294298
}
295299
return true;
296300
}

0 commit comments

Comments
 (0)