You are here

class ReportUri in Content-Security-Policy 8

Same name in this branch
  1. 8 src/Controller/ReportUri.php \Drupal\csp\Controller\ReportUri
  2. 8 src/Plugin/CspReportingHandler/ReportUri.php \Drupal\csp\Plugin\CspReportingHandler\ReportUri

Report URI Controller.

@package Drupal\csp\Controller

Hierarchy

Expanded class hierarchy of ReportUri

File

src/Controller/ReportUri.php, line 16

Namespace

Drupal\csp\Controller
View source
class ReportUri implements ContainerInjectionInterface {

  /**
   * The Request Stack service.
   *
   * @var \Symfony\Component\HttpFoundation\RequestStack
   */
  private $requestStack;

  /**
   * The Logger channel.
   *
   * @var \Psr\Log\LoggerInterface
   */
  private $logger;

  /**
   * Create a new Report URI Controller.
   *
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The Request Stack service.
   * @param \Psr\Log\LoggerInterface $logger
   *   The Logger channel.
   */
  public function __construct(RequestStack $requestStack, LoggerInterface $logger) {
    $this->requestStack = $requestStack;
    $this->logger = $logger;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('request_stack'), $container
      ->get('logger.channel.csp'));
  }

  /**
   * Handle a report submission.
   *
   * @param string $type
   *   The report type.
   *
   * @return \Symfony\Component\HttpFoundation\Response
   *   An empty response.
   */
  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);

    // Return 400: Bad Request if content cannot be parsed.
    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),
    ]);

    // 202: Accepted.
    return new Response('', 202);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ReportUri::$logger private property The Logger channel.
ReportUri::$requestStack private property The Request Stack service.
ReportUri::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ReportUri::log public function Handle a report submission.
ReportUri::__construct public function Create a new Report URI Controller.