Repository navigation
Fixed invalid path in CONNECT method requests. #34412
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
Changes from 1 commit
edd8f6e
0825564
247401a
989cecd
e1283be
669ee65
c9f1597
d8039e3
9426cbb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -49,14 +49,18 @@ const Agent = require('_http_agent'); | |||||
| const { Buffer } = require('buffer'); | ||||||
| const { defaultTriggerAsyncIdScope } = require('internal/async_hooks'); | ||||||
| const { URL, urlToOptions, searchParamsSymbol } = require('internal/url'); | ||||||
| const { kOutHeaders, kNeedDrain, validatePath } = require('internal/http'); | ||||||
| const { kOutHeaders, | ||||||
| kNeedDrain, | ||||||
| isValidCONNECTPath | ||||||
| } = require('internal/http'); | ||||||
| const { connResetException, codes } = require('internal/errors'); | ||||||
| const { | ||||||
| ERR_HTTP_HEADERS_SENT, | ||||||
| ERR_INVALID_ARG_TYPE, | ||||||
| ERR_INVALID_HTTP_TOKEN, | ||||||
| ERR_INVALID_PROTOCOL, | ||||||
| ERR_UNESCAPED_CHARACTERS | ||||||
| ERR_UNESCAPED_CHARACTERS, | ||||||
| ERR_INVALID_ARG_VALUE | ||||||
| } = codes; | ||||||
| const { validateInteger } = require('internal/validators'); | ||||||
| const { getTimerDuration } = require('internal/timers'); | ||||||
|
|
@@ -197,12 +201,25 @@ function ClientRequest(input, options, cb) { | |||||
| } | ||||||
| this.insecureHTTPParser = insecureHTTPParser; | ||||||
|
|
||||||
| this.path = (options.path && method === 'CONNECT' && | ||||||
| options.path.charAt(0) === '/' ? | ||||||
| options.path.slice(1) : options.path) || '/'; | ||||||
|
|
||||||
| if (method === 'CONNECT' && this.path !== '/') { | ||||||
| validatePath(this.path); | ||||||
| path = options.path; | ||||||
| if (path) { | ||||||
| if (method === 'CONNECT') { | ||||||
| if (path.charAt(0) === '/') { | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Arguably a little more idiomatic for core code. |
||||||
| path = path.slice(1) || '/'; | ||||||
| if (!isValidCONNECTPath(path)) { | ||||||
| throw new ERR_INVALID_ARG_VALUE('options.path', | ||||||
| path, | ||||||
| 'must be a valid host:port combo'); | ||||||
| } | ||||||
| this.path = path; | ||||||
| } else { | ||||||
| this.path = path; | ||||||
| } | ||||||
| } else { | ||||||
| this.path = path; | ||||||
| } | ||||||
| } else { | ||||||
| this.path = '/'; | ||||||
| } | ||||||
|
|
||||||
| if (cb) { | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,12 +6,8 @@ const { | |
|
|
||
| const { setUnrefTimeout } = require('internal/timers'); | ||
| const { PerformanceEntry, notify } = internalBinding('performance'); | ||
| const { | ||
| codes: { | ||
| ERR_INVALID_ARG_VALUE | ||
| } | ||
| } = require('internal/errors'); | ||
|
|
||
| const VALID_PATH_REGEX = /^[_0-9A-Za-z]+(?:\.[_0-9A-Za-z]+)*\.?:(?:[1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Benchmarks tests may be required to assess whether performance has been affected.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @rickyes Could you guide me more about these benchmark tests, as how to write one?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @preyunk Usually there is no need to write additional
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The regex here does not properly account for the possibility of IPv6 addresses. For instance, new URL(`http://${path}`)If creating the URL is successful, check that the
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also you need to check that |
||
| let nowCache; | ||
| let utcCache; | ||
|
|
||
|
|
@@ -37,13 +33,11 @@ function resetCache() { | |
| utcCache = undefined; | ||
| } | ||
|
|
||
| function validatePath(path) { | ||
| const pathRegex = /^[_0-9A-Za-z]+(\.[_0-9A-Za-z]+)*\.?:([1-9]|[1-9][0-9]{1,2}|[1-5][0-9]{3}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/; | ||
| if (!pathRegex.test(path)) { | ||
| throw new ERR_INVALID_ARG_VALUE('options.path', | ||
| path, 'must be a valid host:port combo'); | ||
| function isValidCONNECTPath(path) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It might be better to use the |
||
| if (!VALID_PATH_REGEX.test(path)) { | ||
| return false; | ||
| } | ||
| return path; | ||
| return true; | ||
| } | ||
|
|
||
| class HttpRequestTiming extends PerformanceEntry { | ||
|
|
@@ -68,5 +62,5 @@ module.exports = { | |
| nowDate, | ||
| utcDate, | ||
| emitStatistics, | ||
| validatePath | ||
| isValidCONNECTPath | ||
| }; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.