Backed out 4 changesets (bug 1848315) for causing browser_webconsole_csp_violation CLOSED TREE

Backed out changeset 7ef3e9f17105 (bug 1848315)
Backed out changeset 722163aa402f (bug 1848315)
Backed out changeset 2e823f3a6f99 (bug 1848315)
Backed out changeset 17f05d5f385d (bug 1848315)
This commit is contained in:
Sandor Molnar 2024-02-28 16:49:51 +02:00
parent 6fe1bb3a63
commit 66e9f2f79e
9 changed files with 135 additions and 391 deletions

View file

@ -13,18 +13,11 @@ const TEST_URI =
const TEST_VIOLATIONS =
"https://example.com/browser/devtools/client/webconsole/" +
"test/browser/test-csp-many-errors.html";
const bundle = Services.strings.createBundle(
"chrome://global/locale/security/csp.properties"
);
const CSP_VIOLATION_MSG = bundle.formatStringFromName(
"CSPInlineStyleViolation",
["style-src 'none'", "style-src-attr"]
);
const CSP_TOO_MANY_REPORTS_MSG = bundle.formatStringFromName(
"tooManyReports",
[]
);
const CSP_VIOLATION_MSG =
"Content-Security-Policy: The page\u2019s settings blocked the loading of a resource " +
"at inline (\u201cstyle-src\u201d).";
const CSP_TOO_MANY_REPORTS_MSG =
"Content-Security-Policy: Prevented too many CSP reports from being sent within a short period of time.";
add_task(async function () {
// Reduce the limit to reduce the log spam.

View file

@ -3,94 +3,20 @@
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
# CSP Warnings:
# LOCALIZATION NOTE (CSPInlineStyleViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. style-src-elem)
CSPInlineStyleViolation = The pages settings blocked an inline style (%2$S) from being applied because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROInlineStyleViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. style-src-elem)
CSPROInlineStyleViolation = (Report-Only policy) The pages settings would block an inline style (%2$S) from being applied because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPInlineScriptViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src-elem)
CSPInlineScriptViolation = The pages settings blocked an inline script (%2$S) from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROInlineScriptViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src-elem)
CSPROInlineScriptViolation = (Report-Only policy) The pages settings would block an inline script (%2$S) from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPEventHandlerScriptViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src-attr)
CSPEventHandlerScriptViolation = The pages settings blocked an event handler (%2$S) from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROEventHandlerScriptViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src-attr)
CSPROEventHandlerScriptViolation = (Report-Only policy) The pages settings would block an event handler (%2$S) from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPEvalScriptViolation):
# Don't translate 'unsafe-eval'.
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src)
CSPEvalScriptViolation = The pages settings blocked a JavaScript eval (%2$S) from being executed because it violates the following directive: “%1$S” (Missing 'unsafe-eval')
# LOCALIZATION NOTE (CSPROEvalScriptViolation):
# Don't translate 'unsafe-eval'.
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src)
CSPROEvalScriptViolation = (Report-Only policy) The pages settings would block a JavaScript eval (%2$S) from being executed because it violates the following directive: “%1$S” (Missing 'unsafe-eval')
# LOCALIZATION NOTE (CSPWasmEvalScriptViolation):
# WebAssembly is a feature name.
# Don't translate 'wasm-unsafe-eval' or 'unsafe-eval'.
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src)
CSPWasmEvalScriptViolation = The pages settings blocked WebAssembly (%2$S) from being executed because it violates the following directive: “%1$S” (Missing 'wasm-unsafe-eval' or 'unsafe-eval')
# LOCALIZATION NOTE (CSPROWasmEvalScriptViolation):
# WebAssembly is a feature name.
# Don't translate 'wasm-unsafe-eval' or 'unsafe-eval'.
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the type of directive used by the resource (e.g. script-src)
CSPROWasmEvalScriptViolation = (Report-Only policy) The pages settings would block WebAssembly (%2$S) from being executed because it violates the following directive: “%1$S” (Missing 'wasm-unsafe-eval' or 'unsafe-eval')
# LOCALIZATION NOTE (CSPStyleViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# LOCALIZATION NOTE (CSPViolation):
# %1$S is the reason why the resource has not been loaded.
CSPViolation = The pages settings blocked the loading of a resource: %1$S
# LOCALIZATION NOTE (CSPViolationWithURI):
# %1$S is the directive that has been violated.
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. style-src)
CSPStyleViolation = The pages settings blocked a style (%3$S) at %2$S from being applied because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROStyleViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
CSPViolationWithURI = The pages settings blocked the loading of a resource at %2$S (“%1$S”).
# LOCALIZATION NOTE (CSPROViolation):
# %1$S is the reason why the resource has not been loaded.
CSPROViolation = A violation occurred for a report-only CSP policy (“%1$S”). The behavior was allowed, and a CSP report was sent.
# LOCALIZATION NOTE (CSPROViolationWithURI):
# %1$S is the directive that has been violated.
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. style-src)
CSPROStyleViolation = (Report-Only policy) The pages settings would block a style (%3$S) at %2$S from being applied because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPScriptViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. script-src-elem)
CSPScriptViolation = The pages settings blocked a script (%3$S) at %2$S from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROScriptViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. script-src-elem)
CSPROScriptViolation = (Report-Only policy) The pages settings would block a script (%3$S) at %2$S from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPWorkerViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. worker-src)
CSPWorkerViolation = The pages settings blocked a worker script (%3$S) at %2$S from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROWorkerViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. worker-src)
CSPROWorkerViolation = (Report-Only policy) The pages settings would block a worker script (%3$S) at %2$S from being executed because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPGenericViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. image-src)
CSPGenericViolation = The pages settings blocked the loading of a resource (%3$S) at %2$S because it violates the following directive: “%1$S”
# LOCALIZATION NOTE (CSPROGenericViolation):
# %1$S is the entire directive that has been violated. (e.g. "default-src 'none'")
# %2$S is the URI of the resource which violated the directive.
# %3$S is the type of directive used by the resource (e.g. image-src)
CSPROGenericViolation = (Report-Only policy) The pages settings would block the loading of a resource (%3$S) at %2$S because it violates the following directive: “%1$S”
CSPROViolationWithURI = The pages settings observed the loading of a resource at %2$S (“%1$S”). A CSP report is being sent.
# LOCALIZATION NOTE (triedToSendReport):
# %1$S is the URI we attempted to send a report to.
triedToSendReport = Tried to send report to invalid URI: “%1$S”

