mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 17:10:23 +00:00
LibWebView: Add a helper to sanitize a user-provided URL
We currently implement several forms of this method across the Ladybird chromes. As such, we see commits to add special URL handling that only affects a single chrome. Instead, let's consolidate all special handling in a single location for all chromes to make use of. This method can handle resolving file:// URLs, falling back to a search engine query, and validation against the Public Suffix List. These cases were gathered from the various chromes.
This commit is contained in:
parent
54a28afc13
commit
191e20d639
Notes:
sideshowbarker
2024-07-17 11:29:41 +09:00
Author: https://github.com/trflynn89 Commit: https://github.com/SerenityOS/serenity/commit/191e20d639 Pull-request: https://github.com/SerenityOS/serenity/pull/21435 Reviewed-by: https://github.com/ADKaster ✅
3 changed files with 72 additions and 1 deletions
|
@ -9,6 +9,7 @@ set(SOURCES
|
|||
RequestServerAdapter.cpp
|
||||
SourceHighlighter.cpp
|
||||
StylePropertiesModel.cpp
|
||||
URL.cpp
|
||||
UserAgent.cpp
|
||||
ViewImplementation.cpp
|
||||
WebContentClient.cpp
|
||||
|
@ -40,7 +41,7 @@ set(GENERATED_SOURCES
|
|||
)
|
||||
|
||||
serenity_lib(LibWebView webview)
|
||||
target_link_libraries(LibWebView PRIVATE LibCore LibGfx LibGUI LibIPC LibProtocol LibJS LibWeb LibSQL)
|
||||
target_link_libraries(LibWebView PRIVATE LibCore LibFileSystem LibGfx LibGUI LibIPC LibProtocol LibPublicSuffix LibJS LibWeb LibSQL)
|
||||
|
||||
if (SERENITYOS)
|
||||
target_link_libraries(LibWebView PRIVATE LibFileSystemAccessClient)
|
||||
|
|
48
Userland/Libraries/LibWebView/URL.cpp
Normal file
48
Userland/Libraries/LibWebView/URL.cpp
Normal file
|
@ -0,0 +1,48 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/System.h>
|
||||
#include <LibFileSystem/FileSystem.h>
|
||||
#include <LibPublicSuffix/URL.h>
|
||||
#include <LibWebView/URL.h>
|
||||
|
||||
namespace WebView {
|
||||
|
||||
Optional<URL> sanitize_url(StringView url, Optional<StringView> search_engine, AppendTLD append_tld)
|
||||
{
|
||||
if (FileSystem::exists(url)) {
|
||||
auto path = FileSystem::real_path(url);
|
||||
if (path.is_error())
|
||||
return {};
|
||||
|
||||
return URL::create_with_file_scheme(path.value().to_deprecated_string());
|
||||
}
|
||||
|
||||
auto format_search_engine = [&]() -> Optional<URL> {
|
||||
if (!search_engine.has_value())
|
||||
return {};
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
auto result = PublicSuffix::absolute_url(url);
|
||||
if (result.is_error())
|
||||
return format_search_engine();
|
||||
|
||||
return result.release_value();
|
||||
}
|
||||
|
||||
}
|
22
Userland/Libraries/LibWebView/URL.h
Normal file
22
Userland/Libraries/LibWebView/URL.h
Normal file
|
@ -0,0 +1,22 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Tim Flynn <trflynn89@serenityos.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <AK/Optional.h>
|
||||
#include <AK/StringView.h>
|
||||
#include <AK/URL.h>
|
||||
|
||||
namespace WebView {
|
||||
|
||||
enum class AppendTLD {
|
||||
No,
|
||||
Yes,
|
||||
};
|
||||
|
||||
Optional<URL> sanitize_url(StringView, Optional<StringView> search_engine = {}, AppendTLD = AppendTLD::No);
|
||||
|
||||
}
|
Loading…
Reference in a new issue