Skip to content

Commit

Permalink
Merge pull request #125 from mvasilyev/master
Browse files Browse the repository at this point in the history
Adding script nonce parameter for Content Security Policy headers
  • Loading branch information
albertcht authored Jan 26, 2021
2 parents 393b597 + 76e5f1c commit 23f90ec
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions src/InvisibleReCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,11 @@ public function getPolyfillJs()
*
* @return string
*/
public function render($lang = null)
public function render($lang = null, $nonce = null)
{
$html = $this->renderPolyfill();
$html .= $this->renderCaptchaHTML();
$html .= $this->renderFooterJS($lang);
$html .= $this->renderFooterJS($lang, $nonce);
return $html;
}

Expand Down Expand Up @@ -128,9 +128,13 @@ public function renderCaptchaHTML()
*
* @return string
*/
public function renderFooterJS($lang = null)
public function renderFooterJS($lang = null, $nonce = null)
{
$html = '<script src="' . $this->getCaptchaJs($lang) . '" async defer></script>' . PHP_EOL;
$html = '<script src="' . $this->getCaptchaJs($lang) . '" async defer';
if (isset($nonce) && !empty($nonce)){
$html .= ' nonce="' . $nonce . '"';
}
$html .= '></script>' . PHP_EOL;
$html .= '<script>var _submitForm,_captchaForm,_captchaSubmit,_execute=true,_captchaBadge;</script>';
$html .= "<script>window.addEventListener('load', _loadCaptcha);" . PHP_EOL;
$html .= "function _loadCaptcha(){";
Expand Down
4 changes: 2 additions & 2 deletions src/InvisibleReCaptchaServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ public function addBladeDirective(BladeCompiler $blade)
$blade->directive('captchaHTML', function () {
return "<?php echo app('captcha')->renderCaptchaHTML(); ?>";
});
$blade->directive('captchaScripts', function ($lang) {
return "<?php echo app('captcha')->renderFooterJS({$lang}); ?>";
$blade->directive('captchaScripts', function ($lang, $nonce) {
return "<?php echo app('captcha')->renderFooterJS({$lang}, {$nonce}); ?>";
});
}
}

0 comments on commit 23f90ec

Please sign in to comment.