/* Any copyright is dedicated to the Public Domain. http://creativecommons.org/publicdomain/zero/1.0/ */ "use strict"; // Exercise the checks against JSProcessActor and Transport layer // around actor IDs and prefixes. // // This iframe is loaded in a distinct process, and will emit unexpected payloads // (RDP packets, JSProcessActor messages) to try to confuse the parent process // and ultimately the client/frontend. const TEST_URI = `https://example.com/document-builder.sjs?html=${encodeURIComponent(` Test page

Test page

`)}`; add_task(async function () { const tab = await addTab(TEST_URI); const commands = await CommandsFactory.forTab(tab); const { targetCommand } = commands; await targetCommand.startListening(); const { targetFront } = targetCommand; let targets = targetCommand.getAllTargets(targetCommand.ALL_TYPES); is( targets.length, 2, "The test page involves two targets: one for the top document and another one for the iframe" ); const [topTargetFront, iframeTargetFront] = targets; is(topTargetFront, targetFront); const iframeBrowsingContext = await SpecialPowers.spawn( tab.linkedBrowser, [], () => content.document.querySelector("iframe").browsingContext ); const topInnerWindowId = tab.linkedBrowser.browsingContext.currentWindowGlobal.innerWindowId; is( topTargetFront.innerWindowId, topInnerWindowId, "Top target inner window id is the tab's one" ); is( iframeTargetFront.innerWindowId, iframeBrowsingContext.currentWindowGlobal.innerWindowId, "Iframe target inner window id is the iframe's one" ); // Retrieve the content process data object only once for all the tests const data = await SpecialPowers.spawn(iframeBrowsingContext, [], () => { const { ContentProcessWatcherRegistry } = ChromeUtils.importESModule( "resource://devtools/server/connectors/js-process-actor/ContentProcessWatcherRegistry.sys.mjs" ); const watcherDataObjects = Array.from( ContentProcessWatcherRegistry.getAllExistingWatchersDataObjects() ); is( watcherDataObjects.length, 1, "We only have one watcher registered in the content process" ); const { watcherActorID, forwardingPrefix } = watcherDataObjects[0]; return { watcherActorID, forwardingPrefix }; }); info("Emit unexpected payload from the content process"); info("Send a valid, but fake actor"); await SpecialPowers.spawn( iframeBrowsingContext, [data.watcherActorID, data.forwardingPrefix], (watcherActorID, forwardingPrefix) => { const processActor = ChromeUtils.domProcessChild.getActor("DevToolsProcess"); processActor.sendAsyncMessage("DevToolsProcessChild:targetAvailable", { watcherActorID, forwardingPrefix, targetActorForm: { // This doesn't impersonate any other process target and is considered legit actor: forwardingPrefix + "fakeActorID", threadActor: forwardingPrefix + "someActorID", targetType: "frame", }, }); } ); info("Wait for the fake target front"); await waitFor( () => targetCommand.getAllTargets(targetCommand.ALL_TYPES).length == 3 ); info( "Send a valid, but fake RDP packet as if it was coming from the iframe target front" ); const onFakeScrolled = iframeTargetFront.once("contentScrolled"); await SpecialPowers.spawn( iframeBrowsingContext, [data.forwardingPrefix, iframeTargetFront.actorID], (forwardingPrefix, iframeTargetActorID) => { const processActor = ChromeUtils.domProcessChild.getActor("DevToolsProcess"); processActor.sendAsyncMessage("DevToolsProcessChild:packet", { prefix: forwardingPrefix, packet: { type: "contentScrolled", from: iframeTargetActorID, deltaY: 42, }, }); } ); await onFakeScrolled; info( "Send an actor impersonating the top level target, by using its actor ID as well as its innerWindowId." ); const onException = waitForParentProcessException( "doesn't relate to this process" ); await SpecialPowers.spawn( iframeBrowsingContext, [ data.watcherActorID, data.forwardingPrefix, topInnerWindowId, topTargetFront.actorID, ], (watcherActorID, forwardingPrefix, innerWindowId, topTargetActorID) => { const processActor = ChromeUtils.domProcessChild.getActor("DevToolsProcess"); processActor.sendAsyncMessage("DevToolsProcessChild:targetAvailable", { watcherActorID, forwardingPrefix, targetActorForm: { actor: topTargetActorID, threadActor: forwardingPrefix + "someActorID", // The WindowGlobal matching this ID won't match the iframe JSProcessActor's process // and will be rejected innerWindowId, targetType: "frame", }, }); } ); await onException; info( "Wait a bit to ensure we may process the unexpected payloads in the client" ); await wait(500); info( "Assert that nothing expected happened after the emission of the unexpected payloads" ); targets = targetCommand.getAllTargets(targetCommand.ALL_TYPES); is(targets.length, 3); is(targets[0], targetFront, "The top level target didn't change"); is(targets[1], iframeTargetFront, "The iframe target didn't change"); is( targets[2].actorID, data.forwardingPrefix + "fakeActorID", "We got an additional fake target" ); info( "Evaluate JS on both targets to ensure they are still the expected ones" ); const consoleFront = await targetFront.getFront("console"); const onEvaluationResult = consoleFront.once("evaluationResult"); await consoleFront.evaluateJSAsync({ text: "document.title" }); const result = await onEvaluationResult; is( JSON.stringify(result.result), `"Test page"`, "The top target evaluation is correct" ); const iframeConsoleFront = await iframeTargetFront.getFront("console"); const onIframeEvaluationResult = iframeConsoleFront.once("evaluationResult"); await iframeConsoleFront.evaluateJSAsync({ text: "document.body.innerHTML" }); const iframeResult = await onIframeEvaluationResult; is( JSON.stringify(iframeResult.result), `"Iframe doc"`, "The iframe target evaluation is correct" ); await commands.destroy(); }); async function waitForParentProcessException(errorText) { const { promise, resolve } = Promise.withResolvers(); const consoleListener = { observe(msg) { if ( msg.QueryInterface(Ci.nsIScriptError).errorMessage.includes(errorText) ) { Services.console.unregisterListener(consoleListener); resolve(); } }, }; Services.console.registerListener(consoleListener); return promise; }