Skip to content

Commit 3c91014

Browse files
committed
Web Inspector: [Site isolation] "Inspect Element" on a cross-origin iframe's content opens Web Inspector but doesn't scroll to/select the node
https://bugs.webkit.org/show_bug.cgi?id=326924 rdar://187827968 Reviewed by Qianlang Chen. With Site Isolation on, choosing Inspect Element on a node inside a cross-site iframe opened Web Inspector but did not select the node in the Elements tab. The node lives in another process, so the page DOM agent has no node for it, and frame targets have no Inspector domain to deliver Inspector.inspect. Even when the node was reported through DOM.inspect, its frame document was not yet spliced into the page tree, so the Elements tab had nothing to reveal. To fix this, Inspect Element now takes one of two routes depending on where the node lives. A node in the main frame, or in a same-site frame in the main frame's process, still goes through the page DOM agent exactly as before. Any other node goes through the DOM agent of its own frame, which sends DOM.inspect, the event the frontend already routes to inspectElementInFrameTarget(). A same-site frame nested under a cross-site one takes this second route too, since the page agent's tree stops at the remote <iframe> and cannot reach it. Picking an element in inspect mode inside a frame target now reports the same way. If the inspector is opened by the very click that chose the node, the frame holds on to the node until a frontend connects. The frontend then has to connect that frame's document to the page tree before the Elements tab can show it, and that needs the <iframe> elements above it. Those live in the parent frame's process, so the inspected frame cannot supply them. This adds DOM.requestFrameOwner(frameId), which asks a frame's agent to push the owner element of one of its child frames, along with its path. The existing frameId splice then attaches the document below it. Starting from the inspected node, the frontend asks the page target for the owner of its frame. If the page target cannot answer, it asks every frame target; only the parent frame's target can, since each frame agent only looks at its direct children. This does not depend on the frontend's frame model or on the parent's document having arrived: each frame target was sent DOM.getDocument when it was initialized, so that reply arrives before the pushed owner. It repeats one level up if that owner is itself inside a document that is not spliced yet. This keeps working for nested cross-site frames. Once the node is connected to the page document, the Elements tab reveals it. If an owner cannot be found, the node is revealed as it was before. This is easy to reason about because it only asks for what the node needs: one owner per level, instead of searching the page tree for iframes. It also reuses the frameId-based splicing instead of adding a second way to connect frame documents. No new tests: It seems WebKitTestRunner cannot drive the Inspect Element context menu inside a cross-site frame. * Source/JavaScriptCore/inspector/protocol/DOM.json: * Source/WebCore/inspector/FrameInspectorController.cpp: (WebCore::FrameInspectorController::connectFrontend): (WebCore::FrameInspectorController::inspect): (WebCore::FrameInspectorController::disconnectFrontend): (WebCore::FrameInspectorController::inspectedFrameDestroyed): * Source/WebCore/inspector/FrameInspectorController.h: * Source/WebCore/inspector/PageInspectorController.cpp: (WebCore::isCoveredByPageDOMAgent): (WebCore::PageInspectorController::inspect): * Source/WebCore/inspector/agents/InspectorDOMAgent.cpp: (WebCore::InspectorDOMAgent::requestFrameOwner): * Source/WebCore/inspector/agents/InspectorDOMAgent.h: * Source/WebCore/inspector/agents/frame/FrameDOMAgent.cpp: (WebCore::m_focusNodeTimer): (WebCore::FrameDOMAgent::willDestroyFrontendAndBackend): (WebCore::FrameDOMAgent::getDocument): (WebCore::FrameDOMAgent::requestFrameOwner): (WebCore::FrameDOMAgent::focusNode): (WebCore::m_destroyedNodesTimer): Deleted. * Source/WebCore/inspector/agents/frame/FrameDOMAgent.h: * Source/WebInspectorUI/UserInterface/Controllers/DOMManager.js: (WI.DOMManager): (WI.DOMManager.prototype._rootNodeOf): (WI.DOMManager.prototype._isInPageTree): (WI.DOMManager.prototype._revealPendingInspectedFrameNode): (WI.DOMManager.prototype._dispatchPendingInspectedFrameNode): (WI.DOMManager.prototype._requestFrameOwner): (WI.DOMManager.prototype._requestFrameOwnerFromFrameTargets): (WI.DOMManager.prototype._requestFrameOwnerFromTarget): (WI.DOMManager.prototype._documentUpdated): (WI.DOMManager.prototype.inspectElementInFrameTarget): (WI.DOMManager.prototype._trySpliceUnsplicedFrameDocuments): Canonical link: https://commits.webkit.org/324270@main
1 parent fcb71e3 commit 3c91014

