2015-05-20 20:21:09 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* 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 "ImageCacheKey.h"
|
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2015-05-20 20:21:09 +03:00
|
|
|
#include "mozilla/Move.h"
|
2019-02-22 17:25:04 +03:00
|
|
|
#include "mozilla/Unused.h"
|
2018-06-20 20:38:22 +03:00
|
|
|
#include "nsContentUtils.h"
|
2018-08-10 22:55:22 +03:00
|
|
|
#include "nsICookieService.h"
|
2017-07-24 06:51:20 +03:00
|
|
|
#include "nsLayoutUtils.h"
|
2015-05-20 20:21:09 +03:00
|
|
|
#include "nsString.h"
|
2018-07-13 13:02:19 +03:00
|
|
|
#include "mozilla/AntiTrackingCommon.h"
|
2019-05-02 15:27:07 +03:00
|
|
|
#include "mozilla/HashFunctions.h"
|
2019-05-27 17:06:49 +03:00
|
|
|
#include "mozilla/StorageAccess.h"
|
2018-06-02 16:51:42 +03:00
|
|
|
#include "mozilla/dom/BlobURLProtocolHandler.h"
|
2016-12-01 17:12:42 +03:00
|
|
|
#include "mozilla/dom/File.h"
|
2018-01-27 00:08:59 +03:00
|
|
|
#include "mozilla/dom/ServiceWorkerManager.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2019-05-02 15:27:07 +03:00
|
|
|
#include "nsHashKeys.h"
|
2015-10-27 21:12:46 +03:00
|
|
|
#include "nsPrintfCString.h"
|
2015-05-20 20:21:09 +03:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
using namespace dom;
|
|
|
|
|
|
|
|
namespace image {
|
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
static Maybe<uint64_t> BlobSerial(nsIURI* aURI) {
|
2015-05-21 04:49:53 +03:00
|
|
|
nsAutoCString spec;
|
|
|
|
aURI->GetSpec(spec);
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<BlobImpl> blob;
|
2015-05-21 04:49:53 +03:00
|
|
|
if (NS_SUCCEEDED(NS_GetBlobForBlobURISpec(spec, getter_AddRefs(blob))) &&
|
|
|
|
blob) {
|
|
|
|
return Some(blob->GetSerialNumber());
|
|
|
|
}
|
|
|
|
|
|
|
|
return Nothing();
|
|
|
|
}
|
|
|
|
|
2016-08-04 21:22:00 +03:00
|
|
|
ImageCacheKey::ImageCacheKey(nsIURI* aURI, const OriginAttributes& aAttrs,
|
2019-02-22 17:25:04 +03:00
|
|
|
Document* aDocument)
|
2018-06-06 03:42:56 +03:00
|
|
|
: mURI(aURI),
|
2016-08-04 21:22:00 +03:00
|
|
|
mOriginAttributes(aAttrs),
|
2018-06-20 20:38:22 +03:00
|
|
|
mControlledDocument(GetSpecialCaseDocumentToken(aDocument, aURI)),
|
2019-05-02 15:27:07 +03:00
|
|
|
mTopLevelBaseDomain(GetTopLevelBaseDomain(aDocument, aURI)),
|
2018-06-06 03:42:56 +03:00
|
|
|
mIsChrome(false) {
|
|
|
|
if (SchemeIs("blob")) {
|
2015-05-21 04:49:53 +03:00
|
|
|
mBlobSerial = BlobSerial(mURI);
|
2018-06-06 03:42:56 +03:00
|
|
|
} else if (SchemeIs("chrome")) {
|
|
|
|
mIsChrome = true;
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
|
2015-05-20 20:21:11 +03:00
|
|
|
: mURI(aOther.mURI),
|
2015-05-21 04:49:53 +03:00
|
|
|
mBlobSerial(aOther.mBlobSerial),
|
2018-06-06 03:42:56 +03:00
|
|
|
mBlobRef(aOther.mBlobRef),
|
2016-08-04 21:22:00 +03:00
|
|
|
mOriginAttributes(aOther.mOriginAttributes),
|
2015-10-27 21:12:46 +03:00
|
|
|
mControlledDocument(aOther.mControlledDocument),
|
2019-05-02 15:27:07 +03:00
|
|
|
mTopLevelBaseDomain(aOther.mTopLevelBaseDomain),
|
2015-05-20 20:21:09 +03:00
|
|
|
mHash(aOther.mHash),
|
|
|
|
mIsChrome(aOther.mIsChrome) {}
|
|
|
|
|
|
|
|
ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
|
2018-05-30 22:15:35 +03:00
|
|
|
: mURI(std::move(aOther.mURI)),
|
|
|
|
mBlobSerial(std::move(aOther.mBlobSerial)),
|
2018-06-06 03:42:56 +03:00
|
|
|
mBlobRef(std::move(aOther.mBlobRef)),
|
2016-08-04 21:22:00 +03:00
|
|
|
mOriginAttributes(aOther.mOriginAttributes),
|
2015-10-27 21:12:46 +03:00
|
|
|
mControlledDocument(aOther.mControlledDocument),
|
2019-05-02 15:27:07 +03:00
|
|
|
mTopLevelBaseDomain(aOther.mTopLevelBaseDomain),
|
2015-05-20 20:21:09 +03:00
|
|
|
mHash(aOther.mHash),
|
|
|
|
mIsChrome(aOther.mIsChrome) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-05-20 20:21:09 +03:00
|
|
|
bool ImageCacheKey::operator==(const ImageCacheKey& aOther) const {
|
2015-10-27 21:12:46 +03:00
|
|
|
// Don't share the image cache between a controlled document and anything
|
|
|
|
// else.
|
|
|
|
if (mControlledDocument != aOther.mControlledDocument) {
|
|
|
|
return false;
|
|
|
|
}
|
2019-05-02 15:27:07 +03:00
|
|
|
// Don't share the image cache between two top-level documents of different
|
|
|
|
// base domains.
|
|
|
|
if (!mTopLevelBaseDomain.Equals(aOther.mTopLevelBaseDomain,
|
|
|
|
nsCaseInsensitiveCStringComparator())) {
|
|
|
|
return false;
|
|
|
|
}
|
2016-08-04 21:22:00 +03:00
|
|
|
// The origin attributes always have to match.
|
|
|
|
if (mOriginAttributes != aOther.mOriginAttributes) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-05-21 04:49:53 +03:00
|
|
|
if (mBlobSerial || aOther.mBlobSerial) {
|
2019-02-22 17:25:04 +03:00
|
|
|
if (mBlobSerial && mBlobRef.IsEmpty()) {
|
|
|
|
EnsureBlobRef();
|
|
|
|
}
|
|
|
|
if (aOther.mBlobSerial && aOther.mBlobRef.IsEmpty()) {
|
|
|
|
aOther.EnsureBlobRef();
|
|
|
|
}
|
2015-06-05 11:52:06 +03:00
|
|
|
// If at least one of us has a blob serial, just compare the blob serial and
|
|
|
|
// the ref portion of the URIs.
|
|
|
|
return mBlobSerial == aOther.mBlobSerial && mBlobRef == aOther.mBlobRef;
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-blob URIs, compare the URIs.
|
2018-06-06 03:42:56 +03:00
|
|
|
bool equals = false;
|
|
|
|
nsresult rv = mURI->Equals(aOther.mURI, &equals);
|
|
|
|
return NS_SUCCEEDED(rv) && equals;
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
2019-02-22 17:25:04 +03:00
|
|
|
void ImageCacheKey::EnsureBlobRef() const {
|
|
|
|
MOZ_ASSERT(mBlobSerial);
|
|
|
|
MOZ_ASSERT(mBlobRef.IsEmpty());
|
|
|
|
|
|
|
|
nsresult rv = mURI->GetRef(mBlobRef);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ImageCacheKey::EnsureHash() const {
|
|
|
|
MOZ_ASSERT(mHash.isNothing());
|
|
|
|
PLDHashNumber hash = 0;
|
|
|
|
|
|
|
|
// Since we frequently call Hash() several times in a row on the same
|
|
|
|
// ImageCacheKey, as an optimization we compute our hash once and store it.
|
|
|
|
|
|
|
|
nsPrintfCString ptr("%p", mControlledDocument);
|
|
|
|
nsAutoCString suffix;
|
|
|
|
mOriginAttributes.CreateSuffix(suffix);
|
|
|
|
|
|
|
|
if (mBlobSerial) {
|
|
|
|
if (mBlobRef.IsEmpty()) {
|
|
|
|
EnsureBlobRef();
|
|
|
|
}
|
|
|
|
hash = HashGeneric(*mBlobSerial, HashString(mBlobRef));
|
|
|
|
} else {
|
|
|
|
nsAutoCString spec;
|
|
|
|
Unused << mURI->GetSpec(spec);
|
|
|
|
hash = HashString(spec);
|
|
|
|
}
|
|
|
|
|
2019-05-02 15:27:07 +03:00
|
|
|
hash = AddToHash(hash, HashString(suffix), HashString(mTopLevelBaseDomain),
|
|
|
|
HashString(ptr));
|
2019-02-22 17:25:04 +03:00
|
|
|
mHash.emplace(hash);
|
|
|
|
}
|
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
bool ImageCacheKey::SchemeIs(const char* aScheme) {
|
|
|
|
bool matches = false;
|
|
|
|
return NS_SUCCEEDED(mURI->SchemeIs(aScheme, &matches)) && matches;
|
2015-10-27 21:12:46 +03:00
|
|
|
}
|
|
|
|
|
2019-02-26 01:07:58 +03:00
|
|
|
/* static */
|
|
|
|
void* ImageCacheKey::GetSpecialCaseDocumentToken(Document* aDocument,
|
|
|
|
nsIURI* aURI) {
|
2018-08-16 23:44:37 +03:00
|
|
|
// Cookie-averse documents can never have storage granted to them. Since they
|
|
|
|
// may not have inner windows, they would require special handling below, so
|
|
|
|
// just bail out early here.
|
|
|
|
if (!aDocument || aDocument->IsCookieAverse()) {
|
2018-07-13 13:02:19 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2018-06-20 20:38:22 +03:00
|
|
|
// For controlled documents, we cast the pointer into a void* to avoid
|
|
|
|
// dereferencing it (since we only use it for comparisons).
|
2015-10-27 21:12:46 +03:00
|
|
|
RefPtr<ServiceWorkerManager> swm = ServiceWorkerManager::GetInstance();
|
2018-07-13 13:02:19 +03:00
|
|
|
if (swm && aDocument->GetController().isSome()) {
|
|
|
|
return aDocument;
|
|
|
|
}
|
|
|
|
|
2019-05-02 15:27:07 +03:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsCString ImageCacheKey::GetTopLevelBaseDomain(Document* aDocument,
|
|
|
|
nsIURI* aURI) {
|
|
|
|
if (!aDocument || !aDocument->GetInnerWindow()) {
|
|
|
|
return EmptyCString();
|
|
|
|
}
|
|
|
|
|
2018-08-16 23:44:37 +03:00
|
|
|
// If the window is 3rd party resource, let's see if first-party storage
|
|
|
|
// access is granted for this image.
|
2019-01-31 20:51:58 +03:00
|
|
|
if (nsContentUtils::IsThirdPartyTrackingResourceWindow(
|
|
|
|
aDocument->GetInnerWindow())) {
|
2019-05-27 17:06:49 +03:00
|
|
|
return StorageDisabledByAntiTracking(aDocument, aURI)
|
2019-05-02 15:27:07 +03:00
|
|
|
? aDocument->GetBaseDomain()
|
|
|
|
: EmptyCString();
|
2015-10-27 21:12:46 +03:00
|
|
|
}
|
2018-06-20 20:38:22 +03:00
|
|
|
|
2018-07-13 13:02:19 +03:00
|
|
|
// Another scenario is if this image is a 3rd party resource loaded by a
|
|
|
|
// first party context. In this case, we should check if the nsIChannel has
|
|
|
|
// been marked as tracking resource, but we don't have the channel yet at
|
|
|
|
// this point. The best approach here is to be conservative: if we are sure
|
2019-05-02 15:27:07 +03:00
|
|
|
// that the permission is granted, let's return 0. Otherwise, let's make a
|
|
|
|
// unique image cache per the top-level document eTLD+1.
|
2018-08-16 23:44:37 +03:00
|
|
|
if (!AntiTrackingCommon::MaybeIsFirstPartyStorageAccessGrantedFor(
|
|
|
|
aDocument->GetInnerWindow(), aURI)) {
|
2019-05-02 15:27:07 +03:00
|
|
|
nsPIDOMWindowOuter* top = aDocument->GetInnerWindow()->GetScriptableTop();
|
|
|
|
nsPIDOMWindowInner* topInner = top->GetCurrentInnerWindow();
|
|
|
|
if (!topInner) {
|
|
|
|
return aDocument
|
|
|
|
->GetBaseDomain(); // because we don't have anything better!
|
|
|
|
}
|
|
|
|
return topInner->GetExtantDoc() ? topInner->GetExtantDoc()->GetBaseDomain()
|
|
|
|
: EmptyCString();
|
2018-06-20 20:38:22 +03:00
|
|
|
}
|
|
|
|
|
2019-05-02 15:27:07 +03:00
|
|
|
return EmptyCString();
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|