Skip to content

Instantly share code, notes, and snippets.

@navarr
Last active August 22, 2024 14:40
Show Gist options
  • Save navarr/bd9ca2db568d7dd35cc68f895ab04257 to your computer and use it in GitHub Desktop.
Save navarr/bd9ca2db568d7dd35cc68f895ab04257 to your computer and use it in GitHub Desktop.
<?php
/*
* Place a ddhq.csv file in the same directory. This file is the export of CSP reports from DataDog
* Run this file
* This file will spit out the violated directive, site host, blocked host, blocked uri, and site uri
* However, it will limit it to one row per blocked host for brevity
*/
$ddhq = fopen('ddhq.csv', 'r');
$result = fopen('csp-report.csv', 'w');
fputcsv($result, ['Type', 'Host', 'Blocked Domain', 'Blocked URI', 'Host Domain', 'Source File']);
$loggedHosts = [];
while($row = fgetcsv($ddhq)) {
try {
$data = json_decode($row[3], true, 512, JSON_THROW_ON_ERROR);
} catch (Throwable) {
continue;
}
if (!isset($data['csp-report']['blocked-uri_details']['host'])) {
// No host - often chrome-extension
continue;
}
$host = $data['csp-report']['blocked-uri_details']['host'];
if (isset($loggedHosts[$host])) {
continue;
}
$loggedHosts[$host] = true;
fputcsv($result, [
$data['csp-report']['violated-directive'] ?? '',
$data['csp-report']['document-uri_details']['host'] ?? '',
$host ?? '',
$data['csp-report']['blocked-uri'] ?? '',
$data['csp-report']['document-uri'] ?? '',
$data['csp-report']['source-file'] ?? '',
]);
}
fclose($ddhq);
fclose($result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment