[3276] | 1 | <?php |
---|
| 2 | // From: https://github.com/nico3333fr/CSP-useful |
---|
| 3 | // |
---|
| 4 | // Note: this script requires PHP ≥ 5.4. |
---|
| 5 | // Inspired from https://mathiasbynens.be/notes/csp-reports |
---|
| 6 | |
---|
| 7 | // Dareboost wants it? Not a problem. |
---|
| 8 | header('X-Content-Type-Options: "nosniff"'); |
---|
| 9 | |
---|
[3324] | 10 | require dirname(__FILE__).'/../inc/admin/prepend.php'; |
---|
| 11 | |
---|
[3453] | 12 | // Specify admin CSP log file if necessary |
---|
| 13 | if (!defined('LOGFILE')) { |
---|
| 14 | define('LOGFILE',path::real(DC_VAR).'/csp/csp_report.json'); |
---|
| 15 | } |
---|
[3276] | 16 | |
---|
| 17 | // Get the raw POST data |
---|
| 18 | $data = file_get_contents('php://input'); |
---|
| 19 | |
---|
| 20 | // Only continue if it’s valid JSON that is not just `null`, `0`, `false` or an |
---|
| 21 | // empty string, i.e. if it could be a CSP violation report. |
---|
[3453] | 22 | if ($data = json_decode($data,true)) { |
---|
[3276] | 23 | |
---|
| 24 | // get source-file and blocked-URI to perform some tests |
---|
[3478] | 25 | $source_file = isset($data['csp-report']['source-file']) ? $data['csp-report']['source-file'] : ''; |
---|
| 26 | $blocked_uri = isset($data['csp-report']['blocked-uri']) ? $data['csp-report']['blocked-uri'] : ''; |
---|
[3276] | 27 | |
---|
| 28 | if ( |
---|
[3453] | 29 | // avoid false positives notifications coming from Chrome extensions (Wappalyzer, MuteTab, etc.) |
---|
| 30 | // bug here https://code.google.com/p/chromium/issues/detail?id=524356 |
---|
| 31 | strpos($source_file, 'chrome-extension://') === false |
---|
[3276] | 32 | |
---|
[3453] | 33 | // avoid false positives notifications coming from Safari extensions (diigo, evernote, etc.) |
---|
| 34 | && strpos($source_file, 'safari-extension://') === false |
---|
[3451] | 35 | && strpos($blocked_uri, 'safari-extension://') === false |
---|
[3276] | 36 | |
---|
[3453] | 37 | // search engine extensions ? |
---|
| 38 | && strpos($source_file, 'se-extension://') === false |
---|
[3276] | 39 | |
---|
[3453] | 40 | // added by browsers in webviews |
---|
| 41 | && strpos($blocked_uri, 'webviewprogressproxy://') === false |
---|
[3276] | 42 | |
---|
[3451] | 43 | // Google Search App see for details https://github.com/nico3333fr/CSP-useful/commit/ecc8f9b0b379ae643bc754d2db33c8b47e185fd1 |
---|
| 44 | && strpos($blocked_uri, 'gsa://onpageload') === false |
---|
| 45 | |
---|
| 46 | ) { |
---|
[3453] | 47 | // Prepare report data (hash => info) |
---|
| 48 | $hash = hash('md5', |
---|
[3478] | 49 | $blocked_uri. |
---|
| 50 | isset($data['csp-report']['document-uri']) ? $data['csp-report']['document-uri'] : ''. |
---|
| 51 | $source_file. |
---|
| 52 | isset($data['csp-report']['line-number']) ? $data['csp-report']['line-number'] : ''. |
---|
| 53 | isset($data['csp-report']['violated-directive']) ? $data['csp-report']['violated-directive'] : ''); |
---|
[3276] | 54 | |
---|
[3453] | 55 | try { |
---|
| 56 | // Check report dir (create it if necessary) |
---|
| 57 | files::makeDir(dirname(LOGFILE),true); |
---|
[3451] | 58 | |
---|
[3453] | 59 | // Check if report is not already stored in log file |
---|
| 60 | $contents = ''; |
---|
| 61 | if (file_exists(LOGFILE)) { |
---|
| 62 | $contents = file_get_contents(LOGFILE); |
---|
| 63 | if ($contents && $contents != '') { |
---|
| 64 | if (substr($contents,-1) == ',') { |
---|
| 65 | // Remove final comma if present |
---|
| 66 | $contents = substr($contents,0,-1); |
---|
| 67 | } |
---|
| 68 | if ($contents != '') { |
---|
| 69 | $list = json_decode('['.$contents.']',true); |
---|
| 70 | if (is_array($list)) { |
---|
| 71 | foreach ($list as $idx => $value) { |
---|
| 72 | if (isset($value['hash']) && $value['hash'] == $hash) { |
---|
| 73 | // Already stored, ignore |
---|
| 74 | return; |
---|
| 75 | } |
---|
| 76 | } |
---|
| 77 | } |
---|
| 78 | } |
---|
[3451] | 79 | } |
---|
[3453] | 80 | } |
---|
| 81 | |
---|
| 82 | // Add report to the file |
---|
| 83 | if (!($fp = @fopen(LOGFILE,'a'))) { |
---|
| 84 | // Unable to open file, ignore |
---|
[3276] | 85 | return; |
---|
| 86 | } |
---|
[3453] | 87 | |
---|
| 88 | // Prettify the JSON-formatted data |
---|
| 89 | $violation = array_merge(array('hash' => $hash),$data['csp-report']); |
---|
| 90 | $output = json_encode($violation,JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES); |
---|
| 91 | |
---|
| 92 | // The file content will have to be enclosed in brackets [] before |
---|
| 93 | // beeing decoded with json_decoded(<content>,true); |
---|
| 94 | fprintf($fp,($contents != '' ? ',' : '').'%s',$output); |
---|
| 95 | |
---|
| 96 | } catch (Exception $e) { |
---|
| 97 | return; |
---|
[3276] | 98 | } |
---|
[3453] | 99 | } |
---|
[3276] | 100 | } |
---|