Skip to content

[WIP] OCPBUGS-44972: Loosen equivalence criteria for CSP events so that they are reported less frequently #14778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,23 @@ const isRecordExpired = ({ timestamp }: CSPViolationRecord): boolean => {
return timestamp && Date.now() - timestamp > CSP_VIOLATION_EXPIRATION;
};

const sameHostname = (a: string, b: string): boolean => {
const urlA = new URL(a);
const urlB = new URL(b);
return urlA.hostname === urlB.hostname;
};

// CSP violiation records are considered equal if the following properties match:
// - pluginName
// - effectiveDirective
// - sourceFile
// - blockedURI hostname
const cspViolationRecordsAreEqual = (a: CSPViolationRecord, b: CSPViolationRecord): boolean =>
a.pluginName === b.pluginName &&
a.effectiveDirective === b.effectiveDirective &&
a.sourceFile === b.sourceFile &&
sameHostname(a.blockedURI, b.blockedURI);

// Export for testing
export const newCSPViolationReport = (
pluginName: string,
Expand Down Expand Up @@ -74,12 +91,8 @@ export const useCSPViolationDetector = () => {
// update the timestamp. Otherwise, append the new record.
const [updatedRecords] = existingRecords.reduce(
([acc, hasBeenRecorded], existingRecord, i, all) => {
// Exclude originalPolicy and timestamp from equality comparison.
const { timestamp, originalPolicy, ...existingReport } = existingRecord;
const { timestamp: _t, originalPolicy: _o, ...newReport } = newRecord;

// Replace matching report with a newly timestamped record
if (_.isEqual(newReport, existingReport)) {
if (cspViolationRecordsAreEqual(newRecord, existingRecord)) {
return [[...acc, newRecord], true];
}

Expand Down