Skip to content

Commit

Permalink
postMessage from RemoteFrame should have correct event.source
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=265543
rdar://118946789

Reviewed by Pascoe.

event.source should be the DOMWindow of the frame that sent the message,
even if it is a RemoteDOMWindow.  It shouldn't be the DOMWindow of the
frame that is receiving the message.

* LayoutTests/http/tests/site-isolation/post-message-expected.txt:
* LayoutTests/http/tests/site-isolation/post-message.html:
* Source/WebCore/page/LocalDOMWindow.cpp:
(WebCore::LocalDOMWindow::postMessageFromRemoteFrame):
* Source/WebCore/page/LocalDOMWindow.h:
* Source/WebCore/page/RemoteDOMWindow.cpp:
(WebCore::RemoteDOMWindow::postMessage):
* Source/WebCore/page/RemoteFrameClient.h:
* Source/WebKit/UIProcess/WebProcessProxy.cpp:
(WebKit::WebProcessProxy::postMessageToRemote):
* Source/WebKit/UIProcess/WebProcessProxy.h:
* Source/WebKit/UIProcess/WebProcessProxy.messages.in:
* Source/WebKit/WebProcess/WebCoreSupport/WebRemoteFrameClient.cpp:
(WebKit::WebRemoteFrameClient::postMessageToRemote):
* Source/WebKit/WebProcess/WebCoreSupport/WebRemoteFrameClient.h:
* Source/WebKit/WebProcess/WebProcess.cpp:
(WebKit::WebProcess::remotePostMessage):
* Source/WebKit/WebProcess/WebProcess.h:
* Source/WebKit/WebProcess/WebProcess.messages.in:

Canonical link: https://commits.webkit.org/271306@main
  • Loading branch information
achristensen07 committed Nov 29, 2023
1 parent dd412bf commit f789f59
Show file tree
Hide file tree
Showing 14 changed files with 37 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ PASS successfullyParsed is true
TEST COMPLETE
PASS received 'iframe received hello' from http://localhost:8000
PASS received 'iframe received world' from http://localhost:8000
PASS received 'iframe received sending to event source' from http://localhost:8000

3 changes: 2 additions & 1 deletion LayoutTests/http/tests/site-isolation/post-message.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
addEventListener("message", (event) => {
testPassed("received '" + event.data + "' from " + event.origin);
messageCount = messageCount + 1;
if (messageCount == 2) { testRunner.notifyDone() }
if (messageCount == 2) { event.source.postMessage("sending to event source", "*") }
if (messageCount == 3) { testRunner.notifyDone() }
});

onload = ()=>{
Expand Down
10 changes: 4 additions & 6 deletions Source/WebCore/page/LocalDOMWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1002,18 +1002,16 @@ ExceptionOr<void> LocalDOMWindow::postMessage(JSC::JSGlobalObject& lexicalGlobal
return { };
}

void LocalDOMWindow::postMessageFromRemoteFrame(JSC::JSGlobalObject& lexicalGlobalObject, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData> target, const WebCore::MessageWithMessagePorts& message)
void LocalDOMWindow::postMessageFromRemoteFrame(JSC::JSGlobalObject& lexicalGlobalObject, RefPtr<WindowProxy>&& source, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData>&& targetOriginData, const WebCore::MessageWithMessagePorts& message)
{
if (!frame())
return;

RefPtr incumbentWindowProxy = &frame()->windowProxy();

RefPtr<SecurityOrigin> targetOrigin;
if (target)
targetOrigin = target->securityOrigin();
if (targetOriginData)
targetOrigin = targetOriginData->securityOrigin();

processPostMessage(lexicalGlobalObject, sourceOrigin, message, WTFMove(incumbentWindowProxy), WTFMove(targetOrigin));
processPostMessage(lexicalGlobalObject, sourceOrigin, message, WTFMove(source), WTFMove(targetOrigin));
}

DOMSelection* LocalDOMWindow::getSelection()
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/page/LocalDOMWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ class LocalDOMWindow final
{
return postMessage(globalObject, incumbentWindow, message, WindowPostMessageOptions { WTFMove(targetOrigin), WTFMove(transfer) });
}
WEBCORE_EXPORT void postMessageFromRemoteFrame(JSC::JSGlobalObject&, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData> target, const WebCore::MessageWithMessagePorts&);
WEBCORE_EXPORT void postMessageFromRemoteFrame(JSC::JSGlobalObject&, RefPtr<WindowProxy>&& source, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData>&& targetOrigin, const WebCore::MessageWithMessagePorts&);

void languagesChanged();

Expand Down
6 changes: 5 additions & 1 deletion Source/WebCore/page/RemoteDOMWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,10 @@ ExceptionOr<void> RemoteDOMWindow::postMessage(JSC::JSGlobalObject& lexicalGloba
if (!sourceDocument)
return { };

RefPtr sourceFrame = incumbentWindow.frame();
if (!sourceFrame)
return { };

auto targetSecurityOrigin = createTargetOriginForPostMessage(options.targetOrigin, *sourceDocument);
if (targetSecurityOrigin.hasException())
return targetSecurityOrigin.releaseException();
Expand All @@ -159,7 +163,7 @@ ExceptionOr<void> RemoteDOMWindow::postMessage(JSC::JSGlobalObject& lexicalGloba

MessageWithMessagePorts messageWithPorts { messageData.releaseReturnValue(), disentangledPorts.releaseReturnValue() };
if (auto* remoteFrame = frame())
remoteFrame->client().postMessageToRemote(remoteFrame->frameID(), sourceOrigin, target, messageWithPorts);
remoteFrame->client().postMessageToRemote(sourceFrame->frameID(), sourceOrigin, remoteFrame->frameID(), target, messageWithPorts);
return { };
}

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/page/RemoteFrameClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class RemoteFrameClient {
public:
virtual void frameDetached() = 0;
virtual void sizeDidChange(IntSize) = 0;
virtual void postMessageToRemote(FrameIdentifier, const String& sourceOrigin, std::optional<SecurityOriginData>, const MessageWithMessagePorts&) = 0;
virtual void postMessageToRemote(FrameIdentifier source, const String& sourceOrigin, FrameIdentifier target, std::optional<SecurityOriginData> targetOrigin, const MessageWithMessagePorts&) = 0;
virtual void changeLocation(FrameLoadRequest&&) = 0;
virtual String renderTreeAsText(size_t baseIndent, OptionSet<RenderAsTextFlag>) = 0;
virtual void broadcastFrameRemovalToOtherProcesses() = 0;
Expand Down
6 changes: 3 additions & 3 deletions Source/WebKit/UIProcess/WebProcessProxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1386,10 +1386,10 @@ void WebProcessProxy::didDestroyUserGestureToken(uint64_t identifier)
m_userInitiatedActionByAuthorizationTokenMap.remove(*removed->authorizationToken());
}

void WebProcessProxy::postMessageToRemote(WebCore::FrameIdentifier identifier, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData> target, const WebCore::MessageWithMessagePorts& message)
void WebProcessProxy::postMessageToRemote(WebCore::FrameIdentifier source, const String& sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData> targetOrigin, const WebCore::MessageWithMessagePorts& message)
{
if (RefPtr destinationFrame = WebFrameProxy::webFrame(identifier))
destinationFrame->protectedProcess()->send(Messages::WebProcess::RemotePostMessage(identifier, sourceOrigin, target, message), 0);
if (RefPtr targetFrame = WebFrameProxy::webFrame(target))
targetFrame->protectedProcess()->send(Messages::WebProcess::RemotePostMessage(source, sourceOrigin, target, targetOrigin, message), 0);
}

void WebProcessProxy::closeRemoteFrame(WebCore::FrameIdentifier frameID)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/UIProcess/WebProcessProxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -540,7 +540,7 @@ class WebProcessProxy : public AuxiliaryProcessProxy {
void updateBackForwardItem(const BackForwardListItemState&);
void didDestroyFrame(WebCore::FrameIdentifier, WebPageProxyIdentifier);
void didDestroyUserGestureToken(uint64_t);
void postMessageToRemote(WebCore::FrameIdentifier, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData>, const WebCore::MessageWithMessagePorts&);
void postMessageToRemote(WebCore::FrameIdentifier source, const String& sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData> targetOrigin, const WebCore::MessageWithMessagePorts&);
void closeRemoteFrame(WebCore::FrameIdentifier);
void focusRemoteFrame(WebCore::FrameIdentifier);
void renderTreeAsText(WebCore::FrameIdentifier, size_t baseIndent, OptionSet<WebCore::RenderAsTextFlag>, CompletionHandler<void(String&&)>&&);
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/UIProcess/WebProcessProxy.messages.in
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ messages -> WebProcessProxy LegacyReceiver {
SetAppBadge(std::optional<WebKit::WebPageProxyIdentifier> pageIdentifier, WebCore::SecurityOriginData origin, std::optional<uint64_t> badge)
SetClientBadge(WebKit::WebPageProxyIdentifier pageIdentifier, WebCore::SecurityOriginData origin, std::optional<uint64_t> badge)

PostMessageToRemote(WebCore::FrameIdentifier identifier, String sourceOrigin, std::optional<WebCore::SecurityOriginData> target, struct WebCore::MessageWithMessagePorts message)
PostMessageToRemote(WebCore::FrameIdentifier source, String sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData> targetOrigin, struct WebCore::MessageWithMessagePorts message)
CloseRemoteFrame(WebCore::FrameIdentifier identifier)
FocusRemoteFrame(WebCore::FrameIdentifier identifier)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,9 @@ void WebRemoteFrameClient::sizeDidChange(WebCore::IntSize size)
m_frame->updateRemoteFrameSize(size);
}

void WebRemoteFrameClient::postMessageToRemote(WebCore::FrameIdentifier identifier, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData> target, const WebCore::MessageWithMessagePorts& message)
void WebRemoteFrameClient::postMessageToRemote(WebCore::FrameIdentifier source, const String& sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData> targetOrigin, const WebCore::MessageWithMessagePorts& message)
{
WebProcess::singleton().send(Messages::WebProcessProxy::PostMessageToRemote(identifier, sourceOrigin, target, message), 0);
WebProcess::singleton().send(Messages::WebProcessProxy::PostMessageToRemote(source, sourceOrigin, target, targetOrigin, message), 0);
}

void WebRemoteFrameClient::changeLocation(WebCore::FrameLoadRequest&& request)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class WebRemoteFrameClient final : public WebCore::RemoteFrameClient, public Web
private:
void frameDetached() final;
void sizeDidChange(WebCore::IntSize) final;
void postMessageToRemote(WebCore::FrameIdentifier, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData>, const WebCore::MessageWithMessagePorts&) final;
void postMessageToRemote(WebCore::FrameIdentifier source, const String& sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData> targetOrigin, const WebCore::MessageWithMessagePorts&) final;
void changeLocation(WebCore::FrameLoadRequest&&) final;
String renderTreeAsText(size_t baseIndent, OptionSet<WebCore::RenderAsTextFlag>) final;
void broadcastFrameRemovalToOtherProcesses() final;
Expand Down
23 changes: 13 additions & 10 deletions Source/WebKit/WebProcess/WebProcess.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1364,29 +1364,32 @@ void WebProcess::setEnhancedAccessibility(bool flag)
WebCore::AXObjectCache::setEnhancedUserInterfaceAccessibility(flag);
}

void WebProcess::remotePostMessage(WebCore::FrameIdentifier identifier, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData> target, const WebCore::MessageWithMessagePorts& message)
void WebProcess::remotePostMessage(WebCore::FrameIdentifier source, const String& sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData>&& targetOrigin, const WebCore::MessageWithMessagePorts& message)
{
RefPtr webFrame = WebProcess::singleton().webFrame(identifier);
if (!webFrame)
RefPtr targetFrame = WebProcess::singleton().webFrame(target);
if (!targetFrame)
return;

if (!webFrame->coreLocalFrame())
if (!targetFrame->coreLocalFrame())
return;

RefPtr domWindow = webFrame->coreLocalFrame()->window();
if (!domWindow)
RefPtr targetWindow = targetFrame->coreLocalFrame()->window();
if (!targetWindow)
return;

RefPtr frame = domWindow->frame();
if (!frame)
RefPtr targetCoreFrame = targetWindow->frame();
if (!targetCoreFrame)
return;

auto& script = frame->script();
RefPtr sourceFrame = WebProcess::singleton().webFrame(source);
RefPtr sourceWindow = sourceFrame && sourceFrame->coreFrame() ? &sourceFrame->coreFrame()->windowProxy() : nullptr;

auto& script = targetCoreFrame->script();
auto globalObject = script.globalObject(WebCore::mainThreadNormalWorld());
if (!globalObject)
return;

domWindow->postMessageFromRemoteFrame(*globalObject, sourceOrigin, target, message);
targetWindow->postMessageFromRemoteFrame(*globalObject, WTFMove(sourceWindow), sourceOrigin, WTFMove(targetOrigin), message);
}

void WebProcess::renderTreeAsText(WebCore::FrameIdentifier frameIdentifier, size_t baseIndent, OptionSet<WebCore::RenderAsTextFlag> behavior, CompletionHandler<void(String&&)>&& completionHandler)
Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/WebProcess/WebProcess.h
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ class WebProcess : public AuxiliaryProcess
void platformSetCacheModel(CacheModel);

void setEnhancedAccessibility(bool);
void remotePostMessage(WebCore::FrameIdentifier, const String& sourceOrigin, std::optional<WebCore::SecurityOriginData>, const WebCore::MessageWithMessagePorts&);
void remotePostMessage(WebCore::FrameIdentifier source, const String& sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData>&& targetOrigin, const WebCore::MessageWithMessagePorts&);

void renderTreeAsText(WebCore::FrameIdentifier, size_t baseIndent, OptionSet<WebCore::RenderAsTextFlag>, CompletionHandler<void(String&&)>&&);

Expand Down
2 changes: 1 addition & 1 deletion Source/WebKit/WebProcess/WebProcess.messages.in
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ messages -> WebProcess LegacyReceiver NotRefCounted {

ReleaseMemory() -> ()

RemotePostMessage(WebCore::FrameIdentifier frameIdentifier, String sourceOrigin, std::optional<WebCore::SecurityOriginData> target, struct WebCore::MessageWithMessagePorts message)
RemotePostMessage(WebCore::FrameIdentifier source, String sourceOrigin, WebCore::FrameIdentifier target, std::optional<WebCore::SecurityOriginData> targetOrigin, struct WebCore::MessageWithMessagePorts message)

RenderTreeAsText(WebCore::FrameIdentifier frameIdentifier, size_t baseIndent, OptionSet<WebCore::RenderAsTextFlag> behavior) -> (String renderTreeDump) Synchronous

Expand Down

0 comments on commit f789f59

Please sign in to comment.