9 files changed

Lines changed: 299 additions & 13 deletions

File tree

‎Source/JavaScriptCore/inspector/protocol/DOM.json‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,17 @@
563563
{ "name": "nodeId", "$ref": "NodeId", "description": "Node id for given object." }
564564
]
565565
},
566+
{
567+
"name": "requestFrameOwner",
568+
"description": "Requests that the frame owner element (such as <code>iframe</code>) of a child frame is sent to the caller. The child frame may be hosted by another process. All nodes that form the path from the owner element to the root are also sent to the client as a series of <code>setChildNodes</code> notifications.",
569+
"targetTypes": ["frame", "page"],
570+
"parameters": [
571+
{ "name": "frameId", "$ref": "Network.FrameId", "description": "Identifier of a frame whose owner element is in the document tree of this target." }
572+
],
573+
"returns": [
574+
{ "name": "nodeId", "$ref": "NodeId", "description": "Node id for the frame owner element." }
575+
]
576+
},
566577
{
567578
"name": "setInspectModeEnabled",
568579
"description": "Enters the 'inspect' mode. In this mode, elements that user is hovering over are highlighted. Backend then generates 'inspect' command upon element selection.",

‎Source/WebCore/inspector/FrameInspectorController.cpp‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
#include "JSDOMBindingSecurity.h"
5151
#include "JSDOMWindow.h"
5252
#include "JSExecState.h"
53+
#include "LocalFrameInlines.h"
54+
#include "Node.h"
55+
#include "NodeDocument.h"
5356
#include "PageInspectorController.h"
5457
#include "Settings.h"
5558
#include "WebInjectedScriptHost.h"
@@ -227,6 +230,20 @@ void FrameInspectorController::connectFrontend(Inspector::FrontendChannel& front
227230
m_injectedScriptManager->addClient();
228231
m_agents.didCreateFrontendAndBackend();
229232
}
233+
234+
// The node may belong to a document the frame has since navigated away from.
235+
if (RefPtr node = std::exchange(m_pendingNodeToInspect, nullptr); node && m_domAgent && &node->document() == m_frame->document())
236+
CheckedRef { *m_domAgent }->inspect(*node);
237+
}
238+
239+
void FrameInspectorController::inspect(Node& node)
240+
{
241+
if (m_frontendRouter->hasFrontends() && m_domAgent) {
242+
CheckedRef { *m_domAgent }->inspect(node);
243+
return;
244+
}
245+
246+
m_pendingNodeToInspect = WeakPtr<Node, WeakPtrImplWithEventTargetData> { node };
230247
}
231248

