Skip to content

Commit

Permalink
fix: Improve set_port validation (#839)
Browse files Browse the repository at this point in the history
  • Loading branch information
mertcanaltin authored Jan 12, 2025
1 parent f1eee2a commit 0394a7c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
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())) {
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())) {
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();
}

0 comments on commit 0394a7c

Please sign in to comment.