Issue #2402 - Print Related JS-Line on CSP Violation (if any). https://bugzilla.mozilla.org/show_bug.cgi?id=1100630

This commit is contained in:
Brian Smith 2024-01-08 08:49:05 -06:00 • committed by roytam1
commit e21d466d13

View file

@ -264,16 +264,25 @@ nsCSPContext::permitsInternal(CSPDirective aDir,
// decision may be wrong due to the inability to get the nonce, and will // decision may be wrong due to the inability to get the nonce, and will
// incorrectly fail the unit tests. // incorrectly fail the unit tests.
if (!aIsPreload && aSendViolationReports) { if (!aIsPreload && aSendViolationReports) {
uint32_t lineNumber = 0;
uint32_t columnNumber = 0;
nsAutoCString spec;
JSContext* cx = nsContentUtils::GetCurrentJSContext();
if (cx) {
nsJSUtils::GetCallingLocation(cx, spec, &lineNumber, &columnNumber);
// If GetCallingLocation fails linenumber & columnNumber are set to 0
// anyway so we can skip checking if that is the case.
}
this->AsyncReportViolation((aSendContentLocationInViolationReports ? this->AsyncReportViolation((aSendContentLocationInViolationReports ?
aContentLocation : nullptr), aContentLocation : nullptr),
aOriginalURI, /* in case of redirect originalURI is not null */ aOriginalURI, /* in case of redirect originalURI is not null */
violatedDirective, violatedDirective,
p, /* policy index */ p, /* policy index */
EmptyString(), /* no observer subject */ EmptyString(), /* no observer subject */
EmptyString(), /* no source file */ NS_ConvertUTF8toUTF16(spec), /* source file. */
EmptyString(), /* no script sample */ EmptyString(), /* no script sample */
0, /* no line number */ lineNumber, /* line number */
0); /* no column number */ columnNumber); /* column number */
} }
} }
} }
@ -294,6 +303,8 @@ NS_IMPL_ISUPPORTS_CI(nsCSPContext,
nsIContentSecurityPolicy, nsIContentSecurityPolicy,
nsISerializable) nsISerializable)
int32_t nsCSPContext::sScriptSampleMaxLength;
nsCSPContext::nsCSPContext() nsCSPContext::nsCSPContext()
: mInnerWindowID(0) : mInnerWindowID(0)
, mLoadingContext(nullptr) , mLoadingContext(nullptr)
@ -516,6 +527,22 @@ nsCSPContext::reportInlineViolation(nsContentPolicyType aContentType,
codeSample.Truncate(ScriptSampleMaxLength()); codeSample.Truncate(ScriptSampleMaxLength());
codeSample.AppendLiteral("..."); codeSample.AppendLiteral("...");
} }
uint32_t lineNumber = aLineNumber;
uint32_t columnNumber = aColumnNumber;
JSContext* cx = nsContentUtils::GetCurrentJSContext();
if (cx) {
if (!nsJSUtils::GetCallingLocation(cx, sourceFile, &lineNumber,
&columnNumber)) {
// Get Calling Location resets line/col to 0
// so we reset those to the intial arguments
// in case it failed
lineNumber = aLineNumber;
columnNumber = aColumnNumber;
}
}
AsyncReportViolation(selfISupports, // aBlockedContentSource AsyncReportViolation(selfISupports, // aBlockedContentSource
mSelfURI, // aOriginalURI mSelfURI, // aOriginalURI
aViolatedDirective, // aViolatedDirective aViolatedDirective, // aViolatedDirective
@ -523,8 +550,8 @@ nsCSPContext::reportInlineViolation(nsContentPolicyType aContentType,
observerSubject, // aObserverSubject observerSubject, // aObserverSubject
NS_ConvertUTF8toUTF16(sourceFile), // aSourceFile NS_ConvertUTF8toUTF16(sourceFile), // aSourceFile
codeSample, // aScriptSample codeSample, // aScriptSample
aLineNumber, // aLineNum lineNumber, // aLineNum
aColumnNumber); // aColumnNum columnNumber); // aColumnNum
} }
NS_IMETHODIMP NS_IMETHODIMP