-
-
Notifications
You must be signed in to change notification settings - Fork 36.2k
net: reject non-address SocketAddress.parse input #64832
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,7 @@ | |
|
|
||
| const { | ||
| ObjectSetPrototypeOf, | ||
| RegExpPrototypeExec, | ||
| Symbol, | ||
| } = primordials; | ||
|
|
||
|
|
@@ -42,6 +43,11 @@ const { URLParse } = require('internal/url'); | |
| const kHandle = Symbol('kHandle'); | ||
| const kDetail = Symbol('kDetail'); | ||
|
|
||
| // The complete character set of an "${address}:${port}" input. Everything else | ||
| // is rejected: delimiters that would introduce a userinfo, path, query, or | ||
| // fragment component, and characters the URL parser strips, decodes, or remaps. | ||
| const kValidInput = /^[0-9a-fA-FxX.:[\]]+$/; | ||
|
|
||
| class SocketAddress { | ||
| static isSocketAddress(value) { | ||
| return value?.[kHandle] !== undefined; | ||
|
|
@@ -149,6 +155,10 @@ class SocketAddress { | |
| */ | ||
| static parse(input) { | ||
| validateString(input, 'input'); | ||
| // The URL parser below silently discards anything that is not the host or | ||
| // port, and percent-decodes and IDNA-maps what remains, so restrict the | ||
| // input to characters that can appear in an address and port first. | ||
| if (RegExpPrototypeExec(kValidInput, input) === null) return; | ||
|
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 only downside is that this matches hostname-like strings like Should probably also check after parsing that
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. Or indeed could just validate |
||
| // While URL.parse is not expected to throw, there are several | ||
| // other pieces here that do... the destucturing, the SocketAddress | ||
| // constructor, etc. So we wrap this in a try/catch to be safe. | ||
|
|
||
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.
I would personally suggest something like
^ https://url.spec.whatwg.org/#host-parsing