LibWebView: Add a bit of explicit handling for "localhost:port" URLs

LibURL parses "localhost:8000" as a URL with a scheme of "localhost" and
basename of "8000". Similar to "mailto:" URLs.  We then drop the URL as
having an invalid scheme.

Let's explicitly check for such localhost URLs here and prepend a valid
scheme, as it is a bit of a special case.
This commit is contained in:
Timothy Flynn 2025-11-08 08:10:25 -05:00 committed by Tim Flynn
parent a290034a81
commit 0dd8e1e6f2
Notes: github-actions[bot] 2025-11-08 16:15:47 +00:00
2 changed files with 4 additions and 5 deletions

View file

@ -36,22 +36,20 @@ Optional<URL::URL> sanitize_url(StringView location, Optional<SearchEngine> cons
auto url = URL::create_with_url_or_path(location);
if (!url.has_value()) {
if (!url.has_value() || url->scheme() == "localhost"sv) {
url = URL::create_with_url_or_path(ByteString::formatted("https://{}", location));
if (!url.has_value())
return search_url_or_error();
https_scheme_was_guessed = true;
}
// FIXME: Add support for other schemes, e.g. "mailto:". Firefox and Chrome open mailto: locations.
static constexpr Array SUPPORTED_SCHEMES { "about"sv, "data"sv, "file"sv, "http"sv, "https"sv, "resource"sv };
if (!any_of(SUPPORTED_SCHEMES, [&](StringView const& scheme) { return scheme == url->scheme(); }))
return search_url_or_error();
// FIXME: Add support for other schemes, e.g. "mailto:". Firefox and Chrome open mailto: locations.
auto const& host = url->host();
if (host.has_value() && host->is_domain()) {
if (auto const& host = url->host(); host.has_value() && host->is_domain()) {
auto const& domain = host->get<String>();
if (domain.contains('"'))

View file

@ -184,6 +184,7 @@ TEST_CASE(location_to_search_or_url)
expect_url_equals_sanitized_url("https://example.def/"sv, "https://example.def"sv);
expect_url_equals_sanitized_url("https://localhost/"sv, "localhost"sv); // Respect localhost.
expect_url_equals_sanitized_url("https://localhost:8000/"sv, "localhost:8000"sv);
expect_url_equals_sanitized_url("https://localhost/hello"sv, "localhost/hello"sv);
expect_url_equals_sanitized_url("https://localhost/hello.world"sv, "localhost/hello.world"sv);
expect_url_equals_sanitized_url("https://localhost/hello.world?query=123"sv, "localhost/hello.world?query=123"sv);