Changeset 3453:0dab0a0d4621 for admin/csp_report.php
- Timestamp:
- 12/13/16 14:09:04 (9 years ago)
- Branch:
- default
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
admin/csp_report.php
r3452 r3453 10 10 require dirname(__FILE__).'/../inc/admin/prepend.php'; 11 11 12 // Specify log file 13 define('LOGFILE',path::real(DC_VAR).'/csp/csp_report.json'); 12 // Specify admin CSP log file if necessary 13 if (!defined('LOGFILE')) { 14 define('LOGFILE',path::real(DC_VAR).'/csp/csp_report.json'); 15 } 14 16 15 17 // Get the raw POST data … … 18 20 // Only continue if it’s valid JSON that is not just `null`, `0`, `false` or an 19 21 // empty string, i.e. if it could be a CSP violation report. 20 if ($data = json_decode($data, 22 if ($data = json_decode($data,true)) { 21 23 22 24 // get source-file and blocked-URI to perform some tests 23 $source_file 24 $blocked_uri 25 $source_file = $data['csp-report']['source-file']; 26 $blocked_uri = $data['csp-report']['blocked-uri']; 25 27 26 28 if ( 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 27 32 28 // avoid false positives notifications coming from Chrome extensions (Wappalyzer, MuteTab, etc.) 29 // bug here https://code.google.com/p/chromium/issues/detail?id=524356 30 strpos($source_file, 'chrome-extension://') === false 31 32 // avoid false positives notifications coming from Safari extensions (diigo, evernote, etc.) 33 && strpos($source_file, 'safari-extension://') === false 33 // avoid false positives notifications coming from Safari extensions (diigo, evernote, etc.) 34 && strpos($source_file, 'safari-extension://') === false 34 35 && strpos($blocked_uri, 'safari-extension://') === false 35 36 36 37 37 // search engine extensions ? 38 && strpos($source_file, 'se-extension://') === false 38 39 39 40 40 // added by browsers in webviews 41 && strpos($blocked_uri, 'webviewprogressproxy://') === false 41 42 42 43 // Google Search App see for details https://github.com/nico3333fr/CSP-useful/commit/ecc8f9b0b379ae643bc754d2db33c8b47e185fd1 … … 44 45 45 46 ) { 46 // Prettify the JSON-formatted data 47 $data = json_encode( 48 $data, 49 JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES 50 ); 47 // Prepare report data (hash => info) 48 $hash = hash('md5', 49 $data['csp-report']['blocked-uri']. 50 $data['csp-report']['document-uri']. 51 $data['csp-report']['source-file']. 52 $data['csp-report']['line-number']. 53 $data['csp-report']['violated-directive']); 51 54 52 53 54 55 try { 56 // Check report dir (create it if necessary) 57 files::makeDir(dirname(LOGFILE),true); 55 58 56 if (!($fp = @fopen(LOGFILE,'a'))) { 57 return; 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 } 58 79 } 59 // Add an ending comma to JSon encoding data 60 // The file content should be enclosed in brackets [] before beeing decoded 61 fprintf($fp,'%s,',$data); 62 } catch (Exception $e) { 80 } 81 82 // Add report to the file 83 if (!($fp = @fopen(LOGFILE,'a'))) { 84 // Unable to open file, ignore 63 85 return; 64 86 } 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; 65 98 } 99 } 66 100 }
Note: See TracChangeset
for help on using the changeset viewer.