Skip to content

net: reject non-address SocketAddress.parse input - #64832

Open
araujogui wants to merge 1 commit into
nodejs:mainfrom
araujogui:socketaddress-parse-reject-url-components
Open

net: reject non-address SocketAddress.parse input#64832
araujogui wants to merge 1 commit into
nodejs:mainfrom
araujogui:socketaddress-parse-reject-url-components

Conversation

@araujogui

Copy link
Copy Markdown
Member

No description provided.

Signed-off-by: Guilherme Araújo <arauujogui@gmail.com>
Copilot AI review requested due to automatic review settings July 30, 2026 00:59
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

Review requested:

  • @nodejs/net

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@nodejs-github-bot nodejs-github-bot added needs-ci PRs that need a full CI run. net Issues and PRs related to the net subsystem. labels Jul 30, 2026

@Renegade334 Renegade334 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would make the port issue much simpler with this additional validation. If the string ends in /:(\d+)$/, then it can just be sliced off, and if the rest of the string gets parsed as a valid hostname by URLParse then the port can be obtained from the digit string with NumberParseInt.

// 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;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only downside is that this matches hostname-like strings like cabbage.ca, which will be parsed by URLParse and returned from this method as "IPv4 addresses".

Should probably also check after parsing that url.hostname passes isIPv4/isIPv6 as appropriate.

@Renegade334 Renegade334 Jul 30, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or indeed could just validate url.hostname with the simpler /^\[|\.\d+$/ since anything matching those that's not a valid IPv4/IPv6 address would already have been rejected by URLParse.

@codecov

codecov Bot commented Jul 30, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 90.17%. Comparing base (ede6d6a) to head (7788bf4).
⚠️ Report is 33 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #64832      +/-   ##
==========================================
- Coverage   91.90%   90.17%   -1.73%     
==========================================
  Files         383      746     +363     
  Lines      170878   242774   +71896     
  Branches    26181    45746   +19565     
==========================================
+ Hits       157051   218931   +61880     
- Misses      13527    15341    +1814     
- Partials      300     8502    +8202     
Files with missing lines Coverage Δ
lib/internal/socketaddress.js 99.03% <100.00%> (+0.04%) ⬆️

... and 496 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread doc/api/net.md
* Returns: {net.SocketAddress} Returns a `SocketAddress` if parsing was successful.
Otherwise returns `undefined`.

The `input` may contain only hexadecimal digits, `x`, `.`, `:`, `[`, and `]`.

Copy link
Copy Markdown
Member

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

The address portion of `input` must be a valid IPv4 or IPv6 hostname as recognized by the [WHATWG URL parser][]`.

^ https://url.spec.whatwg.org/#host-parsing

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. net Issues and PRs related to the net subsystem.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants