Repository navigation
net: enforce strict socket address formats in SocketAddress.parse #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
Open
araujogui
wants to merge
6
commits into
nodejs:main
Choose a base branch
from
araujogui:socketaddress-parse-reject-url-components
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
5868481
net: reject non-address SocketAddress.parse input
araujogui 2d1242e
net: check hex hostname
araujogui 49dc7d8
net: check cabbage.ca
araujogui 8d276fd
net: implement a socket address parser
araujogui a936229
benchmark: cover rejected input in socket address parse
araujogui 7d1b20d
net: shrink SocketAddress.parse refactor diff
araujogui 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 hidden or 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,49 @@ | ||
| 'use strict'; | ||
|
|
||
| const common = require('../common.js'); | ||
| const { SocketAddress } = require('net'); | ||
|
|
||
| const inputs = { | ||
| 'ipv4': [ | ||
| '127.0.0.1', | ||
| '10.168.209.250', | ||
| '255.255.255.255', | ||
| ], | ||
| 'ipv4-port': [ | ||
| '127.0.0.1:80', | ||
| '10.168.209.250:8080', | ||
| '255.255.255.255:65535', | ||
| ], | ||
| 'ipv6': [ | ||
| '[::1]', | ||
| '[2001:db8::1]', | ||
| '[fe80::1ff:fe23:4567:890a]', | ||
| ], | ||
| 'ipv6-port': [ | ||
| '[::1]:80', | ||
| '[2001:db8::1]:8080', | ||
| '[::ffff:127.0.0.1]:65535', | ||
| ], | ||
| 'invalid': [ | ||
| 'localhost', | ||
| '0x7f.1', | ||
| '127.0.0.1:99999', | ||
| '[::1', | ||
| ], | ||
| }; | ||
|
|
||
| const bench = common.createBenchmark(main, { | ||
| n: [1e6], | ||
| input: Object.keys(inputs), | ||
| }); | ||
|
|
||
| function main({ n, input }) { | ||
| const values = inputs[input]; | ||
| const length = values.length; | ||
|
|
||
| bench.start(); | ||
| for (let i = 0; i < n; i++) { | ||
| SocketAddress.parse(values[i % length]); | ||
| } | ||
| bench.end(n); | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,136 @@ | ||
| #include "node_sockaddr_parser.h" | ||
|
|
||
| namespace node::sockaddr_parser { | ||
|
|
||
| namespace { | ||
|
|
||
| constexpr uint32_t kMaxPort = 65535; | ||
|
|
||
| // Not std::isdigit: that one is locale dependent. | ||
| std::optional<uint32_t> ToDigit(char c) { | ||
| if (c < '0' || c > '9') return std::nullopt; | ||
| return static_cast<uint32_t>(c - '0'); | ||
| } | ||
|
|
||
| // A cursor over the input. A Read that fails consumes nothing. | ||
| class Parser { | ||
| public: | ||
| explicit Parser(std::string_view input) : remaining_(input) {} | ||
|
|
||
| bool done() const { return remaining_.empty(); } | ||
|
|
||
| bool ReadChar(char expected) { | ||
| if (remaining_.empty() || remaining_.front() != expected) return false; | ||
| remaining_.remove_prefix(1); | ||
| return true; | ||
| } | ||
|
|
||
| std::optional<uint32_t> ReadNumber(uint32_t max_value); | ||
| std::optional<uint32_t> ReadTaggedNumber(char tag, uint32_t max_value); | ||
| std::optional<std::string_view> ReadHost(std::string_view delimiters); | ||
|
|
||
| std::optional<parse_result> ReadIPv4SocketAddress(); | ||
| std::optional<parse_result> ReadIPv6SocketAddress(); | ||
| std::optional<parse_result> ReadSocketAddress(); | ||
|
|
||
| private: | ||
| std::string_view remaining_; | ||
| }; | ||
|
|
||
| std::optional<uint32_t> Parser::ReadNumber(uint32_t max_value) { | ||
| const std::string_view start = remaining_; | ||
|
|
||
| uint64_t value = 0; | ||
| size_t digits = 0; | ||
|
|
||
| while (!remaining_.empty()) { | ||
| std::optional<uint32_t> digit = ToDigit(remaining_.front()); | ||
| if (!digit.has_value()) break; | ||
| remaining_.remove_prefix(1); | ||
| value = value * 10 + digit.value(); | ||
| digits++; | ||
| if (value > max_value) break; | ||
| } | ||
|
|
||
| if (digits == 0 || value > max_value) { | ||
| remaining_ = start; | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| return static_cast<uint32_t>(value); | ||
| } | ||
|
|
||
| std::optional<uint32_t> Parser::ReadTaggedNumber(char tag, uint32_t max_value) { | ||
| const std::string_view start = remaining_; | ||
|
|
||
| if (!ReadChar(tag)) return std::nullopt; | ||
|
|
||
| std::optional<uint32_t> value = ReadNumber(max_value); | ||
| if (!value.has_value()) remaining_ = start; | ||
|
|
||
| return value; | ||
| } | ||
|
|
||
| std::optional<std::string_view> Parser::ReadHost(std::string_view delimiters) { | ||
| const size_t end = remaining_.find_first_of(delimiters); | ||
| const std::string_view host = | ||
| end == std::string_view::npos ? remaining_ : remaining_.substr(0, end); | ||
|
|
||
| // uv_inet_pton reads only up to the first NUL. | ||
| if (host.size() > kMaxHostLength || | ||
| host.find('\0') != std::string_view::npos) { | ||
| return std::nullopt; | ||
| } | ||
|
|
||
| remaining_.remove_prefix(host.size()); | ||
|
|
||
| return host; | ||
| } | ||
|
|
||
| std::optional<parse_result> Parser::ReadIPv4SocketAddress() { | ||
| std::optional<std::string_view> host = ReadHost(":"); | ||
| if (!host.has_value()) return std::nullopt; | ||
|
|
||
| parse_result result = {}; | ||
| result.host = *host; | ||
| result.port = | ||
| static_cast<uint16_t>(ReadTaggedNumber(':', kMaxPort).value_or(0)); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| std::optional<parse_result> Parser::ReadIPv6SocketAddress() { | ||
| if (!ReadChar('[')) return std::nullopt; | ||
|
|
||
| // '%' is a delimiter so that a zone id can never reach uv_ip6_addr, which | ||
| // would resolve it with if_nametoindex. ReadChar(']') below rejects it. | ||
| std::optional<std::string_view> host = ReadHost("%]"); | ||
| if (!host.has_value()) return std::nullopt; | ||
|
|
||
| parse_result result = {}; | ||
| result.is_ipv6 = true; | ||
| result.host = *host; | ||
|
|
||
| if (!ReadChar(']')) return std::nullopt; | ||
|
|
||
| result.port = | ||
| static_cast<uint16_t>(ReadTaggedNumber(':', kMaxPort).value_or(0)); | ||
|
|
||
| return result; | ||
| } | ||
|
|
||
| std::optional<parse_result> Parser::ReadSocketAddress() { | ||
| return remaining_.starts_with('[') ? ReadIPv6SocketAddress() | ||
| : ReadIPv4SocketAddress(); | ||
| } | ||
|
|
||
| } // namespace | ||
|
|
||
| std::optional<parse_result> ParseSocketAddress(std::string_view input) { | ||
| Parser parser(input); | ||
| std::optional<parse_result> result = parser.ReadSocketAddress(); | ||
| if (!result.has_value() || !parser.done()) return std::nullopt; | ||
| return result; | ||
| } | ||
|
|
||
| } // namespace node::sockaddr_parser |
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.