Skip to content

Truly independent web browser

LibWebView: Percent-encode search queries as URL components

Andreas Kling14c882656e47
diff --git a/Libraries/LibWebView/SearchEngine.cpp b/Libraries/LibWebView/SearchEngine.cpp
index c33e629f84e..c587ae049a4 100644
--- a/Libraries/LibWebView/SearchEngine.cpp
+++ b/Libraries/LibWebView/SearchEngine.cpp
@@ -43,14 +43,14 @@ String SearchEngine::format_search_query_for_display(StringView query) const
String SearchEngine::format_search_query_for_navigation(StringView query) const
{
- return MUST(query_url.replace("%s"sv, URL::percent_encode(query), ReplaceMode::All));
+ return MUST(query_url.replace("%s"sv, URL::percent_encode(query, URL::PercentEncodeSet::Component), ReplaceMode::All));
}
Optional<URL::URL> SearchEngine::suggestions_url_for_query(StringView query) const
{
if (!suggestions.has_value())
return {};
- return URL::Parser::basic_parse(MUST(suggestions->query_url.replace("%s"sv, URL::percent_encode(query), ReplaceMode::All)));
+ return URL::Parser::basic_parse(MUST(suggestions->query_url.replace("%s"sv, URL::percent_encode(query, URL::PercentEncodeSet::Component), ReplaceMode::All)));
}
}
diff --git a/Tests/LibWebView/TestWebViewURL.cpp b/Tests/LibWebView/TestWebViewURL.cpp
index 84b03648193..97d3e2a445b 100644
--- a/Tests/LibWebView/TestWebViewURL.cpp
+++ b/Tests/LibWebView/TestWebViewURL.cpp
@@ -464,6 +464,23 @@ TEST_CASE(autocomplete_suggestion_display_text)
EXPECT_EQ(WebView::autocomplete_suggestion_display_text(search_suggestion), "https://example.com/"sv);
}
+TEST_CASE(search_query_percent_encoding)
+{
+ expect_url_equals_sanitized_url("https://ecosia.org/search?q=a%26b"sv, "a&b"sv);
+ expect_url_equals_sanitized_url("https://ecosia.org/search?q=c%2B%2B"sv, "c++"sv);
+ expect_url_equals_sanitized_url("https://ecosia.org/search?q=100%25%20of%20%2441"sv, "100% of $41"sv);
+ expect_url_equals_sanitized_url("https://ecosia.org/search?q=a%2Cb%20%2341"sv, "a,b #41"sv);
+
+ WebView::SearchEngine engine_with_suggestions {
+ .name = "Test"_string,
+ .query_url = "https://ecosia.org/search?q=%s"_string,
+ .suggestions = WebView::SearchSuggestions { "https://ac.ecosia.org/autocomplete?q=%s&type=list"_string },
+ };
+ auto suggestions_url = engine_with_suggestions.suggestions_url_for_query("a&b"sv);
+ EXPECT(suggestions_url.has_value());
+ EXPECT_EQ(suggestions_url->to_string(), "https://ac.ecosia.org/autocomplete?q=a%26b&type=list"sv);
+}
+
TEST_CASE(search_query_display_truncation)
{
EXPECT_EQ(s_test_engine.format_search_query_for_display("short"sv), "Search Test for “short”"sv);