232249
void FrameInspectorController::disconnectFrontend(Inspector::FrontendChannel& frontendChannel)
@@ -236,6 +253,7 @@ void FrameInspectorController::disconnectFrontend(Inspector::FrontendChannel& fr
236253

237254
bool disconnectedLastFrontend = !m_frontendRouter->hasFrontends();
238255
if (disconnectedLastFrontend) {
256+
m_pendingNodeToInspect = nullptr;
239257
m_agents.willDestroyFrontendAndBackend(DisconnectReason::InspectorDestroyed);
240258
m_injectedScriptManager->removeClient();
241259
InspectorInstrumentation::unregisterInstrumentingAgents(m_instrumentingAgents.get());
@@ -244,6 +262,8 @@ void FrameInspectorController::disconnectFrontend(Inspector::FrontendChannel& fr
244262

245263
void FrameInspectorController::inspectedFrameDestroyed()
246264
{
265+
m_pendingNodeToInspect = nullptr;
266+
247267
if (!m_frontendRouter->hasFrontends())
248268
return;
249269

‎Source/WebCore/inspector/FrameInspectorController.h‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ class InspectorFrontendClient;
6161
class InspectorInstrumentation;
6262
class InstrumentingAgents;
6363
class LocalFrame;
64+
class Node;
6465
class PageInspectorController;
6566
class RenderObject;
6667
class WebInjectedScriptManager;
@@ -99,6 +100,10 @@ class FrameInspectorController final : public Inspector::InspectorEnvironment, p
99100
WEBCORE_EXPORT void siteIsolationFirstEnabled();
100101
void inspectedFrameDestroyed();
101102

103+
// Selects the node in the frontend. A node chosen while no frontend is connected is selected once the
104+
// first one connects.
105+
WEBCORE_EXPORT void inspect(Node&);
106+
102107
InstrumentingAgents& instrumentingAgents() const { return m_instrumentingAgents.get(); }
103108
WebInjectedScriptManager& injectedScriptManager() const { return m_injectedScriptManager.get(); }
104109

@@ -142,6 +147,9 @@ class FrameInspectorController final : public Inspector::InspectorEnvironment, p
142147

143148
// Non-owning; the agent is owned by m_agents. Kept so the overlay can reach the flex line-start cache.
144149
CheckedPtr<FrameDOMAgent> m_domAgent;
150+
151+
// Weak so a node never keeps its document alive; see inspect().
152+
WeakPtr<Node, WeakPtrImplWithEventTargetData> m_pendingNodeToInspect;
145153
};
146154

147155
} // namespace WebCore

‎Source/WebCore/inspector/PageInspectorController.cpp‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
#include "CommonVM.h"
3737
#include "DOMWrapperWorld.h"
3838
#include "DebuggerSuspensionController.h"
39+
#include "Document.h"
40+
#include "Frame.h"
3941
#include "FrameInspectorController.h"
42+
#include "FrameTree.h"
4043
#include "GraphicsContext.h"
4144
#include "InspectorAnimationAgent.h"
4245
#include "InspectorBackendClient.h"
@@ -56,6 +59,7 @@
5659
#include "JSExecState.h"
5760
#include "LocalDOMWindow.h"
5861
#include "LocalFrame.h"
62+
#include "NodeDocument.h"
5963
#include "Page.h"
6064
#include "PageAuditAgent.h"
6165
#include "PageCanvasAgent.h"
@@ -413,6 +417,17 @@ bool PageInspectorController::shouldShowOverlay() const
413417
return m_overlay->shouldShowOverlay();
414418
}
415419

420+
// The page DOM agent covers the main frame's document and the documents of the frames below it that live in
421+
// this process. A frame with a RemoteFrame ancestor is only reachable through its own FrameDOMAgent.
422+
static bool isCoveredByPageDOMAgent(const LocalFrame& frame)
423+
{
424+
for (RefPtr<const Frame> ancestor = &frame; ancestor; ancestor = ancestor->tree().parent()) {
425+
if (!is<LocalFrame>(*ancestor))
426+
return false;
427+
}
428+
return true;
429+
}
430+
416431
void PageInspectorController::inspect(Node* node)
417432
{
418433
if (!enabled())
@@ -421,6 +436,13 @@ void PageInspectorController::inspect(Node* node)
421436
if (!hasRemoteFrontend())
422437
show();
423438

439+
if (node) {
440+
if (RefPtr frame = node->document().frame(); frame && !isCoveredByPageDOMAgent(*frame)) {
441+
frame->inspectorController().inspect(*node);
442+
return;
443+
}
444+
}
445+
424446
CheckedRef { ensureDOMAgent() }->inspect(node);
425447
}
426448

‎Source/WebCore/inspector/agents/InspectorDOMAgent.cpp‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1907,6 +1907,34 @@ Inspector::Protocol::ErrorStringOr<Inspector::Protocol::DOM::NodeId> InspectorDO
19071907
return nodeId;
19081908
}
19091909

1910+
Inspector::Protocol::ErrorStringOr<Inspector::Protocol::DOM::NodeId> InspectorDOMAgent::requestFrameOwner(const Inspector::Protocol::Network::FrameId& frameId)
1911+
{
1912+
auto parsedFrameId = IdentifierRegistry::parseProtocolFrameId(frameId);
1913+
if (!parsedFrameId)
1914+
return makeUnexpected("Invalid frameId format"_s);
1915+
1916+
// Compare the FrameIdentifier only: the hosting process in a frame ID can be stale in a process that
1917+
// holds a RemoteFrame. Do not look below a RemoteFrame: the frames there are reached through their own
1918+
// FrameDOMAgent, and an owner element in their documents is not part of this agent's tree.
1919+
for (RefPtr frame = &m_inspectedPage->mainFrame(); frame; frame = is<LocalFrame>(*frame) ? frame->tree().traverseNext() : frame->tree().traverseNextSkippingChildren()) {
1920+
if (frame->frameID() != parsedFrameId->second)
1921+
continue;
1922+
1923+
RefPtr ownerElement = frame->ownerElement();
1924+
if (!ownerElement)
1925+
return makeUnexpected("Missing owner element for given frameId"_s);
1926+
1927+
Inspector::Protocol::ErrorString errorString;
1928+
auto nodeId = pushNodePathToFrontend(errorString, ownerElement.get());
1929+
if (!nodeId)
1930+
return makeUnexpected("Unable to push the owner element for given frameId"_s);
1931+
1932+
return nodeId;
1933+
}
1934+
1935+
return makeUnexpected("Missing frame for given frameId"_s);
1936+
}
1937+
19101938
String InspectorDOMAgent::documentURLString(Document* document)
19111939
{
19121940
if (!document || document->url().isNull())

‎Source/WebCore/inspector/agents/InspectorDOMAgent.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ class InspectorDOMAgent final : public InspectorAgentBase, public Inspector::DOM
155155
Inspector::Protocol::ErrorStringOr<void> setInspectModeEnabled(bool, RefPtr<JSON::Object>&& highlightConfig, RefPtr<JSON::Object>&& gridOverlayConfig, RefPtr<JSON::Object>&& flexOverlayConfig, std::optional<bool>&& showRulers);
156156
#endif
157157
Inspector::Protocol::ErrorStringOr<Inspector::Protocol::DOM::NodeId> requestNode(const Inspector::Protocol::Runtime::RemoteObjectId&);
158+
Inspector::Protocol::ErrorStringOr<Inspector::Protocol::DOM::NodeId> requestFrameOwner(const Inspector::Protocol::Network::FrameId&);
158159
Inspector::Protocol::ErrorStringOr<Inspector::Protocol::DOM::NodeId> pushNodeByPathToFrontend(const String& path);
159160
Inspector::Protocol::ErrorStringOr<void> hideHighlight();
160161
Inspector::Protocol::ErrorStringOr<void> highlightRect(int x, int y, int width, int height, RefPtr<JSON::Object>&& color, RefPtr<JSON::Object>&& outlineColor, std::optional<bool>&& usePageCoordinates);

‎Source/WebCore/inspector/agents/frame/FrameDOMAgent.cpp‎

Lines changed: 45 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,7 @@ FrameDOMAgent::FrameDOMAgent(FrameAgentContext& context, InspectorOverlay& overl
174174
, m_overlay(overlay)
175175
, m_injectedScriptManager(context.injectedScriptManager)
176176
, m_destroyedNodesTimer(*this, &FrameDOMAgent::destroyedNodesTimerFired)
177+
, m_focusNodeTimer(*this, &FrameDOMAgent::focusNode)
177178
{
178179
}
179180

@@ -216,6 +217,7 @@ void FrameDOMAgent::willDestroyFrontendAndBackend(Inspector::DisconnectReason)
216217

217218
m_inspectedNode = nullptr;
218219
m_nodeToFocus = nullptr;
220+
m_focusNodeTimer.stop();
219221
m_mousedOverNode = nullptr;
220222

221223
m_searchingForNode = false;
@@ -640,6 +642,12 @@ Inspector::CommandResult<Ref<Inspector::Protocol::DOM::Node>> FrameDOMAgent::get
640642
if (CheckedPtr cssAgent = Ref { m_instrumentingAgents.get() }->enabledFrameCSSAgent())
641643
cssAgent->didBindDocument();
642644

645+
// The frontend builds this frame's document tree from this reply, so a node chosen before the document
646+
// was requested can only be pushed once the reply has been sent. A timer runs after the reply; pushing
647+
// from inside this command would send setChildNodes and DOM.inspect before the frontend has a tree.
648+
if (m_nodeToFocus)
649+
m_focusNodeTimer.startOneShot(0_s);
650+
643651
return root;
644652
}
645653

@@ -1650,6 +1658,35 @@ Inspector::CommandResult<int> FrameDOMAgent::requestNode(const String& objectId)
16501658
return nodeId;
16511659
}
16521660

