Skip to content

Commit 5ff052e

Browse files
committed
Allow set CSP in the Response via config
1 parent f5a6a13 commit 5ff052e

File tree

2 files changed

+24
-0
lines changed

2 files changed

+24
-0
lines changed

src/App.php

+7
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
use Framework\Email\Mailers\SMTPMailer;
2828
use Framework\Helpers\Isolation;
2929
use Framework\HTTP\AntiCSRF;
30+
use Framework\HTTP\CSP;
3031
use Framework\HTTP\Debug\HTTPCollector;
3132
use Framework\HTTP\Request;
3233
use Framework\HTTP\Response;
@@ -1181,6 +1182,12 @@ protected static function setResponse(string $instance) : Response
11811182
? $service->setNoCache()
11821183
: $service->setCache($config['cache']['seconds'], $config['cache']['public'] ?? false);
11831184
}
1185+
if ( ! empty($config['csp'])) {
1186+
$service->setCsp(new CSP($config['csp']));
1187+
}
1188+
if ( ! empty($config['csp_report_only'])) {
1189+
$service->setCspReportOnly(new CSP($config['csp_report_only']));
1190+
}
11841191
return static::setService('response', $service, $instance);
11851192
}
11861193

tests/configs/response.config.php

+17
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@
1010
/**
1111
* @see App::response()
1212
*/
13+
14+
use Framework\HTTP\CSP;
15+
1316
return [
1417
'default' => [
1518
'headers' => [
@@ -21,6 +24,20 @@
2124
'seconds' => 60,
2225
'public' => true,
2326
],
27+
'csp' => [
28+
CSP::defaultSrc => [
29+
'self',
30+
],
31+
CSP::styleSrc => [
32+
'self',
33+
'cdn.foo.tld',
34+
],
35+
],
36+
'csp_report_only' => [
37+
CSP::defaultSrc => [
38+
'self',
39+
],
40+
],
2441
'request_instance' => 'default',
2542
],
2643
];

0 commit comments

Comments
 (0)