Skip to content

Commit 59679bd

Browse files
committed
HttpExtension: allows bools in CSP policy
1 parent bc3a9ac commit 59679bd

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

src/Bridges/HttpDI/HttpExtension.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,12 @@ private static function buildValue(array $config): string
127127
static $nonQuoted = ['require-sri-for' => 1, 'sandbox' => 1];
128128
$value = '';
129129
foreach ($config as $type => $policy) {
130+
if ($policy === false) {
131+
continue;
132+
}
133+
$policy = $policy === true ? [] : (array) $policy;
130134
$value .= $type;
131-
foreach ((array) $policy as $item) {
135+
foreach ($policy as $item) {
132136
$value .= !isset($nonQuoted[$type]) && preg_match('#^[a-z-]+\z#', $item) ? " '$item'" : " $item";
133137
}
134138
$value .= '; ';

tests/Http.DI/HttpExtension.csp.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ http:
3737
cspReport:
3838
default-src: "'nonce'"
3939
report-uri: https://example.com/report
40+
upgrade-insecure-requests: true
41+
block-all-mixed-content: false
4042
EOD
4143
, 'neon'));
4244

@@ -49,7 +51,7 @@ $headers = headers_list();
4951

5052
preg_match('#nonce-([\w+/]+=*)#', implode($headers), $nonce);
5153
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);
52-
Assert::contains("Content-Security-Policy-Report-Only: default-src 'nonce-$nonce[1]'; report-uri https://example.com/report;", $headers);
54+
Assert::contains("Content-Security-Policy-Report-Only: default-src 'nonce-$nonce[1]'; report-uri https://example.com/report; upgrade-insecure-requests;", $headers);
5355

5456

5557
echo ' '; @ob_flush(); flush();

0 commit comments

Comments
 (0)