Skip to content

Commit

Permalink
LibWebView: Don't add '.com' to valid TLD when Ctrl is pressed
Browse files Browse the repository at this point in the history
If Ctrl is pressed when a string is entered into the location bar and
that string doesn't contain a valid TLD, ".com" is added to that string.

Previously, only a small, fixed set of TLDs was checked. We now use the
public suffix list to determine if the given address contains a valid
TLD.
  • Loading branch information
tcl3 committed Jan 20, 2025
1 parent 026fc6c commit ca4fcde
Showing 1 changed file with 11 additions and 10 deletions.
21 changes: 11 additions & 10 deletions Libraries/LibWebView/URL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,21 +28,22 @@ Optional<URL::URL> sanitize_url(StringView url, Optional<StringView> search_engi
return MUST(String::formatted(*search_engine, URL::percent_decode(url)));
};

String url_buffer;

if (append_tld == AppendTLD::Yes) {
// FIXME: Expand the list of top level domains.
if (!url.ends_with(".com"sv) && !url.ends_with(".net"sv) && !url.ends_with(".org"sv)) {
url_buffer = MUST(String::formatted("{}.com", url));
url = url_buffer;
}
}

ByteString url_with_scheme = url;
if (!(url_with_scheme.starts_with("about:"sv) || url_with_scheme.contains("://"sv) || url_with_scheme.starts_with("data:"sv)))
url_with_scheme = ByteString::formatted("https://{}"sv, url_with_scheme);

auto result = URL::create_with_url_or_path(url_with_scheme);

if (result.is_valid() && append_tld == AppendTLD::Yes) {
if (auto maybe_host = result.host(); maybe_host.has_value()) {
auto serialized_host = maybe_host->serialize();
auto maybe_public_suffix = URL::get_public_suffix(maybe_host->serialize());
dbgln("Maybe public suffix: {}", maybe_public_suffix);
if (!maybe_public_suffix.has_value() || *maybe_public_suffix == serialized_host)
result.set_host(MUST(String::formatted("{}.com", maybe_host->serialize())));
}
}

if (!result.is_valid())
return format_search_engine();

Expand Down

0 comments on commit ca4fcde

Please sign in to comment.