2015-04-16 22:00:15 +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/CacheOpParent.h"
|
|
|
|
|
2020-06-19 16:23:33 +03:00
|
|
|
#include "mozilla/StaticPrefs_browser.h"
|
2016-08-23 07:09:32 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/AutoUtils.h"
|
2020-04-15 02:51:44 +03:00
|
|
|
#include "mozilla/dom/cache/ManagerId.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
#include "mozilla/dom/cache/ReadStream.h"
|
|
|
|
#include "mozilla/dom/cache/SavedTypes.h"
|
|
|
|
#include "mozilla/ipc/FileDescriptorSetParent.h"
|
|
|
|
#include "mozilla/ipc/InputStreamUtils.h"
|
2017-10-26 14:35:08 +03:00
|
|
|
#include "mozilla/ipc/IPCStreamUtils.h"
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
namespace mozilla::dom::cache {
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
using mozilla::ipc::FileDescriptorSetParent;
|
|
|
|
using mozilla::ipc::PBackgroundParent;
|
|
|
|
|
|
|
|
CacheOpParent::CacheOpParent(PBackgroundParent* aIpcManager, CacheId aCacheId,
|
|
|
|
const CacheOpArgs& aOpArgs)
|
|
|
|
: mIpcManager(aIpcManager),
|
|
|
|
mCacheId(aCacheId),
|
|
|
|
mNamespace(INVALID_NAMESPACE),
|
|
|
|
mOpArgs(aOpArgs) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mIpcManager);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheOpParent::CacheOpParent(PBackgroundParent* aIpcManager,
|
|
|
|
Namespace aNamespace, const CacheOpArgs& aOpArgs)
|
|
|
|
: mIpcManager(aIpcManager),
|
|
|
|
mCacheId(INVALID_CACHE_ID),
|
|
|
|
mNamespace(aNamespace),
|
|
|
|
mOpArgs(aOpArgs) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mIpcManager);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
CacheOpParent::~CacheOpParent() { NS_ASSERT_OWNINGTHREAD(CacheOpParent); }
|
|
|
|
|
2020-05-11 15:13:07 +03:00
|
|
|
void CacheOpParent::Execute(const SafeRefPtr<ManagerId>& aManagerId) {
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpParent);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mManager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVerifier);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2020-05-11 15:09:02 +03:00
|
|
|
auto managerOrErr = cache::Manager::AcquireCreateIfNonExistent(aManagerId);
|
|
|
|
if (NS_WARN_IF(managerOrErr.isErr())) {
|
|
|
|
ErrorResult result(managerOrErr.unwrapErr());
|
2020-02-05 19:15:07 +03:00
|
|
|
Unused << Send__delete__(this, std::move(result), void_t());
|
2015-04-16 22:00:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:09:02 +03:00
|
|
|
Execute(managerOrErr.unwrap());
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:09:02 +03:00
|
|
|
void CacheOpParent::Execute(SafeRefPtr<cache::Manager> aManager) {
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpParent);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mManager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVerifier);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2020-05-11 15:09:02 +03:00
|
|
|
mManager = std::move(aManager);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
// Handle put op
|
|
|
|
if (mOpArgs.type() == CacheOpArgs::TCachePutAllArgs) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mCacheId != INVALID_CACHE_ID);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
const CachePutAllArgs& args = mOpArgs.get_CachePutAllArgs();
|
|
|
|
const nsTArray<CacheRequestResponse>& list = args.requestResponseList();
|
|
|
|
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsIInputStream>, 256> requestStreamList;
|
|
|
|
AutoTArray<nsCOMPtr<nsIInputStream>, 256> responseStreamList;
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
for (uint32_t i = 0; i < list.Length(); ++i) {
|
|
|
|
requestStreamList.AppendElement(
|
|
|
|
DeserializeCacheStream(list[i].request().body()));
|
|
|
|
responseStreamList.AppendElement(
|
|
|
|
DeserializeCacheStream(list[i].response().body()));
|
|
|
|
}
|
|
|
|
|
|
|
|
mManager->ExecutePutAll(this, mCacheId, args.requestResponseList(),
|
|
|
|
requestStreamList, responseStreamList);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle all other cache ops
|
|
|
|
if (mCacheId != INVALID_CACHE_ID) {
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mNamespace == INVALID_NAMESPACE);
|
2015-04-16 22:00:15 +03:00
|
|
|
mManager->ExecuteCacheOp(this, mCacheId, mOpArgs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle all storage ops
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mNamespace != INVALID_NAMESPACE);
|
2015-04-16 22:00:15 +03:00
|
|
|
mManager->ExecuteStorageOp(this, mNamespace, mOpArgs);
|
|
|
|
}
|
|
|
|
|
|
|
|
void CacheOpParent::WaitForVerification(PrincipalVerifier* aVerifier) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpParent);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mManager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mVerifier);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
mVerifier = aVerifier;
|
2021-01-21 14:58:37 +03:00
|
|
|
mVerifier->AddListener(*this);
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void CacheOpParent::ActorDestroy(ActorDestroyReason aReason) {
|
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpParent);
|
|
|
|
|
|
|
|
if (mVerifier) {
|
2021-01-21 14:58:37 +03:00
|
|
|
mVerifier->RemoveListener(*this);
|
2015-04-16 22:00:15 +03:00
|
|
|
mVerifier = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mManager) {
|
|
|
|
mManager->RemoveListener(this);
|
|
|
|
mManager = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
mIpcManager = nullptr;
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:13:07 +03:00
|
|
|
void CacheOpParent::OnPrincipalVerified(
|
|
|
|
nsresult aRv, const SafeRefPtr<ManagerId>& aManagerId) {
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpParent);
|
|
|
|
|
2021-01-21 14:58:37 +03:00
|
|
|
mVerifier->RemoveListener(*this);
|
2015-04-16 22:00:15 +03:00
|
|
|
mVerifier = nullptr;
|
|
|
|
|
|
|
|
if (NS_WARN_IF(NS_FAILED(aRv))) {
|
2016-07-01 07:17:00 +03:00
|
|
|
ErrorResult result(aRv);
|
2020-02-05 19:15:07 +03:00
|
|
|
Unused << Send__delete__(this, std::move(result), void_t());
|
2015-04-16 22:00:15 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
Execute(aManagerId);
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:11:33 +03:00
|
|
|
void CacheOpParent::OnOpComplete(ErrorResult&& aRv,
|
|
|
|
const CacheOpResult& aResult,
|
|
|
|
CacheId aOpenedCacheId,
|
|
|
|
const Maybe<StreamInfo>& aStreamInfo) {
|
2015-04-16 22:00:15 +03:00
|
|
|
NS_ASSERT_OWNINGTHREAD(CacheOpParent);
|
2017-01-06 23:41:15 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mIpcManager);
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(mManager);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2015-04-16 22:00:16 +03:00
|
|
|
// Never send an op-specific result if we have an error. Instead, send
|
|
|
|
// void_t() to ensure that we don't leak actors on the child side.
|
2015-05-01 18:15:52 +03:00
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
2020-02-05 19:15:07 +03:00
|
|
|
Unused << Send__delete__(this, std::move(aRv), void_t());
|
2015-04-16 22:00:16 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-23 04:44:58 +03:00
|
|
|
if (aStreamInfo.isSome()) {
|
|
|
|
ProcessCrossOriginResourcePolicyHeader(aRv,
|
|
|
|
aStreamInfo->mSavedResponseList);
|
|
|
|
if (NS_WARN_IF(aRv.Failed())) {
|
|
|
|
Unused << Send__delete__(this, std::move(aRv), void_t());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-11 15:11:33 +03:00
|
|
|
uint32_t entryCount =
|
|
|
|
std::max(1lu, aStreamInfo ? static_cast<unsigned long>(std::max(
|
|
|
|
aStreamInfo->mSavedResponseList.Length(),
|
|
|
|
aStreamInfo->mSavedRequestList.Length()))
|
|
|
|
: 0lu);
|
2016-05-15 20:32:09 +03:00
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
// The result must contain the appropriate type at this point. It may
|
|
|
|
// or may not contain the additional result data yet. For types that
|
|
|
|
// do not need special processing, it should already be set. If the
|
|
|
|
// result requires actor-specific operations, then we do that below.
|
|
|
|
// If the type and data types don't match, then we will trigger an
|
|
|
|
// assertion in AutoParentOpResult::Add().
|
2016-05-15 20:32:09 +03:00
|
|
|
AutoParentOpResult result(mIpcManager, aResult, entryCount);
|
2015-04-16 22:00:15 +03:00
|
|
|
|
|
|
|
if (aOpenedCacheId != INVALID_CACHE_ID) {
|
2020-05-11 15:09:02 +03:00
|
|
|
result.Add(aOpenedCacheId, mManager.clonePtr());
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2020-05-11 15:11:33 +03:00
|
|
|
if (aStreamInfo) {
|
|
|
|
const auto& streamInfo = *aStreamInfo;
|
|
|
|
|
|
|
|
for (const auto& savedResponse : streamInfo.mSavedResponseList) {
|
|
|
|
result.Add(savedResponse, streamInfo.mStreamList);
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2020-05-11 15:11:33 +03:00
|
|
|
for (const auto& savedRequest : streamInfo.mSavedRequestList) {
|
|
|
|
result.Add(savedRequest, streamInfo.mStreamList);
|
|
|
|
}
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2020-02-05 19:15:07 +03:00
|
|
|
Unused << Send__delete__(this, std::move(aRv), result.SendAsOpResult());
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2015-04-16 22:00:15 +03:00
|
|
|
already_AddRefed<nsIInputStream> CacheOpParent::DeserializeCacheStream(
|
2019-03-21 14:09:44 +03:00
|
|
|
const Maybe<CacheReadStream>& aMaybeStream) {
|
|
|
|
if (aMaybeStream.isNothing()) {
|
2015-04-16 22:00:15 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2019-03-21 14:09:44 +03:00
|
|
|
const CacheReadStream& readStream = aMaybeStream.ref();
|
2015-04-16 22:00:15 +03:00
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
// Option 1: One of our own ReadStreams was passed back to us with a stream
|
2015-04-16 22:00:15 +03:00
|
|
|
// control actor.
|
|
|
|
stream = ReadStream::Create(readStream);
|
|
|
|
if (stream) {
|
|
|
|
return stream.forget();
|
|
|
|
}
|
|
|
|
|
2016-05-15 20:32:09 +03:00
|
|
|
// Option 2: A stream was serialized using normal methods or passed
|
2017-03-14 14:28:58 +03:00
|
|
|
// as a PChildToParentStream actor. Use the standard method for
|
2016-05-15 20:32:09 +03:00
|
|
|
// extracting the resulting stream.
|
|
|
|
return DeserializeIPCStream(readStream.stream());
|
2015-04-16 22:00:15 +03:00
|
|
|
}
|
|
|
|
|
2020-05-23 04:44:58 +03:00
|
|
|
void CacheOpParent::ProcessCrossOriginResourcePolicyHeader(
|
|
|
|
ErrorResult& aRv, const nsTArray<SavedResponse>& aResponses) {
|
2020-06-19 16:23:33 +03:00
|
|
|
if (!StaticPrefs::browser_tabs_remote_useCrossOriginEmbedderPolicy()) {
|
|
|
|
return;
|
|
|
|
}
|
2020-05-23 04:44:58 +03:00
|
|
|
// Only checking for match/matchAll.
|
|
|
|
nsILoadInfo::CrossOriginEmbedderPolicy loadingCOEP =
|
|
|
|
nsILoadInfo::EMBEDDER_POLICY_NULL;
|
|
|
|
Maybe<PrincipalInfo> principalInfo;
|
|
|
|
switch (mOpArgs.type()) {
|
|
|
|
case CacheOpArgs::TCacheMatchArgs: {
|
|
|
|
loadingCOEP =
|
|
|
|
mOpArgs.get_CacheMatchArgs().request().loadingEmbedderPolicy();
|
|
|
|
principalInfo = mOpArgs.get_CacheMatchArgs().request().principalInfo();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CacheOpArgs::TCacheMatchAllArgs: {
|
|
|
|
if (mOpArgs.get_CacheMatchAllArgs().maybeRequest().isSome()) {
|
|
|
|
loadingCOEP = mOpArgs.get_CacheMatchAllArgs()
|
|
|
|
.maybeRequest()
|
|
|
|
.ref()
|
|
|
|
.loadingEmbedderPolicy();
|
|
|
|
principalInfo = mOpArgs.get_CacheMatchAllArgs()
|
|
|
|
.maybeRequest()
|
|
|
|
.ref()
|
|
|
|
.principalInfo();
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default: {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// skip checking if the request has no principal for same-origin/same-site
|
|
|
|
// checking.
|
|
|
|
if (principalInfo.isNothing() ||
|
|
|
|
principalInfo.ref().type() != PrincipalInfo::TContentPrincipalInfo) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const ContentPrincipalInfo& contentPrincipalInfo =
|
|
|
|
principalInfo.ref().get_ContentPrincipalInfo();
|
|
|
|
|
|
|
|
for (auto it = aResponses.cbegin(); it != aResponses.cend(); ++it) {
|
2020-06-18 13:02:53 +03:00
|
|
|
if (it->mValue.type() != ResponseType::Opaque &&
|
|
|
|
it->mValue.type() != ResponseType::Opaqueredirect) {
|
|
|
|
continue;
|
|
|
|
}
|
2020-12-16 22:10:57 +03:00
|
|
|
|
|
|
|
const auto& headers = it->mValue.headers();
|
|
|
|
const auto corpHeaderIt =
|
|
|
|
std::find_if(headers.cbegin(), headers.cend(), [](const auto& header) {
|
|
|
|
return header.name().EqualsLiteral("Cross-Origin-Resource-Policy");
|
|
|
|
});
|
2020-05-23 04:44:58 +03:00
|
|
|
|
|
|
|
// According to https://github.com/w3c/ServiceWorker/issues/1490, the cache
|
|
|
|
// response is expected with CORP header, otherwise, throw the type error.
|
|
|
|
// Note that this is different with the CORP checking for fetch metioned in
|
|
|
|
// https://wicg.github.io/cross-origin-embedder-policy/#corp-check.
|
|
|
|
// For fetch, if the response has no CORP header, "same-origin" checking
|
|
|
|
// will be performed.
|
2020-12-16 22:10:57 +03:00
|
|
|
if (corpHeaderIt == headers.cend() &&
|
2020-05-23 04:44:58 +03:00
|
|
|
loadingCOEP == nsILoadInfo::EMBEDDER_POLICY_REQUIRE_CORP) {
|
|
|
|
aRv.ThrowTypeError("Response is expected with CORP header.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Skip the case if the response has no principal for same-origin/same-site
|
|
|
|
// checking.
|
|
|
|
if (it->mValue.principalInfo().isNothing() ||
|
|
|
|
it->mValue.principalInfo().ref().type() !=
|
|
|
|
PrincipalInfo::TContentPrincipalInfo) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ContentPrincipalInfo& responseContentPrincipalInfo =
|
|
|
|
it->mValue.principalInfo().ref().get_ContentPrincipalInfo();
|
|
|
|
|
2020-12-16 22:10:57 +03:00
|
|
|
const auto& corp =
|
|
|
|
corpHeaderIt == headers.cend() ? EmptyCString() : corpHeaderIt->value();
|
|
|
|
|
2020-05-23 04:44:58 +03:00
|
|
|
if (corp.EqualsLiteral("same-origin")) {
|
|
|
|
if (responseContentPrincipalInfo == contentPrincipalInfo) {
|
|
|
|
aRv.ThrowTypeError("Response is expected from same origin.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else if (corp.EqualsLiteral("same-site")) {
|
|
|
|
if (!responseContentPrincipalInfo.baseDomain().Equals(
|
|
|
|
contentPrincipalInfo.baseDomain())) {
|
|
|
|
aRv.ThrowTypeError("Response is expected from same site.");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-04 20:04:01 +03:00
|
|
|
} // namespace mozilla::dom::cache
|