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 #839

Merged
merged 18 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all 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
9 changes: 3 additions & 6 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,12 +782,9 @@ bool url::set_port(const std::string_view input) {
port = std::nullopt;
return true;
}
// Input should not start with control characters.
if (ada::unicode::is_c0_control_or_space(trimmed.front())) {
return false;
}
// Input should contain at least one ascii digit.
if (input.find_first_of("0123456789") == std::string_view::npos) {

// Input should not start with a non-digit character.
if (!ada::unicode::is_ascii_digit(trimmed.front())) {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand Down
9 changes: 3 additions & 6 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,9 @@ bool url_aggregator::set_port(const std::string_view input) {
clear_port();
return true;
}
// Input should not start with control characters.
if (ada::unicode::is_c0_control_or_space(trimmed.front())) {
return false;
}
// Input should contain at least one ascii digit.
if (input.find_first_of("0123456789") == std::string_view::npos) {

// Input should not start with a non-digit character.
if (!ada::unicode::is_ascii_digit(trimmed.front())) {
anonrig marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

Expand Down
15 changes: 14 additions & 1 deletion tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,17 @@ TYPED_TEST(basic_tests, negativeport) {
auto url = ada::parse<TypeParam>("https://www.google.com");
ASSERT_FALSE(url->set_port("-1"));
SUCCEED();
}
}

// https://github.com/ada-url/ada/issues/826
TYPED_TEST(basic_tests, set_invalid_port) {
auto url = ada::parse<TypeParam>("fake://dummy.test");
ASSERT_TRUE(url);
ASSERT_FALSE(url->set_port("invalid80"));
ASSERT_EQ(url->get_port(), "");
ASSERT_TRUE(url->set_port("80valid"));
ASSERT_TRUE(url->is_valid);
ASSERT_EQ(url->get_port(), "80");
ASSERT_TRUE(url->is_valid);
SUCCEED();
}
Loading