Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Fix CSP report-uri directive defaulting to 'none' when empty value provided #93

Merged
merged 1 commit into from
Jan 19, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/Header/ContentSecurityPolicy.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public function setDirective($name, array $sources)
));
}
if (empty($sources)) {
if ('report-uri' === $name) {
if (isset($this->directives[$name])) {
unset($this->directives[$name]);
}
return $this;
}
$this->directives[$name] = "'none'";
return $this;
}
Expand Down
26 changes: 26 additions & 0 deletions test/Header/ContentSecurityPolicyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,30 @@ public function testPreventsCRLFAttackViaDirective()
$this->setExpectedException('Zend\Http\Header\Exception\InvalidArgumentException');
$header->setDirective('default-src', ["\rsome\r\nCRLF\ninjection"]);
}

public function testContentSecurityPolicySetDirectiveWithEmptyReportUriDefaultsToUnset()
{
$csp = new ContentSecurityPolicy();
$csp->setDirective('report-uri', []);
$this->assertEquals(
"Content-Security-Policy: ",
$csp->toString()
);
}

public function testContentSecurityPolicySetDirectiveWithEmptyReportUriRemovesExistingValue()
{
$csp = new ContentSecurityPolicy();
$csp->setDirective('report-uri', ['csp-error']);
$this->assertEquals(
"Content-Security-Policy: report-uri csp-error;",
$csp->toString()
);

$csp->setDirective('report-uri', []);
$this->assertEquals(
"Content-Security-Policy: ",
$csp->toString()
);
}
}