Skip to content

http: add isValidHeaderName() and isValidHeaderValue() - #66334

Open
jasnell wants to merge 2 commits into
nodejs:mainfrom
jasnell:jasnell/http-header-validators
Open

jasnell wants to merge 2 commits into
nodejs:mainfrom
jasnell:jasnell/http-header-validators

Conversation

@jasnell

@jasnell jasnell commented Sep 27, 2026 •

Copy link
Copy Markdown
Member

Add non-throwing counterparts of http.validateHeaderName() and
http.validateHeaderValue() that return a boolean instead of throwing.

Rejecting an invalid header with the existing validators costs a few
microseconds rather than ~20ns for the boolean check. libraries like
undici keep private copies of the token and field-value tables from
_http_common. This allows them to reuse the core implementations.

Keep in mind, this is mostly to improve the performance of the invalid
path.

http/is_valid_header.js n=1000000 httpValidation="strict" input="valid" method="isValidHeaderName": 53,281,475.53882091
http/is_valid_header.js n=1000000 httpValidation="strict" input="invalid" method="isValidHeaderName": 55,459,748.83943543
http/is_valid_header.js n=1000000 httpValidation="strict" input="valid" method="validateHeaderName": 41,952,415.053701185
http/is_valid_header.js n=1000000 httpValidation="strict" input="invalid" method="validateHeaderName": 345,903.66184467427
http/is_valid_header.js n=1000000 httpValidation="strict" input="valid" method="isValidHeaderValue": 44,726,217.62766685
http/is_valid_header.js n=1000000 httpValidation="relaxed" input="valid" method="isValidHeaderValue": 39,695,759.00037822
http/is_valid_header.js n=1000000 httpValidation="strict" input="invalid" method="isValidHeaderValue": 39,393,631.16861916
http/is_valid_header.js n=1000000 httpValidation="relaxed" input="invalid" method="isValidHeaderValue": 39,331,495.16279641
http/is_valid_header.js n=1000000 httpValidation="strict" input="valid" method="validateHeaderValue": 35,809,327.506200835
http/is_valid_header.js n=1000000 httpValidation="strict" input="invalid" method="validateHeaderValue": 336,332.74856077647

@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/http
  • @nodejs/net
  • @nodejs/performance

@nodejs-github-bot nodejs-github-bot added http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. labels Sep 27, 2026
@jasnell
jasnell requested review from mcollina and ronag September 27, 2026 02:37
Add non-throwing counterparts of http.validateHeaderName() and
http.validateHeaderValue() that return a boolean instead of throwing.

Rejecting an invalid header with the existing validators costs a few
microseconds, because an error object and its stack trace are created,
compared to ~20ns for the boolean check. Userland HTTP implementations
such as undici (fetch Headers, request options) therefore keep private
copies of the token and field-value tables from _http_common. These new
functions let them reuse the core implementation.

isValidHeaderValue() accepts an optional `httpValidation` option
('strict' or 'relaxed') that has the same meaning as the option of the
same name on http.createServer() and http.request().

Signed-off-by: James M Snell <jasnell@gmail.com>
Compare http.isValidHeaderName() and http.isValidHeaderValue() with
http.validateHeaderName() and http.validateHeaderValue() wrapped in
try/catch, for valid and invalid input, and for both 'strict' and
'relaxed' header value validation.

Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell
jasnell force-pushed the jasnell/http-header-validators branch from 125da84 to 1b50a1d Compare September 27, 2026 02:42
@jasnell jasnell added the semver-minor PRs that contain new features and should be released in the next minor version. label Sep 27, 2026
@codecov

codecov Bot commented Sep 27, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.36%. Comparing base (cd908df) to head (1b50a1d).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66334      +/-   ##
==========================================
- Coverage   90.37%   90.36%   -0.02%     
==========================================
  Files         792      792              
  Lines      275324   275368      +44     
  Branches    52764    52770       +6     
==========================================
+ Hits       248828   248834       +6     
- Misses      16918    16960      +42     
+ Partials     9578     9574       -4     
Files with missing lines Coverage Δ
lib/_http_outgoing.js 97.95% <100.00%> (+0.05%) ⬆️
lib/http.js 98.85% <100.00%> (+0.01%) ⬆️

... and 32 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread lib/_http_outgoing.js
@jasnell jasnell added request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. author ready PRs with CI started, the required approvals, and no outstanding review comments. labels Sep 27, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Sep 27, 2026
@nodejs-github-bot

nodejs-github-bot commented Sep 27, 2026 •

Copy link
Copy Markdown
Collaborator

@jasnell jasnell added the commit-queue PRs queued for automated landing through the Commit Queue. label Sep 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs with CI started, the required approvals, and no outstanding review comments. commit-queue PRs queued for automated landing through the Commit Queue. http Issues and PRs related to the http subsystem. needs-ci PRs that need a full CI run. semver-minor PRs that contain new features and should be released in the next minor version.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants