Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
WebDriver: do not try to parse http body if method is not POST
https://bugs.webkit.org/show_bug.cgi?id=179918

Reviewed by Darin Adler.

As said in the spec:

  5. If request’s method is POST:

    1. Let parse result be the result of parsing as JSON with request’s body as the argument. If this process
       throws an exception, return an error with error code invalid argument and jump back to step 1 in this
       overall algorithm.

    2. If parse result is not an Object, send an error with error code invalid argument and jump back to step 1
       in this overall algorithm.

    Otherwise, let parameters be parse result.

  Otherwise, let parameters be null.

6.3 Processing Model
https://w3c.github.io/webdriver/webdriver-spec.html#processing-model

Now, w3c tests are sending null as body of delete session command (it used to be just empty), making it fail
with invalid argument error.

* WebDriverService.cpp:
(WebDriver::WebDriverService::findCommand):
(WebDriver::WebDriverService::handleRequest):
* WebDriverService.h:

Canonical link: https://commits.webkit.org/195951@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@225083 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
carlosgcampos committed Nov 21, 2017
1 parent b16d5f6 commit 149ca51
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 9 deletions.
33 changes: 33 additions & 0 deletions Source/WebDriver/ChangeLog
@@ -1,3 +1,36 @@
2017-11-21 Carlos Garcia Campos <cgarcia@igalia.com>

WebDriver: do not try to parse http body if method is not POST
https://bugs.webkit.org/show_bug.cgi?id=179918

Reviewed by Darin Adler.

As said in the spec:

5. If request’s method is POST:

1. Let parse result be the result of parsing as JSON with request’s body as the argument. If this process
throws an exception, return an error with error code invalid argument and jump back to step 1 in this
overall algorithm.

2. If parse result is not an Object, send an error with error code invalid argument and jump back to step 1
in this overall algorithm.

Otherwise, let parameters be parse result.

Otherwise, let parameters be null.

6.3 Processing Model
https://w3c.github.io/webdriver/webdriver-spec.html#processing-model

Now, w3c tests are sending null as body of delete session command (it used to be just empty), making it fail
with invalid argument error.

* WebDriverService.cpp:
(WebDriver::WebDriverService::findCommand):
(WebDriver::WebDriverService::handleRequest):
* WebDriverService.h:

2017-11-21 Carlos Garcia Campos <cgarcia@igalia.com>

WebDriver: crash in Session::computeElementLayout when called without a current browsing context
Expand Down
17 changes: 9 additions & 8 deletions Source/WebDriver/WebDriverService.cpp
Expand Up @@ -174,15 +174,11 @@ std::optional<WebDriverService::HTTPMethod> WebDriverService::toCommandHTTPMetho
return std::nullopt;
}

bool WebDriverService::findCommand(const String& method, const String& path, CommandHandler* handler, HashMap<String, String>& parameters)
bool WebDriverService::findCommand(HTTPMethod method, const String& path, CommandHandler* handler, HashMap<String, String>& parameters)
{
auto commandMethod = toCommandHTTPMethod(method);
if (!commandMethod)
return false;

size_t length = WTF_ARRAY_LENGTH(s_commands);
for (size_t i = 0; i < length; ++i) {
if (s_commands[i].method != *commandMethod)
if (s_commands[i].method != method)
continue;

Vector<String> pathTokens;
Expand Down Expand Up @@ -213,15 +209,20 @@ bool WebDriverService::findCommand(const String& method, const String& path, Com

void WebDriverService::handleRequest(HTTPRequestHandler::Request&& request, Function<void (HTTPRequestHandler::Response&&)>&& replyHandler)
{
auto method = toCommandHTTPMethod(request.method);
if (!method) {
sendResponse(WTFMove(replyHandler), CommandResult::fail(CommandResult::ErrorCode::UnknownCommand, String("Unknown method: " + request.method)));
return;
}
CommandHandler handler;
HashMap<String, String> parameters;
if (!findCommand(request.method, request.path, &handler, parameters)) {
if (!findCommand(method.value(), request.path, &handler, parameters)) {
sendResponse(WTFMove(replyHandler), CommandResult::fail(CommandResult::ErrorCode::UnknownCommand, String("Unknown command: " + request.path)));
return;
}

RefPtr<InspectorObject> parametersObject;
if (request.dataLength) {
if (method.value() == HTTPMethod::Post && request.dataLength) {
RefPtr<InspectorValue> messageValue;
if (!InspectorValue::parseJSON(String::fromUTF8(request.data, request.dataLength), messageValue)) {
sendResponse(WTFMove(replyHandler), CommandResult::fail(CommandResult::ErrorCode::InvalidArgument));
Expand Down
2 changes: 1 addition & 1 deletion Source/WebDriver/WebDriverService.h
Expand Up @@ -62,7 +62,7 @@ class WebDriverService final : public HTTPRequestHandler {
static const Command s_commands[];

static std::optional<HTTPMethod> toCommandHTTPMethod(const String& method);
static bool findCommand(const String& method, const String& path, CommandHandler*, HashMap<String, String>& parameters);
static bool findCommand(HTTPMethod, const String& path, CommandHandler*, HashMap<String, String>& parameters);

void newSession(RefPtr<Inspector::InspectorObject>&&, Function<void (CommandResult&&)>&&);
void deleteSession(RefPtr<Inspector::InspectorObject>&&, Function<void (CommandResult&&)>&&);
Expand Down

0 comments on commit 149ca51

Please sign in to comment.