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"
|
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"
|
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"
|
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-01-02 16:05:23 +03:00
|
|
|
Document* aDocument, nsresult& aRv)
|
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)),
|
2018-06-06 03:42:56 +03:00
|
|
|
mHash(0),
|
|
|
|
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
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
// 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.
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
nsPrintfCString ptr("%p", mControlledDocument);
|
|
|
|
nsAutoCString suffix;
|
|
|
|
mOriginAttributes.CreateSuffix(suffix);
|
|
|
|
|
|
|
|
if (mBlobSerial) {
|
|
|
|
aRv = mURI->GetRef(mBlobRef);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
|
|
|
mHash = HashGeneric(*mBlobSerial, HashString(mBlobRef));
|
|
|
|
} else {
|
|
|
|
nsAutoCString spec;
|
|
|
|
aRv = mURI->GetSpec(spec);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(aRv);
|
|
|
|
mHash = HashString(spec);
|
2015-05-21 04:49:53 +03:00
|
|
|
}
|
2015-05-20 20:21:09 +03:00
|
|
|
|
2018-06-06 03:42:56 +03:00
|
|
|
mHash = AddToHash(mHash, HashString(suffix), HashString(ptr));
|
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),
|
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),
|
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;
|
|
|
|
}
|
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) {
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-06-20 20:38:22 +03:00
|
|
|
/* static */ void* ImageCacheKey::GetSpecialCaseDocumentToken(
|
2019-01-02 16:05:23 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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())) {
|
2018-08-16 23:44:37 +03:00
|
|
|
return nsContentUtils::StorageDisabledByAntiTracking(aDocument, aURI)
|
|
|
|
? aDocument
|
|
|
|
: nullptr;
|
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
|
|
|
|
// that the permission is granted, let's return a nullptr. Otherwise, let's
|
|
|
|
// make a unique image cache.
|
2018-08-16 23:44:37 +03:00
|
|
|
if (!AntiTrackingCommon::MaybeIsFirstPartyStorageAccessGrantedFor(
|
|
|
|
aDocument->GetInnerWindow(), aURI)) {
|
2018-07-13 13:02:19 +03:00
|
|
|
return aDocument;
|
2018-06-20 20:38:22 +03:00
|
|
|
}
|
|
|
|
|
2018-07-13 13:02:19 +03:00
|
|
|
return nullptr;
|
2015-05-20 20:21:09 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|