ReportUri.php in Content-Security-Policy 8
File
src/Controller/ReportUri.php
View source
<?php
namespace Drupal\csp\Controller;
use Drupal\Core\DependencyInjection\ContainerInjectionInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
class ReportUri implements ContainerInjectionInterface {
private $requestStack;
private $logger;
public function __construct(RequestStack $requestStack, LoggerInterface $logger) {
$this->requestStack = $requestStack;
$this->logger = $logger;
}
public static function create(ContainerInterface $container) {
return new static($container
->get('request_stack'), $container
->get('logger.channel.csp'));
}
public function log($type) {
$validTypes = [
'enforce',
'reportOnly',
];
if (!in_array($type, $validTypes)) {
return new Response('', 404);
}
$reportJson = $this->requestStack
->getCurrentRequest()
->getContent();
$report = json_decode($reportJson);
if (empty($report) || json_last_error() != JSON_ERROR_NONE) {
return new Response('', 400);
}
$this->logger
->info("@type <br/>\n<pre>@data</pre>", [
'@type' => $type,
'@data' => json_encode($report, JSON_PRETTY_PRINT),
]);
return new Response('', 202);
}
}