Skip to content

lib: reject string "0" in validatePort when allowZero is false#64174

Merged
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
watilde:fix-validate-port-string-zero
Jul 6, 2026
Merged

lib: reject string "0" in validatePort when allowZero is false#64174
nodejs-github-bot merged 1 commit into
nodejs:mainfrom
watilde:fix-validate-port-string-zero

Conversation

@watilde

@watilde watilde commented Jun 27, 2026

Copy link
Copy Markdown
Member

The allowZero guard compared the raw value with port === 0, but validatePort accepts strings and coerces them with +port in every other clause. Since '0' === 0 is false, string forms of zero ('0', ' 0 ', '00', '0x0', ...) slipped past the guard when allowZero was false, while the numeric 0 was correctly rejected.

This is reachable via dgram's send(), connect(), and bind(), which call validatePort(port, 'Port', false): passing '0' was silently accepted instead of throwing ERR_SOCKET_BAD_PORT.

Coerce the value with +port so the zero check matches the rest of the validation.

The allowZero guard compared the raw value with `port === 0`, but
validatePort accepts strings and coerces them with `+port` in every
other clause. Since `'0' === 0` is false, string forms of zero
('0', ' 0 ', '00', '0x0', ...) slipped past the guard when
allowZero was false, while the numeric 0 was correctly rejected.

This is reachable via dgram's send(), connect(), and bind(), which
call validatePort(port, 'Port', false): passing '0' was silently
accepted instead of throwing ERR_SOCKET_BAD_PORT.

Coerce the value with `+port` so the zero check matches the rest of
the validation.

Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com>
@nodejs-github-bot nodejs-github-bot added the needs-ci PRs that need a full CI run. label Jun 27, 2026
@watilde watilde added request-ci Add this label to start a Jenkins CI on a PR. review wanted PRs that need reviews. labels Jun 29, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jul 4, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@richardlau richardlau added the commit-queue Add this label to land a pull request using GitHub Actions. label Jul 6, 2026
@nodejs-github-bot nodejs-github-bot removed the commit-queue Add this label to land a pull request using GitHub Actions. label Jul 6, 2026
@nodejs-github-bot nodejs-github-bot merged commit 1677526 into nodejs:main Jul 6, 2026
78 checks passed
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Landed in 1677526

@watilde watilde deleted the fix-validate-port-string-zero branch July 6, 2026 13:04
richardlau pushed a commit that referenced this pull request Jul 6, 2026
The allowZero guard compared the raw value with `port === 0`, but
validatePort accepts strings and coerces them with `+port` in every
other clause. Since `'0' === 0` is false, string forms of zero
('0', ' 0 ', '00', '0x0', ...) slipped past the guard when
allowZero was false, while the numeric 0 was correctly rejected.

This is reachable via dgram's send(), connect(), and bind(), which
call validatePort(port, 'Port', false): passing '0' was silently
accepted instead of throwing ERR_SOCKET_BAD_PORT.

Coerce the value with `+port` so the zero check matches the rest of
the validation.

Signed-off-by: Daijiro Wachi <daijiro.wachi@gmail.com>
PR-URL: #64174
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Richard Lau <richard.lau@ibm.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-ci PRs that need a full CI run. review wanted PRs that need reviews.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants