Bug 1728331 - Part 5: Respect AppType in ImageCacheKey, r=emilio

This attribute is not used in Gecko, but exists for use by other
applications. Specifically, the APP_TYPE_EDITOR type is given permission
to load privileged images as tested by browser_docshell_type_editor.js.
Before these changes, that test passed because the docshell was loaded
in a different process, so the cache was empty when each load occurred,
but after my changes the process ends up being re-used, so the image
cache bypasses this check.

This changes the image cache key to also include the app type
information so that it will be compared before re-using the entry.

Differential Revision: https://phabricator.services.mozilla.com/D126557
This commit is contained in:
Nika Layzell 2024-06-24 23:19:29 +00:00
parent d6e6375a87
commit 19d2b08163
2 changed files with 36 additions and 4 deletions

View File

@ -36,7 +36,8 @@ ImageCacheKey::ImageCacheKey(nsIURI* aURI, CORSMode aCORSMode,
mOriginAttributes(aAttrs),
mControlledDocument(GetSpecialCaseDocumentToken(aDocument)),
mIsolationKey(GetIsolationKey(aDocument, aURI)),
mCORSMode(aCORSMode) {}
mCORSMode(aCORSMode),
mAppType(GetAppType(aDocument)) {}
ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
: mURI(aOther.mURI),
@ -44,7 +45,8 @@ ImageCacheKey::ImageCacheKey(const ImageCacheKey& aOther)
mControlledDocument(aOther.mControlledDocument),
mIsolationKey(aOther.mIsolationKey),
mHash(aOther.mHash),
mCORSMode(aOther.mCORSMode) {}
mCORSMode(aOther.mCORSMode),
mAppType(aOther.mAppType) {}
ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
: mURI(std::move(aOther.mURI)),
@ -52,7 +54,8 @@ ImageCacheKey::ImageCacheKey(ImageCacheKey&& aOther)
mControlledDocument(aOther.mControlledDocument),
mIsolationKey(aOther.mIsolationKey),
mHash(aOther.mHash),
mCORSMode(aOther.mCORSMode) {}
mCORSMode(aOther.mCORSMode),
mAppType(aOther.mAppType) {}
bool ImageCacheKey::operator==(const ImageCacheKey& aOther) const {
// Don't share the image cache between a controlled document and anything
@ -74,6 +77,10 @@ bool ImageCacheKey::operator==(const ImageCacheKey& aOther) const {
if (mCORSMode != aOther.mCORSMode) {
return false;
}
// Don't share the image cache between two different appTypes
if (mAppType != aOther.mAppType) {
return false;
}
// For non-blob URIs, compare the URIs.
bool equals = false;
@ -97,7 +104,7 @@ void ImageCacheKey::EnsureHash() const {
hash = HashString(spec);
hash = AddToHash(hash, HashString(suffix), HashString(mIsolationKey),
HashString(ptr));
HashString(ptr), mAppType);
mHash.emplace(hash);
}
@ -167,5 +174,24 @@ nsCString ImageCacheKey::GetIsolationKey(Document* aDocument, nsIURI* aURI) {
return ""_ns;
}
/* static */
nsIDocShell::AppType ImageCacheKey::GetAppType(Document* aDocument) {
if (!aDocument) {
return nsIDocShell::APP_TYPE_UNKNOWN;
}
nsCOMPtr<nsIDocShellTreeItem> dsti = aDocument->GetDocShell();
if (!dsti) {
return nsIDocShell::APP_TYPE_UNKNOWN;
}
nsCOMPtr<nsIDocShellTreeItem> root;
dsti->GetInProcessRootTreeItem(getter_AddRefs(root));
if (nsCOMPtr<nsIDocShell> docShell = do_QueryInterface(root)) {
return docShell->GetAppType();
}
return nsIDocShell::APP_TYPE_UNKNOWN;
}
} // namespace image
} // namespace mozilla

View File

@ -14,6 +14,7 @@
#include "mozilla/Maybe.h"
#include "mozilla/RefPtr.h"
#include "PLDHashTable.h"
#include "nsIDocShell.h"
class nsIURI;
@ -71,6 +72,10 @@ class ImageCacheKey final {
// document's base domain. This is handled by this method.
static nsCString GetIsolationKey(dom::Document* aDocument, nsIURI* aURI);
// The AppType of the docshell an image is loaded in can influence whether the
// image is allowed to load. The specific AppType is fetched by this method.
static nsIDocShell::AppType GetAppType(dom::Document* aDocument);
void EnsureHash() const;
nsCOMPtr<nsIURI> mURI;
@ -79,6 +84,7 @@ class ImageCacheKey final {
nsCString mIsolationKey;
mutable Maybe<PLDHashNumber> mHash;
const CORSMode mCORSMode;
const nsIDocShell::AppType mAppType;
};
} // namespace image