Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forms cannot be submitted after two minutes using v3 #36

Open
coreyworrell opened this issue Jan 24, 2022 · 7 comments
Open

Forms cannot be submitted after two minutes using v3 #36

coreyworrell opened this issue Jan 24, 2022 · 7 comments

Comments

@coreyworrell
Copy link

According to the docs there is a 2 minute limit on a valid CAPTCHA, and it appears that this plugin executes the CAPTCHA on page load rather than when a form is actually submitted. If you load page, wait over 2 minutes, then try to submit, the CAPTCHA always fails.

@coreyworrell
Copy link
Author

This seems to happen with the v2 invisible option as well.

@godiez
Copy link

godiez commented Feb 4, 2022

Hi,

I run into this bug, but actually I got the first fail (timeout / duplicate), then a re-submit the form (without reload the page) and works fine.

@remyPulse
Copy link

https://developers.google.com/recaptcha/docs/v3

Note: reCAPTCHA tokens expire after two minutes. If you're protecting an action with reCAPTCHA, make sure to call execute when the user takes the action rather than on page load.

@godiez
Copy link

godiez commented Jun 2, 2022

With this workaround

$onLoad = <<<JSELEMENT JSELEMENT; if ('v3' == $field['customParameters']['version']) { $onLoad = <<<JSELEMENT setInterval(function () { onLoad{$hashedFormName}(); }, 110000); JSELEMENT; }

I solved the two minutes expiring issue.

@remyPulse
Copy link

any hint about where you put this line ? thanks in advance :-)

@godiez
Copy link

godiez commented Jun 2, 2022

Yeah, sure. On this file

Here's my full code on 'recaptcha.html.php' file:

<?php

$defaultInputClass = (isset($inputClass)) ? $inputClass : 'input';
$containerType     = 'div-wrapper';

include __DIR__.'/../../../../app/bundles/FormBundle/Views/Field/field_helper.php';

$action   = $app->getRequest()->get('objectAction');
$settings = $field['properties'];

$formName    = str_replace('_', '', $formName);
$hashedFormName = md5($formName);
$formButtons = (!empty($inForm)) ? $view->render(
    'MauticFormBundle:Builder:actions.html.php',
    [
        'deleted'        => false,
        'id'             => $id,
        'formId'         => $formId,
        'formName'       => $formName,
        'disallowDelete' => false,
    ]
) : '';

$label = (!$field['showLabel'])
    ? ''
    : <<<HTML
<label $labelAttr>{$view->escape($field['label'])}</label>
HTML;
$onLoad = <<<JSELEMENT
JSELEMENT;
if ('v3' == $field['customParameters']['version']) {
    $onLoad = <<<JSELEMENT
    setInterval(function () {
        onLoad{$hashedFormName}();
    }, 110000);
JSELEMENT;
}

$jsElement = <<<JSELEMENT
	<script type="text/javascript">
    $onLoad
    function verifyCallback_{$hashedFormName}( response ) {
        document.getElementById("mauticform_input_{$formName}_{$field['alias']}").value = response;
    }
    function onLoad{$hashedFormName}() { 
        grecaptcha.execute('{$field['customParameters']['site_key']}', {action: 'form'}).then(function(token) {
            verifyCallback_{$hashedFormName}(token);
         }); 
    }
</script>
JSELEMENT;

if($field['customParameters']['version'] == 'v2') {
$jsElement .= <<<JSELEMENT
<script src='https://www.google.com/recaptcha/api.js'></script>
JSELEMENT;
} else {
$jsElement .= <<<JSELEMENT
<script src='https://www.google.com/recaptcha/api.js?onload=onLoad{$hashedFormName}&render={$field['customParameters']['site_key']}'></script>
JSELEMENT;
}

$html = <<<HTML
    {$jsElement}
	<div $containerAttr>
        {$label}
HTML;

if($field['customParameters']['version'] == 'v2') {
$html .= <<<HTML
<div class="g-recaptcha" data-sitekey="{$field['customParameters']['site_key']}" data-callback="verifyCallback_{$hashedFormName}"></div>
HTML;
}

$html .= <<<HTML
        <input $inputAttr type="hidden">
        <span class="mauticform-errormsg" style="display: none;"></span>
    </div>
HTML;
?>



<?php
echo $html;
?>

@kuzmany
Copy link
Contributor

kuzmany commented Feb 10, 2023

I've create PR #44

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants