Truly independent web browser
AK: Reject IPv6 addresses with missing pieces
Andreas Kling28aff02189eb
diff --git a/AK/IPv6Address.h b/AK/IPv6Address.hindex f1d86a23966..1d2008ecf95 100644--- a/AK/IPv6Address.h+++ b/AK/IPv6Address.h@@ -202,22 +202,22 @@ public:if (found_compressed)return {};int empty_parts = 1;- bool is_leading = (i == 0);- bool is_trailing = false;for (size_t j = i + 1; j < parts.size(); j++) {if (!parts[j].trim_whitespace().is_empty())break;empty_parts++;- if (j == parts.size() - 1)- is_trailing = true;}+ bool is_leading = (i == 0);+ bool is_trailing = (i + empty_parts == parts.size());if (is_leading && is_trailing) {- if (empty_parts > 3)+ // The only address made of nothing but colons is "::".+ if (empty_parts != 3)return {};return IPv6Address();}if (is_leading || is_trailing) {- if (empty_parts > 2)+ // A compression at either end is spelled "::", so a single leading or trailing colon is invalid.+ if (empty_parts != 2)return {};} else if (empty_parts > 1) {return {};@@ -245,6 +245,10 @@ public:piece++;}+ // Without a "::" compression, all eight pieces must be spelled out. With one, it stands for at least one piece.+ if (found_compressed ? have_pieces > 7 : have_pieces != 8)+ return {};+return IPv6Address(addr);}diff --git a/Tests/AK/TestIPv6Address.cpp b/Tests/AK/TestIPv6Address.cppindex 998b7b3263d..be7e5c62cce 100644--- a/Tests/AK/TestIPv6Address.cpp+++ b/Tests/AK/TestIPv6Address.cpp@@ -93,6 +93,18 @@ TEST_CASE(should_not_make_ipv6_address_from_string)EXPECT_EQ(IPv6Address::from_string("[::304:506:708:90a:b0c:d0e:f10]]"sv), OptionalNone {});EXPECT_EQ(IPv6Address::from_string("[[::304:506:708:90a:b0c:d0e:f10]"sv), OptionalNone {});EXPECT_EQ(IPv6Address::from_string("[[::304:506:708:90a:b0c:d0e:f10]]"sv), OptionalNone {});+ EXPECT(!IPv6Address::from_string("f"sv).has_value());+ EXPECT(!IPv6Address::from_string("[f]"sv).has_value());+ EXPECT(!IPv6Address::from_string("cafe"sv).has_value());+ EXPECT(!IPv6Address::from_string("1:2"sv).has_value());+ EXPECT(!IPv6Address::from_string("1:2:3:4:5:6:7"sv).has_value());+ EXPECT(!IPv6Address::from_string("1:2:3:4:5:6:7:8:9"sv).has_value());+ EXPECT(!IPv6Address::from_string(":1:2:3:4:5:6:7"sv).has_value());+ EXPECT(!IPv6Address::from_string("1:2:3:4:5:6:7:"sv).has_value());+ EXPECT(!IPv6Address::from_string("1:2:3:4::5:6:7:8"sv).has_value());+ EXPECT(!IPv6Address::from_string(""sv).has_value());+ EXPECT(!IPv6Address::from_string(":"sv).has_value());+ EXPECT(!IPv6Address::from_string(":::"sv).has_value());}TEST_CASE(ipv4_mapped_ipv6)diff --git a/Tests/LibWebView/TestWebViewURL.cpp b/Tests/LibWebView/TestWebViewURL.cppindex 4c04753dc72..85864cf0724 100644--- a/Tests/LibWebView/TestWebViewURL.cpp+++ b/Tests/LibWebView/TestWebViewURL.cpp@@ -360,6 +360,8 @@ TEST_CASE(location_looks_like_url)expect_location_does_not_look_like_url("example.org hello"sv);expect_location_does_not_look_like_url("example.def"sv);expect_location_does_not_look_like_url("javascript://alert"sv);+ expect_location_does_not_look_like_url("f"sv);+ expect_location_does_not_look_like_url("cafe"sv);expect_location_looks_like_url("example"sv, WebView::AppendTLD::Yes);}