Dotclear

Changeset 3453:0dab0a0d4621


Ignore:
Timestamp:
12/13/16 14:09:04 (9 years ago)
Author:
franck <carnet.franck.paul@…>
Branch:
default
Message:

Do not store repeated violation in CSP report data, addresses #1867

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin/csp_report.php

    r3452 r3453  
    1010require dirname(__FILE__).'/../inc/admin/prepend.php'; 
    1111 
    12 // Specify log file 
    13 define('LOGFILE',path::real(DC_VAR).'/csp/csp_report.json'); 
     12// Specify admin CSP log file if necessary 
     13if (!defined('LOGFILE')) { 
     14     define('LOGFILE',path::real(DC_VAR).'/csp/csp_report.json'); 
     15} 
    1416 
    1517// Get the raw POST data 
     
    1820// Only continue if it’s valid JSON that is not just `null`, `0`, `false` or an 
    1921// empty string, i.e. if it could be a CSP violation report. 
    20 if ($data = json_decode($data, true)) { 
     22if ($data = json_decode($data,true)) { 
    2123 
    2224     // get source-file and blocked-URI to perform some tests 
    23      $source_file   = $data['csp-report']['source-file']; 
    24      $blocked_uri   = $data['csp-report']['blocked-uri']; 
     25     $source_file = $data['csp-report']['source-file']; 
     26     $blocked_uri = $data['csp-report']['blocked-uri']; 
    2527 
    2628     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 
    2732 
    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 
    3435          && strpos($blocked_uri, 'safari-extension://') === false 
    3536 
    36     // search engine extensions ? 
    37     && strpos($source_file, 'se-extension://') === false 
     37          // search engine extensions ? 
     38          && strpos($source_file, 'se-extension://') === false 
    3839 
    39     // added by browsers in webviews 
    40     && strpos($blocked_uri, 'webviewprogressproxy://') === false 
     40          // added by browsers in webviews 
     41          && strpos($blocked_uri, 'webviewprogressproxy://') === false 
    4142 
    4243          // Google Search App see for details https://github.com/nico3333fr/CSP-useful/commit/ecc8f9b0b379ae643bc754d2db33c8b47e185fd1 
     
    4445 
    4546     ) { 
    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']); 
    5154 
    52                try { 
    53                     // Check report dir (create it if necessary) 
    54                     files::makeDir(dirname(LOGFILE),true); 
     55          try { 
     56               // Check report dir (create it if necessary) 
     57               files::makeDir(dirname(LOGFILE),true); 
    5558 
    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                         } 
    5879                    } 
    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 
    6385                    return; 
    6486               } 
     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; 
    6598          } 
     99     } 
    66100} 
Note: See TracChangeset for help on using the changeset viewer.

Sites map