1661+
Inspector::CommandResult<int> FrameDOMAgent::requestFrameOwner(const String& frameId)
1662+
{
1663+
auto parsedFrameId = IdentifierRegistry::parseProtocolFrameId(frameId);
1664+
if (!parsedFrameId)
1665+
return makeUnexpected("Invalid frameId format"_s);
1666+
1667+
// Only direct children: every frame has its own FrameDOMAgent, and the frontend asks the target of the
1668+
// child's parent. Compare the FrameIdentifier only: the hosting process in a frame ID can be stale in a
1669+
// process that holds a RemoteFrame.
1670+
Ref frame = m_inspectedFrame;
1671+
for (RefPtr child = frame->tree().firstChild(); child; child = child->tree().nextSibling()) {
1672+
if (child->frameID() != parsedFrameId->second)
1673+
continue;
1674+
1675+
RefPtr ownerElement = child->ownerElement();
1676+
if (!ownerElement)
1677+
return makeUnexpected("Missing owner element for given frameId"_s);
1678+
1679+
Inspector::Protocol::ErrorString errorString;
1680+
auto nodeId = pushNodePathToFrontend(errorString, ownerElement.get());
1681+
if (!nodeId)
1682+
return makeUnexpected(errorString);
1683+
1684+
return nodeId;
1685+
}
1686+
1687+
return makeUnexpected("Missing child frame for given frameId"_s);
1688+
}
1689+
16531690
Inspector::CommandResult<void> FrameDOMAgent::setInspectedNode(int nodeId)
16541691
{
16551692
Inspector::Protocol::ErrorString errorString;
@@ -1981,18 +2018,18 @@ void FrameDOMAgent::focusNode()
19812018
if (!m_documentRequested)
19822019
return;
19832020

1984-
ASSERT(m_nodeToFocus);
1985-
auto node = std::exchange(m_nodeToFocus, nullptr);
1986-
RefPtr frame = node->document().frame();
1987-
if (!frame)
2021+
if (!m_document || !m_nodeToId.contains(*m_document))
19882022
return;
19892023

1990-
auto& globalObject = mainWorldGlobalObject(*frame);
1991-
auto injectedScript = m_injectedScriptManager->injectedScriptFor(&globalObject);
1992-
if (injectedScript.hasNoValue())
2024+
auto node = std::exchange(m_nodeToFocus, nullptr);
2025+
if (!node)
19932026
return;
19942027

1995-
injectedScript.inspectObject(InspectorDOMAgent::nodeAsScriptValue(globalObject, node.get()));
2028+
// Frame targets have no Inspector domain, so Inspector.inspect cannot reach the frontend from here.
2029+
// DOM.inspect is routed by the frontend to this frame target's tree.
2030+
Inspector::Protocol::ErrorString ignored;
2031+
if (auto nodeId = pushNodePathToFrontend(ignored, node.get()))
2032+
m_frontendDispatcher->inspect(nodeId);
19962033
}
19972034

19982035
// MARK: - Layout Overlays

‎Source/WebCore/inspector/agents/frame/FrameDOMAgent.h‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,7 @@ class FrameDOMAgent final : public InspectorAgentBase, public Inspector::DOMBack
106106
Inspector::CommandResult<Ref<JSON::ArrayOf<int>>> getSearchResults(const String& searchId, int fromIndex, int toIndex) override;
107107
Inspector::CommandResult<void> discardSearchResults(const String& searchId) override;
108108
Inspector::CommandResult<int> requestNode(const String& objectId) override;
109+
Inspector::CommandResult<int> requestFrameOwner(const String& frameId) override;
109110
#if PLATFORM(IOS_FAMILY)
110111
Inspector::CommandResult<void> setInspectModeEnabled(bool enabled, RefPtr<JSON::Object>&& highlightConfig, RefPtr<JSON::Object>&& gridOverlayConfig, RefPtr<JSON::Object>&& flexOverlayConfig) override;
111112
#else
@@ -159,6 +160,7 @@ class FrameDOMAgent final : public InspectorAgentBase, public Inspector::DOMBack
159160
void flexibleBoxRendererBeganLayout(const RenderObject&);
160161
void flexibleBoxRendererWrappedToNextLine(const RenderObject&, size_t lineStartItemIndex);
161162
bool handleMousePress();
163+
void inspect(Node&);
162164
bool handleTouchEvent(Node&);
163165
void mouseDidMoveOverElement(const HitTestResult&, OptionSet<PlatformEventModifier>);
164166
void mouseDidLeaveFrame();
@@ -208,7 +210,6 @@ class FrameDOMAgent final : public InspectorAgentBase, public Inspector::DOMBack
208210

209211
void setSearchingForNode(Inspector::Protocol::ErrorString&, bool enabled, RefPtr<JSON::Object>&& highlightConfig, RefPtr<JSON::Object>&& gridOverlayConfig, RefPtr<JSON::Object>&& flexOverlayConfig, bool showRulers);
210212
void highlightMousedOverNode();
211-
void inspect(Node&);
212213
void focusNode();
213214

214215
InspectorOverlay& overlay() const { return m_overlay.get(); }
@@ -273,6 +274,7 @@ class FrameDOMAgent final : public InspectorAgentBase, public Inspector::DOMBack
273274
Vector<Inspector::Protocol::DOM::NodeId> m_destroyedDetachedNodeIdentifiers;
274275
Vector<std::pair<Inspector::Protocol::DOM::NodeId, Inspector::Protocol::DOM::NodeId>> m_destroyedAttachedNodeIdentifiers;
275276
Timer m_destroyedNodesTimer;
277+
Timer m_focusNodeTimer;
276278

277279
using SearchResults = HashMap<String, Vector<RefPtr<Node>>>;
278280
SearchResults m_searchResults;

0 commit comments

Comments
 (0)