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 6 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
12 changes: 10 additions & 2 deletions src/url.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -782,12 +782,20 @@ bool url::set_port(const std::string_view input) {
port = std::nullopt;
return true;
}

auto non_digit_pos = std::find_if_not(trimmed.begin(), trimmed.end(),
anonrig marked this conversation as resolved.
Show resolved Hide resolved
ada::unicode::is_ascii_digit);
if (non_digit_pos != trimmed.end()) {
trimmed.erase(non_digit_pos, trimmed.end());
}

mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
// 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 contain ascii digits.
if (!std::ranges::all_of(trimmed, ada::unicode::is_ascii_digit)) {
return false;
}
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
12 changes: 10 additions & 2 deletions src/url_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,12 +285,20 @@ bool url_aggregator::set_port(const std::string_view input) {
clear_port();
return true;
}

auto non_digit_pos = std::find_if_not(trimmed.begin(), trimmed.end(),
ada::unicode::is_ascii_digit);
if (non_digit_pos != trimmed.end()) {
trimmed.erase(non_digit_pos, trimmed.end());
}

mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
// 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 contain ascii digits.
if (!std::ranges::all_of(trimmed, ada::unicode::is_ascii_digit)) {
return false;
}
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved

Expand Down
11 changes: 10 additions & 1 deletion tests/basic_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -462,4 +462,13 @@ 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"));
mertcanaltin marked this conversation as resolved.
Show resolved Hide resolved
ASSERT_TRUE(url->is_valid);
SUCCEED();
}
Loading