Last active
August 22, 2024 14:40
-
-
Save navarr/bd9ca2db568d7dd35cc68f895ab04257 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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