-
Notifications
You must be signed in to change notification settings - Fork 30.1k
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
net: Validate port in createServer().listen() #5732
Closed
Closed
Changes from all commits
Commits
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 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 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 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 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,27 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const net = require('net'); | ||
|
||
const invalidPort = -1 >>> 0; | ||
const errorMessage = /"port" argument must be \>= 0 and \< 65536/; | ||
|
||
net.Server().listen(common.PORT, function() { | ||
assert.equal(this._connectionKey, '6::::' + common.PORT); | ||
this.close(); | ||
}); | ||
|
||
// The first argument is a configuration object | ||
assert.throws(() => { | ||
net.Server().listen({ port: invalidPort }, common.fail); | ||
}, errorMessage); | ||
|
||
// The first argument is the port, no IP given. | ||
assert.throws(() => { | ||
net.Server().listen(invalidPort, common.fail); | ||
}, errorMessage); | ||
|
||
// The first argument is the port, the second an IP. | ||
assert.throws(() => { | ||
net.Server().listen(invalidPort, '0.0.0.0', common.fail); | ||
}, errorMessage); |
This file contains 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
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.
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.
Can I remove
typeof port !== 'undefined' &&
since #5733 already landed?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 don't think so. The changes in #5733 make
isLegalPort()
validate the value ofport
.undefined
isn't a valid port, and is specific to this use case.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.
Although you could change it to
port === undefined
:-)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.
-1... the current code allows
undefined
to pass through.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.
Is the following a test?
If
undefined
is meant to be allowed to pass through then should probably document that at least in the form of a test.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.
Looking at the code where
assertPort
is being used, we see:Notice that
listen
is called withh.port | 0
, so that whenh.port
is undefined,assertPort
falls through and it defaults to0
... specifically to account for when h.port is not explicitly specified.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.
@trevnorris: we do have a test that uses
undefined
(https://github.com/dirceu/node/blob/fix-5727/test/parallel/test-net-listen-port-option.js#L7). Should I change it to explicitly check if the port is a number?@jasnell: got it. Speaking of which, wouldn't be clearer to use something like this?
Otherwise, is there anything else that could be improved on the PR?
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.
Sorry, just getting back to this one. Since #5733 landed,
isLegalPort(undefined)
now returns false. @dirceu, not certain if that changes any of the details that you're doing here. Can you take another look?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.
@jasnell I just rebased with master and everything seems to be OK.