Skip to content

Commit d6af5bf

Browse files
shannonboothtcl3
authored andcommitted
LibURL: Allow inputs containing only whitespace
The check for: ``` if (start_index >= end_index) return {}; ``` To prevent an out of bounds when trimming the start and end of the input of whitespace was preventing valid URLs (only having whitespace in the input) from being parsed. Instead, prevent start_index from ever getting above end_index in the first place, and don't treat empty inputs as an error. Fixes one WPT test on: https://wpt.live/url/url-constructor.any.html
1 parent 4f5af3e commit d6af5bf

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

Tests/LibWeb/Text/expected/URL/url.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,16 @@ port => '9000'
108108
pathname => '/path'
109109
search => '?query'
110110
hash => '#frag'
111+
new URL(' \t', 'http://ladybird.org/foo/bar')
112+
protocol => 'http:'
113+
username => ''
114+
password => ''
115+
host => 'ladybird.org'
116+
hostname => 'ladybird.org'
117+
port => ''
118+
pathname => '/foo/bar'
119+
search => ''
120+
hash => ''
111121
=========================================
112122
URL.parse('ftp://serenityos.org:21', undefined)
113123
protocol => 'ftp:'
@@ -219,3 +229,13 @@ port => '9000'
219229
pathname => '/path'
220230
search => '?query'
221231
hash => '#frag'
232+
URL.parse(' \t', 'http://ladybird.org/foo/bar')
233+
protocol => 'http:'
234+
username => ''
235+
password => ''
236+
host => 'ladybird.org'
237+
hostname => 'ladybird.org'
238+
port => ''
239+
pathname => '/foo/bar'
240+
search => ''
241+
hash => ''

Tests/LibWeb/Text/input/URL/url.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
{ input: 'file://a%C2%ADb/p' },
3333
{ input: 'http://user%20name:pa%40ss%3Aword@www.ladybird.org' },
3434
{ input: 'h\tt\nt\rp://h\to\ns\rt:9\t0\n0\r0/p\ta\nt\rh?q\tu\ne\rry#f\tr\na\rg' },
35+
{ input: ' \t', base: 'http://ladybird.org/foo/bar' },
3536
];
3637

3738
for (url of urls) {

Userland/Libraries/LibURL/Parser.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -808,29 +808,22 @@ URL Parser::basic_parse(StringView raw_input, Optional<URL> const& base_url, Opt
808808
// 2. If input contains any leading or trailing C0 control or space, invalid-URL-unit validation error.
809809
// 3. Remove any leading and trailing C0 control or space from input.
810810
bool has_validation_error = false;
811-
for (size_t i = 0; i < raw_input.length(); ++i) {
812-
u8 ch = raw_input[i];
813-
if (is_ascii_c0_control_or_space(ch)) {
814-
++start_index;
815-
has_validation_error = true;
816-
} else {
811+
812+
for (; start_index < raw_input.length(); ++start_index) {
813+
if (!is_ascii_c0_control_or_space(raw_input[start_index]))
817814
break;
818-
}
815+
has_validation_error = true;
819816
}
820-
for (ssize_t i = raw_input.length() - 1; i >= 0; --i) {
821-
u8 ch = raw_input[i];
822-
if (is_ascii_c0_control_or_space(ch)) {
823-
--end_index;
824-
has_validation_error = true;
825-
} else {
817+
818+
for (; end_index > start_index; --end_index) {
819+
if (!is_ascii_c0_control_or_space(raw_input[end_index - 1]))
826820
break;
827-
}
821+
has_validation_error = true;
828822
}
823+
829824
if (has_validation_error)
830825
report_validation_error();
831826
}
832-
if (start_index >= end_index)
833-
return {};
834827

835828
ByteString processed_input = raw_input.substring_view(start_index, end_index - start_index);
836829

0 commit comments

Comments
 (0)