Bug 1384741 - Part 1: Add facility to buffer up CSP violation reports. r=bz

MozReview-Commit-ID: G4JLTmP1wD7
This commit is contained in:
Cameron McCormack 2017-08-07 10:09:32 +08:00
Родитель 0d870b1b90
Коммит fdf6f9c5ef
3 изменённых файлов: 65 добавлений и 0 удалений

Просмотреть файл

@ -1354,6 +1354,7 @@ nsIDocument::nsIDocument()
mIsContentDocument(false),
mMightHaveStaleServoData(false),
mDidCallBeginLoad(false),
mBufferingCSPViolations(false),
mIsScopedStyleEnabled(eScopedStyle_Unknown),
mCompatMode(eCompatibility_FullStandards),
mReadyState(ReadyState::READYSTATE_UNINITIALIZED),

Просмотреть файл

@ -835,6 +835,54 @@ public:
mSandboxFlags = sandboxFlags;
}
/**
* After calling this function, any CSP violation reports will be buffered up
* by the document (by calling BufferCSPViolation) instead of being sent
* immediately.
*
* This facility is used by the user font cache, which wants to pre-emptively
* check whether a given font load would violate CSP directives, and so
* shouldn't immediately send the report.
*/
void StartBufferingCSPViolations()
{
MOZ_ASSERT(!mBufferingCSPViolations);
mBufferingCSPViolations = true;
}
/**
* Stops buffering CSP violation reports, and stores any buffered reports in
* aResult.
*/
void StopBufferingCSPViolations(nsTArray<nsCOMPtr<nsIRunnable>>& aResult)
{
MOZ_ASSERT(mBufferingCSPViolations);
mBufferingCSPViolations = false;
aResult.SwapElements(mBufferedCSPViolations);
mBufferedCSPViolations.Clear();
}
/**
* Returns whether we are currently buffering CSP violation reports.
*/
bool ShouldBufferCSPViolations() const
{
return mBufferingCSPViolations;
}
/**
* Called when a CSP violation is encountered that would generate a report
* while buffering is enabled.
*/
void BufferCSPViolation(nsIRunnable* aReportingRunnable)
{
MOZ_ASSERT(mBufferingCSPViolations);
// Dropping the CSP violation report seems preferable to OOMing.
mBufferedCSPViolations.AppendElement(aReportingRunnable, mozilla::fallible);
}
/**
* Access HTTP header data (this may also get set from other
* sources, like HTML META tags).
@ -3309,6 +3357,10 @@ protected:
// True if we have called BeginLoad and are expecting a paired EndLoad call.
bool mDidCallBeginLoad : 1;
// True if any CSP violation reports for this doucment will be buffered in
// mBufferedCSPViolations instead of being sent immediately.
bool mBufferingCSPViolations : 1;
// Whether <style scoped> support is enabled in this document.
enum { eScopedStyle_Unknown, eScopedStyle_Disabled, eScopedStyle_Enabled };
unsigned int mIsScopedStyleEnabled : 2;
@ -3489,6 +3541,10 @@ protected:
// calling NoteScriptTrackingStatus(). Currently we assume that a URL not
// existing in the set means the corresponding script isn't a tracking script.
nsTHashtable<nsCStringHashKey> mTrackingScripts;
// CSP violation reports that have been buffered up due to a call to
// StartBufferingCSPViolations.
nsTArray<nsCOMPtr<nsIRunnable>> mBufferedCSPViolations;
};
NS_DEFINE_STATIC_IID_ACCESSOR(nsIDocument, NS_IDOCUMENT_IID)

Просмотреть файл

@ -1228,6 +1228,14 @@ nsCSPContext::AsyncReportViolation(nsISupports* aBlockedContentSource,
aLineNum,
this);
// If the document is currently buffering up CSP violation reports, send the
// runnable to it instead of dispatching it immediately.
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mLoadingContext);
if (doc && doc->ShouldBufferCSPViolations()) {
doc->BufferCSPViolation(task);
return NS_OK;
}
if (XRE_IsContentProcess()) {
if (mEventTarget) {
mEventTarget->Dispatch(task.forget(), NS_DISPATCH_NORMAL);