Skip to content
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

fix: Improve set_port validation and get_host behavior #829

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/ada_c.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,19 @@ bool ada_set_port(ada_url result, const char* input, size_t length) noexcept {
if (!r) {
return false;
}
return r->set_port(std::string_view(input, length));

std::string_view port_input(input, length);

if (port_input.find_first_not_of("0123456789") != std::string_view::npos) {
return false;
}

int port = std::stoi(std::string(port_input));
if (port < 1 || port > 65535) {
return false;
}

return r->set_port(port_input);
anonrig marked this conversation as resolved.
Show resolved Hide resolved
}

bool ada_set_pathname(ada_url result, const char* input,
Expand Down
7 changes: 7 additions & 0 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,13 @@ bool url_aggregator::set_port(const std::string_view input) {
// Revert changes if parse_port fails.
uint32_t previous_port = components.port;
parse_port(trimmed);
anonrig marked this conversation as resolved.
Show resolved Hide resolved

std::string port_value = std::string(trimmed);
int port = std::stoi(port_value);
if (port < 1 || port > 65535) {
return false;
}

if (is_valid) {
return true;
}
Expand Down
1 change: 1 addition & 0 deletions tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ TYPED_TEST(basic_tests, insane_url) {
ASSERT_EQ(r->get_hostname(), "");
ASSERT_EQ(r->get_port(), "");
ASSERT_EQ(r->get_pathname(), "@EEEEEEEEEE");
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
ASSERT_FALSE(r->set_port("invalid80"));
SUCCEED();
}

Expand Down