-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from comsolit/master
fix #31 please make it possible to report and inforce policies
- Loading branch information
Showing
15 changed files
with
515 additions
and
220 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,4 +38,4 @@ function ($source) use ($keywords) { | |
$sourceList | ||
); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
<?php | ||
|
||
namespace Nelmio\SecurityBundle\ContentSecurityPolicy; | ||
|
||
class DirectiveSet | ||
{ | ||
private static $directiveNames = array( | ||
'default-src', | ||
'script-src', | ||
'object-src', | ||
'style-src', | ||
'img-src', | ||
'media-src', | ||
'frame-src', | ||
'font-src', | ||
'connect-src', | ||
'report-uri' | ||
); | ||
|
||
private $directiceValues = array(); | ||
|
||
public function getDirective($name) | ||
{ | ||
$this->checkDirectiveName($name); | ||
|
||
if (array_key_exists($name, $this->directiceValues)) { | ||
return $this->directiceValues[$name]; | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function setDirective($name, $value) | ||
{ | ||
$this->checkDirectiveName($name); | ||
$this->directiceValues[$name] = $value; | ||
} | ||
|
||
public function setDirectives(array $directives) | ||
{ | ||
foreach ($directives as $name => $value) { | ||
$this->setDirective($name, $value); | ||
} | ||
} | ||
|
||
public function buildHeaderValue() | ||
{ | ||
$policy = array(); | ||
foreach ($this->directiceValues as $name => $value) { | ||
$policy[] = $name . ' ' . $value; | ||
} | ||
|
||
return join('; ', $policy); | ||
} | ||
|
||
public static function fromLegacyConfig(array $config) | ||
{ | ||
$directiveSet = new self(); | ||
$parser = new ContentSecurityPolicyParser(); | ||
|
||
foreach (self::getLegacyNamesMap() as $old => $new) { | ||
if (!array_key_exists($old, $config)) { | ||
continue; | ||
} | ||
|
||
if ($old === 'report_uri') { | ||
$directiveSet->setDirective($new, $config[$old]); | ||
} else { | ||
$directiveSet->setDirective($new, $parser->parseSourceList($config[$old])); | ||
} | ||
} | ||
|
||
return $directiveSet; | ||
} | ||
|
||
public static function fromConfig(array $config, $kind) | ||
{ | ||
$directiveSet = new self(); | ||
if (!array_key_exists($kind, $config)) { | ||
return $directiveSet; | ||
} | ||
|
||
$parser = new ContentSecurityPolicyParser(); | ||
foreach (self::getNames() as $name) { | ||
if (!array_key_exists($name, $config[$kind])) { | ||
continue; | ||
} | ||
|
||
$directiveSet->setDirective($name, $parser->parseSourceList($config[$kind][$name])); | ||
} | ||
|
||
return $directiveSet; | ||
} | ||
|
||
public static function getNames() | ||
{ | ||
return self::$directiveNames; | ||
} | ||
|
||
/** | ||
* @deprecated | ||
*/ | ||
public static function getLegacyNamesMap() | ||
{ | ||
return array( | ||
'default' => 'default-src', | ||
'script' => 'script-src', | ||
'object' => 'object-src', | ||
'style' => 'style-src', | ||
'img' => 'img-src', | ||
'media' => 'media-src', | ||
'frame' => 'frame-src', | ||
'font' => 'font-src', | ||
'connect' => 'connect-src', | ||
'report_uri' => 'report-uri' | ||
); | ||
} | ||
|
||
private function checkDirectiveName($name) | ||
{ | ||
if (!in_array($name, self::$directiveNames)) { | ||
throw new \InvalidArgumentException('Unknown CSP directive name: ' . $name); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.