Skip to content

Commit 5931bf4

Browse files
committed
Add enable() and disable() methods to CSP
1 parent 52810bc commit 5931bf4

2 files changed

Lines changed: 36 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

0 commit comments

Comments
 (0)