-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathContentSecurityPolicyController.php
More file actions
75 lines (58 loc) · 2.28 KB
/
Copy pathContentSecurityPolicyController.php
File metadata and controls
75 lines (58 loc) · 2.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php
declare(strict_types=1);
namespace Netlogix\Sentry\Controller;
use Neos\Flow\Log\ThrowableStorageInterface;
use Neos\Flow\Mvc\Controller\ActionController;
use Neos\Flow\Annotations as Flow;
use Psr\Http\Client\ClientExceptionInterface;
use Psr\Http\Client\ClientInterface;
use Psr\Http\Message\RequestFactoryInterface;
use Psr\Http\Message\StreamFactoryInterface;
use Sentry\SentrySdk;
class ContentSecurityPolicyController extends ActionController
{
protected $supportedMediaTypes = ['application/csp-report'];
#[Flow\InjectConfiguration(path: 'csp.enable')]
protected bool $enabled = true;
#[Flow\InjectConfiguration(path: 'csp.reports.includedHeaders')]
protected array $includedHeaders = [];
#[Flow\Inject]
protected ThrowableStorageInterface $throwableStorage;
public function indexAction(): string
{
if (!$this->enabled) {
return '';
}
$reportingEndpoint = $this->getSentryReportingEndpoint();
if ($reportingEndpoint === null) {
return '';
}
// TODO: Only report a limited amount to avoid filling up sentry
$body = $this->request->getHttpRequest()->getBody();
$body->rewind();
$postBody = $body->getContents();
$client = $this->objectManager->get(ClientInterface::class);
$requestFactory = $this->objectManager->get(RequestFactoryInterface::class);
$streamFactory = $this->objectManager->get(StreamFactoryInterface::class);
$request = $requestFactory->createRequest('POST', $reportingEndpoint)
->withBody($streamFactory->createStream($postBody));
foreach (array_keys(array_filter($this->includedHeaders)) as $header) {
$headerValue = $this->request->getHttpRequest()->getHeaderLine($header);
if ($headerValue === '') {
continue;
}
$request = $request
->withHeader($header, $headerValue);
}
try {
$client->sendRequest($request);
} catch (ClientExceptionInterface $e) {
$this->throwableStorage->logThrowable($e);
}
return '';
}
protected function getSentryReportingEndpoint(): ?string
{
return SentrySdk::getCurrentHub()->getClient()?->getCspReportUrl();
}
}