Repository navigation
http: cache header validation in setHeader() #67232
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
araujogui
wants to merge
5
commits into
nodejs:main
Choose a base branch
from
araujogui:http-cache-header-validation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+251
−18
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d14f709
http: cache header validation in setHeader()
araujogui bc9874b
http: add eviction to header name cache
araujogui a0bc32d
http: sample header name cache admissions
araujogui ab4fe1b
http: do not cache credential header values
araujogui 503f25f
http: do not cache Proxy-Authorization values
araujogui File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,74 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common.js'); | ||
| const http = require('http'); | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| headers: [2, 12], | ||
| values: ['static', 'dynamic'], | ||
| // 'many' rotates through more unique names than the validation cache holds. | ||
| names: ['fixed', 'many'], | ||
| // Also set headers carrying credentials, whose values are never cached. | ||
| credentials: ['none', 'static', 'dynamic'], | ||
| duration: 5, | ||
| }); | ||
|
|
||
| const credentialHeaders = [ | ||
| ['Authorization', 'Bearer eyJhbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIn0.c2lnbmF0dXJl'], | ||
| ['Cookie', 'sid=0123456789abcdef0123456789abcdef; theme=dark'], | ||
| ['Set-Cookie', 'sid=0123456789abcdef0123456789abcdef; Path=/; HttpOnly; Secure'], | ||
| ]; | ||
|
|
||
| const manyNames = []; | ||
| for (let i = 0; i < 1024; i++) { | ||
| manyNames.push(`X-Custom-Header-${i}`); | ||
| } | ||
|
|
||
| const restHeaders = [ | ||
| ['Content-Type', 'application/json; charset=utf-8'], | ||
| ['Cache-Control', 'no-store'], | ||
| ['X-Content-Type-Options', 'nosniff'], | ||
| ['X-Frame-Options', 'DENY'], | ||
| ['Strict-Transport-Security', 'max-age=31536000; includeSubDomains'], | ||
| ['Access-Control-Allow-Origin', '*'], | ||
| ['Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE'], | ||
| ['Vary', 'Origin, Accept-Encoding'], | ||
| ['X-RateLimit-Limit', '1000'], | ||
| ['X-RateLimit-Remaining', '999'], | ||
| ['ETag', 'W/"2a-abc"'], | ||
| ['X-Request-Id', '00000000'], | ||
| ]; | ||
|
|
||
| function main({ headers, values, names, credentials, duration }) { | ||
| const list = restHeaders.slice(0, headers); | ||
| const body = '{"ok":true}'; | ||
| let id = 0; | ||
| let next = 0; | ||
|
|
||
| const server = http.createServer((req, res) => { | ||
| for (const { 0: name, 1: value } of list) { | ||
| res.setHeader( | ||
| names === 'many' ? manyNames[next++ % manyNames.length] : name, | ||
| values === 'dynamic' ? `${value}-${id}` : value, | ||
| ); | ||
| } | ||
| if (credentials !== 'none') { | ||
| for (const { 0: name, 1: value } of credentialHeaders) { | ||
| res.setHeader(name, credentials === 'dynamic' ? `${value}-${id}` : value); | ||
| } | ||
| } | ||
| id++; | ||
| res.end(body); | ||
| }); | ||
|
|
||
| server.listen(0, () => { | ||
| bench.http({ | ||
| path: '/', | ||
| connections: 100, | ||
| duration, | ||
| port: server.address().port, | ||
| }, () => { | ||
| server.close(); | ||
| }); | ||
| }); | ||
| } |
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common'); | ||
| const assert = require('assert'); | ||
| const http = require('http'); | ||
|
|
||
| { | ||
| // Rotate the name cache through several generations. | ||
| const msg = new http.OutgoingMessage(); | ||
| for (let i = 0; i < 5000; i++) { | ||
| msg.setHeader(`X-Many-${i}`, 'v'); | ||
| } | ||
| msg.setHeader('X-Many-0', 'evicted'); | ||
| msg.setHeader('X-Many-4999', 'cached'); | ||
| assert.strictEqual(msg.getHeader('x-many-0'), 'evicted'); | ||
| assert.strictEqual(msg.getHeader('x-many-4999'), 'cached'); | ||
| // Names that were not admitted to the cache still validate values. | ||
| for (let i = 0; i < 8; i++) { | ||
| assert.throws(() => { | ||
| msg.setHeader(`X-Uncached-${i}`, 'bad\u0007value'); | ||
| }, { code: 'ERR_INVALID_CHAR' }); | ||
| } | ||
| assert.throws(() => { | ||
| msg.setHeader('Bad Header', 'v'); | ||
| }, { code: 'ERR_INVALID_HTTP_TOKEN' }); | ||
| } | ||
|
|
||
| { | ||
| // Values of headers carrying credentials are not cached, and are validated | ||
| // on every call. | ||
| for (const name of [ | ||
| 'Authorization', | ||
| 'Cookie', | ||
| 'Proxy-Authorization', | ||
| 'Set-Cookie', | ||
| 'set-cookie', | ||
| ]) { | ||
| const msg = new http.OutgoingMessage(); | ||
| // Set the name often enough for it to be admitted to the name cache. | ||
| for (let i = 0; i < 16; i++) { | ||
| msg.setHeader(name, 'secret=1'); | ||
| } | ||
| assert.throws(() => { | ||
| msg.setHeader(name, 'bad\u0007value'); | ||
| }, { code: 'ERR_INVALID_CHAR' }); | ||
| msg.setHeader(name, 'secret=1'); | ||
| assert.strictEqual(msg.getHeader(name), 'secret=1'); | ||
| msg.appendHeader(name, 'secret=2'); | ||
| assert.deepStrictEqual(msg.getHeader(name), ['secret=1', 'secret=2']); | ||
| } | ||
| } | ||
|
|
||
| const server = http.createServer(common.mustCall((req, res) => { | ||
| res.setHeader('Content-Type', 'application/json; charset=utf-8'); | ||
| res.setHeader('X-Test', 'valid-value'); | ||
| res.setHeader('Content-Type', 'application/json; charset=utf-8'); | ||
| res.setHeader('X-Test', 'valid-value'); | ||
|
|
||
| assert.throws(() => { | ||
| res.setHeader('X-Test', 'bad\u0007value'); | ||
| }, { code: 'ERR_INVALID_CHAR' }); | ||
|
|
||
| const cookies = ['a=1', 'b=2']; | ||
| res.setHeader('Set-Cookie', cookies); | ||
| cookies[1] = 'bad\r\nvalue'; | ||
| assert.throws(() => { | ||
| res.setHeader('Set-Cookie', cookies); | ||
| }, { code: 'ERR_INVALID_CHAR' }); | ||
| res.setHeader('Set-Cookie', 'ok=1'); | ||
|
|
||
| assert.throws(() => { | ||
| res.setHeader('Bad Header', 'v'); | ||
| }, { code: 'ERR_INVALID_HTTP_TOKEN' }); | ||
|
|
||
| res.end('ok'); | ||
| })); | ||
|
|
||
| server.listen(0, common.mustCall(() => { | ||
| http.get({ port: server.address().port }, common.mustCall((res) => { | ||
| assert.strictEqual(res.statusCode, 200); | ||
| res.resume(); | ||
| res.on('end', common.mustCall(() => server.close())); | ||
| })); | ||
| })); |
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.