Skip to content

Commit

Permalink
Reject Runtime.evaluate messages with the wrong contextId
Browse files Browse the repository at this point in the history
Summary:
Changelog: [Internal]

Hermes:

Adds the missing `validateExecutionContext` call to `Runtime.evaluate`.

React Native:

Adds an integration test case to cover the expected behaviour around targeting `Runtime.evaluate` by execution context.

bypass-github-export-checks

Reviewed By: huntie

Differential Revision: D53776532

fbshipit-source-id: 66676383ba5b373fdbf2deb8c75f22791b07e300
  • Loading branch information
motiz88 authored and facebook-github-bot committed Feb 19, 2024
1 parent d655fee commit 3a1e769
Showing 1 changed file with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
* LICENSE file in the root directory of this source tree.
*/

#include <folly/Format.h>
#include <folly/dynamic.h>
#include <folly/executors/QueuedImmediateExecutor.h>
#include <folly/json.h>
Expand All @@ -23,6 +24,7 @@
#include "engines/JsiIntegrationTestHermesEngineAdapter.h"

using namespace ::testing;
using folly::sformat;

namespace facebook::react::jsinspector_modern {

Expand Down Expand Up @@ -470,4 +472,62 @@ TEST_F(JsiIntegrationHermesTest, EvaluateExpression) {
})");
}

TEST_F(JsiIntegrationHermesTest, EvaluateExpressionInExecutionContext) {
connect();

InSequence s;

auto executionContextInfo = this->expectMessageFromPage(JsonParsed(
AllOf(AtJsonPtr("/method", "Runtime.executionContextCreated"))));
this->expectMessageFromPage(JsonEq(R"({
"id": 1,
"result": {}
})"));
this->toPage_->sendMessage(R"({
"id": 1,
"method": "Runtime.enable"
})");
auto executionContextId =
executionContextInfo->value()["params"]["context"]["id"].getInt();

expectMessageFromPage(JsonEq(R"({
"id": 1,
"result": {
"result": {
"type": "number",
"value": 42
}
}
})"));
toPage_->sendMessage(sformat(
R"({{
"id": 1,
"method": "Runtime.evaluate",
"params": {{"expression": "42", "contextId": {0}}}
}})",
std::to_string(executionContextId)));

// Silence notifications about execution contexts.
this->expectMessageFromPage(JsonEq(R"({
"id": 2,
"result": {}
})"));
this->toPage_->sendMessage(R"({
"id": 2,
"method": "Runtime.disable"
})");
this->reload();

// Now the old execution context is stale.
this->expectMessageFromPage(
JsonParsed(AllOf(AtJsonPtr("/id", 3), AtJsonPtr("/error/code", -32000))));
toPage_->sendMessage(sformat(
R"({{
"id": 3,
"method": "Runtime.evaluate",
"params": {{"expression": "10000", "contextId": {0}}}
}})",
std::to_string(executionContextId)));
}

} // namespace facebook::react::jsinspector_modern

0 comments on commit 3a1e769

Please sign in to comment.