-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add injectCSPHeader() (PSR-7) and getHeaderArray() methods
- Loading branch information
1 parent
8d8b993
commit 84ad3c5
Showing
4 changed files
with
98 additions
and
14 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
<?php | ||
namespace ParagonIE\CSPBuilder; | ||
|
||
use \Psr\Http\Message\MessageInterface; | ||
|
||
class CSPBuilder | ||
{ | ||
const FORMAT_APACHE = 'apache'; | ||
|
@@ -215,6 +217,22 @@ public function getCompiledHeader() | |
|
||
/** | ||
* | ||
* @param type $legacy | ||
* @return array | ||
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
This comment has been minimized.
Sorry, something went wrong.
Ocramius
Contributor
|
||
*/ | ||
public function getHeaderArray($legacy = true) | ||
{ | ||
if ($this->needsCompile) { | ||
$this->compile(); | ||
} | ||
$return = []; | ||
foreach ($this->getHeaderKeys($legacy) as $key) { | ||
$return[$key] = $this->compiled; | ||
} | ||
return $return; | ||
} | ||
|
||
/** | ||
* Add a new nonce to the existing CSP | ||
* | ||
* @param string $directive | ||
|
@@ -231,6 +249,25 @@ public function hash($directive = 'script-src', $script = '', $algo = 'sha256') | |
$algo => \strtr('+/', '-_', $hash) | ||
]; | ||
} | ||
return $this; | ||
} | ||
|
||
/** | ||
* PSR-7 header injection | ||
* | ||
* @param \Psr\Http\Message\MessageInterface $message | ||
* @param bool $legacy | ||
* @return \Psr\Http\Message\MessageInterface | ||
*/ | ||
function injectCSPHeader(MessageInterface $message, $legacy = false) | ||
{ | ||
if ($this->needsCompile) { | ||
$this->compile(); | ||
} | ||
foreach ($this->getHeaderKeys($legacy) as $key) { | ||
$message = $message->withAddedHeader($key, $this->compiled); | ||
} | ||
return $message; | ||
} | ||
|
||
/** | ||
|
@@ -315,20 +352,10 @@ public function sendCSPHeader($legacy = true) | |
if ($this->needsCompile) { | ||
$this->compile(); | ||
} | ||
// Are we doing a report-only header? | ||
$which = $this->reportOnly | ||
? 'Content-Security-Policy-Report-Only' | ||
: 'Content-Security-Policy'; | ||
|
||
\header($which.': '.$this->compiled); | ||
if ($legacy) { | ||
// Add deprecated headers for compatibility with old clients | ||
\header('X-'.$which.': '.$this->compiled); | ||
$which = $this->reportOnly | ||
? 'X-Webkit-CSP-Report-Only' | ||
: 'X-Webkit-CSP'; | ||
\header($which.': '.$this->compiled); | ||
foreach ($this->getHeaderKeys($legacy) as $key) { | ||
\header($key.': '.$this->compiled); | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
|
@@ -418,6 +445,30 @@ protected function compileSubgroup($directive, $policies = null) | |
return \rtrim($ret, ' ').'; '; | ||
} | ||
|
||
/** | ||
* Get an array of header keys to return | ||
* | ||
* @param bool $legacy | ||
* @return array | ||
*/ | ||
protected function getHeaderKeys($legacy = true) | ||
{ | ||
$return = [ | ||
$this->reportOnly | ||
? 'Content-Security-Policy-Report-Only' | ||
: 'Content-Security-Policy' | ||
]; | ||
if ($legacy) { | ||
$return []= $this->reportOnly | ||
? 'X-Content-Security-Policy-Report-Only' | ||
: 'X-Content-Security-Policy'; | ||
$return []= $this->reportOnly | ||
? 'X-Webkit-CSP-Report-Only' | ||
: 'X-Webkit-CSP'; | ||
} | ||
return $return; | ||
} | ||
|
||
/** | ||
* Is this user currently connected over HTTPS? | ||
* | ||
|
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
string[]