Skip to content

Commit

Permalink
Merge pull request #48 from iangcarroll/feature/delete-directive
Browse files Browse the repository at this point in the history
Add a removeDirective function to remove directives
  • Loading branch information
paragonie-security authored Sep 2, 2020
2 parents f0903eb + 1eecb91 commit 8d5e690
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/CSPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,22 @@ public function setDirective(string $key, $value = []): self
return $this;
}

/**
* Removes a directive.
*
* This allows removing a directive if the presence of it might cause
* undesired behavioral changes.
*
* @param string $key
*
* @return self
*/
public function removeDirective(string $key): self
{
unset($this->policies[$key]);
return $this;
}

/**
* Allow/disallow filesystem: URIs for a given directive
*
Expand Down
20 changes: 20 additions & 0 deletions test/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -240,4 +240,24 @@ public function testSandbox()

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

/**
* @covers \ParagonIE\CSPBuilder\CSPBuilder
*/
public function testRemovingDirectives()
{
$csp = new CSPBuilder();
$csp->addSource('frame-ancestors', 'https://example.com');
$csp->addSource('style-src', 'https://example.com');
$compiled = $csp->compile();

$this->assertContains('frame-ancestors https://example.com', $compiled);
$this->assertContains('style-src https://example.com', $compiled);

$csp->removeDirective('style-src');
$compiled = $csp->compile();

$this->assertContains('frame-ancestors https://example.com', $compiled);
$this->assertNotContains('style-src https://example.com', $compiled);
}
}

0 comments on commit 8d5e690

Please sign in to comment.