1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-22 06:05:07 +00:00
serenity/Userland/Services/WebDriver/Session.cpp
Timothy Flynn a3d6c2f6af Browser+WebContent+WebDriver: Move Get Named Cookie to WebContent
Instead of sending *all* cookies over IPC and filtering by name, we now
filter by name from the cookie jar and send just the first matching
cookie.
2022-11-11 18:02:04 +00:00

466 lines
19 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* Copyright (c) 2022, Florent Castelli <florent.castelli@gmail.com>
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
* Copyright (c) 2022, Tim Flynn <trflynn89@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "Session.h"
#include "BrowserConnection.h"
#include "Client.h"
#include <AK/NumericLimits.h>
#include <AK/Time.h>
#include <LibCore/LocalServer.h>
#include <LibCore/Stream.h>
#include <LibCore/System.h>
#include <LibGfx/Point.h>
#include <LibGfx/Rect.h>
#include <LibGfx/Size.h>
#include <LibWeb/Cookie/Cookie.h>
#include <LibWeb/Cookie/ParsedCookie.h>
#include <LibWeb/WebDriver/ExecuteScript.h>
#include <unistd.h>
namespace WebDriver {
Session::Session(unsigned session_id, NonnullRefPtr<Client> client)
: m_client(move(client))
, m_id(session_id)
{
}
Session::~Session()
{
if (m_started) {
auto error = stop();
if (error.is_error()) {
warnln("Failed to stop session {}: {}", m_id, error.error());
}
}
}
ErrorOr<Session::Window*, Web::WebDriver::Error> Session::current_window()
{
auto window = m_windows.get(m_current_window_handle);
if (!window.has_value())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::NoSuchWindow, "Window not found");
return window.release_value();
}
ErrorOr<void, Web::WebDriver::Error> Session::check_for_open_top_level_browsing_context_or_return_error()
{
(void)TRY(current_window());
return {};
}
ErrorOr<NonnullRefPtr<Core::LocalServer>> Session::create_server(String const& socket_path, ServerType type, NonnullRefPtr<ServerPromise> promise)
{
dbgln("Listening for WebDriver connection on {}", socket_path);
auto server = TRY(Core::LocalServer::try_create());
server->listen(socket_path);
server->on_accept = [this, type, promise](auto client_socket) mutable {
switch (type) {
case ServerType::Browser: {
auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) BrowserConnection(move(client_socket), m_client, session_id()));
if (maybe_connection.is_error()) {
promise->resolve(maybe_connection.release_error());
return;
}
dbgln("WebDriver is connected to Browser socket");
m_browser_connection = maybe_connection.release_value();
break;
}
case ServerType::WebContent: {
auto maybe_connection = adopt_nonnull_ref_or_enomem(new (nothrow) WebContentConnection(move(client_socket), m_client, session_id()));
if (maybe_connection.is_error()) {
promise->resolve(maybe_connection.release_error());
return;
}
dbgln("WebDriver is connected to WebContent socket");
m_web_content_connection = maybe_connection.release_value();
break;
}
}
if (m_browser_connection && m_web_content_connection)
promise->resolve({});
};
server->on_accept_error = [promise](auto error) mutable {
promise->resolve(move(error));
};
return server;
}
ErrorOr<void> Session::start()
{
auto promise = TRY(ServerPromise::try_create());
auto browser_socket_path = String::formatted("/tmp/webdriver/browser_{}_{}", getpid(), m_id);
auto browser_server = TRY(create_server(browser_socket_path, ServerType::Browser, promise));
auto web_content_socket_path = String::formatted("/tmp/webdriver/content_{}_{}", getpid(), m_id);
auto web_content_server = TRY(create_server(web_content_socket_path, ServerType::WebContent, promise));
char const* argv[] = {
"/bin/Browser",
"--webdriver-browser-path",
browser_socket_path.characters(),
"--webdriver-content-path",
web_content_socket_path.characters(),
nullptr,
};
TRY(Core::System::posix_spawn("/bin/Browser"sv, nullptr, nullptr, const_cast<char**>(argv), environ));
// FIXME: Allow this to be more asynchronous. For now, this at least allows us to propagate
// errors received while accepting the Browser and WebContent sockets.
TRY(promise->await());
m_started = true;
m_windows.set("main", make<Session::Window>("main", true));
m_current_window_handle = "main";
return {};
}
// https://w3c.github.io/webdriver/#dfn-close-the-session
Web::WebDriver::Response Session::stop()
{
// 1. Perform the following substeps based on the remote ends type:
// NOTE: We perform the "Remote end is an endpoint node" steps in the WebContent process.
m_web_content_connection->close_session();
m_web_content_connection = nullptr;
// 2. Remove the current session from active sessions.
// NOTE: Handled by WebDriver::Client.
// 3. Perform any implementation-specific cleanup steps.
m_browser_connection->async_quit();
m_started = false;
// 4. If an error has occurred in any of the steps above, return the error, otherwise return success with data null.
return JsonValue {};
}
// 9.1 Get Timeouts, https://w3c.github.io/webdriver/#dfn-get-timeouts
JsonObject Session::get_timeouts()
{
// 1. Let timeouts be the timeouts object for sessions timeouts configuration
auto timeouts = timeouts_object(m_timeouts_configuration);
// 2. Return success with data timeouts.
return timeouts;
}
// 9.2 Set Timeouts, https://w3c.github.io/webdriver/#dfn-set-timeouts
Web::WebDriver::Response Session::set_timeouts(JsonValue const& payload)
{
// 1. Let timeouts be the result of trying to JSON deserialize as a timeouts configuration the requests parameters.
auto timeouts = TRY(json_deserialize_as_a_timeouts_configuration(payload));
// 2. Make the session timeouts the new timeouts.
m_timeouts_configuration = move(timeouts);
// 3. Return success with data null.
return JsonValue {};
}
// 10.3 Back, https://w3c.github.io/webdriver/#dfn-back
Web::WebDriver::Response Session::back()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts and return its value if it is an error.
// 3. Traverse the history by a delta 1 for the current browsing context.
m_browser_connection->async_back();
// FIXME: 4. If the previous step completed results in a pageHide event firing, wait until pageShow event
// fires or for the session page load timeout milliseconds to pass, whichever occurs sooner.
// FIXME: 5. If the previous step completed by the session page load timeout being reached, and user
// prompts have been handled, return error with error code timeout.
// 6. Return success with data null.
return JsonValue();
}
// 10.4 Forward, https://w3c.github.io/webdriver/#dfn-forward
Web::WebDriver::Response Session::forward()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts and return its value if it is an error.
// 3. Traverse the history by a delta 1 for the current browsing context.
m_browser_connection->async_forward();
// FIXME: 4. If the previous step completed results in a pageHide event firing, wait until pageShow event
// fires or for the session page load timeout milliseconds to pass, whichever occurs sooner.
// FIXME: 5. If the previous step completed by the session page load timeout being reached, and user
// prompts have been handled, return error with error code timeout.
// 6. Return success with data null.
return JsonValue();
}
// 10.5 Refresh, https://w3c.github.io/webdriver/#dfn-refresh
Web::WebDriver::Response Session::refresh()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts and return its value if it is an error.
// 3. Initiate an overridden reload of the current top-level browsing contexts active document.
m_browser_connection->async_refresh();
// FIXME: 4. If url is special except for file:
// FIXME: 1. Try to wait for navigation to complete.
// FIXME: 2. Try to run the post-navigation checks.
// FIXME: 5. Set the current browsing context with current top-level browsing context.
// 6. Return success with data null.
return JsonValue();
}
// 10.6 Get Title, https://w3c.github.io/webdriver/#dfn-get-title
Web::WebDriver::Response Session::get_title()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts and return its value if it is an error.
// 3. Let title be the initial value of the title IDL attribute of the current top-level browsing context's active document.
// 4. Return success with data title.
return JsonValue(m_browser_connection->get_title());
}
// 11.1 Get Window Handle, https://w3c.github.io/webdriver/#get-window-handle
Web::WebDriver::Response Session::get_window_handle()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// 2. Return success with data being the window handle associated with the current top-level browsing context.
return JsonValue { m_current_window_handle };
}
// 11.2 Close Window, https://w3c.github.io/webdriver/#dfn-close-window
ErrorOr<void, Variant<Web::WebDriver::Error, Error>> Session::close_window()
{
// 1. If the current top-level browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// 2. Close the current top-level browsing context.
m_windows.remove(m_current_window_handle);
// 3. If there are no more open top-level browsing contexts, then close the session.
if (m_windows.is_empty()) {
auto result = stop();
if (result.is_error()) {
return Variant<Web::WebDriver::Error, Error>(result.release_error());
}
}
return {};
}
// 11.4 Get Window Handles, https://w3c.github.io/webdriver/#dfn-get-window-handles
Web::WebDriver::Response Session::get_window_handles() const
{
// 1. Let handles be a JSON List.
auto handles = JsonArray {};
// 2. For each top-level browsing context in the remote end, push the associated window handle onto handles.
for (auto const& window_handle : m_windows.keys())
handles.append(window_handle);
// 3. Return success with data handles.
return JsonValue { handles };
}
// 14.3 Add Cookie, https://w3c.github.io/webdriver/#dfn-adding-a-cookie
Web::WebDriver::Response Session::add_cookie(JsonValue const& payload)
{
// 1. Let data be the result of getting a property named cookie from the parameters argument.
if (!payload.is_object() || !payload.as_object().has_object("cookie"sv))
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Payload doesn't have a cookie object");
auto const& maybe_data = payload.as_object().get("cookie"sv);
// 2. If data is not a JSON Object with all the required (non-optional) JSON keys listed in the table for cookie conversion,
// return error with error code invalid argument.
// NOTE: Table is here: https://w3c.github.io/webdriver/#dfn-table-for-cookie-conversion
if (!maybe_data.is_object())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Value \"cookie\' is not an object");
auto const& data = maybe_data.as_object();
if (!data.has("name"sv) || !data.has("value"sv))
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Cookie-Object doesn't contain all required keys");
// 3. If the current browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 4. Handle any user prompts, and return its value if it is an error.
// FIXME: 5. If the current browsing contexts document element is a cookie-averse Document object,
// return error with error code invalid cookie domain.
// 6. If cookie name or cookie value is null,
// FIXME: cookie domain is not equal to the current browsing contexts active documents domain,
// cookie secure only or cookie HTTP only are not boolean types,
// or cookie expiry time is not an integer type, or it less than 0 or greater than the maximum safe integer,
// return error with error code invalid argument.
if (data.get("name"sv).is_null() || data.get("value"sv).is_null())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Cookie-Object is malformed: name or value are null");
if (data.has("secure"sv) && !data.get("secure"sv).is_bool())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Cookie-Object is malformed: secure is not bool");
if (data.has("httpOnly"sv) && !data.get("httpOnly"sv).is_bool())
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Cookie-Object is malformed: httpOnly is not bool");
Optional<Core::DateTime> expiry_time;
if (data.has("expiry"sv)) {
auto expiry_argument = data.get("expiry"sv);
if (!expiry_argument.is_u32()) {
// NOTE: less than 0 or greater than safe integer are handled by the JSON parser
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Cookie-Object is malformed: expiry is not u32");
}
expiry_time = Core::DateTime::from_timestamp(expiry_argument.as_u32());
}
// 7. Create a cookie in the cookie store associated with the active documents address using
// cookie name name, cookie value value, and an attribute-value list of the following cookie concepts
// listed in the table for cookie conversion from data:
Web::Cookie::ParsedCookie cookie;
if (auto name_attribute = data.get("name"sv); name_attribute.is_string())
cookie.name = name_attribute.as_string();
else
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Expect name attribute to be string");
if (auto value_attribute = data.get("value"sv); value_attribute.is_string())
cookie.value = value_attribute.as_string();
else
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Expect value attribute to be string");
// Cookie path
// The value if the entry exists, otherwise "/".
if (data.has("path"sv)) {
if (auto path_attribute = data.get("path"sv); path_attribute.is_string())
cookie.path = path_attribute.as_string();
else
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Expect path attribute to be string");
} else {
cookie.path = "/";
}
// Cookie domain
// The value if the entry exists, otherwise the current browsing contexts active documents URL domain.
// NOTE: The otherwise case is handled by the CookieJar
if (data.has("domain"sv)) {
if (auto domain_attribute = data.get("domain"sv); domain_attribute.is_string())
cookie.domain = domain_attribute.as_string();
else
return Web::WebDriver::Error::from_code(Web::WebDriver::ErrorCode::InvalidArgument, "Expect domain attribute to be string");
}
// Cookie secure only
// The value if the entry exists, otherwise false.
if (data.has("secure"sv)) {
cookie.secure_attribute_present = data.get("secure"sv).as_bool();
} else {
cookie.secure_attribute_present = false;
}
// Cookie HTTP only
// The value if the entry exists, otherwise false.
if (data.has("httpOnly"sv)) {
cookie.http_only_attribute_present = data.get("httpOnly"sv).as_bool();
} else {
cookie.http_only_attribute_present = false;
}
// Cookie expiry time
// The value if the entry exists, otherwise leave unset to indicate that this is a session cookie.
cookie.expiry_time_from_expires_attribute = expiry_time;
// FIXME: Cookie same site
// The value if the entry exists, otherwise leave unset to indicate that no same site policy is defined.
m_browser_connection->async_add_cookie(move(cookie));
// If there is an error during this step, return error with error code unable to set cookie.
// NOTE: This probably should only apply to the actual setting of the cookie in the Browser,
// which cannot fail in our case.
// Thus, the error-codes used above are 400 "invalid argument".
// 8. Return success with data null.
return JsonValue();
}
// https://w3c.github.io/webdriver/#dfn-delete-cookies
void Session::delete_cookies(Optional<StringView> const& name)
{
// For each cookie among all associated cookies of the current browsing contexts active document,
// run the substeps of the first matching condition:
for (auto& cookie : m_browser_connection->get_all_cookies()) {
// -> name is undefined
// -> name is equal to cookie name
if (!name.has_value() || name.value() == cookie.name) {
// Set the cookie expiry time to a Unix timestamp in the past.
cookie.expiry_time = Core::DateTime::from_timestamp(0);
m_browser_connection->async_update_cookie(cookie);
}
// -> Otherwise
// Do nothing.
}
}
// 14.4 Delete Cookie, https://w3c.github.io/webdriver/#dfn-delete-cookie
Web::WebDriver::Response Session::delete_cookie(StringView name)
{
// 1. If the current browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts, and return its value if it is an error.
// 3. Delete cookies using the url variable name parameter as the filter argument.
delete_cookies(name);
// 4. Return success with data null.
return JsonValue();
}
// 14.5 Delete All Cookies, https://w3c.github.io/webdriver/#dfn-delete-all-cookies
Web::WebDriver::Response Session::delete_all_cookies()
{
// 1. If the current browsing context is no longer open, return error with error code no such window.
TRY(check_for_open_top_level_browsing_context_or_return_error());
// FIXME: 2. Handle any user prompts, and return its value if it is an error.
// 3. Delete cookies, giving no filtering argument.
delete_cookies();
// 4. Return success with data null.
return JsonValue();
}
}