Skip to content

Commit efae23e

Browse files
authored
feat: Add support to enable/disable CSP at runtime (#10397)
* Add enable() and disable() methods to CSP * add docs and changelog * change docs from response based to request based
1 parent ad75bb0 commit efae23e

5 files changed

Lines changed: 71 additions & 0 deletions

File tree

system/HTTP/ContentSecurityPolicy.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,22 @@ public function enabled(): bool
402402
return $this->CSPEnabled;
403403
}
404404

405+
/**
406+
* Enable Content Security Policy enforcement.
407+
*/
408+
public function enable(): void
409+
{
410+
$this->CSPEnabled = true;
411+
}
412+
413+
/**
414+
* Disable Content Security Policy enforcement.
415+
*/
416+
public function disable(): void
417+
{
418+
$this->CSPEnabled = false;
419+
}
420+
405421
/**
406422
* Whether adding nonce in style-* directives is enabled or not.
407423
*/

tests/system/HTTP/ContentSecurityPolicyTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,26 @@ public function testExistence(): void
9191
$this->assertHeaderEmitted('Content-Security-Policy:');
9292
}
9393

94+
#[PreserveGlobalState(false)]
95+
#[RunInSeparateProcess]
96+
public function testDisable(): void
97+
{
98+
$this->prepare(true);
99+
$this->csp->disable();
100+
$this->assertTrue($this->work());
101+
$this->assertHeaderNotEmitted('Content-Security-Policy:');
102+
}
103+
104+
#[PreserveGlobalState(false)]
105+
#[RunInSeparateProcess]
106+
public function testEnable(): void
107+
{
108+
$this->prepare(false);
109+
$this->csp->enable();
110+
$this->assertTrue($this->work());
111+
$this->assertHeaderEmitted('Content-Security-Policy:');
112+
}
113+
94114
#[PreserveGlobalState(false)]
95115
#[RunInSeparateProcess]
96116
public function testReportOnly(): void

user_guide_src/source/changelogs/v4.8.0.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,7 @@ HTTP
324324
For example: ``php index.php command -- --myarg`` will pass ``--myarg`` as an argument instead of an option.
325325
- ``CLIRequest`` now supports options with values specified using an equals sign (e.g., ``--option=value``) in addition to the existing space-separated syntax (e.g., ``--option value``).
326326
This provides more flexibility in how you can pass options to CLI requests.
327+
- Added ``enable()`` and ``disable()`` methods to ``ContentSecurityPolicy`` to enable or disable the CSP header for the current request. See :ref:`csp-enable-disable`.
327328
- Added ``$enableStyleNonce`` and ``$enableScriptNonce`` options to ``Config\App`` to automatically add nonces to control whether to add nonces to style-* and script-* directives in the Content Security Policy (CSP) header when CSP is enabled. See :ref:`csp-control-nonce-generation` for details.
328329
- Added ``URI::withQuery()``, ``URI::withQueryArray()``, ``URI::withoutQueryVars()``, and ``URI::withOnlyQueryVars()`` to return cloned URIs with replaced or filtered query variables.
329330
- Added ``URI::withQueryVar()`` and ``URI::withQueryVars()`` to return a cloned URI with query variables added or replaced.

user_guide_src/source/outgoing/csp.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,30 @@ name or an array of them:
7878
The first parameter to each of the "add" methods is an appropriate string value,
7979
or an array of them.
8080

81+
.. _csp-enable-disable:
82+
83+
Enable or Disable CSP at Runtime
84+
================================
85+
86+
.. versionadded:: 4.8.0
87+
88+
You can enable or disable CSP for the current request by using the
89+
``enable()`` and ``disable()`` methods on the CSP instance.
90+
91+
For example, to enable/disable CSP for a specific response:
92+
93+
.. literalinclude:: csp/017.php
94+
95+
The runtime value takes precedence over the ``CSPEnabled`` configuration value
96+
for the current request.
97+
98+
These methods only affect the current request. They do not modify the
99+
application configuration. On subsequent requests, the ``CSPEnabled`` value in
100+
**app/Config/App.php** is used again.
101+
102+
This allows applications to keep CSP enabled globally while temporarily
103+
disabling it for specific requests, or enable it only where required.
104+
81105
Report Only
82106
===========
83107

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
// get the CSP instance
4+
$csp = $this->response->getCSP();
5+
6+
// Disable CSP for the request
7+
$csp->disable();
8+
9+
// Re-enable CSP for the request
10+
$csp->enable();

0 commit comments

Comments
 (0)