Truly independent web browser
LibWebView: Search for a typed number instead of going to an address
Andreas Klingf811f37b2f86
diff --git a/Libraries/LibWebView/URL.cpp b/Libraries/LibWebView/URL.cppindex 13e77d36866..445a66862aa 100644--- a/Libraries/LibWebView/URL.cpp+++ b/Libraries/LibWebView/URL.cpp@@ -29,7 +29,7 @@ bool is_url_handled_internally(URL::URL const& url)static bool has_valid_explicit_port(StringView input){- auto authority_end = input.find_any_of("/?#"sv).value_or(input.length());+ auto authority_end = input.find_any_of("/\\?#"sv).value_or(input.length());auto authority = input.substring_view(0, authority_end);Optional<size_t> port_separator;@@ -87,6 +87,19 @@ static bool should_guess_http_scheme_for_host(URL::Host const& host)return false;}+// The URL parser accepts abbreviated IPv4 addresses, so numeric text such as "88" parses as 0.0.0.88. Text without a+// scheme or a port is only taken for an IPv4 address when all four parts are there.+static bool is_abbreviated_ipv4_hostname(StringView text)+{+ auto hostname_end = text.find_any_of(":/\\?#"sv).value_or(text.length());+ auto hostname = text.substring_view(0, hostname_end);+ auto host = URL::Parser::parse_host(hostname);+ if (!host.has_value() || !host->has<IPv4Address>())+ return false;+ auto parts = hostname.split_view('.', SplitBehavior::KeepEmpty);+ return parts.size() != 4 || any_of(parts, [](auto part) { return part.is_empty(); });+}+ClassifiedUserInput classify_user_input(StringView location, AppendTLD append_tld){location = location.trim_whitespace();@@ -123,6 +136,9 @@ ClassifiedUserInput classify_user_input(StringView location, AppendTLD append_tlinput_has_explicit_port = true;} else if (url.has_value()) {return { UserInputClassification::ExternalURL, move(url) };+ } else if (is_abbreviated_ipv4_hostname(location)) {+ // NB: With a port, the text is taken for an address all the same.+ return {};} else {schemeless_location = location;@@ -205,19 +221,13 @@ Optional<URL::URL> url_from_text(StringView text)if (trimmed_text.is_empty() || trimmed_text.is_whitespace())return {};- if (trimmed_text.starts_with('.') || trimmed_text.starts_with('/'))+ // NB: Text that starts with a port, path, query or fragment has no host to go to.+ if (trimmed_text.starts_with('.') || trimmed_text.starts_with('/') || trimmed_text.starts_with('\\') || trimmed_text.starts_with(':') || trimmed_text.starts_with('?') || trimmed_text.starts_with('#'))return {};- // The URL parser accepts abbreviated IPv4 addresses, causing numeric text such as "88" to be parsed as- // 0.0.0.88. For selected text, only recognize an IPv4 address when all four parts are present.- auto hostname_end = trimmed_text.find_any_of(":/?#"sv).value_or(trimmed_text.length());- auto hostname = trimmed_text.substring_view(0, hostname_end);- auto is_numeric_hostname = all_of(hostname.bytes(), [](auto byte) { return is_ascii_digit(byte) || byte == '.'; });- if (is_numeric_hostname) {- auto parts = hostname.split_view('.', SplitBehavior::KeepEmpty);- if (parts.size() != 4 || any_of(parts, [](auto part) { return part.is_empty(); }))- return {};- }+ // NB: Selected numbers such as "12:30" aren't addresses either, even though they look like a host and a port.+ if (is_abbreviated_ipv4_hostname(trimmed_text))+ return {};return sanitize_url(trimmed_text);}diff --git a/Tests/LibWebView/TestWebViewURL.cpp b/Tests/LibWebView/TestWebViewURL.cppindex 85864cf0724..0416a320cf3 100644--- a/Tests/LibWebView/TestWebViewURL.cpp+++ b/Tests/LibWebView/TestWebViewURL.cpp@@ -268,6 +268,11 @@ TEST_CASE(location_to_search_or_url)expect_url_equals_sanitized_url("http://[::1]:8000/"sv, "[::1]:8000"sv);expect_url_equals_sanitized_url("http://0.0.0.0:8000/"sv, "0.0.0.0:8000"sv); // The unspecified address reaches loopback in practice.+ // Numbers that the URL parser would read as abbreviated IPv4 addresses are searched for instead.+ expect_search_url_equals_sanitized_url("127"sv);+ expect_search_url_equals_sanitized_url("2026"sv);+ expect_search_url_equals_sanitized_url("1.5"sv);+// Private-use and link-local addresses also get the http guess. Like loopback hosts, they can't obtain publicly-// trusted TLS certs.expect_url_equals_sanitized_url("http://192.168.0.42:8000/"sv, "192.168.0.42:8000"sv);@@ -317,6 +322,25 @@ TEST_CASE(location_to_search_or_url)expect_search_url_equals_sanitized_url("javascript:alert(1)"sv);}+TEST_CASE(abbreviated_ipv4_user_input)+{+ for (auto input : { "127\\notes"sv, "127\\notes:8000"sv, "0x7f"sv, "0X7F"sv, "0x7f.1"sv, "127.0x1/path"sv, "0177"sv, "%31%32%37"sv, "127.0.0.1."sv }) {+ expect_search_url_equals_sanitized_url(input);+ expect_user_input_classification(input, WebView::UserInputClassification::Search);+ expect_location_does_not_look_like_url(input);+ expect_context_menu_text_not_url(input);+ }++ // A complete address, an explicit scheme or a port still requests navigation.+ expect_url_equals_sanitized_url("http://127.0.0.1/notes"sv, "127.0.0.1\\notes"sv);+ expect_url_equals_sanitized_url("http://127.0.0.1/"sv, "0x7f.0.0.1"sv);+ expect_url_equals_sanitized_url("https://0.0.0.127/"sv, "https://0x7f"sv);+ expect_url_equals_sanitized_url("http://0.0.0.127:8000/"sv, "0x7f:8000"sv);+ expect_url_equals_sanitized_url("http://0.0.0.127:8000/notes"sv, "0x7f:8000\\notes"sv);+ expect_context_menu_text_url("0x7f.0.0.1"sv, "http://127.0.0.1/"sv);+ expect_url_equals_sanitized_url("https://0x7f.example/"sv, "0x7f.example"sv);+}+TEST_CASE(classify_user_input){using enum WebView::UserInputClassification;@@ -381,9 +405,15 @@ TEST_CASE(context_menu_url_text)expect_context_menu_text_not_url("192.168"sv);expect_context_menu_text_not_url("192.168.0"sv);expect_context_menu_text_not_url("192.168.0.999"sv);+ expect_context_menu_text_not_url("12:30"sv);expect_context_menu_text_not_url("./Meta/ladybird.py"sv);expect_context_menu_text_not_url("../ladybird.org"sv);expect_context_menu_text_not_url("/tmp/ladybird.org"sv);+ expect_context_menu_text_not_url("\\example.com"sv);+ expect_context_menu_text_not_url("\\127.0.0.1"sv);+ expect_context_menu_text_not_url(":8080/foo"sv);+ expect_context_menu_text_not_url("?x"sv);+ expect_context_menu_text_not_url("#top"sv);}TEST_CASE(autocomplete_url_matching)