Skip to content

Commit

Permalink
Change to syslog logging
Browse files Browse the repository at this point in the history
  • Loading branch information
deathbearbrown committed May 26, 2022
1 parent ef1c80f commit 3d72953
Showing 1 changed file with 14 additions and 19 deletions.
33 changes: 14 additions & 19 deletions www/csp-violation-report-endpoint.php
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
<?php
// Send `204 No Content` status code.
http_response_code(204);
// collect data from post request
$data = file_get_contents('php://input');
if ($data = json_decode($data)) {
// Remove slashes from the JSON-formatted data.
$data = json_encode(
$data,
JSON_UNESCAPED_SLASHES
);
# set options for syslog daemon
openlog('report-uri', LOG_NDELAY, LOG_USER);

// Start configure
$log_file = dirname(__FILE__) . '/csp-violations.log';
$log_file_size_limit = 1000000; // bytes - once exceeded no further entries are added
// End configuration


http_response_code(204); // HTTP 204 No Content

$json_data = file_get_contents('php://input');

// We pretty print the JSON before adding it to the log file
if ($json_data = json_decode($json_data)) {
$json_data = json_encode($json_data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);

if (filesize($log_file) > $log_file_size_limit) {
exit(0);
}

file_put_contents($log_file, $json_data, FILE_APPEND | LOCK_EX);
# send warning about csp report
syslog(LOG_WARNING, $data);
}

0 comments on commit 3d72953

Please sign in to comment.