Skip to content

Commit a72805a

Browse files
committed
change!: rename config throw-invalid-directive-exception to throw-exception-on-configuration-error
1 parent 5a2b973 commit a72805a

7 files changed

Lines changed: 46 additions & 30 deletions

File tree

Classes/Factory/PolicyFactory.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
class PolicyFactory
1919
{
2020
/**
21-
* @Flow\InjectConfiguration(path="throw-invalid-directive-exception")
21+
* @Flow\InjectConfiguration(path="throw-exception-on-configuration-error")
2222
*/
23-
protected bool $throwInvalidDirectiveException;
23+
protected bool $throwExceptionOnConfigurationError;
2424

2525
/**
2626
* @Flow\Inject
@@ -60,15 +60,14 @@ public function create(Nonce $nonce, array $defaultDirectives, array $customDire
6060
foreach ($resultDirectives as $directive => $values) {
6161
try {
6262
$policy->addDirective($directive, $values);
63-
} catch (InvalidDirectiveException $e
64-
) {
65-
if ($this->throwInvalidDirectiveException) {
63+
} catch (InvalidDirectiveException $exception) {
64+
if ($this->throwExceptionOnConfigurationError) {
6665
// For development we want to make sure directives are configured correctly.
67-
throw $e;
66+
throw $exception;
6867
} else {
6968
// In production we just log the error and continue. If a directive is invalid, we still
7069
// want to apply the rest of the policy.
71-
$this->logger->critical($e->getMessage());
70+
$this->logger->critical($exception->getMessage());
7271
continue;
7372
}
7473
}

Classes/Http/CspHeaderMiddleware.php

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,10 @@ class CspHeaderMiddleware implements MiddlewareInterface
5050
*/
5151
protected array $policies;
5252

53-
// TODO: rename to throw-on-configuration-error in next major version
5453
/**
55-
* @Flow\InjectConfiguration(path="throw-invalid-directive-exception")
54+
* @Flow\InjectConfiguration(path="throw-exception-on-configuration-error")
5655
*/
57-
protected bool $throwInvalidDirectiveException;
56+
protected bool $throwExceptionOnConfigurationError;
5857

5958
/**
6059
* @Flow\Inject
@@ -69,7 +68,7 @@ class CspHeaderMiddleware implements MiddlewareInterface
6968
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
7069
{
7170
$response = $handler->handle($request);
72-
if (! $this->enabled) {
71+
if (!$this->enabled) {
7372
return $response;
7473
}
7574

@@ -102,7 +101,7 @@ private function getPolicyByCurrentContext(ServerRequestInterface $request): Pol
102101
$result = preg_match('#' . str_replace('#', '\#', $pattern) . '#', $path);
103102
if ($result === false) {
104103
$message = sprintf('Invalid matchUri pattern "%s": %s', $pattern, preg_last_error_msg());
105-
if ($this->throwInvalidDirectiveException) {
104+
if ($this->throwExceptionOnConfigurationError) {
106105
throw new InvalidArgumentException($message);
107106
}
108107
$this->logger->critical($message);
@@ -141,14 +140,14 @@ private function addNonceToTags(string $markup): string
141140
}
142141

143142
/**
144-
* @param string[] $tagNames
143+
* @param string[] $tagNames
145144
*/
146145
private function checkTagAndReplaceUsingACallback(
147146
array $tagNames,
148147
string $contentMarkup,
149148
callable $hitCallback
150149
): string {
151-
$regex = '/<('.implode('|', $tagNames).').*?>/';
150+
$regex = '/<(' . implode('|', $tagNames) . ').*?>/';
152151

153152
return preg_replace_callback(
154153
$regex,
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
Flowpack:
22
ContentSecurityPolicy:
3-
throw-invalid-directive-exception: false
3+
throw-exception-on-configuration-error: false

Configuration/Settings.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ Flowpack:
22
ContentSecurityPolicy:
33
enabled: true
44
report-only: false
5-
throw-invalid-directive-exception: true
5+
throw-exception-on-configuration-error: true
66
content-security-policy:
77
default:
88
base-uri:

README.md

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
# Flowpack.ContentSecurityPolicy
22

33
<!-- TOC -->
4+
45
* [Flowpack.ContentSecurityPolicy](#flowpackcontentsecuritypolicy)
5-
* [Introduction](#introduction)
6-
* [Usage](#usage)
7-
* [Custom directives and values](#custom-directives-and-values)
8-
* [Show CSP configuration](#show-csp-configuration)
9-
* [Disable or report only](#disable-or-report-only)
10-
* [Nonce](#nonce)
11-
* [Backend](#backend)
12-
* [Custom backend routes](#custom-backend-routes)
13-
* [Thank you](#thank-you)
6+
* [Introduction](#introduction)
7+
* [Usage](#usage)
8+
* [Custom directives and values](#custom-directives-and-values)
9+
* [Show CSP configuration](#show-csp-configuration)
10+
* [Configuration errors](#configuration-errors)
11+
* [Disable or report only](#disable-or-report-only)
12+
* [Nonce](#nonce)
13+
* [Backend](#backend)
14+
* [Custom backend routes](#custom-backend-routes)
15+
* [Thank you](#thank-you)
16+
1417
<!-- TOC -->
1518

1619
## Introduction
@@ -94,6 +97,20 @@ If you fully want to override the entire default config then just override the d
9497
To show the parsed configuration, the built-in command `./flow cspconfig:show` can be used.
9598
It shows all directives used by the frontend and the backend.
9699

100+
## Throwing exceptions on configuration errors
101+
102+
By default, in production, invalid directives are logged as critical errors and skipped so the rest of the policy is
103+
still applied.
104+
Also invalid custom backend routes are logged and ignored in production.
105+
In development, exceptions are throw instead, which surfaces misconfiguration immediately.
106+
You can change that behavior like this:
107+
108+
```yaml
109+
Flowpack:
110+
ContentSecurityPolicy:
111+
throw-exception-on-configuration-error: true
112+
```
113+
97114
## Disable or report only
98115

99116
To disable the header simply set `enabled` to false.

Tests/Unit/Factory/PolicyFactoryTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ protected function setUp(): void
3838

3939
$this->policyFactoryReflection = new ReflectionClass($this->policyFactory);
4040
$this->policyFactoryReflection->getProperty('logger')->setValue($this->policyFactory, $this->loggerMock);
41-
$this->policyFactoryReflection->getProperty('throwInvalidDirectiveException')->setValue(
41+
$this->policyFactoryReflection->getProperty('throwExceptionOnConfigurationError')->setValue(
4242
$this->policyFactory,
4343
true
4444
);
@@ -100,7 +100,7 @@ public function testCreateShouldFailWithInvalidDirective(): void
100100
public function testCreateShouldLogInvalidDirectiveInProduction(): void
101101
{
102102
$nonceMock = $this->createMock(Nonce::class);
103-
$this->policyFactoryReflection->getProperty('throwInvalidDirectiveException')->setValue(
103+
$this->policyFactoryReflection->getProperty('throwExceptionOnConfigurationError')->setValue(
104104
$this->policyFactory,
105105
false
106106
);
@@ -114,7 +114,7 @@ public function testCreateShouldLogInvalidDirectiveInProduction(): void
114114
$this->loggerMock->expects($this->once())->method('critical');
115115
$this->policyFactory->create($nonceMock, $defaultDirective, $customDirective);
116116

117-
$this->policyFactoryReflection->getProperty('throwInvalidDirectiveException')->setValue(
117+
$this->policyFactoryReflection->getProperty('throwExceptionOnConfigurationError')->setValue(
118118
$this->policyFactory,
119119
true
120120
);

Tests/Unit/Http/CspHeaderMiddlewareTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Psr\Log\LoggerInterface;
2222
use ReflectionClass;
2323
use Throwable;
24+
2425
use function PHPUnit\Framework\once;
2526

2627
#[CoversClass(CspHeaderMiddleware::class)]
@@ -78,7 +79,7 @@ protected function setUp(): void
7879
['backend' => ['matchUris' => ['^/neos']], 'custom-backend' => ['matchUris' => []]]
7980
);
8081

81-
$reflectionProperty = $this->middlewareReflection->getProperty('throwInvalidDirectiveException');
82+
$reflectionProperty = $this->middlewareReflection->getProperty('throwExceptionOnConfigurationError');
8283
$reflectionProperty->setValue($this->middleware, true);
8384

8485
$reflectionProperty = $this->middlewareReflection->getProperty('logger');
@@ -205,7 +206,7 @@ public function testProcessThrowsOnInvalidMatchUriPattern(): void
205206

206207
public function testProcessLogsInvalidMatchUriPatternInProduction(): void
207208
{
208-
$reflectionProperty = $this->middlewareReflection->getProperty('throwInvalidDirectiveException');
209+
$reflectionProperty = $this->middlewareReflection->getProperty('throwExceptionOnConfigurationError');
209210
$reflectionProperty->setValue($this->middleware, false);
210211

211212
$reflectionProperty = $this->middlewareReflection->getProperty('policies');

0 commit comments

Comments
 (0)