Skip to content

Commit bd80521

Browse files
authored
fix(url): handle tab/newline-only inputs in set_port (#961)
1 parent dfed89b commit bd80521

File tree

3 files changed

+42
-4
lines changed

3 files changed

+42
-4
lines changed

src/url.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -776,10 +776,16 @@ bool url::set_port(const std::string_view input) {
776776
if (cannot_have_credentials_or_port()) {
777777
return false;
778778
}
779+
780+
if (input.empty()) {
781+
port = std::nullopt;
782+
return true;
783+
}
784+
779785
std::string trimmed(input);
780786
helpers::remove_ascii_tab_or_newline(trimmed);
787+
781788
if (trimmed.empty()) {
782-
port = std::nullopt;
783789
return true;
784790
}
785791

@@ -788,9 +794,15 @@ bool url::set_port(const std::string_view input) {
788794
return false;
789795
}
790796

797+
// Find the first non-digit character to determine the length of digits
798+
auto first_non_digit =
799+
std::ranges::find_if_not(trimmed, ada::unicode::is_ascii_digit);
800+
std::string_view digits_to_parse =
801+
std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
802+
791803
// Revert changes if parse_port fails.
792804
std::optional<uint16_t> previous_port = port;
793-
parse_port(trimmed);
805+
parse_port(digits_to_parse);
794806
if (is_valid) {
795807
return true;
796808
}

src/url_aggregator.cpp

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -280,10 +280,16 @@ bool url_aggregator::set_port(const std::string_view input) {
280280
if (cannot_have_credentials_or_port()) {
281281
return false;
282282
}
283+
284+
if (input.empty()) {
285+
clear_port();
286+
return true;
287+
}
288+
283289
std::string trimmed(input);
284290
helpers::remove_ascii_tab_or_newline(trimmed);
291+
285292
if (trimmed.empty()) {
286-
clear_port();
287293
return true;
288294
}
289295

@@ -292,9 +298,15 @@ bool url_aggregator::set_port(const std::string_view input) {
292298
return false;
293299
}
294300

301+
// Find the first non-digit character to determine the length of digits
302+
auto first_non_digit =
303+
std::ranges::find_if_not(trimmed, ada::unicode::is_ascii_digit);
304+
std::string_view digits_to_parse =
305+
std::string_view(trimmed.data(), first_non_digit - trimmed.begin());
306+
295307
// Revert changes if parse_port fails.
296308
uint32_t previous_port = components.port;
297-
parse_port(trimmed);
309+
parse_port(digits_to_parse);
298310
if (is_valid) {
299311
return true;
300312
}

tests/wpt/ada_extra_setters_tests.json

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@
9898
"expected": {
9999
"port": "4"
100100
}
101+
},
102+
{
103+
"href": "https://domain.com:3000",
104+
"new_value": "\n\t80\n\t80\n\t",
105+
"expected": {
106+
"port": "8080"
107+
}
108+
},
109+
{
110+
"href": "https://domain.com:3000",
111+
"new_value": "\n\n\t\t",
112+
"expected": {
113+
"port": "3000"
114+
}
101115
}
102116
],
103117
"hash": [

0 commit comments

Comments
 (0)