From 6014727c20ad108e8345e89cba8e9eace3c157b3 Mon Sep 17 00:00:00 2001 From: Luke Warlow Date: Mon, 24 Jun 2024 02:12:19 +0100 Subject: [PATCH] LibWebView: Allow data URLs in sanitize_url Allow navigation to data URLs from browser UI. --- Tests/LibWebView/TestWebViewURL.cpp | 23 ++++++++++++++++------- Userland/Libraries/LibWebView/URL.cpp | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/Tests/LibWebView/TestWebViewURL.cpp b/Tests/LibWebView/TestWebViewURL.cpp index 9a3816745c0..8b065f3d476 100644 --- a/Tests/LibWebView/TestWebViewURL.cpp +++ b/Tests/LibWebView/TestWebViewURL.cpp @@ -17,6 +17,14 @@ static void compare_url_parts(StringView url, WebView::URLParts const& expected) EXPECT_EQ(result->remainder, expected.remainder); } +static bool is_sanitized_url_the_same(StringView url) +{ + auto sanitized_url = WebView::sanitize_url(url); + if (!sanitized_url.has_value()) + return false; + return sanitized_url->to_string().value() == url; +} + TEST_CASE(invalid_url) { EXPECT(!WebView::break_url_into_parts(""sv).has_value()); @@ -82,13 +90,6 @@ TEST_CASE(http_url) TEST_CASE(about_url) { - auto is_sanitized_url_the_same = [](StringView url) { - auto sanitized_url = WebView::sanitize_url(url); - if (!sanitized_url.has_value()) - return false; - return sanitized_url->to_string().value() == url; - }; - EXPECT(!is_sanitized_url_the_same("about"sv)); EXPECT(!is_sanitized_url_the_same("about blabla:"sv)); EXPECT(!is_sanitized_url_the_same("blabla about:"sv)); @@ -96,3 +97,11 @@ TEST_CASE(about_url) EXPECT(is_sanitized_url_the_same("about:about"sv)); EXPECT(is_sanitized_url_the_same("about:version"sv)); } + +TEST_CASE(data_url) +{ + EXPECT(is_sanitized_url_the_same("data:text/html"sv)); + + EXPECT(!is_sanitized_url_the_same("data text/html"sv)); + EXPECT(!is_sanitized_url_the_same("text/html data:"sv)); +} diff --git a/Userland/Libraries/LibWebView/URL.cpp b/Userland/Libraries/LibWebView/URL.cpp index a4ffd5d3635..7b6cc258217 100644 --- a/Userland/Libraries/LibWebView/URL.cpp +++ b/Userland/Libraries/LibWebView/URL.cpp @@ -62,7 +62,7 @@ Optional sanitize_url(StringView url, Optional search_engi } ByteString url_with_scheme = url; - if (!(url_with_scheme.starts_with("about:"sv) || url_with_scheme.contains("://"sv))) + 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);