-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathCspHeaderMiddleware.php
More file actions
167 lines (141 loc) · 4.91 KB
/
CspHeaderMiddleware.php
File metadata and controls
167 lines (141 loc) · 4.91 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
<?php
declare(strict_types=1);
namespace Flowpack\ContentSecurityPolicy\Http;
use Flowpack\ContentSecurityPolicy\Exceptions\DirectivesNormalizerException;
use Flowpack\ContentSecurityPolicy\Exceptions\InvalidDirectiveException;
use Flowpack\ContentSecurityPolicy\Factory\PolicyFactory;
use Flowpack\ContentSecurityPolicy\Helpers\TagHelper;
use Flowpack\ContentSecurityPolicy\Model\Nonce;
use Flowpack\ContentSecurityPolicy\Model\Policy;
use GuzzleHttp\Psr7\Utils;
use InvalidArgumentException;
use Neos\Flow\Annotations as Flow;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\MiddlewareInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Psr\Log\LoggerInterface;
class CspHeaderMiddleware implements MiddlewareInterface
{
private const NONCE = 'nonce';
/**
* @Flow\InjectConfiguration(path="enabled")
*/
protected bool $enabled;
/**
* @Flow\Inject
*/
protected Nonce $nonce;
/**
* @Flow\Inject
*/
protected PolicyFactory $policyFactory;
/**
* @Flow\InjectConfiguration(path="content-security-policy")
* @var array<string, array<string, array<string|int, string|bool>>>
*/
protected array $configuration;
/**
* @Flow\InjectConfiguration(path="policies")
* @var array<string, array<string, list<string>>>
*/
protected array $policies;
/**
* @Flow\InjectConfiguration(path="throw-exception-on-configuration-error")
*/
protected bool $throwExceptionOnConfigurationError;
/**
* @Flow\Inject
*/
protected LoggerInterface $logger;
/**
* @inheritDoc
* @throws InvalidDirectiveException
* @throws DirectivesNormalizerException
*/
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$response = $handler->handle($request);
if (!$this->enabled) {
return $response;
}
$policy = $this->getPolicyByCurrentContext($request);
if ($policy->hasNonceDirectiveValue()) {
$body = $response->getBody();
$newBody = $this->addNonceToTags((string)$body);
$body = Utils::streamFor($newBody);
$response = $response->withBody($body);
}
return $response->withAddedHeader($policy->getSecurityHeaderKey(), (string)$policy);
}
/**
* @throws InvalidDirectiveException
* @throws DirectivesNormalizerException
*/
private function getPolicyByCurrentContext(ServerRequestInterface $request): Policy
{
$path = $request->getUri()->getPath();
$backendUris = array_merge(
$this->policies['backend']['matchUris'] ?? [],
$this->policies['custom-backend']['matchUris'] ?? []
);
foreach ($backendUris as $pattern) {
$result = preg_match('#' . str_replace('#', '\#', $pattern) . '#', $path);
if ($result === false) {
$message = sprintf('Invalid matchUri pattern "%s": %s', $pattern, preg_last_error_msg());
if ($this->throwExceptionOnConfigurationError) {
throw new InvalidArgumentException($message);
}
$this->logger->critical($message);
continue;
}
if ($result === 1) {
return $this->policyFactory->create(
$this->nonce,
$this->configuration['backend'],
$this->configuration['custom-backend']
);
}
}
return $this->policyFactory->create(
$this->nonce,
$this->configuration['default'],
$this->configuration['custom']
);
}
private function addNonceToTags(string $markup): string
{
$tagNames = ['script', 'style'];
return $this->checkTagAndReplaceUsingACallback($tagNames, $markup, function (
$tagMarkup,
): string {
if (TagHelper::tagHasAttribute($tagMarkup, self::NONCE)) {
return TagHelper::tagChangeAttributeValue($tagMarkup, self::NONCE, $this->nonce->getValue());
}
return TagHelper::tagAddAttribute($tagMarkup, self::NONCE, $this->nonce->getValue());
});
}
/**
* @param string[] $tagNames
*/
private function checkTagAndReplaceUsingACallback(
array $tagNames,
string $contentMarkup,
callable $hitCallback
): string {
$regex = '/<(' . implode('|', $tagNames) . ').*?>/';
return preg_replace_callback(
$regex,
function ($hits) use ($hitCallback) {
$tagMarkup = $hits[0];
$tagName = $hits[1];
return call_user_func(
$hitCallback,
$tagMarkup,
$tagName
);
},
$contentMarkup
);
}
}