Skip to content

Commit

Permalink
feat: add ContentSecurityPolicy::clearDirective()
Browse files Browse the repository at this point in the history
  • Loading branch information
kenjis committed Nov 15, 2023
1 parent b7a918e commit d390403
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions system/HTTP/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -819,4 +819,14 @@ protected function addToHeader(string $name, $values = null)
$this->reportOnlyHeaders[$name] = implode(' ', $reportSources);
}
}

/**
* Clear the directive.
*
* @param string $directive CSP directive
*/
public function clearDirective(string $directive): void
{
$this->{$this->directives[$directive]} = [];
}
}
15 changes: 15 additions & 0 deletions tests/system/HTTP/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -642,4 +642,19 @@ public function testHeaderScriptNonceEmittedOnceGetScriptNonceCalled(): void
$result = $this->getHeaderEmitted('Content-Security-Policy');
$this->assertStringContainsString("script-src 'self' 'nonce-", $result);
}

public function testClearDirective(): void
{
$this->prepare();

$this->csp->addStyleSrc('css.example.com');
$this->csp->clearDirective('style-src');

$this->csp->finalize($this->response);

$header = $this->response->getHeaderLine('Content-Security-Policy');

$this->assertStringNotContainsString('style-src ', $header);
$this->assertStringNotContainsString('css.example.com', $header);
}
}

0 comments on commit d390403

Please sign in to comment.