Skip to content

Commit bc3a9ac

Browse files
peldaxdg
authored andcommitted
HttpExtension: fixed quotating in sections require-sri-for & sandbox [Closes #143]
1 parent dbaff22 commit bc3a9ac

2 files changed

Lines changed: 6 additions & 2 deletions

File tree

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,12 @@ public function afterCompile(Nette\PhpGenerator\ClassType $class)
124124

125125
private static function buildValue(array $config): string
126126
{
127+
static $nonQuoted = ['require-sri-for' => 1, 'sandbox' => 1];
127128
$value = '';
128129
foreach ($config as $type => $policy) {
129130
$value .= $type;
130131
foreach ((array) $policy as $item) {
131-
$value .= preg_match('#^[a-z-]+\z#', $item) ? " '$item'" : " $item";
132+
$value .= !isset($nonQuoted[$type]) && preg_match('#^[a-z-]+\z#', $item) ? " '$item'" : " $item";
132133
}
133134
$value .= '; ';
134135
}

tests/Http.DI/HttpExtension.csp.phpt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ http:
3030
style-src:
3131
- self
3232
- https://example.com
33+
require-sri-for: style
34+
sandbox: allow-forms
35+
plugin-types: application/x-java-applet
3336
3437
cspReport:
3538
default-src: "'nonce'"
@@ -45,7 +48,7 @@ $container->initialize();
4548
$headers = headers_list();
4649

4750
preg_match('#nonce-([\w+/]+=*)#', implode($headers), $nonce);
48-
Assert::contains("Content-Security-Policy: default-src 'self' https://example.com; upgrade-insecure-requests; script-src 'nonce-$nonce[1]'; style-src 'self' https://example.com;", $headers);
51+
Assert::contains("Content-Security-Policy: default-src 'self' https://example.com; upgrade-insecure-requests; script-src 'nonce-$nonce[1]'; style-src 'self' https://example.com; require-sri-for style; sandbox allow-forms; plugin-types application/x-java-applet;", $headers);
4952
Assert::contains("Content-Security-Policy-Report-Only: default-src 'nonce-$nonce[1]'; report-uri https://example.com/report;", $headers);
5053

5154

0 commit comments

Comments
 (0)