Skip to content

Commit

Permalink
Add back security.csp.enable
Browse files Browse the repository at this point in the history
  • Loading branch information
K4sum1 committed Nov 23, 2024
1 parent 89b414d commit bd87ffd
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 9 deletions.
5 changes: 5 additions & 0 deletions dom/base/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3813,6 +3813,11 @@ void Document::ApplySettingsFromCSP(bool aSpeculative) {
nsresult Document::InitCSP(nsIChannel* aChannel) {
MOZ_ASSERT(!mScriptGlobalObject,
"CSP must be initialized before mScriptGlobalObject is set!");
if (!StaticPrefs::security_csp_enable()) {
MOZ_LOG(gCspPRLog, LogLevel::Debug,
("CSP is disabled, skipping CSP init for document %p", this));
return NS_OK;
}

// If this is a data document - no need to set CSP.
if (mLoadedAsData) {
Expand Down
6 changes: 4 additions & 2 deletions dom/security/nsCSPService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ bool subjectToCSP(nsIURI* aURI, nsContentPolicyType aContentType) {
// Please note, the correct way to opt-out of CSP using a custom
// protocolHandler is to set one of the nsIProtocolHandler flags
// that are allowlistet in subjectToCSP()
if (!subjectToCSP(aContentLocation, contentType)) {
if (!StaticPrefs::security_csp_enable() ||
!subjectToCSP(aContentLocation, contentType)) {
return NS_OK;
}

Expand Down Expand Up @@ -314,7 +315,8 @@ nsresult CSPService::ConsultCSPForRedirect(nsIURI* aOriginalURI,
// protocolHandler is to set one of the nsIProtocolHandler flags
// that are allowlistet in subjectToCSP()
nsContentPolicyType policyType = aLoadInfo->InternalContentPolicyType();
if (!subjectToCSP(aNewURI, policyType)) {
if (!StaticPrefs::security_csp_enable() ||
!subjectToCSP(aNewURI, policyType)) {
return NS_OK;
}

Expand Down
2 changes: 1 addition & 1 deletion dom/security/nsCSPUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ bool CSP_ShouldResponseInheritCSP(nsIChannel* aChannel) {

void CSP_ApplyMetaCSPToDoc(mozilla::dom::Document& aDoc,
const nsAString& aPolicyStr) {
if (aDoc.IsLoadedAsData()) {
if (!mozilla::StaticPrefs::security_csp_enable() || aDoc.IsLoadedAsData()) {
return;
}

Expand Down
15 changes: 9 additions & 6 deletions dom/workers/loader/NetworkLoadHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include "nsNetUtil.h"

#include "mozilla/Encoding.h"
#include "mozilla/StaticPrefs_security.h"
#include "mozilla/dom/BlobURLProtocolHandler.h"
#include "mozilla/dom/InternalResponse.h"
#include "mozilla/dom/ServiceWorkerBinding.h"
Expand Down Expand Up @@ -238,12 +239,14 @@ nsresult NetworkLoadHandler::DataReceivedFromNetwork(nsIStreamLoader* aLoader,
nsCOMPtr<nsIContentSecurityPolicy> csp = mWorkerRef->Private()->GetCsp();
// We did inherit CSP in bug 1223647. If we do not already have a CSP, we
// should get it from the HTTP headers on the worker script.
if (!csp) {
rv = mWorkerRef->Private()->SetCSPFromHeaderValues(tCspHeaderValue,
tCspROHeaderValue);
NS_ENSURE_SUCCESS(rv, rv);
} else {
csp->EnsureEventTarget(mWorkerRef->Private()->MainThreadEventTarget());
if (StaticPrefs::security_csp_enable()) {
if (!csp) {
rv = mWorkerRef->Private()->SetCSPFromHeaderValues(tCspHeaderValue,
tCspROHeaderValue);
NS_ENSURE_SUCCESS(rv, rv);
} else {
csp->EnsureEventTarget(mWorkerRef->Private()->MainThreadEventTarget());
}
}

mWorkerRef->Private()->UpdateReferrerInfoFromHeader(tRPHeaderCValue);
Expand Down
5 changes: 5 additions & 0 deletions modules/libpref/init/StaticPrefList.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15281,6 +15281,11 @@
value: 100
mirror: always

- name: security.csp.enable
type: bool
value: true
mirror: always

# Time span in seconds for reporting limit.
- name: security.csp.reporting.limit.timespan
type: uint32_t
Expand Down
4 changes: 4 additions & 0 deletions parser/html/nsHtml5TreeOpExecutor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1332,6 +1332,10 @@ void nsHtml5TreeOpExecutor::UpdateReferrerInfoFromMeta(
}

void nsHtml5TreeOpExecutor::AddSpeculationCSP(const nsAString& aCSP) {
if (!StaticPrefs::security_csp_enable()) {
return;
}

NS_ASSERTION(NS_IsMainThread(), "Wrong thread!");

nsresult rv = NS_OK;
Expand Down

0 comments on commit bd87ffd

Please sign in to comment.