random = $random; $this->dynamicCollector = $dynamicCollector; } /** * Generate nonce and add it to the CSP header * * @return string * @throws LocalizedException */ public function generateNonce(): string { if (empty($this->nonce)) { $this->nonce = $this->random->getRandomString( self::NONCE_LENGTH, Random::CHARS_DIGITS . Random::CHARS_LOWERS ); $policy = new FetchPolicy( 'script-src', false, [], [], false, false, false, [$this->nonce], [] ); $this->dynamicCollector->add($policy); } return base64_encode($this->nonce); } }