View file

@ -189,11 +189,10 @@ bool nsCSPContext::permitsInternal(
bool permits = true;
nsAutoString violatedDirective;
nsAutoString violatedDirectiveString;
for (uint32_t p = 0; p < mPolicies.Length(); p++) {
if (!mPolicies[p]->permits(aDir, aLoadInfo, aContentLocation,
!!aOriginalURIIfRedirect, aSpecific,
violatedDirective, violatedDirectiveString)) {
violatedDirective)) {
// If the policy is violated and not report-only, reject the load and
// report to the console
if (!mPolicies[p]->getReportOnlyFlag()) {
@ -201,6 +200,12 @@ bool nsCSPContext::permitsInternal(
permits = false;
}
// In CSP 3.0 the effective directive doesn't become the actually used
// directive in case of a fallback from e.g. script-src-elem to
// script-src or default-src.
nsAutoString effectiveDirective;
effectiveDirective.AssignASCII(CSP_CSPDirectiveToString(aDir));
// Callers should set |aSendViolationReports| to false if this is a
// preload - the decision may be wrong due to the inability to get the
// nonce, and will incorrectly fail the unit tests.
@ -221,11 +226,9 @@ bool nsCSPContext::permitsInternal(
BlockedContentSource::eUnknown, /* a BlockedContentSource */
aOriginalURIIfRedirect, /* in case of redirect originalURI is not
null */
violatedDirective, violatedDirectiveString,
aDir, // aViolatedDirective
p, // policy index
u""_ns, // no observer subject
spec, // source file
violatedDirective, effectiveDirective, p, /* policy index */
u""_ns, /* no observer subject */
spec, /* source file */
false, // aReportSample (no sample)
u""_ns, /* no script sample */
lineNumber, /* line number */
@ -516,8 +519,7 @@ void nsCSPContext::reportInlineViolation(
CSPDirective aDirective, Element* aTriggeringElement,
nsICSPEventListener* aCSPEventListener, const nsAString& aNonce,
bool aReportSample, const nsAString& aSample,
const nsAString& aViolatedDirective,
const nsAString& aViolatedDirectiveString, CSPDirective aEffectiveDirective,
const nsAString& aViolatedDirective, const nsAString& aEffectiveDirective,
uint32_t aViolatedPolicyIndex, // TODO, use report only flag for that
uint32_t aLineNumber, uint32_t aColumnNumber) {
nsString observerSubject;
@ -562,15 +564,14 @@ void nsCSPContext::reportInlineViolation(
BlockedContentSource::eInline, // aBlockedSource
mSelfURI, // aOriginalURI
aViolatedDirective, // aViolatedDirective
aViolatedDirectiveString,
aEffectiveDirective, // aEffectiveDirective
aViolatedPolicyIndex, // aViolatedPolicyIndex
observerSubject, // aObserverSubject
sourceFile, // aSourceFile
aReportSample, // aReportSample
aSample, // aScriptSample
lineNumber, // aLineNum
columnNumber); // aColumnNum
aEffectiveDirective, // aEffectiveDirective
aViolatedPolicyIndex, // aViolatedPolicyIndex
observerSubject, // aObserverSubject
sourceFile, // aSourceFile
aReportSample, // aReportSample
aSample, // aScriptSample
lineNumber, // aLineNum
columnNumber); // aColumnNum
}
NS_IMETHODIMP
@ -671,16 +672,19 @@ nsCSPContext::GetAllowsInline(CSPDirective aDirective, bool aHasUnsafeHash,
*outAllowsInline = false;
}
nsAutoString violatedDirective;
nsAutoString violatedDirectiveString;
bool reportSample = false;
mPolicies[i]->getViolatedDirectiveInformation(
aDirective, violatedDirective, violatedDirectiveString,
&reportSample);
mPolicies[i]->getDirectiveStringAndReportSampleForContentType(
aDirective, violatedDirective, &reportSample);
// In CSP 3.0 the effective directive doesn't become the actually used
// directive in case of a fallback from e.g. script-src-elem to
// script-src or default-src.
nsAutoString effectiveDirective;
effectiveDirective.AssignASCII(CSP_CSPDirectiveToString(aDirective));
reportInlineViolation(aDirective, aTriggeringElement, aCSPEventListener,
aNonce, reportSample, content, violatedDirective,
violatedDirectiveString, aDirective, i, aLineNumber,
aColumnNumber);
effectiveDirective, i, aLineNumber, aColumnNumber);
}
}
@ -743,18 +747,15 @@ nsCSPContext::LogViolationDetails(
}
nsAutoString violatedDirective;
nsAutoString violatedDirectiveString;
bool reportSample = false;
mPolicies[p]->getViolatedDirectiveInformation(
SCRIPT_SRC_DIRECTIVE, violatedDirective, violatedDirectiveString,
&reportSample);
mPolicies[p]->getDirectiveStringAndReportSampleForContentType(
SCRIPT_SRC_DIRECTIVE, violatedDirective, &reportSample);
AsyncReportViolation(
aTriggeringElement, aCSPEventListener, nullptr, blockedContentSource,
nullptr, violatedDirective, violatedDirectiveString,
CSPDirective::SCRIPT_SRC_DIRECTIVE /* aEffectiveDirective */, p,
observerSubject, aSourceFile, reportSample, aScriptSample, aLineNum,
aColumnNum);
AsyncReportViolation(aTriggeringElement, aCSPEventListener, nullptr,
blockedContentSource, nullptr, violatedDirective,
u"script-src"_ns /* aEffectiveDirective */, p,
observerSubject, aSourceFile, reportSample,
aScriptSample, aLineNum, aColumnNum);
}
return NS_OK;
}
@ -1374,12 +1375,10 @@ class CSPReportSenderRunnable final : public Runnable {
nsIURI* aBlockedURI,
nsCSPContext::BlockedContentSource aBlockedContentSource,
nsIURI* aOriginalURI, uint32_t aViolatedPolicyIndex, bool aReportOnlyFlag,
const nsAString& aViolatedDirective,
const nsAString& aViolatedDirectiveString,
const CSPDirective aEffectiveDirective, const nsAString& aObserverSubject,
const nsAString& aSourceFile, bool aReportSample,
const nsAString& aScriptSample, uint32_t aLineNum, uint32_t aColumnNum,
nsCSPContext* aCSPContext)
const nsAString& aViolatedDirective, const nsAString& aEffectiveDirective,
const nsAString& aObserverSubject, const nsAString& aSourceFile,
bool aReportSample, const nsAString& aScriptSample, uint32_t aLineNum,
uint32_t aColumnNum, nsCSPContext* aCSPContext)
: mozilla::Runnable("CSPReportSenderRunnable"),
mTriggeringElement(aTriggeringElement),
mCSPEventListener(aCSPEventListener),
@ -1390,7 +1389,6 @@ class CSPReportSenderRunnable final : public Runnable {
mReportOnlyFlag(aReportOnlyFlag),
mReportSample(aReportSample),
mViolatedDirective(aViolatedDirective),
mViolatedDirectiveString(aViolatedDirectiveString),
mEffectiveDirective(aEffectiveDirective),
mSourceFile(aSourceFile),
mScriptSample(aScriptSample),
@ -1438,18 +1436,16 @@ class CSPReportSenderRunnable final : public Runnable {
NS_IMETHOD Run() override {
MOZ_ASSERT(NS_IsMainThread());
nsresult rv;
// 0) prepare violation data
mozilla::dom::SecurityPolicyViolationEventInit init;
nsAutoCString blockedContentSource;
BlockedContentSourceToString(mBlockedContentSource, blockedContentSource);
nsAutoString effectiveDirective;
effectiveDirective.AssignASCII(
CSP_CSPDirectiveToString(mEffectiveDirective));
nsresult rv = mCSPContext->GatherSecurityPolicyViolationEventData(
mBlockedURI, blockedContentSource, mOriginalURI, effectiveDirective,
rv = mCSPContext->GatherSecurityPolicyViolationEventData(
mBlockedURI, blockedContentSource, mOriginalURI, mEffectiveDirective,
mViolatedPolicyIndex, mSourceFile,
mReportSample ? mScriptSample : EmptyString(), mLineNum, mColumnNum,
init);
@ -1468,7 +1464,32 @@ class CSPReportSenderRunnable final : public Runnable {
mCSPContext->SendReports(init, mViolatedPolicyIndex);
// 3) log to console (one per policy violation)
ReportToConsole();
if (mBlockedURI) {
mBlockedURI->GetSpec(blockedContentSource);
if (blockedContentSource.Length() >
nsCSPContext::ScriptSampleMaxLength()) {
bool isData = mBlockedURI->SchemeIs("data");
if (NS_SUCCEEDED(rv) && isData &&
blockedContentSource.Length() >
nsCSPContext::ScriptSampleMaxLength()) {
blockedContentSource.Truncate(nsCSPContext::ScriptSampleMaxLength());
blockedContentSource.Append(
NS_ConvertUTF16toUTF8(nsContentUtils::GetLocalizedEllipsis()));
}
}
}
if (blockedContentSource.Length() > 0) {
nsString blockedContentSource16 =
NS_ConvertUTF8toUTF16(blockedContentSource);
AutoTArray<nsString, 2> params = {mViolatedDirective,
blockedContentSource16};
mCSPContext->logToConsole(
mReportOnlyFlag ? "CSPROViolationWithURI" : "CSPViolationWithURI",
params, mSourceFile, mScriptSample, mLineNum, mColumnNum,
nsIScriptError::errorFlag);
}
// 4) fire violation event
// A frame-ancestors violation has occurred, but we should not dispatch
@ -1482,104 +1503,6 @@ class CSPReportSenderRunnable final : public Runnable {
}
private:
void ReportToConsole() const {
NS_ConvertUTF8toUTF16 effectiveDirective(
CSP_CSPDirectiveToString(mEffectiveDirective));
switch (mBlockedContentSource) {
case nsCSPContext::BlockedContentSource::eInline: {
const char* errorName = nullptr;
if (mEffectiveDirective == CSPDirective::STYLE_SRC_ATTR_DIRECTIVE ||
mEffectiveDirective == CSPDirective::STYLE_SRC_ELEM_DIRECTIVE) {
errorName = mReportOnlyFlag ? "CSPROInlineStyleViolation"
: "CSPInlineStyleViolation";
} else if (mEffectiveDirective ==
CSPDirective::SCRIPT_SRC_ATTR_DIRECTIVE) {
errorName = mReportOnlyFlag ? "CSPROEventHandlerScriptViolation"
: "CSPEventHandlerScriptViolation";
} else {
MOZ_ASSERT(mEffectiveDirective ==
CSPDirective::SCRIPT_SRC_ELEM_DIRECTIVE);
errorName = mReportOnlyFlag ? "CSPROInlineScriptViolation"
: "CSPInlineScriptViolation";
}
AutoTArray<nsString, 2> params = {mViolatedDirectiveString,
effectiveDirective};
mCSPContext->logToConsole(errorName, params, mSourceFile, mScriptSample,
mLineNum, mColumnNum,
nsIScriptError::errorFlag);
break;
}
case nsCSPContext::BlockedContentSource::eEval: {
AutoTArray<nsString, 2> params = {mViolatedDirectiveString,
effectiveDirective};
mCSPContext->logToConsole(mReportOnlyFlag ? "CSPROEvalScriptViolation"
: "CSPEvalScriptViolation",
params, mSourceFile, mScriptSample, mLineNum,
mColumnNum, nsIScriptError::errorFlag);
break;
}
case nsCSPContext::BlockedContentSource::eWasmEval: {
AutoTArray<nsString, 2> params = {mViolatedDirectiveString,
effectiveDirective};
mCSPContext->logToConsole(mReportOnlyFlag
? "CSPROWasmEvalScriptViolation"
: "CSPWasmEvalScriptViolation",
params, mSourceFile, mScriptSample, mLineNum,
mColumnNum, nsIScriptError::errorFlag);
break;
}
case nsCSPContext::BlockedContentSource::eSelf:
case nsCSPContext::BlockedContentSource::eUnknown: {
nsAutoString source(u"<unknown>"_ns);
if (mBlockedURI) {
nsAutoCString uri;
mBlockedURI->GetSpec(uri);
if (mBlockedURI->SchemeIs("data") &&
uri.Length() > nsCSPContext::ScriptSampleMaxLength()) {
uri.Truncate(nsCSPContext::ScriptSampleMaxLength());
uri.Append(
NS_ConvertUTF16toUTF8(nsContentUtils::GetLocalizedEllipsis()));
}
if (!uri.IsEmpty()) {
CopyUTF8toUTF16(uri, source);
}
}
const char* errorName = nullptr;
switch (mEffectiveDirective) {
case CSPDirective::STYLE_SRC_ELEM_DIRECTIVE:
errorName =
mReportOnlyFlag ? "CSPROStyleViolation" : "CSPStyleViolation";
break;
case CSPDirective::SCRIPT_SRC_ELEM_DIRECTIVE:
errorName =
mReportOnlyFlag ? "CSPROScriptViolation" : "CSPScriptViolation";
break;
case CSPDirective::WORKER_SRC_DIRECTIVE:
errorName =
mReportOnlyFlag ? "CSPROWorkerViolation" : "CSPWorkerViolation";
break;
default:
errorName = mReportOnlyFlag ? "CSPROGenericViolation"
: "CSPGenericViolation";
}
AutoTArray<nsString, 3> params = {mViolatedDirectiveString, source,
effectiveDirective};
mCSPContext->logToConsole(errorName, params, mSourceFile, mScriptSample,
mLineNum, mColumnNum,
nsIScriptError::errorFlag);
}
}
}
RefPtr<Element> mTriggeringElement;
nsCOMPtr<nsICSPEventListener> mCSPEventListener;
nsCOMPtr<nsIURI> mBlockedURI;
@ -1589,8 +1512,7 @@ class CSPReportSenderRunnable final : public Runnable {
bool mReportOnlyFlag;
bool mReportSample;
nsString mViolatedDirective;
nsString mViolatedDirectiveString;
CSPDirective mEffectiveDirective;
nsString mEffectiveDirective;
nsCOMPtr<nsISupports> mObserverSubject;
nsString mSourceFile;
nsString mScriptSample;
@ -1632,8 +1554,7 @@ nsresult nsCSPContext::AsyncReportViolation(
Element* aTriggeringElement, nsICSPEventListener* aCSPEventListener,
nsIURI* aBlockedURI, BlockedContentSource aBlockedContentSource,
nsIURI* aOriginalURI, const nsAString& aViolatedDirective,
const nsAString& aViolatedDirectiveString,
const CSPDirective aEffectiveDirective, uint32_t aViolatedPolicyIndex,
const nsAString& aEffectiveDirective, uint32_t aViolatedPolicyIndex,
const nsAString& aObserverSubject, const nsAString& aSourceFile,
bool aReportSample, const nsAString& aScriptSample, uint32_t aLineNum,
uint32_t aColumnNum) {
@ -1644,8 +1565,8 @@ nsresult nsCSPContext::AsyncReportViolation(
aTriggeringElement, aCSPEventListener, aBlockedURI, aBlockedContentSource,
aOriginalURI, aViolatedPolicyIndex,
mPolicies[aViolatedPolicyIndex]->getReportOnlyFlag(), aViolatedDirective,
aViolatedDirectiveString, aEffectiveDirective, aObserverSubject,
aSourceFile, aReportSample, aScriptSample, aLineNum, aColumnNum, this);
aEffectiveDirective, aObserverSubject, aSourceFile, aReportSample,
aScriptSample, aLineNum, aColumnNum, this);
if (XRE_IsContentProcess()) {
if (mEventTarget) {

View file

@ -126,12 +126,10 @@ class nsCSPContext : public nsIContentSecurityPolicy {
mozilla::dom::Element* aTriggeringElement,
nsICSPEventListener* aCSPEventListener, nsIURI* aBlockedURI,
BlockedContentSource aBlockedContentSource, nsIURI* aOriginalURI,
const nsAString& aViolatedDirective,
const nsAString& aViolatedDirectiveString,
const CSPDirective aEffectiveDirective, uint32_t aViolatedPolicyIndex,
const nsAString& aObserverSubject, const nsAString& aSourceFile,
bool aReportSample, const nsAString& aScriptSample, uint32_t aLineNum,
uint32_t aColumnNum);
const nsAString& aViolatedDirective, const nsAString& aEffectiveDirective,
uint32_t aViolatedPolicyIndex, const nsAString& aObserverSubject,
const nsAString& aSourceFile, bool aReportSample,
const nsAString& aScriptSample, uint32_t aLineNum, uint32_t aColumnNum);
// Hands off! Don't call this method unless you know what you
// are doing. It's only supposed to be called from within
@ -170,8 +168,7 @@ class nsCSPContext : public nsIContentSecurityPolicy {
const nsAString& aNonce, bool aReportSample,
const nsAString& aSample,
const nsAString& aViolatedDirective,
const nsAString& aViolatedDirectiveString,
CSPDirective aEffectiveDirective,
const nsAString& aEffectiveDirective,
uint32_t aViolatedPolicyIndex,
uint32_t aLineNumber, uint32_t aColumnNumber);

View file

@ -1569,8 +1569,7 @@ nsCSPPolicy::~nsCSPPolicy() {
bool nsCSPPolicy::permits(CSPDirective aDir, nsILoadInfo* aLoadInfo,
nsIURI* aUri, bool aWasRedirected, bool aSpecific,
nsAString& outViolatedDirective,
nsAString& outViolatedDirectiveString) const {
nsAString& outViolatedDirective) const {
if (CSPUTILSLOGENABLED()) {
CSPUTILSLOG(("nsCSPPolicy::permits, aUri: %s, aDir: %s, aSpecific: %s",
aUri->GetSpecOrDefault().get(), CSP_CSPDirectiveToString(aDir),
@ -1590,7 +1589,6 @@ bool nsCSPPolicy::permits(CSPDirective aDir, nsILoadInfo* aLoadInfo,
if (!mDirectives[i]->permits(aDir, aLoadInfo, aUri, aWasRedirected,
mReportOnly, mUpgradeInsecDir)) {
mDirectives[i]->getDirName(outViolatedDirective);
mDirectives[i]->toString(outViolatedDirectiveString);
return false;
}
return true;
@ -1606,7 +1604,6 @@ bool nsCSPPolicy::permits(CSPDirective aDir, nsILoadInfo* aLoadInfo,
if (!defaultDir->permits(aDir, aLoadInfo, aUri, aWasRedirected, mReportOnly,
mUpgradeInsecDir)) {
defaultDir->getDirName(outViolatedDirective);
defaultDir->toString(outViolatedDirectiveString);
return false;
}
return true;
@ -1695,22 +1692,43 @@ bool nsCSPPolicy::allowsAllInlineBehavior(CSPDirective aDir) const {
* The parameter outDirective is the equivalent of 'outViolatedDirective'
* for the ::permits() function family.
*/
void nsCSPPolicy::getViolatedDirectiveInformation(CSPDirective aDirective,
nsAString& outDirective,
nsAString& outDirectiveString,
bool* aReportSample) const {
void nsCSPPolicy::getDirectiveStringAndReportSampleForContentType(
CSPDirective aDirective, nsAString& outDirective,
bool* aReportSample) const {
MOZ_ASSERT(aReportSample);
*aReportSample = false;
nsCSPDirective* directive = matchingOrDefaultDirective(aDirective);
if (!directive) {
MOZ_ASSERT_UNREACHABLE("Can not query violated directive");
outDirective.AppendLiteral("couldNotQueryViolatedDirective");
outDirective.Truncate();
nsCSPDirective* defaultDir = nullptr;
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
if (mDirectives[i]->isDefaultDirective()) {
defaultDir = mDirectives[i];
continue;
}
if (mDirectives[i]->equals(aDirective)) {
mDirectives[i]->getDirName(outDirective);
*aReportSample = mDirectives[i]->hasReportSampleKeyword();
return;
}
}
// if we haven't found a matching directive yet,
// the contentType must be restricted by the default directive
if (defaultDir) {
defaultDir->getDirName(outDirective);
*aReportSample = defaultDir->hasReportSampleKeyword();
return;
}
NS_ASSERTION(false, "Can not query directive string for contentType!");
outDirective.AppendLiteral("couldNotQueryViolatedDirective");
}
directive->getDirName(outDirective);
directive->toString(outDirectiveString);
*aReportSample = directive->hasReportSampleKeyword();
void nsCSPPolicy::getDirectiveAsString(CSPDirective aDir,
nsAString& outDirective) const {
for (uint32_t i = 0; i < mDirectives.Length(); i++) {
if (mDirectives[i]->equals(aDir)) {
mDirectives[i]->toString(outDirective);
return;
}
}
}
/*

View file

@ -619,8 +619,7 @@ class nsCSPPolicy {
bool permits(CSPDirective aDirective, nsILoadInfo* aLoadInfo, nsIURI* aUri,
bool aWasRedirected, bool aSpecific,
nsAString& outViolatedDirective,
nsAString& outViolatedDirectiveString) const;
nsAString& outViolatedDirective) const;
bool allows(CSPDirective aDirective, enum CSPKeyword aKeyword,
const nsAString& aHashOrNonce) const;
void toString(nsAString& outStr) const;
@ -651,10 +650,11 @@ class nsCSPPolicy {
void getReportURIs(nsTArray<nsString>& outReportURIs) const;
void getViolatedDirectiveInformation(CSPDirective aDirective,
nsAString& outDirective,
nsAString& outDirectiveString,
bool* aReportSample) const;
void getDirectiveStringAndReportSampleForContentType(
CSPDirective aDirective, nsAString& outDirective,
bool* aReportSample) const;
void getDirectiveAsString(CSPDirective aDir, nsAString& outDirective) const;
uint32_t getSandboxFlags() const;

View file

@ -1,33 +0,0 @@
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="Content-Security-Policy" content="default-src 'nonce-abc';">
<title></title>
</head>
<!-- event handler -->
<body onload="alert('onload');">
<!-- img-src -->
<img src="image.png">
<!-- external script -->
<script src=script.js></script>
<!-- inline script -->
<script>
alert("failure");
</script>
<script nonce="abc">
/* worker-src */
new Worker("/worker.js")
</script>
<script nonce="abc">
// eslint-disable-next-line no-eval
eval("hello world");
</script>
</body>
</html>

View file

@ -433,9 +433,6 @@ skip-if = [
["test_connect-src.html"]
["test_csp_error_messages.html"]
support-files = ["file_csp_error_messages.html"]
["test_csp_frame_ancestors_about_blank.html"]
support-files = [
"file_csp_frame_ancestors_about_blank.html",

View file

@ -1,75 +0,0 @@
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Test some specialized CSP errors</title>
<script src="/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<iframe id="cspframe"></iframe>
<script class="testbody" type="text/javascript">
SimpleTest.waitForExplicitFinish();
function cleanup() {
SpecialPowers.postConsoleSentinel();
SimpleTest.finish();
};
let errors = [];
function add(name) {
ok(!errors.includes(name), `duplicate error for ${name}`);
errors.push(name);
}
SpecialPowers.registerConsoleListener(msg => {
if (!msg.errorMessage) {
return;
}
let {errorMessage} = msg;
function contains(str) {
ok(errorMessage.includes(str), `error message contains "${str}"`);
}
if (errorMessage.includes("(script-src-attr)")) {
contains("blocked an event handler");
contains("from being executed");
contains("Source: alert('onload');");
add("event handler");
} else if (errorMessage.includes("(img-src)")) {
contains("blocked the loading of a resource");
contains("/image.png");
add("image");
} else if (errorMessage.includes("an inline script")) {
contains("(script-src-elem)");
contains("from being executed");
add("inline script");
} else if (errorMessage.includes("a script")) {
contains("(script-src-elem)");
contains("from being executed");
contains("/script.js");
add("script");
} else if (errorMessage.includes("(worker-src)")) {
contains("(worker-src)");
contains("from being executed");
contains("/worker.js");
add("worker");
} else if (errorMessage.includes("a JavaScript eval")) {
contains("(script-src)");
contains("from being executed");
contains("Missing 'unsafe-eval'")
add("eval");
}
if (errors.length == 6) {
SimpleTest.executeSoon(cleanup);
}
});
document.getElementById('cspframe').src = 'file_csp_error_messages.html';
</script>
</body>
</html>