Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

CSP's nonce support for ReCaptchaBuilderV3 #95

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/ReCaptchaBuilderV3.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,9 @@ public function getValidationUrlWithToken(): string
]
);
}

/**
* Write script HTML tag in you HTML code
* Write script HTML tag in your HTML code
* Insert before </head> tag
*
* @param array|null $configuration
Expand All @@ -70,17 +70,19 @@ public function getValidationUrlWithToken(): string
public function htmlScriptTagJsApi(?array $configuration = []): string
{

$nonce = isset($configuration['nonce']) ? implode('', ['nonce="', $configuration['nonce'], '"']) : '';

if ($this->skip_by_ip) {
return '';
}

$html = "<script src=\"" . $this->api_js_url . "?render={$this->api_site_key}\"></script>";
$html = "<script {$nonce} src=\"" . $this->api_js_url . "?render={$this->api_site_key}\"></script>";

$action = Arr::get($configuration, 'action', 'homepage');

$js_custom_validation = Arr::get($configuration, 'custom_validation', '');

// Check if set custom_validation. That function will override default fetch validation function
// Check if set custom_validation. That function will override the default fetch validation function
if ($js_custom_validation) {

$validate_function = ($js_custom_validation) ? "{$js_custom_validation}(token);" : '';
Expand All @@ -107,7 +109,7 @@ public function htmlScriptTagJsApi(?array $configuration = []): string
});";
}

$html .= "<script>
$html .= "<script {$nonce}>
var csrfToken = document.head.querySelector('meta[name=\"csrf-token\"]');
grecaptcha.ready(function() {
grecaptcha.execute('{$this->api_site_key}', {action: '{$action}'}).then(function(token) {
Expand Down