Skip to content

Commit de20ce2

Browse files
committed
BUGFIX: valid custom directive is not added to csp header
1 parent d333486 commit de20ce2

File tree

2 files changed

+40
-14
lines changed

2 files changed

+40
-14
lines changed

Classes/Factory/PolicyFactory.php

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,27 +15,28 @@
1515
class PolicyFactory
1616
{
1717
/**
18-
* @param string[][] $defaultDirective
19-
* @param string[][] $customDirective
18+
* @param string[][] $defaultDirectives
19+
* @param string[][] $customDirectives
2020
* @throws InvalidDirectiveException
2121
*/
22-
public function create(Nonce $nonce, array $defaultDirective, array $customDirective): Policy
22+
public function create(Nonce $nonce, array $defaultDirectives, array $customDirectives): Policy
2323
{
24-
$directiveCollections = [$defaultDirective, $customDirective];
25-
$defaultDirective = array_shift($directiveCollections);
26-
27-
array_walk($defaultDirective, function (array &$item, string $key) use ($directiveCollections) {
28-
foreach ($directiveCollections as $collection) {
29-
if (array_key_exists($key, $collection)) {
30-
$item = array_unique([...$item, ...$collection[$key]]);
31-
}
24+
$resultDirectives = $defaultDirectives;
25+
foreach ($customDirectives as $key => $customDirective) {
26+
if (array_key_exists($key, $resultDirectives)) {
27+
$resultDirectives[$key] = array_merge($resultDirectives[$key], $customDirective);
28+
} else {
29+
// Custom directive is not present in default, still needs to be added.
30+
$resultDirectives[$key] = $customDirective;
3231
}
33-
});
32+
33+
$resultDirectives[$key] = array_unique($resultDirectives[$key]);
34+
}
3435

3536
$policy = new Policy();
3637
$policy->setNonce($nonce);
3738

38-
foreach ($defaultDirective as $directive => $values) {
39+
foreach ($resultDirectives as $directive => $values) {
3940
$policy->addDirective($directive, $values);
4041
}
4142

Tests/Unit/Factory/PolicyFactoryTest.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Unit\Factory;
66

7+
use Flowpack\ContentSecurityPolicy\Exceptions\InvalidDirectiveException;
78
use Flowpack\ContentSecurityPolicy\Factory\PolicyFactory;
89
use Flowpack\ContentSecurityPolicy\Model\Directive;
910
use Flowpack\ContentSecurityPolicy\Model\Nonce;
@@ -51,6 +52,29 @@ public function testCreateShouldReturnPolicy(): void
5152
self::assertSame($expected, $result->getDirectives());
5253
}
5354

55+
public function testCreateShouldFailWithInvalidDirective(): void
56+
{
57+
$policyFactory = new PolicyFactory();
58+
$nonceMock = $this->createMock(Nonce::class);
59+
60+
$defaultDirective = [
61+
'base-uri' => [
62+
'self',
63+
],
64+
'script-src' => [
65+
'self',
66+
],
67+
];
68+
$customDirective = [
69+
'invalid' => [
70+
'{nonce}',
71+
],
72+
];
73+
74+
$this->expectException(InvalidDirectiveException::class);
75+
$policyFactory->create($nonceMock, $defaultDirective, $customDirective);
76+
}
77+
5478
public function testCreateShouldAddDirectiveWhichIsPresentInCustomButNotDefaultConfiguration(): void
5579
{
5680
$policyFactory = new PolicyFactory();
@@ -70,6 +94,7 @@ public function testCreateShouldAddDirectiveWhichIsPresentInCustomButNotDefaultC
7094
],
7195
'worker-src' => [
7296
'self',
97+
'self',
7398
],
7499
];
75100

@@ -82,7 +107,7 @@ public function testCreateShouldAddDirectiveWhichIsPresentInCustomButNotDefaultC
82107
"'nonce-'",
83108
],
84109
'worker-src' => [
85-
'self',
110+
"'self'",
86111
],
87112
];
88113

0 commit comments

Comments
 (0)