Skip to content

Commit

Permalink
AK: Strip leading/trailing C0-control-or-space in URLs correctly
Browse files Browse the repository at this point in the history
We have to stop scanning once we hit a non-strippable character.
Add some tests to cover this.
  • Loading branch information
awesomekling committed Jun 1, 2021
1 parent 468bb54 commit c0d1a75
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
8 changes: 6 additions & 2 deletions AK/URLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -196,12 +196,16 @@ URL URLParser::parse(Badge<URL>, const StringView& raw_input, URL const* base_ur
if (raw_input[i] <= 0x20) {
++start_index;
has_validation_error = true;
} else {
break;
}
}
for (size_t i = 0; i < raw_input.length(); ++i) {
if (raw_input[raw_input.length() - 1 - i] <= 0x20) {
for (ssize_t i = raw_input.length() - 1; i >= 0; --i) {
if (raw_input[i] <= 0x20) {
--end_index;
has_validation_error = true;
} else {
break;
}
}
if (has_validation_error)
Expand Down
21 changes: 21 additions & 0 deletions Tests/AK/TestURL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,3 +307,24 @@ TEST_CASE(complete_url)

EXPECT(base_url.complete_url("../index.html#fragment").equals(base_url));
}

TEST_CASE(leading_whitespace)
{
URL url { " https://foo.com/" };
EXPECT(url.is_valid());
EXPECT_EQ(url.to_string(), "https://foo.com/");
}

TEST_CASE(trailing_whitespace)
{
URL url { "https://foo.com/ " };
EXPECT(url.is_valid());
EXPECT_EQ(url.to_string(), "https://foo.com/");
}

TEST_CASE(leading_and_trailing_whitespace)
{
URL url { " https://foo.com/ " };
EXPECT(url.is_valid());
EXPECT_EQ(url.to_string(), "https://foo.com/");
}

0 comments on commit c0d1a75

Please sign in to comment.