id = $id; $this->noneAllowed = $noneAllowed; $this->hostSources = array_values(array_unique($hostSources)); $this->schemeSources = array_values(array_unique($schemeSources)); $this->selfAllowed = $selfAllowed; $this->inlineAllowed = $inlineAllowed; $this->evalAllowed = $evalAllowed; $this->nonceValues = array_values(array_unique($nonceValues)); $this->hashes = $hashValues; $this->dynamicAllowed = $dynamicAllowed; $this->eventHandlersAllowed = $eventHandlersAllowed; } /** * @inheritDoc */ public function getId(): string { return $this->id; } /** * Items can be loaded from given hosts. * * @return string[] */ public function getHostSources(): array { return $this->hostSources; } /** * Items can be loaded using following schemes. * * @return string[] */ public function getSchemeSources(): array { return $this->schemeSources; } /** * Items can be loaded from the same host/port as the HTML page. * * @return bool */ public function isSelfAllowed(): bool { return $this->selfAllowed; } /** * Items can be loaded from tags present on the original HTML page. * * @return bool */ public function isInlineAllowed(): bool { return $this->inlineAllowed; } /** * Allows creating items from strings. * * For example using "eval()" for JavaScript. * * @return bool */ public function isEvalAllowed(): bool { return $this->evalAllowed; } /** * Content type governed by this policy is disabled completely. * * @return bool */ public function isNoneAllowed(): bool { return $this->noneAllowed; } /** * @inheritDoc * @SuppressWarnings(PHPMD.CyclomaticComplexity) * @SuppressWarnings(PHPMD.NPathComplexity) */ public function getValue(): string { if ($this->isNoneAllowed()) { return '\'none\''; } else { $sources = $this->getHostSources(); foreach ($this->getSchemeSources() as $schemeSource) { $sources[] = $schemeSource .':'; } if ($this->isSelfAllowed()) { $sources[] = '\'self\''; } if ($this->isInlineAllowed()) { $sources[] = '\'unsafe-inline\''; } if ($this->isEvalAllowed()) { $sources[] = '\'unsafe-eval\''; } if ($this->isDynamicAllowed()) { $sources[] = '\'strict-dynamic\''; } if ($this->areEventHandlersAllowed()) { $sources[] = '\'unsafe-hashes\''; } if (!$this->isInlineAllowed()) { foreach ($this->getNonceValues() as $nonce) { $sources[] = '\'nonce-' . base64_encode($nonce) . '\''; } foreach ($this->getHashes() as $hash => $algorithm) { $sources[] = "'$algorithm-$hash'"; } } return implode(' ', $sources); } } /** * Unique cryptographically random numbers marking inline items as trusted. * * Contains only numbers, not encoded. * * @return string[] */ public function getNonceValues(): array { return $this->nonceValues; } /** * Unique hashes generated based on inline items marking them as trusted. * * Contains only hashes themselves, encoded into base64. Keys are the hashes, values are algorithms used. * * @return string[] */ public function getHashes(): array { return $this->hashes; } /** * Is trust to inline items propagated to items loaded by root items. * * @return bool */ public function isDynamicAllowed(): bool { return $this->dynamicAllowed; } /** * Allows to whitelist event handlers (but not javascript: URLs) with hashes. * * @return bool */ public function areEventHandlersAllowed(): bool { return $this->eventHandlersAllowed; } }