Skip to content

Commit

Permalink
Prevent semicolons and CRLFs from being injected in report-uri
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Mar 26, 2023
1 parent 172dc0a commit b0ef3f3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/CSPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ public function compile(): string
if (!is_string($this->policies['report-uri'])) {
throw new TypeError('report-uri policy somehow not a string');
}
$compiled [] = 'report-uri ' . $this->policies['report-uri'] . '; ';
$compiled [] = 'report-uri ' . $this->enc($this->policies['report-uri'], 'report-uri') . '; ';
}
if (!empty($this->policies['report-to'])) {
if (!is_string($this->policies['report-to'])) {
Expand Down Expand Up @@ -1035,6 +1035,8 @@ protected function getHeaderKeys(bool $legacy = true): array
protected function enc(string $piece, string $type = 'default'): string
{
switch ($type) {
case 'report-uri':
return str_replace(["\r", "\n", ';'], '', $piece);
case 'mime':
if (preg_match('#^([a-z0-9\-/]+)#', $piece, $matches)) {
return $matches[1];
Expand Down
6 changes: 3 additions & 3 deletions test/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -273,19 +273,19 @@ public function testSandbox()
$csp->setDirective('sandbox');
$compiled = $csp->compile();

$this->assertEquals($compiled, 'sandbox; ');
$this->assertEquals($compiled, 'sandbox');

$csp->addSource('sandbox', 'allow-scripts');
$compiled = $csp->compile();

$this->assertEquals($compiled, 'sandbox allow-scripts; ');
$this->assertEquals($compiled, 'sandbox allow-scripts');

$csp->setDirective('sandbox', [
'allow' => ['allow-popups-to-escape-sandbox'],
]);
$compiled = $csp->compile();

$this->assertEquals($compiled, 'sandbox allow-popups-to-escape-sandbox; ');
$this->assertEquals($compiled, 'sandbox allow-popups-to-escape-sandbox');
}

/**
Expand Down

0 comments on commit b0ef3f3

Please sign in to comment.