From 3a1e7695738c5bd467315ba75f888cd642846fbc Mon Sep 17 00:00:00 2001 From: Moti Zilberman Date: Mon, 19 Feb 2024 05:34:11 -0800 Subject: [PATCH] Reject Runtime.evaluate messages with the wrong contextId 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 --- .../tests/JsiIntegrationTest.cpp | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) diff --git a/packages/react-native/ReactCommon/jsinspector-modern/tests/JsiIntegrationTest.cpp b/packages/react-native/ReactCommon/jsinspector-modern/tests/JsiIntegrationTest.cpp index 7ea5c9b708f088..5cf2e1f0e73ca0 100644 --- a/packages/react-native/ReactCommon/jsinspector-modern/tests/JsiIntegrationTest.cpp +++ b/packages/react-native/ReactCommon/jsinspector-modern/tests/JsiIntegrationTest.cpp @@ -5,6 +5,7 @@ * LICENSE file in the root directory of this source tree. */ +#include #include #include #include @@ -23,6 +24,7 @@ #include "engines/JsiIntegrationTestHermesEngineAdapter.h" using namespace ::testing; +using folly::sformat; namespace facebook::react::jsinspector_modern { @@ -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