Skip to content

Truly independent web browser

WebDriver: Accept PAC proxy capabilities

Jelle Raaijmakersd6fcb9f5e301
diff --git a/Libraries/LibWebCommon/WebDriver/Capabilities.cpp b/Libraries/LibWebCommon/WebDriver/Capabilities.cpp
index dd9e35bbfe5..d15a31aebdb 100644
--- a/Libraries/LibWebCommon/WebDriver/Capabilities.cpp
+++ b/Libraries/LibWebCommon/WebDriver/Capabilities.cpp
@@ -311,7 +311,7 @@ static JsonValue match_capabilities(JsonObject const& capabilities, SessionFlags
else if (name == "proxy"sv) {
// If the has proxy configuration flag is set, or if the proxy configuration defined in value is not one that
// passes the endpoint node's implementation-specific validity checks, return success with data null.
- if (has_proxy_configuration())
+ if (has_proxy_configuration() || !is_supported_proxy_configuration(value))
return AK::Error::from_string_literal("proxy");
}
// -> "unhandledPromptBehavior"
diff --git a/Libraries/LibWebCommon/WebDriver/Proxy.cpp b/Libraries/LibWebCommon/WebDriver/Proxy.cpp
index b7c78f0510c..aac4c3b6249 100644
--- a/Libraries/LibWebCommon/WebDriver/Proxy.cpp
+++ b/Libraries/LibWebCommon/WebDriver/Proxy.cpp
@@ -14,7 +14,7 @@ namespace Web::WebDriver {
// https://w3c.github.io/webdriver/#dfn-has-proxy-configuration
// An endpoint node has an associated has proxy configuration flag that indicates whether the proxy is already configured.
// The default value of the flag is true if the endpoint doesn't support proxy configuration, or false otherwise.
-static constexpr bool s_default_has_proxy_configuration = true;
+static constexpr bool s_default_has_proxy_configuration = false;
static bool s_has_proxy_configuration = s_default_has_proxy_configuration;
bool has_proxy_configuration()
@@ -32,6 +32,31 @@ void reset_has_proxy_configuration()
s_has_proxy_configuration = s_default_has_proxy_configuration;
}
+// FIXME: Support the other proxy types.
+// The browser cannot yet evaluate PAC files, so requests made in a session with a PAC proxy fail instead of being sent
+// anywhere. This still lets test harnesses such as wptrunner create the session.
+bool is_supported_proxy_configuration(JsonValue const& proxy)
+{
+ if (!proxy.is_object())
+ return false;
+
+ auto const& object = proxy.as_object();
+
+ // Without a PAC file to fail requests for, the browser would fall back to its environment's proxy configuration.
+ auto proxy_type = object.get_string("proxyType"sv);
+ return proxy_type.has_value() && *proxy_type == "pac"sv && object.has_string("proxyAutoconfigUrl"sv);
+}
+
+Optional<String> proxy_autoconfig_url(JsonValue const& proxy)
+{
+ if (!is_supported_proxy_configuration(proxy))
+ return {};
+
+ if (auto url = proxy.as_object().get_string("proxyAutoconfigUrl"sv); url.has_value())
+ return *url;
+ return {};
+}
+
// https://w3c.github.io/webdriver/#dfn-proxy-configuration
static ErrorOr<void, Error> validate_proxy_item(StringView key, JsonValue const& value)
{
diff --git a/Libraries/LibWebCommon/WebDriver/Proxy.h b/Libraries/LibWebCommon/WebDriver/Proxy.h
index 9d3e29bf027..2205377691d 100644
--- a/Libraries/LibWebCommon/WebDriver/Proxy.h
+++ b/Libraries/LibWebCommon/WebDriver/Proxy.h
@@ -16,6 +16,9 @@ WEBCOMMON_API bool has_proxy_configuration();
WEBCOMMON_API void set_has_proxy_configuration(bool);
WEBCOMMON_API void reset_has_proxy_configuration();
+WEBCOMMON_API bool is_supported_proxy_configuration(JsonValue const&);
+WEBCOMMON_API Optional<String> proxy_autoconfig_url(JsonValue const&);
+
WEBCOMMON_API ErrorOr<JsonObject, Error> deserialize_as_a_proxy(JsonValue const&);
}
diff --git a/Services/WebDriver/Client.h b/Services/WebDriver/Client.h
index 966e55f2ed6..aab0773b508 100644
--- a/Services/WebDriver/Client.h
+++ b/Services/WebDriver/Client.h
@@ -17,7 +17,7 @@
namespace WebDriver {
-using LaunchBrowserCallback = Function<ErrorOr<Core::Process>(ByteString const& socket_path, bool headless)>;
+using LaunchBrowserCallback = Function<ErrorOr<Core::Process>(ByteString const& socket_path, bool headless, Optional<ByteString> const& proxy_autoconfig_url)>;
class Client final : public Web::WebDriver::Client {
C_OBJECT_ABSTRACT(Client);
diff --git a/Services/WebDriver/Session.cpp b/Services/WebDriver/Session.cpp
index 8c0cf350eb1..bb63dfb4cc4 100644
--- a/Services/WebDriver/Session.cpp
+++ b/Services/WebDriver/Session.cpp
@@ -92,9 +92,13 @@ ErrorOr<NonnullRefPtr<Session::NewSessionPromise>> Session::create(NonnullRefPtr
// Take implementation-defined steps to set the user agent proxy using the extracted proxy configuration. If the
// defined proxy cannot be configured return error with error code session not created. Otherwise set the has
// proxy configuration flag to true.
- session->close();
- state->promise->reject(Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::SessionNotCreated, "Proxy configuration is not yet supported"sv));
- return;
+ // The browser was already launched with the proxy configuration.
+ if (!Web::WebDriver::is_supported_proxy_configuration(*proxy)) {
+ session->close();
+ state->promise->reject(Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::SessionNotCreated, "Proxy configuration is not yet supported"sv));
+ return;
+ }
+ Web::WebDriver::set_has_proxy_configuration(true);
}
// -> Otherwise
else {
@@ -183,9 +187,22 @@ ErrorOr<NonnullRefPtr<Session::NewSessionPromise>> Session::create(NonnullRefPtr
return state->promise;
}
+static Optional<ByteString> proxy_autoconfig_url(JsonObject const& capabilities)
+{
+ auto proxy = capabilities.get("proxy"sv);
+ if (!proxy.has_value())
+ return {};
+
+ auto url = Web::WebDriver::proxy_autoconfig_url(*proxy);
+ if (!url.has_value())
+ return {};
+ return url->to_byte_string();
+}
+
Session::Session(NonnullRefPtr<Client> client, JsonObject const& capabilities, String session_id, Web::WebDriver::SessionFlags flags)
: m_client(move(client))
, m_options(capabilities)
+ , m_proxy_autoconfig_url(proxy_autoconfig_url(capabilities))
, m_session_id(move(session_id))
, m_session_flags(flags)
, m_event_loop(Core::EventLoop::current())
@@ -672,7 +689,7 @@ ErrorOr<NonnullRefPtr<Session::ServerPromise>> Session::start(LaunchBrowserCallb
#endif
TRY(create_server());
- m_browser_process = TRY(launch_browser_callback(m_browser_endpoint, m_options.headless));
+ m_browser_process = TRY(launch_browser_callback(m_browser_endpoint, m_options.headless, m_proxy_autoconfig_url));
arm_browser_startup_timeout(promise);
return promise;
}
diff --git a/Services/WebDriver/Session.h b/Services/WebDriver/Session.h
index a92b875de47..017933c4c0c 100644
--- a/Services/WebDriver/Session.h
+++ b/Services/WebDriver/Session.h
@@ -124,6 +124,7 @@ private:
NonnullRefPtr<Client> m_client;
Web::WebDriver::LadybirdOptions m_options;
+ Optional<ByteString> m_proxy_autoconfig_url;
String m_session_id;
Web::WebDriver::SessionFlags m_session_flags { Web::WebDriver::SessionFlags::Default };
diff --git a/Services/WebDriver/main.cpp b/Services/WebDriver/main.cpp
index d44265d1c54..1fa159cefec 100644
--- a/Services/WebDriver/main.cpp
+++ b/Services/WebDriver/main.cpp
@@ -38,7 +38,7 @@ static ErrorOr<Core::Process> launch_process(StringView application, ReadonlySpa
return result;
}
-static Vector<ByteString> create_arguments(ByteString const& webdriver_endpoint, Optional<StringView> profile_name, Optional<StringView> profile_path, bool headless, bool expose_experimental_interfaces, bool expose_internals_object, bool force_cpu_painting, bool disable_sandbox, Optional<StringView> debug_process, Optional<StringView> default_time_zone, Optional<StringView> resource_substitution_map_path, StringView site_isolation_mode)
+static Vector<ByteString> create_arguments(ByteString const& webdriver_endpoint, Optional<StringView> profile_name, Optional<StringView> profile_path, bool headless, bool expose_experimental_interfaces, bool expose_internals_object, bool force_cpu_painting, bool disable_sandbox, Optional<StringView> debug_process, Optional<StringView> default_time_zone, Optional<StringView> resource_substitution_map_path, StringView site_isolation_mode, Optional<ByteString> const& proxy_autoconfig_url)
{
Vector<ByteString> arguments;
#if defined(AK_OS_MACOS)
@@ -82,6 +82,9 @@ static Vector<ByteString> create_arguments(ByteString const& webdriver_endpoint,
if (resource_substitution_map_path.has_value())
arguments.append(ByteString::formatted("--resource-map={}", resource_substitution_map_path.value()));
+ if (proxy_autoconfig_url.has_value())
+ arguments.append(ByteString::formatted("--proxy-autoconfig-url={}", *proxy_autoconfig_url));
+
arguments.append(ByteString::formatted("--site-isolation={}", site_isolation_mode));
arguments.append("about:blank"sv);
@@ -208,8 +211,8 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
return;
}
- auto launch_browser_callback = [&](ByteString const& webdriver_endpoint, bool headless) {
- auto arguments = create_arguments(webdriver_endpoint, profile_name, profile_path, headless, expose_experimental_interfaces, expose_internals_object, force_cpu_painting, disable_sandbox, debug_process, default_time_zone, resource_substitution_map_path, site_isolation_mode);
+ auto launch_browser_callback = [&](ByteString const& webdriver_endpoint, bool headless, Optional<ByteString> const& proxy_autoconfig_url) {
+ auto arguments = create_arguments(webdriver_endpoint, profile_name, profile_path, headless, expose_experimental_interfaces, expose_internals_object, force_cpu_painting, disable_sandbox, debug_process, default_time_zone, resource_substitution_map_path, site_isolation_mode, proxy_autoconfig_url);
return launch_process("Ladybird"sv, arguments.span());
};
diff --git a/Tests/LibWebView/TestWebDriverMachBootstrap.cpp b/Tests/LibWebView/TestWebDriverMachBootstrap.cpp
index 8d355a3aef5..31d902f574c 100644
--- a/Tests/LibWebView/TestWebDriverMachBootstrap.cpp
+++ b/Tests/LibWebView/TestWebDriverMachBootstrap.cpp
@@ -24,7 +24,7 @@ TEST_CASE(queued_bootstrap_callbacks_are_discarded_when_browser_launch_fails)
auto peer = TRY_OR_FAIL(Core::TCPSocket::connect("127.0.0.1", server->local_port().value()));
auto socket = TRY_OR_FAIL(server->accept());
auto buffered_socket = TRY_OR_FAIL(Core::BufferedTCPSocket::create(move(socket)));
- auto client = TRY_OR_FAIL(WebDriver::Client::try_create(move(buffered_socket), [](auto const& endpoint, bool) -> ErrorOr<Core::Process> {
+ auto client = TRY_OR_FAIL(WebDriver::Client::try_create(move(buffered_socket), [](auto const& endpoint, bool, auto const&) -> ErrorOr<Core::Process> {
auto server_port = TRY(Core::MachPort::look_up_from_bootstrap_server(endpoint));
mach_port_t raw_task_name_port = MACH_PORT_NULL;
VERIFY(task_get_special_port(mach_task_self(), TASK_NAME_PORT, &raw_task_name_port) == KERN_SUCCESS);