Skip to content

Commit 02ca49e

Browse files
committed
Bug 1890748 - Move responsibility of FeaturePolicy initialization to nsILoadInfo. r=freddyb,necko-reviewers,jesup,dom-core,sefeng
Differential Revision: https://phabricator.services.mozilla.com/D207140
1 parent 133721b commit 02ca49e

27 files changed

+214
-159
lines changed

docshell/base/CanonicalBrowsingContext.cpp

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2780,12 +2780,8 @@ void CanonicalBrowsingContext::CancelSessionStoreUpdate() {
27802780
}
27812781

27822782
void CanonicalBrowsingContext::SetContainerFeaturePolicy(
2783-
FeaturePolicy* aContainerFeaturePolicy) {
2784-
mContainerFeaturePolicy = aContainerFeaturePolicy;
2785-
2786-
if (WindowGlobalParent* current = GetCurrentWindowGlobal()) {
2787-
Unused << current->SendSetContainerFeaturePolicy(mContainerFeaturePolicy);
2788-
}
2783+
Maybe<FeaturePolicyInfo>&& aContainerFeaturePolicyInfo) {
2784+
mContainerFeaturePolicyInfo = std::move(aContainerFeaturePolicyInfo);
27892785
}
27902786

27912787
already_AddRefed<CanonicalBrowsingContext>
@@ -3212,15 +3208,15 @@ NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(CanonicalBrowsingContext,
32123208
if (tmp->mSessionHistory) {
32133209
tmp->mSessionHistory->SetBrowsingContext(nullptr);
32143210
}
3215-
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSessionHistory, mContainerFeaturePolicy,
3216-
mCurrentBrowserParent, mWebProgress,
3211+
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSessionHistory, mCurrentBrowserParent,
3212+
mWebProgress,
32173213
mSessionStoreSessionStorageUpdateTimer)
32183214
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
32193215

32203216
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(CanonicalBrowsingContext,
32213217
BrowsingContext)
3222-
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSessionHistory, mContainerFeaturePolicy,
3223-
mCurrentBrowserParent, mWebProgress,
3218+
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSessionHistory, mCurrentBrowserParent,
3219+
mWebProgress,
32243220
mSessionStoreSessionStorageUpdateTimer)
32253221
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
32263222

