Skip to content
This repository has been archived by the owner on Nov 3, 2021. It is now read-only.

Commit

Permalink
Bug 1254194: Apply a content security policy to all WebExtension docu…
Browse files Browse the repository at this point in the history
…ments. r=gabor

MozReview-Commit-ID: HsFFbWdq00b

--HG--
extra : rebase_source : 07e4b6ec8c32f696d5b5987091ffc5ebde2c3061
extra : histedit_source : 20983fe6a9590d7f410276fac248c3d2f711caaa
  • Loading branch information
kmaglione committed Apr 24, 2016
1 parent 623a4f8 commit 6d36833
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 2 deletions.
7 changes: 7 additions & 0 deletions caps/BasePrincipal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,13 @@ BasePrincipal::GetAppId(uint32_t* aAppId)
return NS_OK;
}

NS_IMETHODIMP
BasePrincipal::GetAddonId(nsAString& aAddonId)
{
aAddonId.Assign(mOriginAttributes.mAddonId);
return NS_OK;
}

NS_IMETHODIMP
BasePrincipal::GetUserContextId(uint32_t* aUserContextId)
{
Expand Down
1 change: 1 addition & 0 deletions caps/BasePrincipal.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ class BasePrincipal : public nsJSPrincipals
NS_IMETHOD GetOriginSuffix(nsACString& aOriginSuffix) final;
NS_IMETHOD GetAppStatus(uint16_t* aAppStatus) final;
NS_IMETHOD GetAppId(uint32_t* aAppStatus) final;
NS_IMETHOD GetAddonId(nsAString& aAddonId) final;
NS_IMETHOD GetIsInIsolatedMozBrowserElement(bool* aIsInIsolatedMozBrowserElement) final;
NS_IMETHOD GetUnknownAppId(bool* aUnknownAppId) final;
NS_IMETHOD GetUserContextId(uint32_t* aUserContextId) final;
Expand Down
5 changes: 5 additions & 0 deletions caps/nsIPrincipal.idl
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,11 @@ interface nsIPrincipal : nsISerializable
*/
[infallible] readonly attribute unsigned long appId;

/**
* Gets the ID of the add-on this principal belongs to.
*/
readonly attribute AString addonId;

/**
* Gets the id of the user context this principal is inside. If this
* principal is inside the default userContext, this returns
Expand Down
27 changes: 25 additions & 2 deletions dom/base/nsDocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@
#include "imgRequestProxy.h"
#include "nsWrapperCacheInlines.h"
#include "nsSandboxFlags.h"
#include "nsIAddonPolicyService.h"
#include "nsIAppsService.h"
#include "mozilla/dom/AnimatableBinding.h"
#include "mozilla/dom/AnonymousContent.h"
Expand Down Expand Up @@ -2814,12 +2815,18 @@ nsDocument::InitCSP(nsIChannel* aChannel)
}
}

// Check if this is part of the Loop/Hello service
bool applyLoopCSP = IsLoopDocument(aChannel);
// Check if this is a document from a WebExtension.
nsString addonId;
principal->GetAddonId(addonId);
bool applyAddonCSP = !addonId.IsEmpty();

// Check if this is part of the Loop/Hello service
bool applyLoopCSP = IsLoopDocument(aChannel);

// If there's no CSP to apply, go ahead and return early
if (!applyAppDefaultCSP &&
!applyAppManifestCSP &&
!applyAddonCSP &&
!applyLoopCSP &&
cspHeaderValue.IsEmpty() &&
cspROHeaderValue.IsEmpty()) {
Expand Down Expand Up @@ -2877,6 +2884,22 @@ nsDocument::InitCSP(nsIChannel* aChannel)
csp->AppendPolicy(appManifestCSP, false, false);
}

// ----- if the doc is an addon, apply its CSP.
if (applyAddonCSP) {
nsCOMPtr<nsIAddonPolicyService> aps = do_GetService("@mozilla.org/addons/policy-service;1");

nsAutoString addonCSP;
rv = aps->GetBaseCSP(addonCSP);
if (NS_SUCCEEDED(rv)) {
csp->AppendPolicy(addonCSP, false, false);
}

rv = aps->GetAddonCSP(addonId, addonCSP);
if (NS_SUCCEEDED(rv)) {
csp->AppendPolicy(addonCSP, false, false);
}
}

// ----- if the doc is part of Loop, apply the loop CSP
if (applyLoopCSP) {
nsAdoptingString loopCSP;
Expand Down

0 comments on commit 6d36833

Please sign in to comment.