Node.js Reference ยท HTTP, HTTPS, net, TLS, dgram and dns
Node.js http.validateHeaderName() and validateHeaderValue()
Reference for http.validateHeaderName() and http.validateHeaderValue() in Node.js: check header names and values before use and avoid header injection.
http.validateHeaderName(name) and http.validateHeaderValue(name, value) run the same checks that setHeader() and writeHead() run internally, but on their own. Each returns nothing when the input is fine and throws an error with a specific code when it is not.
They let you check user-supplied or computed headers early, for example before you store them in a config, so a bad value fails at the place that created it instead of deep inside a response handler. They also make header injection visible: a value with a line break would let an attacker add headers, and the validator rejects it.
http.validateHeaderName('X-Request-Id'); http.validateHeaderValue('X-Request-Id', 'abc-123');
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.
Syntax
http.validateHeaderName(name[, label]) http.validateHeaderValue(name, value)
| Name | Description |
|---|---|
name |
The header name. Must be a non-empty string made only of HTTP token characters (letters, digits and a few symbols such as -, _ and .; no spaces, colons or non-ASCII characters). |
label |
Optional text used at the start of the error message instead of "Header name" (handy for wrappers). |
value |
The header value. Must be defined and must not contain characters that are not allowed in a header value, such as line breaks. |
Returns: undefined when the input is valid.
Errors:
ERR_INVALID_HTTP_TOKENwhen the name is empty, not a string, or contains characters such as a space or colon.ERR_HTTP_INVALID_HEADER_VALUEwhen the value isundefined.ERR_INVALID_CHARwhen the value contains a forbidden character such as a carriage return, a line feed or a character above code point 255.
Usage notes
- Valid does not mean safe or meaningful. The checks cover syntax only.
Content-Length: bananais syntactically fine. - Values may be strings, numbers or arrays.
setHeader()converts numbers to strings and sends an array as repeated headers; the validator accepts all three shapes as long as no character is forbidden. - Case does not matter for names; HTTP header names are case-insensitive and Node lower-cases them on the receiving side.
- Request headers get the same checks when you call
http.request()with aheadersoption, so an invalid name or value throws synchronously there. - You rarely need these when you only use
setHeader(), because it validates for you. They are most useful when headers are collected earlier, such as in configuration or a plugin API. - Always validate or encode user input before it ends up in a header; redirect
Locationvalues and cookie names are the usual culprits.