Skip to content

Truly independent web browser

LibWeb+LibWebView+Services: Fail requests under a PAC proxy

Jelle Raaijmakers2af88f29a238
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.cpp b/Libraries/LibWeb/Loader/ResourceLoader.cpp
index 80c9225103a..0ddb75b53e1 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.cpp
+++ b/Libraries/LibWeb/Loader/ResourceLoader.cpp
@@ -36,6 +36,22 @@ static RefPtr<ResourceLoader>& resource_loader()
return resource_loader;
}
+static Optional<URL::URL>& proxy_autoconfig_url_storage()
+{
+ static Optional<URL::URL>& proxy_autoconfig_url = *new Optional<URL::URL>;
+ return proxy_autoconfig_url;
+}
+
+void ResourceLoader::set_proxy_autoconfig_url(URL::URL url)
+{
+ proxy_autoconfig_url_storage() = move(url);
+}
+
+Optional<URL::URL> const& ResourceLoader::proxy_autoconfig_url()
+{
+ return proxy_autoconfig_url_storage();
+}
+
void ResourceLoader::initialize(GC::Heap& heap, NonnullRefPtr<Requests::RequestClient> request_client)
{
resource_loader() = adopt_ref(*new ResourceLoader(heap, move(request_client)));
@@ -86,6 +102,10 @@ void ResourceLoader::prefetch_dns(GC::Ptr<DOM::Document> document, URL::URL cons
}
}
+ // Speculation would resolve and connect without the proxy that the PAC file would have picked.
+ if (proxy_autoconfig_url().has_value())
+ return;
+
// FIXME: We could put this request in a queue until the client connection is re-established.
if (m_request_client)
m_request_client->ensure_connection(url, RequestServer::CacheLevel::ResolveOnly);
@@ -103,6 +123,10 @@ void ResourceLoader::preconnect(GC::Ptr<DOM::Document> document, URL::URL const&
}
}
+ // Speculation would resolve and connect without the proxy that the PAC file would have picked.
+ if (proxy_autoconfig_url().has_value())
+ return;
+
// FIXME: We could put this request in a queue until the client connection is re-established.
if (m_request_client)
m_request_client->ensure_connection(url, RequestServer::CacheLevel::CreateConnection);
@@ -417,6 +441,15 @@ RefPtr<Requests::Request> ResourceLoader::load(LoadRequest& request, GC::Root<On
return nullptr;
}
+ // We cannot evaluate PAC files yet, and connecting directly would ignore the proxy that was asked for.
+ if (proxy_autoconfig_url().has_value()) {
+ dbgln("FIXME: Evaluate the PAC file to find the proxy for {}; failing the request", url);
+ auto error = "PAC proxy configurations are not supported"sv;
+ log_failure(request, error);
+ on_complete->function()(false, {}, error);
+ return nullptr;
+ }
+
auto protocol_request = start_network_request(request, transfer_lease);
if (!protocol_request) {
on_complete->function()(false, {}, "Failed to start network request"sv);
diff --git a/Libraries/LibWeb/Loader/ResourceLoader.h b/Libraries/LibWeb/Loader/ResourceLoader.h
index 45204fec13d..1a077dc2bcf 100644
--- a/Libraries/LibWeb/Loader/ResourceLoader.h
+++ b/Libraries/LibWeb/Loader/ResourceLoader.h
@@ -33,6 +33,9 @@ public:
static bool is_initialized();
static ResourceLoader& the();
+ static void set_proxy_autoconfig_url(URL::URL);
+ static Optional<URL::URL> const& proxy_autoconfig_url();
+
void set_client(NonnullRefPtr<Requests::RequestClient>);
using OnHeadersReceived = GC::Function<void(Requests::Request*, HTTP::HeaderList const& response_headers, Optional<u32> status_code, Optional<String> const& reason_phrase, Optional<Core::ImmutableBytes> javascript_bytecode, Optional<u64> javascript_bytecode_cache_vary_key, Requests::CacheState cache_state)>;
diff --git a/Libraries/LibWeb/WebSockets/WebSocket.cpp b/Libraries/LibWeb/WebSockets/WebSocket.cpp
index d8e6c25ab97..c3880f5143d 100644
--- a/Libraries/LibWeb/WebSockets/WebSocket.cpp
+++ b/Libraries/LibWeb/WebSockets/WebSocket.cpp
@@ -185,6 +185,11 @@ ErrorOr<void> WebSocket::establish_web_socket_connection(URL::URL const& url_rec
additional_headers->append({ "User-Agent"sv, ResourceLoader::the().user_agent_for_websocket_url(url_record).to_byte_string() });
+ if (ResourceLoader::proxy_autoconfig_url().has_value()) {
+ dbgln("FIXME: Evaluate the PAC file to find the proxy for {}; failing the WebSocket connection", url_record);
+ return Error::from_string_literal("PAC proxy configurations are not supported");
+ }
+
auto request_client = ResourceLoader::the().request_client();
// FIXME: We could put this request in a queue until the client connection is re-established.
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cpp
index 3d4bde1aa71..be85b30aa47 100644
--- a/Libraries/LibWebView/Application.cpp
+++ b/Libraries/LibWebView/Application.cpp
@@ -313,6 +313,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)
Optional<StringView> user_agent_preset;
Optional<StringView> dns_server_address;
Optional<StringView> default_time_zone;
+ Optional<StringView> proxy_autoconfig_url;
Optional<u16> dns_server_port;
bool use_dns_over_tls = false;
bool enable_test_mode = false;
@@ -366,6 +367,19 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)
args_parser.add_option(window_width, "Set viewport width in pixels (default: 800) (currently only supported for headless mode)", "window-width", 0, "pixels");
args_parser.add_option(window_height, "Set viewport height in pixels (default: 600) (currently only supported for headless mode)", "window-height", 0, "pixels");
args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
+ args_parser.add_option(Core::ArgsParser::Option {
+ .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
+ .help_string = "URL of a PAC file (not supported yet, requests fail)",
+ .long_name = "proxy-autoconfig-url",
+ .value_name = "url",
+ .accept_value = [&](StringView value) {
+ if (!URL::Parser::basic_parse(value).has_value())
+ return false;
+ proxy_autoconfig_url = value;
+ return true;
+ },
+ .hide_mode = Core::ArgsParser::OptionHideMode::CommandLineAndMarkdown,
+ });
args_parser.add_option(new_window, "Force opening in a new window", "new-window", 'n');
#if !defined(AK_OS_ANDROID)
args_parser.add_option(force_new_process, "Force creation of a new browser process", "force-new-process");
@@ -663,6 +677,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)
.paint_viewport_scrollbars = disable_scrollbar_painting ? PaintViewportScrollbars::No : PaintViewportScrollbars::Yes,
.file_scheme_urls_have_tuple_origins = file_scheme_urls_have_tuple_origins ? FileSchemeUrlsHaveTupleOrigins::Yes : FileSchemeUrlsHaveTupleOrigins::No,
.default_time_zone = default_time_zone,
+ .proxy_autoconfig_url = proxy_autoconfig_url.map([](auto value) { return value.to_byte_string(); }),
};
create_platform_options(m_browser_options, m_request_server_options, m_web_content_options);
diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cpp
index a07d1736cd1..b65a90c4bfd 100644
--- a/Libraries/LibWebView/HelperProcess.cpp
+++ b/Libraries/LibWebView/HelperProcess.cpp
@@ -348,6 +348,9 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(IsP
arguments.append("--default-time-zone");
arguments.append(web_content_options.default_time_zone.value());
}
+
+ if (web_content_options.proxy_autoconfig_url.has_value())
+ arguments.append(ByteString::formatted("--proxy-autoconfig-url={}", *web_content_options.proxy_autoconfig_url));
if (auto server = mach_server_name(); server.has_value()) {
arguments.append("--mach-server-name"sv);
arguments.append(server.value());
@@ -523,6 +526,8 @@ ErrorOr<NonnullRefPtr<WebWorkerClient>> launch_web_worker_process(Web::HTML::Age
arguments.append("--enable-http-memory-cache"sv);
if (web_content_options.file_scheme_urls_have_tuple_origins == FileSchemeUrlsHaveTupleOrigins::Yes)
arguments.append("--tuple-file-origins"sv);
+ if (web_content_options.proxy_autoconfig_url.has_value())
+ arguments.append(ByteString::formatted("--proxy-autoconfig-url={}", *web_content_options.proxy_autoconfig_url));
arguments.append("--type"sv);
switch (type) {
diff --git a/Libraries/LibWebView/Options.h b/Libraries/LibWebView/Options.h
index 1289b4d23bb..83d4acf9c58 100644
--- a/Libraries/LibWebView/Options.h
+++ b/Libraries/LibWebView/Options.h
@@ -189,6 +189,7 @@ struct WebContentOptions {
PaintViewportScrollbars paint_viewport_scrollbars { PaintViewportScrollbars::Yes };
FileSchemeUrlsHaveTupleOrigins file_scheme_urls_have_tuple_origins { FileSchemeUrlsHaveTupleOrigins::No };
Optional<StringView> default_time_zone {};
+ Optional<ByteString> proxy_autoconfig_url {};
};
}
diff --git a/Services/WebContent/main.cpp b/Services/WebContent/main.cpp
index 972deef02b9..2f426a2c252 100644
--- a/Services/WebContent/main.cpp
+++ b/Services/WebContent/main.cpp
@@ -25,6 +25,7 @@
#include <LibMain/Main.h>
#include <LibMediaClient/Client.h>
#include <LibRequests/RequestClient.h>
+#include <LibURL/Parser.h>
#include <LibUnicode/TimeZone.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h>
@@ -210,6 +211,19 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
args_parser.add_option(is_headless, "Report that the browser is running in headless mode", "headless");
args_parser.add_option(default_time_zone, "Default time zone", "default-time-zone", 0, "time-zone-id");
args_parser.add_option(file_origins_are_tuple_origins, "Treat file:// URLs as having tuple origins", "tuple-file-origins");
+ args_parser.add_option(Core::ArgsParser::Option {
+ .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
+ .help_string = "URL of a PAC file (not supported yet, requests fail)",
+ .long_name = "proxy-autoconfig-url",
+ .value_name = "url",
+ .accept_value = [&](StringView value) {
+ auto url = URL::Parser::basic_parse(value);
+ if (!url.has_value())
+ return false;
+ Web::ResourceLoader::set_proxy_autoconfig_url(url.release_value());
+ return true;
+ },
+ });
args_parser.parse(arguments);
diff --git a/Services/WebWorker/main.cpp b/Services/WebWorker/main.cpp
index 71f92f484c9..225b6556f78 100644
--- a/Services/WebWorker/main.cpp
+++ b/Services/WebWorker/main.cpp
@@ -21,6 +21,7 @@
#include <LibMain/Main.h>
#include <LibMediaClient/Client.h>
#include <LibRequests/RequestClient.h>
+#include <LibURL/Parser.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/HTML/WindowOrWorkerGlobalScope.h>
@@ -82,6 +83,19 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
args_parser.add_option(file_origins_are_tuple_origins, "Treat file:// URLs as having tuple origins", "tuple-file-origins");
args_parser.add_option(disable_sandbox, "Disable process sandboxing", "disable-sandbox");
args_parser.add_option(enable_test_mode, "Enable test mode", "test-mode");
+ args_parser.add_option(Core::ArgsParser::Option {
+ .argument_mode = Core::ArgsParser::OptionArgumentMode::Required,
+ .help_string = "URL of a PAC file (not supported yet, requests fail)",
+ .long_name = "proxy-autoconfig-url",
+ .value_name = "url",
+ .accept_value = [&](StringView value) {
+ auto url = URL::Parser::basic_parse(value);
+ if (!url.has_value())
+ return false;
+ Web::ResourceLoader::set_proxy_autoconfig_url(url.release_value());
+ return true;
+ },
+ });
args_parser.parse(arguments);