Skip to content
This repository has been archived by the owner on Aug 4, 2022. It is now read-only.

Commit

Permalink
Bug 1557887 - Part 3: Extend nsIDocShell.createAboutBlankContentViewe…
Browse files Browse the repository at this point in the history
…r() to accept a storage principal argument; r=baku

Differential Revision: https://phabricator.services.mozilla.com/D34457
  • Loading branch information
ehsan committed Jun 12, 2019
1 parent a4d974f commit 6d75d17
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
34 changes: 23 additions & 11 deletions docshell/base/nsDocShell.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7009,6 +7009,7 @@ nsresult nsDocShell::EnsureContentViewer() {
nsCOMPtr<nsIContentSecurityPolicy> cspToInheritForAboutBlank;
nsCOMPtr<nsIURI> baseURI;
nsIPrincipal* principal = GetInheritedPrincipal(false);
nsIPrincipal* storagePrincipal = GetInheritedPrincipal(false, true);
nsCOMPtr<nsIDocShellTreeItem> parentItem;
GetSameTypeParent(getter_AddRefs(parentItem));
if (parentItem) {
Expand All @@ -7022,7 +7023,7 @@ nsresult nsDocShell::EnsureContentViewer() {
}

nsresult rv = CreateAboutBlankContentViewer(
principal, cspToInheritForAboutBlank, baseURI);
principal, storagePrincipal, cspToInheritForAboutBlank, baseURI);

NS_ENSURE_STATE(mContentViewer);

Expand All @@ -7048,7 +7049,8 @@ nsresult nsDocShell::EnsureContentViewer() {
}

nsresult nsDocShell::CreateAboutBlankContentViewer(
nsIPrincipal* aPrincipal, nsIContentSecurityPolicy* aCSP, nsIURI* aBaseURI,
nsIPrincipal* aPrincipal, nsIPrincipal* aStoragePrincipal,
nsIContentSecurityPolicy* aCSP, nsIURI* aBaseURI,
bool aTryToSaveOldPresentation, bool aCheckPermitUnload) {
RefPtr<Document> blankDoc;
nsCOMPtr<nsIContentViewer> viewer;
Expand Down Expand Up @@ -7137,7 +7139,7 @@ nsresult nsDocShell::CreateAboutBlankContentViewer(
NS_LITERAL_CSTRING("text/html"));

if (docFactory) {
nsCOMPtr<nsIPrincipal> principal;
nsCOMPtr<nsIPrincipal> principal, storagePrincipal;
if (mSandboxFlags & SANDBOXED_ORIGIN) {
if (aPrincipal) {
principal = NullPrincipal::CreateWithInheritedAttributes(aPrincipal);
Expand Down Expand Up @@ -7203,8 +7205,10 @@ nsresult nsDocShell::CreateAboutBlankContentViewer(

NS_IMETHODIMP
nsDocShell::CreateAboutBlankContentViewer(nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal,
nsIContentSecurityPolicy* aCSP) {
return CreateAboutBlankContentViewer(aPrincipal, aCSP, nullptr);
return CreateAboutBlankContentViewer(aPrincipal, aStoragePrincipal, aCSP,
nullptr);
}

bool nsDocShell::CanSavePresentation(uint32_t aLoadType,
Expand Down Expand Up @@ -9437,7 +9441,7 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
}

// clear the decks to prevent context bleed-through (bug 298255)
rv = CreateAboutBlankContentViewer(nullptr, nullptr, nullptr);
rv = CreateAboutBlankContentViewer(nullptr, nullptr, nullptr, nullptr);
if (NS_FAILED(rv)) {
return NS_ERROR_FAILURE;
}
Expand Down Expand Up @@ -9666,7 +9670,8 @@ nsresult nsDocShell::InternalLoad(nsDocShellLoadState* aLoadState,
return rv;
}

nsIPrincipal* nsDocShell::GetInheritedPrincipal(bool aConsiderCurrentDocument) {
nsIPrincipal* nsDocShell::GetInheritedPrincipal(
bool aConsiderCurrentDocument, bool aConsiderStoragePrincipal) {
RefPtr<Document> document;
bool inheritedFromCurrent = false;

Expand Down Expand Up @@ -9699,7 +9704,9 @@ nsIPrincipal* nsDocShell::GetInheritedPrincipal(bool aConsiderCurrentDocument) {

//-- Get the document's principal
if (document) {
nsIPrincipal* docPrincipal = document->NodePrincipal();
nsIPrincipal* docPrincipal = aConsiderStoragePrincipal
? document->EffectiveStoragePrincipal()
: document->NodePrincipal();

// Don't allow loads in typeContent docShells to inherit the system
// principal from existing documents.
Expand Down Expand Up @@ -11680,6 +11687,8 @@ nsresult nsDocShell::LoadHistoryEntry(nsISHEntry* aEntry, uint32_t aLoadType) {
aEntry->GetContentType(contentType);
nsCOMPtr<nsIPrincipal> triggeringPrincipal = aEntry->GetTriggeringPrincipal();
nsCOMPtr<nsIPrincipal> principalToInherit = aEntry->GetPrincipalToInherit();
nsCOMPtr<nsIPrincipal> storagePrincipalToInherit =
aEntry->GetStoragePrincipalToInherit();
nsCOMPtr<nsIContentSecurityPolicy> csp = aEntry->GetCsp();
nsCOMPtr<nsIReferrerInfo> referrerInfo = aEntry->GetReferrerInfo();

Expand All @@ -11697,8 +11706,9 @@ nsresult nsDocShell::LoadHistoryEntry(nsISHEntry* aEntry, uint32_t aLoadType) {
// Don't cache the presentation if we're going to just reload the
// current entry. Caching would lead to trying to save the different
// content viewers in the same nsISHEntry object.
rv = CreateAboutBlankContentViewer(principalToInherit, nullptr, nullptr,
aEntry != mOSHE);
rv = CreateAboutBlankContentViewer(principalToInherit,
storagePrincipalToInherit, nullptr,
nullptr, aEntry != mOSHE);

if (NS_FAILED(rv)) {
// The creation of the intermittent about:blank content
Expand Down Expand Up @@ -12998,8 +13008,10 @@ nsDocShell::InitOrReusePrintPreviewViewer(nsIWebBrowserPrint** aPrintPreview) {
NullPrincipal::CreateWithInheritedAttributes(this);
nsCOMPtr<nsIURI> uri;
NS_NewURI(getter_AddRefs(uri), NS_LITERAL_CSTRING("about:printpreview"));
nsresult rv =
CreateAboutBlankContentViewer(principal, /* aCsp = */ nullptr, uri);
// Reuse the null principal for the storage principal.
// XXXehsan is that the right principal to use here?
nsresult rv = CreateAboutBlankContentViewer(principal, principal,
/* aCsp = */ nullptr, uri);
NS_ENSURE_SUCCESS(rv, rv);
// Here we manually set current URI since we have just created a
// brand new content viewer (about:blank) to host preview.
Expand Down
6 changes: 5 additions & 1 deletion docshell/base/nsDocShell.h
Original file line number Diff line number Diff line change
Expand Up @@ -498,6 +498,7 @@ class nsDocShell final : public nsDocLoader,
// passed in, the about:blank principal will end up being used.
// aCSP, if any, will be used for the new about:blank load.
nsresult CreateAboutBlankContentViewer(nsIPrincipal* aPrincipal,
nsIPrincipal* aStoragePrincipal,
nsIContentSecurityPolicy* aCSP,
nsIURI* aBaseURI,
bool aTryToSaveOldPresentation = true,
Expand Down Expand Up @@ -649,7 +650,10 @@ class nsDocShell final : public nsDocLoader,
// If that fails too, we force creation of a content viewer and use the
// resulting principal. If aConsiderCurrentDocument is false, we just look
// at the parent.
nsIPrincipal* GetInheritedPrincipal(bool aConsiderCurrentDocument);
// If aConsiderStoragePrincipal is true, we consider the storage principal
// instead of the node principal.
nsIPrincipal* GetInheritedPrincipal(bool aConsiderCurrentDocument,
bool aConsiderStoragePrincipal = false);

/**
* Helper function that determines if channel is an HTTP POST.
Expand Down
2 changes: 2 additions & 0 deletions docshell/base/nsIDocShell.idl
Original file line number Diff line number Diff line change
Expand Up @@ -613,9 +613,11 @@ interface nsIDocShell : nsIDocShellTreeItem
/**
* Create a new about:blank document and content viewer.
* @param aPrincipal the principal to use for the new document.
* @param aStoragePrincipal the storage principal to use for the new document.
* @param aCsp the CSP to use for the new document.
*/
void createAboutBlankContentViewer(in nsIPrincipal aPrincipal,
in nsIPrincipal aStoragePrincipal,
[optional] in nsIContentSecurityPolicy aCSP);

/**
Expand Down
5 changes: 4 additions & 1 deletion dom/base/nsGlobalWindowOuter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,7 +1580,10 @@ void nsGlobalWindowOuter::SetInitialPrincipalToSubject(
#endif
}

GetDocShell()->CreateAboutBlankContentViewer(newWindowPrincipal, aCSP);
// Use the subject (or system) principal as the storage principal too until
// the new window finishes navigating and gets a real storage principal.
GetDocShell()->CreateAboutBlankContentViewer(newWindowPrincipal,
newWindowPrincipal, aCSP);

if (mDoc) {
mDoc->SetIsInitialDocument(true);
Expand Down
4 changes: 3 additions & 1 deletion xpfe/appshell/nsWebShellWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,9 @@ nsresult nsWebShellWindow::Initialize(
if (nsContentUtils::IsExpandedPrincipal(principal)) {
principal = nullptr;
}
rv = mDocShell->CreateAboutBlankContentViewer(principal,
// Use the subject (or system) principal as the storage principal too until
// the new window finishes navigating and gets a real storage principal.
rv = mDocShell->CreateAboutBlankContentViewer(principal, principal,
/* aCsp = */ nullptr);
NS_ENSURE_SUCCESS(rv, rv);
RefPtr<Document> doc = mDocShell->GetDocument();
Expand Down

0 comments on commit 6d75d17

Please sign in to comment.