docshell/base/CanonicalBrowsingContext.h

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "mozilla/dom/BrowsingContext.h"
1111
#include "mozilla/dom/MediaControlKeySource.h"
1212
#include "mozilla/dom/BrowsingContextWebProgress.h"
13+
#include "mozilla/dom/FeaturePolicy.h"
1314
#include "mozilla/dom/ProcessIsolation.h"
1415
#include "mozilla/dom/Promise.h"
1516
#include "mozilla/dom/SessionHistoryEntry.h"
@@ -315,9 +316,10 @@ class CanonicalBrowsingContext final : public BrowsingContext {
315316

316317
void ResetScalingZoom();
317318

318-
void SetContainerFeaturePolicy(FeaturePolicy* aContainerFeaturePolicy);
319-
FeaturePolicy* GetContainerFeaturePolicy() const {
320-
return mContainerFeaturePolicy;
319+
void SetContainerFeaturePolicy(
320+
Maybe<FeaturePolicyInfo>&& aContainerFeaturePolicyInfo);
321+
const Maybe<FeaturePolicyInfo>& GetContainerFeaturePolicy() const {
322+
return mContainerFeaturePolicyInfo;
321323
}
322324

323325
void SetRestoreData(SessionStoreRestoreData* aData, ErrorResult& aError);
@@ -582,7 +584,7 @@ class CanonicalBrowsingContext final : public BrowsingContext {
582584
nsCOMPtr<nsIWebProgressListener> mDocShellProgressBridge;
583585
RefPtr<nsBrowserStatusFilter> mStatusFilter;
584586

585-
RefPtr<FeaturePolicy> mContainerFeaturePolicy;
587+
Maybe<FeaturePolicyInfo> mContainerFeaturePolicyInfo;
586588

587589
friend class BrowserSessionStore;
588590
WeakPtr<SessionStoreFormData>& GetSessionStoreFormDataRef() {

docshell/base/nsDocShell.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6596,8 +6596,6 @@ nsresult nsDocShell::CreateAboutBlankDocumentViewer(
65966596
// after being set here.
65976597
blankDoc->SetSandboxFlags(sandboxFlags);
65986598

6599-
blankDoc->InitFeaturePolicy();
6600-
66016599
// create a content viewer for us and the new document
66026600
docFactory->CreateInstanceForDocument(
66036601
NS_ISUPPORTS_CAST(nsIDocShell*, this), blankDoc, "view",
@@ -6615,6 +6613,12 @@ nsresult nsDocShell::CreateAboutBlankDocumentViewer(
66156613
/* aLocationFlags */ 0);
66166614
rv = mIsBeingDestroyed ? NS_ERROR_NOT_AVAILABLE : NS_OK;
66176615
}
6616+
6617+
if (Element* embedderElement = blankDoc->GetEmbedderElement()) {
6618+
blankDoc->InitFeaturePolicy(AsVariant(embedderElement));
6619+
} else {
6620+
blankDoc->InitFeaturePolicy(AsVariant(Nothing{}));
6621+
}
66186622
}
66196623
}
66206624

dom/base/Document.cpp

Lines changed: 45 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,15 @@
180180
#include "mozilla/dom/HTMLBodyElement.h"
181181
#include "mozilla/dom/HTMLCollectionBinding.h"
182182
#include "mozilla/dom/HTMLDialogElement.h"
183+
#include "mozilla/dom/HTMLEmbedElement.h"
183184
#include "mozilla/dom/HTMLFormElement.h"
184185
#include "mozilla/dom/HTMLIFrameElement.h"
185186
#include "mozilla/dom/HTMLImageElement.h"
186187
#include "mozilla/dom/HTMLInputElement.h"
187188
#include "mozilla/dom/HTMLLinkElement.h"
188189
#include "mozilla/dom/HTMLMediaElement.h"
189190
#include "mozilla/dom/HTMLMetaElement.h"
191+
#include "mozilla/dom/HTMLObjectElement.h"
190192
#include "mozilla/dom/HTMLSharedElement.h"
191193
#include "mozilla/dom/HTMLTextAreaElement.h"
192194
#include "mozilla/dom/ImageTracker.h"
@@ -3947,74 +3949,68 @@ nsresult Document::InitCSP(nsIChannel* aChannel) {
39473949
return NS_OK;
39483950
}
39493951

3950-
static Document* GetInProcessParentDocumentFrom(BrowsingContext* aContext) {
3951-
BrowsingContext* parentContext = aContext->GetParent();
3952-
if (!parentContext) {
3953-
return nullptr;
3952+
static FeaturePolicy* GetFeaturePolicyFromElement(Element* aElement) {
3953+
if (auto* iframe = HTMLIFrameElement::FromNodeOrNull(aElement)) {
3954+
return iframe->FeaturePolicy();
39543955
}
39553956

3956-
WindowContext* windowContext = parentContext->GetCurrentWindowContext();
3957-
if (!windowContext) {
3957+
if (!HTMLObjectElement::FromNodeOrNull(aElement) &&
3958+
!HTMLEmbedElement::FromNodeOrNull(aElement)) {
39583959
return nullptr;
39593960
}
39603961

3961-
return windowContext->GetDocument();
3962+
return aElement->OwnerDoc()->FeaturePolicy();
39623963
}
39633964

3964-
already_AddRefed<dom::FeaturePolicy> Document::GetParentFeaturePolicy() {
3965-
BrowsingContext* browsingContext = GetBrowsingContext();
3966-
if (!browsingContext) {
3967-
return nullptr;
3968-
}
3969-
if (!browsingContext->IsContentSubframe()) {
3970-
return nullptr;
3971-
}
3965+
void Document::InitFeaturePolicy(
3966+
const Variant<Nothing, FeaturePolicyInfo, Element*>&
3967+
aContainerFeaturePolicy) {
3968+
MOZ_ASSERT(mFeaturePolicy, "we should have FeaturePolicy created");
39723969

3973-
HTMLIFrameElement* iframe =
3974-
HTMLIFrameElement::FromNodeOrNull(browsingContext->GetEmbedderElement());
3975-
if (iframe) {
3976-
return do_AddRef(iframe->FeaturePolicy());
3977-
}
3970+
mFeaturePolicy->ResetDeclaredPolicy();
39783971

3979-
if (XRE_IsParentProcess()) {
3980-
return do_AddRef(browsingContext->Canonical()->GetContainerFeaturePolicy());
3981-
}
3972+
mFeaturePolicy->SetDefaultOrigin(NodePrincipal());
39823973

3983-
if (Document* parentDocument =
3984-
GetInProcessParentDocumentFrom(browsingContext)) {
3985-
return do_AddRef(parentDocument->FeaturePolicy());
3986-
}
3974+
RefPtr<dom::FeaturePolicy> featurePolicy = mFeaturePolicy;
3975+
aContainerFeaturePolicy.match(
3976+
[](const Nothing&) {},
3977+
[featurePolicy](const FeaturePolicyInfo& aContainerFeaturePolicy) {
3978+
// Let's inherit the policy from the possibly cross-origin container.
3979+
featurePolicy->InheritPolicy(aContainerFeaturePolicy);
3980+
featurePolicy->SetSrcOrigin(aContainerFeaturePolicy.mSrcOrigin);
3981+
},
3982+
[featurePolicy](Element* aContainer) {
3983+
// Let's inherit the policy from the parent container element if it
3984+
// exists.
3985+
if (RefPtr<dom::FeaturePolicy> containerFeaturePolicy =
3986+
GetFeaturePolicyFromElement(aContainer)) {
3987+
featurePolicy->InheritPolicy(containerFeaturePolicy);
3988+
featurePolicy->SetSrcOrigin(containerFeaturePolicy->GetSrcOrigin());
3989+
}
3990+
});
3991+
}
39873992

3988-
WindowContext* windowContext = browsingContext->GetCurrentWindowContext();
3989-
if (!windowContext) {
3993+
Element* GetEmbedderElementFrom(BrowsingContext* aBrowsingContext) {
3994+
if (!aBrowsingContext) {
39903995
return nullptr;
39913996
}
3992-
3993-
WindowGlobalChild* child = windowContext->GetWindowGlobalChild();
3994-
if (!child) {
3997+
if (!aBrowsingContext->IsContentSubframe()) {
39953998
return nullptr;
39963999
}
39974000

3998-
return do_AddRef(child->GetContainerFeaturePolicy());
3999-
}
4000-
4001-
void Document::InitFeaturePolicy() {
4002-
MOZ_ASSERT(mFeaturePolicy, "we should have FeaturePolicy created");
4003-
4004-
mFeaturePolicy->ResetDeclaredPolicy();
4005-
4006-
mFeaturePolicy->SetDefaultOrigin(NodePrincipal());
4007-
4008-
RefPtr<mozilla::dom::FeaturePolicy> parentPolicy = GetParentFeaturePolicy();
4009-
if (parentPolicy) {
4010-
// Let's inherit the policy from the parent HTMLIFrameElement if it exists.
4011-
mFeaturePolicy->InheritPolicy(parentPolicy);
4012-
mFeaturePolicy->SetSrcOrigin(parentPolicy->GetSrcOrigin());
4013-
}
4001+
return aBrowsingContext->GetEmbedderElement();
40144002
}
40154003

40164004
nsresult Document::InitFeaturePolicy(nsIChannel* aChannel) {
4017-
InitFeaturePolicy();
4005+
nsCOMPtr<nsILoadInfo> loadInfo = aChannel->LoadInfo();
4006+
if (Element* embedderElement = GetEmbedderElementFrom(GetBrowsingContext())) {
4007+
InitFeaturePolicy(AsVariant(embedderElement));
4008+
} else if (Maybe<FeaturePolicyInfo> featurePolicyContainer =
4009+
loadInfo->GetContainerFeaturePolicyInfo()) {
4010+
InitFeaturePolicy(AsVariant(*featurePolicyContainer));
4011+
} else {
4012+
InitFeaturePolicy(AsVariant(Nothing{}));
4013+
}
40184014

40194015
// We don't want to parse the http Feature-Policy header if this pref is off.
40204016
if (!StaticPrefs::dom_security_featurePolicy_header_enabled()) {

dom/base/Document.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,8 @@ class Document : public nsINode,
15031503

15041504
void DoNotifyPossibleTitleChange();
15051505

1506-
void InitFeaturePolicy();
1506+
void InitFeaturePolicy(const Variant<Nothing, FeaturePolicyInfo, Element*>&
1507+
aContainerFeaturePolicy);
15071508
nsresult InitFeaturePolicy(nsIChannel* aChannel);
15081509

15091510
void EnsureNotEnteringAndExitFullscreen();

dom/base/nsObjectLoadingContent.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1838,7 +1838,8 @@ void nsObjectLoadingContent::MaybeStoreCrossOriginFeaturePolicy() {
18381838

18391839
FeaturePolicy* featurePolicy = el->OwnerDoc()->FeaturePolicy();
18401840

1841-
if (ContentChild* cc = ContentChild::GetSingleton()) {
1842-
Unused << cc->SendSetContainerFeaturePolicy(browsingContext, featurePolicy);
1841+
if (ContentChild* cc = ContentChild::GetSingleton(); cc && featurePolicy) {
1842+
Unused << cc->SendSetContainerFeaturePolicy(
1843+
browsingContext, Some(featurePolicy->ToFeaturePolicyInfo()));
18431844
}
18441845
}

dom/html/HTMLIFrameElement.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -255,8 +255,8 @@ void HTMLIFrameElement::MaybeStoreCrossOriginFeaturePolicy() {
255255
}
256256

257257
if (ContentChild* cc = ContentChild::GetSingleton()) {
258-
Unused << cc->SendSetContainerFeaturePolicy(browsingContext,
259-
mFeaturePolicy);
258+
Unused << cc->SendSetContainerFeaturePolicy(
259+
browsingContext, Some(mFeaturePolicy->ToFeaturePolicyInfo()));
260260
}
261261
}
262262

dom/ipc/ContentParent.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8239,13 +8239,13 @@ IPCResult ContentParent::RecvFOGData(ByteBuf&& buf) {
82398239

82408240
mozilla::ipc::IPCResult ContentParent::RecvSetContainerFeaturePolicy(
82418241
const MaybeDiscardedBrowsingContext& aContainerContext,
8242-
FeaturePolicy* aContainerFeaturePolicy) {
8242+
MaybeFeaturePolicyInfo&& aContainerFeaturePolicyInfo) {
82438243
if (aContainerContext.IsNullOrDiscarded()) {
82448244
return IPC_OK();
82458245
}
82468246

82478247
auto* context = aContainerContext.get_canonical();
8248-
context->SetContainerFeaturePolicy(aContainerFeaturePolicy);
8248+
context->SetContainerFeaturePolicy(std::move(aContainerFeaturePolicyInfo));
82498249

82508250
return IPC_OK();
82518251
}

dom/ipc/ContentParent.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1389,7 +1389,7 @@ class ContentParent final : public PContentParent,
13891389

13901390
mozilla::ipc::IPCResult RecvSetContainerFeaturePolicy(
13911391
const MaybeDiscardedBrowsingContext& aContainerContext,
1392-
FeaturePolicy* aContainerFeaturePolicy);
1392+
MaybeFeaturePolicyInfo&& aContainerFeaturePolicyInfo);
13931393

13941394
mozilla::ipc::IPCResult RecvGetSystemIcon(nsIURI* aURI,
13951395
GetSystemIconResolver&& aResolver);

dom/ipc/DOMTypes.ipdlh

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -132,16 +132,6 @@ struct FrameScriptInfo
132132
bool runInGlobalScope;
133133
};
134134

135-
struct FeaturePolicyInfo
136-
{
137-
nsString[] inheritedDeniedFeatureNames;
138-
nsString[] attributeEnabledFeatureNames;
139-
nsString declaredString;
140-
nullable nsIPrincipal defaultOrigin;
141-
nullable nsIPrincipal selfOrigin;
142-
nullable nsIPrincipal srcOrigin;
143-
};
144-
145135
/**
146136
* The information required to complete a window creation request.
147137
*/

0 commit comments

Comments
 (0)