Skip to content

Commit

Permalink
Add support for blob:, filesystem: and mediastream: URIs.
Browse files Browse the repository at this point in the history
See #17
  • Loading branch information
paragonie-security committed Nov 20, 2017
1 parent c109412 commit aee68bf
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions src/CSPBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,21 @@ public function sendCSPHeader(bool $legacy = true): bool
return true;
}

/**
* @param string $directive
* @param bool $allow
* @return self
* @throws \Exception
*/
public function setBlobAllowed(string $directive = '', bool $allow = false): self
{
if (!\in_array($directive, self::$directives)) {
throw new \Exception('Directive ' . $directive . ' does not exist');
}
$this->policies[$directive]['blob'] = $allow;
return $this;
}

/**
* @param string $directive
* @param bool $allow
Expand All @@ -487,6 +502,36 @@ public function setDataAllowed(string $directive = '', bool $allow = false): sel
return $this;
}

/**
* @param string $directive
* @param bool $allow
* @return self
* @throws \Exception
*/
public function setFileSystemAllowed(string $directive = '', bool $allow = false): self
{
if (!\in_array($directive, self::$directives)) {
throw new \Exception('Directive ' . $directive . ' does not exist');
}
$this->policies[$directive]['filesystem'] = $allow;
return $this;
}

/**
* @param string $directive
* @param bool $allow
* @return self
* @throws \Exception
*/
public function setMediaStreamAllowed(string $directive = '', bool $allow = false): self
{
if (!\in_array($directive, self::$directives)) {
throw new \Exception('Directive ' . $directive . ' does not exist');
}
$this->policies[$directive]['mediastream'] = $allow;
return $this;
}

/**
* Allow self URIs for a given directive
*
Expand Down Expand Up @@ -651,9 +696,18 @@ protected function compileSubgroup(string $directive, $policies = []): string
if (!empty($policies['unsafe-eval'])) {
$ret .= "'unsafe-eval' ";
}
if (!empty($policies['blob'])) {
$ret .= "blob: ";
}
if (!empty($policies['data'])) {
$ret .= "data: ";
}
if (!empty($policies['mediastream'])) {
$ret .= "mediastream: ";
}
if (!empty($policies['filesystem'])) {
$ret .= "filesystem: ";
}
if (!empty($policies['strict-dynamic'])) {
$ret .= "'strict-dynamic' ";
}
Expand Down

0 comments on commit aee68bf

Please sign in to comment.