diff --git a/Userland/Applications/Browser/WebDriverConnection.cpp b/Userland/Applications/Browser/WebDriverConnection.cpp index 606cbabe49..cd39d16826 100644 --- a/Userland/Applications/Browser/WebDriverConnection.cpp +++ b/Userland/Applications/Browser/WebDriverConnection.cpp @@ -95,6 +95,20 @@ Messages::WebDriverSessionClient::GetNamedCookieResponse WebDriverConnection::ge return { {} }; } +void WebDriverConnection::add_cookie(Web::Cookie::ParsedCookie const& cookie) +{ + dbgln("WebDriverConnection: add_cookie"); + if (auto browser_window = m_browser_window.strong_ref()) { + auto& tab = browser_window->active_tab(); + if (tab.on_set_cookie) { + // FIXME: The spec doesn't say anything about the source + // but can we assume a cookie created through a HTTP-request to the WebDriver + // to be (source) from an HTTP-API? + tab.on_set_cookie(tab.url(), cookie, Web::Cookie::Source::Http); + } + } +} + void WebDriverConnection::update_cookie(Web::Cookie::Cookie const& cookie) { if (auto browser_window = m_browser_window.strong_ref()) { diff --git a/Userland/Applications/Browser/WebDriverConnection.h b/Userland/Applications/Browser/WebDriverConnection.h index 138f914add..263984cd3a 100644 --- a/Userland/Applications/Browser/WebDriverConnection.h +++ b/Userland/Applications/Browser/WebDriverConnection.h @@ -45,6 +45,7 @@ public: virtual void forward() override; virtual Messages::WebDriverSessionClient::GetAllCookiesResponse get_all_cookies() override; virtual Messages::WebDriverSessionClient::GetNamedCookieResponse get_named_cookie(String const& name) override; + virtual void add_cookie(Web::Cookie::ParsedCookie const&) override; virtual void update_cookie(Web::Cookie::Cookie const&) override; private: diff --git a/Userland/Applications/Browser/WebDriverSessionClient.ipc b/Userland/Applications/Browser/WebDriverSessionClient.ipc index f45e3f0266..457bc43610 100644 --- a/Userland/Applications/Browser/WebDriverSessionClient.ipc +++ b/Userland/Applications/Browser/WebDriverSessionClient.ipc @@ -1,6 +1,7 @@ #include #include #include +#include endpoint WebDriverSessionClient { quit() =| @@ -13,6 +14,7 @@ endpoint WebDriverSessionClient { forward() =| get_all_cookies() => (Vector cookies) get_named_cookie(String name) => (Optional cookie) + add_cookie(Web::Cookie::ParsedCookie cookie) =| update_cookie(Web::Cookie::Cookie cookie) =| } diff --git a/Userland/Services/WebDriver/Client.cpp b/Userland/Services/WebDriver/Client.cpp index cb2ede70a8..12e3d2cdd3 100644 --- a/Userland/Services/WebDriver/Client.cpp +++ b/Userland/Services/WebDriver/Client.cpp @@ -32,6 +32,7 @@ Vector Client::s_routes = { { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "forward" }, &Client::handle_forward }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie" }, &Client::handle_get_all_cookies }, { HTTP::HttpRequest::Method::GET, { "session", ":session_id", "cookie", ":name" }, &Client::handle_get_named_cookie }, + { HTTP::HttpRequest::Method::POST, { "session", ":session_id", "cookie" }, &Client::handle_add_cookie }, { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie", ":name" }, &Client::handle_delete_cookie }, { HTTP::HttpRequest::Method::DELETE, { "session", ":session_id", "cookie" }, &Client::handle_delete_all_cookies }, }; @@ -516,6 +517,17 @@ ErrorOr Client::handle_get_named_cookie(Vector return make_json_value(cookies); } +// POST /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-adding-a-cookie +ErrorOr Client::handle_add_cookie(Vector parameters, JsonValue const& payload) +{ + dbgln_if(WEBDRIVER_DEBUG, "Handling POST /session//cookie"); + Session* session = TRY(find_session_with_id(parameters[0])); + + // NOTE: Spec steps handled in Session::add_cookie(). + auto result = TRY(session->add_cookie(payload)); + return make_json_value(result); +} + // DELETE /session/{session id}/cookie/{name} https://w3c.github.io/webdriver/#dfn-delete-cookie ErrorOr Client::handle_delete_cookie(Vector parameters, JsonValue const&) { diff --git a/Userland/Services/WebDriver/Client.h b/Userland/Services/WebDriver/Client.h index 2b50bb6631..fca28d43de 100644 --- a/Userland/Services/WebDriver/Client.h +++ b/Userland/Services/WebDriver/Client.h @@ -58,6 +58,7 @@ private: ErrorOr handle_forward(Vector, JsonValue const& payload); ErrorOr handle_get_all_cookies(Vector, JsonValue const& payload); ErrorOr handle_get_named_cookie(Vector, JsonValue const& payload); + ErrorOr handle_add_cookie(Vector, JsonValue const& payload); ErrorOr handle_delete_cookie(Vector, JsonValue const& payload); ErrorOr handle_delete_all_cookies(Vector, JsonValue const& payload); diff --git a/Userland/Services/WebDriver/Session.cpp b/Userland/Services/WebDriver/Session.cpp index 5f2456b3c1..c8e85ab3c7 100644 --- a/Userland/Services/WebDriver/Session.cpp +++ b/Userland/Services/WebDriver/Session.cpp @@ -13,6 +13,7 @@ #include #include #include +#include #include namespace WebDriver { @@ -297,6 +298,126 @@ ErrorOr Session::get_named_cookie(String const& name) return HttpError { 404, "no such cookie", "Cookie not found" }; } +// POST /session/{session id}/cookie https://w3c.github.io/webdriver/#dfn-adding-a-cookie +ErrorOr 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 HttpError { 400, "invalid argument", "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 HttpError { 400, "invalid argument", "Value \"cookie\' is not an object" }; + + auto const& data = maybe_data.as_object(); + + if (!data.has("name"sv) || !data.has("value"sv)) + return HttpError { 400, "invalid argument", "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. + auto current_window = get_window_object(); + if (!current_window.has_value()) + return HttpError { 404, "no such window", "Window not found" }; + + // FIXME: 4. Handle any user prompts, and return its value if it is an error. + + // FIXME: 5. If the current browsing context’s 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 context’s active document’s 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 HttpError { 400, "invalid argument", "Cookie-Object is malformed: name or value are null" }; + if (data.has("secure"sv) && !data.get("secure"sv).is_bool()) + return HttpError { 400, "invalid argument", "Cookie-Object is malformed: secure is not bool" }; + if (data.has("httpOnly"sv) && !data.get("httpOnly"sv).is_bool()) + return HttpError { 400, "invalid argument", "Cookie-Object is malformed: httpOnly is not bool" }; + Optional 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 HttpError { 400, "invalid argument", "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 document’s 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 HttpError { 400, "invalid argument", "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 HttpError { 400, "invalid argument", "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 HttpError { 400, "invalid argument", "Expect path attribute to be string" }; + } else { + cookie.path = "/"; + } + + // Cookie domain + // The value if the entry exists, otherwise the current browsing context’s active document’s 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 HttpError { 400, "invalid argument", "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 const& name) { diff --git a/Userland/Services/WebDriver/Session.h b/Userland/Services/WebDriver/Session.h index 4dfa61c299..cf3d45696d 100644 --- a/Userland/Services/WebDriver/Session.h +++ b/Userland/Services/WebDriver/Session.h @@ -42,6 +42,7 @@ public: ErrorOr forward(); ErrorOr get_all_cookies(); ErrorOr get_named_cookie(String const& name); + ErrorOr add_cookie(JsonValue const& payload); ErrorOr delete_cookie(StringView const& name); ErrorOr delete_all_cookies();