2015-06-17 13:44:27 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "PostMessageEvent.h"
|
|
|
|
|
|
|
|
#include "MessageEvent.h"
|
|
|
|
#include "mozilla/dom/BlobBinding.h"
|
2015-09-29 07:31:56 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2015-07-22 19:03:07 +03:00
|
|
|
#include "mozilla/dom/FileList.h"
|
2015-07-22 19:04:22 +03:00
|
|
|
#include "mozilla/dom/FileListBinding.h"
|
2016-01-30 20:05:36 +03:00
|
|
|
#include "mozilla/dom/MessageEventBinding.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "mozilla/dom/MessagePort.h"
|
|
|
|
#include "mozilla/dom/MessagePortBinding.h"
|
|
|
|
#include "mozilla/dom/PMessagePort.h"
|
|
|
|
#include "mozilla/dom/StructuredCloneTags.h"
|
2016-01-30 20:05:36 +03:00
|
|
|
#include "mozilla/dom/UnionConversions.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "mozilla/EventDispatcher.h"
|
2016-04-11 19:02:35 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
#include "nsGlobalWindow.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsIPrincipal.h"
|
2016-04-11 19:02:35 +03:00
|
|
|
#include "nsIScriptError.h"
|
2015-07-01 16:56:00 +03:00
|
|
|
#include "nsPresContext.h"
|
2016-04-11 19:02:35 +03:00
|
|
|
#include "nsQueryObject.h"
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
|
|
|
PostMessageEvent::PostMessageEvent(nsGlobalWindow* aSource,
|
|
|
|
const nsAString& aCallerOrigin,
|
|
|
|
nsGlobalWindow* aTargetWindow,
|
|
|
|
nsIPrincipal* aProvidedPrincipal,
|
2016-01-30 00:32:01 +03:00
|
|
|
nsIDocument* aSourceDocument,
|
2015-06-17 13:44:27 +03:00
|
|
|
bool aTrustedCaller)
|
2015-09-30 15:22:08 +03:00
|
|
|
: StructuredCloneHolder(CloningSupported, TransferringSupported,
|
2016-07-21 16:29:42 +03:00
|
|
|
StructuredCloneScope::SameProcessSameThread),
|
2015-07-28 10:38:16 +03:00
|
|
|
mSource(aSource),
|
2015-06-17 13:44:27 +03:00
|
|
|
mCallerOrigin(aCallerOrigin),
|
|
|
|
mTargetWindow(aTargetWindow),
|
|
|
|
mProvidedPrincipal(aProvidedPrincipal),
|
2016-01-30 00:32:01 +03:00
|
|
|
mSourceDocument(aSourceDocument),
|
2015-06-17 13:44:27 +03:00
|
|
|
mTrustedCaller(aTrustedCaller)
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(PostMessageEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
PostMessageEvent::~PostMessageEvent()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(PostMessageEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PostMessageEvent::Run()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mTargetWindow->IsOuterWindow(),
|
|
|
|
"should have been passed an outer window!");
|
|
|
|
MOZ_ASSERT(!mSource || mSource->IsOuterWindow(),
|
|
|
|
"should have been passed an outer window!");
|
|
|
|
|
2016-08-02 23:28:17 +03:00
|
|
|
// Note: We don't init this AutoJSAPI with targetWindow, because we do not
|
|
|
|
// want exceptions during message deserialization to trigger error events on
|
|
|
|
// targetWindow.
|
2015-06-17 13:44:27 +03:00
|
|
|
AutoJSAPI jsapi;
|
|
|
|
jsapi.Init();
|
|
|
|
JSContext* cx = jsapi.cx();
|
|
|
|
|
2016-01-30 00:32:01 +03:00
|
|
|
// The document is just used for the principal mismatch error message below.
|
|
|
|
// Use a stack variable so mSourceDocument is not held onto after this method
|
|
|
|
// finishes, regardless of the method outcome.
|
|
|
|
nsCOMPtr<nsIDocument> sourceDocument;
|
|
|
|
sourceDocument.swap(mSourceDocument);
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
// If we bailed before this point we're going to leak mMessage, but
|
|
|
|
// that's probably better than crashing.
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsGlobalWindow> targetWindow;
|
2015-06-17 13:44:27 +03:00
|
|
|
if (mTargetWindow->IsClosedOrClosing() ||
|
|
|
|
!(targetWindow = mTargetWindow->GetCurrentInnerWindowInternal()) ||
|
|
|
|
targetWindow->IsClosedOrClosing())
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
MOZ_ASSERT(targetWindow->IsInnerWindow(),
|
|
|
|
"we ordered an inner window!");
|
2016-08-02 23:21:16 +03:00
|
|
|
JSAutoCompartment ac(cx, targetWindow->GetWrapper());
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
// Ensure that any origin which might have been provided is the origin of this
|
|
|
|
// window's document. Note that we do this *now* instead of when postMessage
|
|
|
|
// is called because the target window might have been navigated to a
|
|
|
|
// different location between then and now. If this check happened when
|
|
|
|
// postMessage was called, it would be fairly easy for a malicious webpage to
|
|
|
|
// intercept messages intended for another site by carefully timing navigation
|
|
|
|
// of the target window so it changed location after postMessage but before
|
|
|
|
// now.
|
|
|
|
if (mProvidedPrincipal) {
|
|
|
|
// Get the target's origin either from its principal or, in the case the
|
|
|
|
// principal doesn't carry a URI (e.g. the system principal), the target's
|
|
|
|
// document.
|
|
|
|
nsIPrincipal* targetPrin = targetWindow->GetPrincipal();
|
|
|
|
if (NS_WARN_IF(!targetPrin))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Note: This is contrary to the spec with respect to file: URLs, which
|
|
|
|
// the spec groups into a single origin, but given we intentionally
|
|
|
|
// don't do that in other places it seems better to hold the line for
|
|
|
|
// now. Long-term, we want HTML5 to address this so that we can
|
|
|
|
// be compliant while being safer.
|
|
|
|
if (!targetPrin->Equals(mProvidedPrincipal)) {
|
2016-01-30 00:32:01 +03:00
|
|
|
nsAutoString providedOrigin, targetOrigin;
|
|
|
|
nsresult rv = nsContentUtils::GetUTFOrigin(targetPrin, targetOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = nsContentUtils::GetUTFOrigin(mProvidedPrincipal, providedOrigin);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
const char16_t* params[] = { providedOrigin.get(), targetOrigin.get() };
|
|
|
|
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::errorFlag,
|
|
|
|
NS_LITERAL_CSTRING("DOM Window"), sourceDocument,
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"TargetPrincipalDoesNotMatch",
|
|
|
|
params, ArrayLength(params));
|
|
|
|
|
2015-06-17 13:44:27 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-31 03:38:00 +03:00
|
|
|
ErrorResult rv;
|
2015-06-17 13:44:27 +03:00
|
|
|
JS::Rooted<JS::Value> messageData(cx);
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = targetWindow->AsInner();
|
2015-07-31 03:38:00 +03:00
|
|
|
|
|
|
|
Read(window, cx, &messageData, rv);
|
|
|
|
if (NS_WARN_IF(rv.Failed())) {
|
|
|
|
return rv.StealNSResult();
|
2015-06-17 13:44:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create the event
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> eventTarget = do_QueryObject(targetWindow);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<MessageEvent> event =
|
2015-06-17 13:44:27 +03:00
|
|
|
new MessageEvent(eventTarget, nullptr, nullptr);
|
|
|
|
|
2016-01-30 20:05:36 +03:00
|
|
|
|
|
|
|
Nullable<WindowProxyOrMessagePort> source;
|
|
|
|
source.SetValue().SetAsWindowProxy() = mSource ? mSource->AsOuter() : nullptr;
|
|
|
|
|
|
|
|
event->InitMessageEvent(nullptr, NS_LITERAL_STRING("message"),
|
|
|
|
false /*non-bubbling */, false /*cancelable */,
|
|
|
|
messageData, mCallerOrigin,
|
|
|
|
EmptyString(), source, nullptr);
|
2015-06-17 13:44:27 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<MessagePort>> ports = TakeTransferredPorts();
|
2016-10-13 16:19:24 +03:00
|
|
|
event->SetPorts(Move(ports));
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
// We can't simply call dispatchEvent on the window because doing so ends
|
|
|
|
// up flipping the trusted bit on the event, and we don't want that to
|
|
|
|
// happen because then untrusted content can call postMessage on a chrome
|
|
|
|
// window if it can get a reference to it.
|
|
|
|
|
|
|
|
nsIPresShell *shell = targetWindow->GetExtantDoc()->GetShell();
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext;
|
2015-06-17 13:44:27 +03:00
|
|
|
if (shell)
|
|
|
|
presContext = shell->GetPresContext();
|
|
|
|
|
|
|
|
event->SetTrusted(mTrustedCaller);
|
2016-02-12 18:40:07 +03:00
|
|
|
WidgetEvent* internalEvent = event->WidgetEventPtr();
|
2015-06-17 13:44:27 +03:00
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2016-01-30 20:05:36 +03:00
|
|
|
EventDispatcher::Dispatch(window,
|
2015-06-17 13:44:27 +03:00
|
|
|
presContext,
|
|
|
|
internalEvent,
|
|
|
|
static_cast<dom::Event*>(event.get()),
|
|
|
|
&status);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|