Skip to content

Commit

Permalink
Fix PHP 7.1 support
Browse files Browse the repository at this point in the history
  • Loading branch information
paragonie-security committed Dec 18, 2023
1 parent 17b59da commit efe15b3
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions src/CSPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,10 @@ public function compile(): string
if (!is_string($this->policies['report-uri'])) {
throw new TypeError('report-uri policy somehow not a string');
}
$compiled [] = sprintf('report-uri %s; ', $this->enc($this->policies['report-uri']), 'report-uri');
$compiled []= sprintf(
'report-uri %s; ',
$this->enc($this->policies['report-uri'], 'report-uri')
);
}
if (!empty($this->policies['report-to'])) {
if (!is_string($this->policies['report-to'])) {
Expand All @@ -178,30 +181,32 @@ public function compile(): string
return $this->compiled;
}

public function compileReportEndpoints()
/**
* @psalm-suppress TypeDoesNotContainType
*/
public function compileReportEndpoints(): string
{
if (!empty($this->reportEndpoints) && $this->needsCompileEndpoints) {
// If it's a string, it's probably something like `report-to: key=endpoint
// Do nothing
if (!is_array($this->reportEndpoints)) {
throw new TypeError('Report endpoints is not an array');
}
if (is_array($this->reportEndpoints)) {
$jsonValidator = new Validator();
$reportTo = [];
$schema = file_get_contents(__DIR__ . '/../schema/reportto.json');
foreach ($this->reportEndpoints as $reportEndpoint) {
$reportEndpointAsJSON = \Opis\JsonSchema\Helper::toJSON($reportEndpoint);
$isValid = $jsonValidator->validate($reportEndpointAsJSON, $schema);
if ($isValid->isValid()) {
$reportTo[] = json_encode($reportEndpointAsJSON);
}

$jsonValidator = new Validator();
$reportTo = [];
$schema = file_get_contents(__DIR__ . '/../schema/reportto.json');
foreach ($this->reportEndpoints as $reportEndpoint) {
$reportEndpointAsJSON = \Opis\JsonSchema\Helper::toJSON($reportEndpoint);
$isValid = $jsonValidator->validate($reportEndpointAsJSON, $schema);
if ($isValid->isValid()) {
$reportTo[] = json_encode($reportEndpointAsJSON);
}
$this->compiledEndpoints = rtrim(implode(',', $reportTo));
}
$this->compiledEndpoints = rtrim(implode(',', $reportTo));
$this->needsCompileEndpoints = false;
}
return $this->compiledEndpoints;
}

/**
Expand Down Expand Up @@ -317,7 +322,7 @@ public function addDirective(string $key, $value = null): self
* @param array|string $reportEndpoint
* @return void
*/
public function addReportEndpoints(array|string $reportEndpoint): void
public function addReportEndpoints($reportEndpoint): void
{
$this->needsCompileEndpoints = true;
$this->reportEndpoints[] = Helper::toJSON($reportEndpoint);
Expand Down Expand Up @@ -418,6 +423,8 @@ public static function fromFile(string $filename = ''): self
* @param string $header
* @return self
* @throws Exception
*
* @psalm-suppress DocblockTypeContradiction
*/
public static function fromHeader(string $header = ''): self
{
Expand All @@ -428,7 +435,7 @@ public static function fromHeader(string $header = ''): self
foreach ($directives as $directive) {
[$name, $values] = explode(' ', trim($directive), 2) + [null, null];

if (null === $name) {
if (is_null($name)) {
continue;
}

Expand Down Expand Up @@ -511,6 +518,7 @@ public function getCompiledReportEndpointsHeader(): string
*/
public function getHeaderArray(bool $legacy = true): array
{
$return = [];
if ($this->needsCompile) {
$this->compile();
}
Expand Down Expand Up @@ -544,7 +552,7 @@ public function getRequireHeaders(): array
}

/**
* @return array|string
* @return array
*/
public function getReportEndpoints(): array
{
Expand Down Expand Up @@ -602,7 +610,7 @@ public function injectCSPHeader(MessageInterface $message, bool $legacy = false)
$message = $message->withAddedHeader($key, $this->compiled);
}
if (!empty($this->compileReportEndpoints())) {
$message = $message->withAddedHeader('report-to', $this->reportTo);
$message = $message->withAddedHeader('report-to', $this->compiledEndpoints);
}

return $message;
Expand Down Expand Up @@ -874,7 +882,7 @@ public function removeDirective(string $key): self
* @param array|string $reportEndpoints
* @return void
*/
public function setReportEndpoints(array|string $reportEndpoints): void
public function setReportEndpoints($reportEndpoints): void
{
$this->needsCompileEndpoints = true;
$toJSON = Helper::toJSON($reportEndpoints);
Expand All @@ -883,8 +891,11 @@ public function setReportEndpoints(array|string $reportEndpoints): void
$this->reportEndpoints = $toJSON;
}


public function removeReportEndpoint(string $key)
/**
* @param string $key
* @return void
*/
public function removeReportEndpoint(string $key): void
{
foreach ($this->reportEndpoints as $idx => $endpoint) {
if ($endpoint->group === $key) {
Expand Down

0 comments on commit efe15b3

Please sign in to comment.