2015-03-02 16:20:00 +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 "mozilla/dom/cache/PrincipalVerifier.h"
|
|
|
|
|
|
|
|
#include "mozilla/dom/ContentParent.h"
|
|
|
|
#include "mozilla/dom/cache/ManagerId.h"
|
|
|
|
#include "mozilla/ipc/BackgroundParent.h"
|
|
|
|
#include "mozilla/ipc/PBackgroundParent.h"
|
|
|
|
#include "mozilla/ipc/BackgroundUtils.h"
|
2015-10-26 19:14:47 +03:00
|
|
|
#include "nsContentUtils.h"
|
2015-03-02 16:20:00 +03:00
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
namespace cache {
|
|
|
|
|
|
|
|
using mozilla::ipc::AssertIsOnBackgroundThread;
|
|
|
|
using mozilla::ipc::BackgroundParent;
|
|
|
|
using mozilla::ipc::PBackgroundParent;
|
|
|
|
using mozilla::ipc::PrincipalInfo;
|
|
|
|
using mozilla::ipc::PrincipalInfoToPrincipal;
|
|
|
|
|
|
|
|
// static
|
|
|
|
already_AddRefed<PrincipalVerifier> PrincipalVerifier::CreateAndDispatch(
|
|
|
|
Listener* aListener, PBackgroundParent* aActor,
|
|
|
|
const PrincipalInfo& aPrincipalInfo) {
|
|
|
|
// We must get the ContentParent actor from the PBackgroundParent. This
|
|
|
|
// only works on the PBackground thread.
|
|
|
|
AssertIsOnBackgroundThread();
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<PrincipalVerifier> verifier =
|
|
|
|
new PrincipalVerifier(aListener, aActor, aPrincipalInfo);
|
2015-03-02 16:20:00 +03:00
|
|
|
|
2016-03-28 20:28:15 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(NS_DispatchToMainThread(verifier));
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
return verifier.forget();
|
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
void PrincipalVerifier::AddListener(Listener* aListener) {
|
2015-03-02 16:20:00 +03:00
|
|
|
AssertIsOnBackgroundThread();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
2015-04-16 22:00:15 +03:00
|
|
|
MOZ_ASSERT(!mListenerList.Contains(aListener));
|
|
|
|
mListenerList.AppendElement(aListener);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrincipalVerifier::RemoveListener(Listener* aListener) {
|
|
|
|
AssertIsOnBackgroundThread();
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
2015-04-16 22:00:15 +03:00
|
|
|
MOZ_ALWAYS_TRUE(mListenerList.RemoveElement(aListener));
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PrincipalVerifier::PrincipalVerifier(Listener* aListener,
|
|
|
|
PBackgroundParent* aActor,
|
|
|
|
const PrincipalInfo& aPrincipalInfo)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("dom::cache::PrincipalVerifier"),
|
|
|
|
mActor(BackgroundParent::GetContentParent(aActor)),
|
2015-03-02 16:20:00 +03:00
|
|
|
mPrincipalInfo(aPrincipalInfo),
|
2017-06-01 23:42:05 +03:00
|
|
|
mInitiatingEventTarget(GetCurrentThreadSerialEventTarget()),
|
2015-03-02 16:20:00 +03:00
|
|
|
mResult(NS_OK) {
|
|
|
|
AssertIsOnBackgroundThread();
|
2017-06-01 23:42:05 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mInitiatingEventTarget);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aListener);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
mListenerList.AppendElement(aListener);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
PrincipalVerifier::~PrincipalVerifier() {
|
|
|
|
// Since the PrincipalVerifier is a Runnable that executes on multiple
|
|
|
|
// threads, its a race to see which thread de-refs us last. Therefore
|
|
|
|
// we cannot guarantee which thread we destruct on.
|
|
|
|
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mListenerList.IsEmpty());
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
// We should always be able to explicitly release the actor on the main
|
|
|
|
// thread.
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mActor);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
PrincipalVerifier::Run() {
|
|
|
|
// Executed twice. First, on the main thread and then back on the
|
|
|
|
// originating thread.
|
|
|
|
|
|
|
|
if (NS_IsMainThread()) {
|
|
|
|
VerifyOnMainThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
CompleteOnInitiatingThread();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrincipalVerifier::VerifyOnMainThread() {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
// No matter what happens, we need to release the actor before leaving
|
|
|
|
// this method.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ContentParent> actor;
|
2015-03-02 16:20:00 +03:00
|
|
|
actor.swap(mActor);
|
|
|
|
|
|
|
|
nsresult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsIPrincipal> principal =
|
|
|
|
PrincipalInfoToPrincipal(mPrincipalInfo, &rv);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DispatchToInitiatingThread(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-06-02 12:05:28 +03:00
|
|
|
// We disallow null principal on the client side, but double-check here.
|
|
|
|
if (NS_WARN_IF(principal->GetIsNullPrincipal())) {
|
2015-03-02 16:20:00 +03:00
|
|
|
DispatchToInitiatingThread(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-23 12:42:02 +03:00
|
|
|
// Verify if a child process uses system principal, which is not allowed
|
|
|
|
// to prevent system principal is spoofed.
|
2019-03-24 23:36:25 +03:00
|
|
|
if (NS_WARN_IF(actor && principal->IsSystemPrincipal())) {
|
2015-03-02 16:20:00 +03:00
|
|
|
DispatchToInitiatingThread(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-07-23 12:42:02 +03:00
|
|
|
actor = nullptr;
|
2015-03-02 16:20:00 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Sanity check principal origin by using it to construct a URI and security
|
|
|
|
// checking it. Don't do this for the system principal, though, as its origin
|
|
|
|
// is a synthetic [System Principal] string.
|
2019-03-24 23:36:25 +03:00
|
|
|
if (!principal->IsSystemPrincipal()) {
|
2015-03-02 16:20:00 +03:00
|
|
|
nsAutoCString origin;
|
2015-05-15 02:55:52 +03:00
|
|
|
rv = principal->GetOriginNoSuffix(origin);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DispatchToInitiatingThread(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uri), origin);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DispatchToInitiatingThread(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
rv = principal->CheckMayLoad(uri, false, false);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DispatchToInitiatingThread(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
rv = ManagerId::Create(principal, getter_AddRefs(mManagerId));
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
DispatchToInitiatingThread(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
DispatchToInitiatingThread(NS_OK);
|
|
|
|
}
|
|
|
|
|
|
|
|
void PrincipalVerifier::CompleteOnInitiatingThread() {
|
|
|
|
AssertIsOnBackgroundThread();
|
2015-04-16 22:00:15 +03:00
|
|
|
ListenerList::ForwardIterator iter(mListenerList);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
iter.GetNext()->OnPrincipalVerified(mResult, mManagerId);
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// The listener must clear its reference in OnPrincipalVerified()
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mListenerList.IsEmpty());
|
2015-03-02 16:20:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void PrincipalVerifier::DispatchToInitiatingThread(nsresult aRv) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
|
|
|
mResult = aRv;
|
|
|
|
|
|
|
|
// The Cache ShutdownObserver does not track all principal verifiers, so we
|
|
|
|
// cannot ensure this always succeeds. Instead, simply warn on failures.
|
|
|
|
// This will result in a new CacheStorage object delaying operations until
|
|
|
|
// shutdown completes and the browser goes away. This is as graceful as
|
|
|
|
// we can get here.
|
2017-06-01 23:42:05 +03:00
|
|
|
nsresult rv =
|
|
|
|
mInitiatingEventTarget->Dispatch(this, nsIThread::DISPATCH_NORMAL);
|
2015-03-02 16:20:00 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
NS_WARNING(
|
|
|
|
"Cache unable to complete principal verification due to shutdown.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace cache
|
2015-03-02 16:20:00 +03:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|