Skip to content

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

Open
watilde wants to merge 1 commit into
nodejs:mainfrom
watilde:fix-validate-port-string-zero
Open

lib: reject string "0" in validatePort when allowZero is false#64174
watilde wants to merge 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
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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants