Skip to content

Commit d390403

Browse files
committed
feat: add ContentSecurityPolicy::clearDirective()
1 parent b7a918e commit d390403

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

system/HTTP/ContentSecurityPolicy.php

+10
Original file line numberDiff line numberDiff line change
@@ -819,4 +819,14 @@ protected function addToHeader(string $name, $values = null)
819819
$this->reportOnlyHeaders[$name] = implode(' ', $reportSources);
820820
}
821821
}
822+
823+
/**
824+
* Clear the directive.
825+
*
826+
* @param string $directive CSP directive
827+
*/
828+
public function clearDirective(string $directive): void
829+
{
830+
$this->{$this->directives[$directive]} = [];
831+
}
822832
}

tests/system/HTTP/ContentSecurityPolicyTest.php

+15
Original file line numberDiff line numberDiff line change
@@ -642,4 +642,19 @@ public function testHeaderScriptNonceEmittedOnceGetScriptNonceCalled(): void
642642
$result = $this->getHeaderEmitted('Content-Security-Policy');
643643
$this->assertStringContainsString("script-src 'self' 'nonce-", $result);
644644
}
645+
646+
public function testClearDirective(): void
647+
{
648+
$this->prepare();
649+
650+
$this->csp->addStyleSrc('css.example.com');
651+
$this->csp->clearDirective('style-src');
652+
653+
$this->csp->finalize($this->response);
654+
655+
$header = $this->response->getHeaderLine('Content-Security-Policy');
656+
657+
$this->assertStringNotContainsString('style-src ', $header);
658+
$this->assertStringNotContainsString('css.example.com', $header);
659+
}
645660
}

0 commit comments

Comments
 (0)