sepiariver / recaptchav2

Google's ReCaptcha V2 + V3 for MODX CMS: https://sepiariver.com/modx/recaptchav2-supports-recaptchav3/
https://sepiariver.com/
GNU General Public License v2.0
21 stars 15 forks source link

Problems with ReCaptchaV2 and FormIt #45

Closed MedusaDelft closed 5 years ago

MedusaDelft commented 5 years ago

Hi,

I have installed ModX 2.6.5 with FormIt 4.2.3 and ReCaptchaV2 3.1.0-rc on Apache/php 7.2.

When I add recaptchav3 to the FormIt-hook, I get various errors in my server-log and the response of the server is: Error 500 - Unable to process request. I upgraded ModX to 2.7.1, same problem. In the log-file I see (I removed the domain name):

2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: Stack trace:, referer: https://www..nl/contact.html |   |   |   | Apache error
-- | -- | -- | -- | -- | -- | -- | -- | --
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #0 /var/www/vhosts/.nl/httpdocs/core/model/modx/modscript.class.php(76): include(), referer: https://www..nl/contact.html |   |   |   | Apache error
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #1 /var/www/vhosts/.nl/httpdocs/core/components/formit/src/FormIt/Hook.php(148): modScript->process(NULL), referer: https://www..nl/contact.html |   |   |   | Apache error
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #2 /var/www/vhosts/.nl/httpdocs/core/components/formit/src/FormIt/Hook.php(106): Sterc\\FormIt\\Hook->load('recaptchav3', Array, Array), referer: https://www..nl/contact.html |   |   |   | Apache error
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #3 /var/www/vhosts/.nl/httpdocs/core/components/formit/src/FormIt/Request.php(401): Sterc\\FormIt\\Hook->loadMultiple(Array, Array), referer: https://www..nl/contact.html |   |   |   | Apache error
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #4 /var/www/vhosts/.nl/httpdocs/core/components/formit/src/FormIt/Request.php(344): Sterc\\FormIt\\Request->runPostHooks(), referer: https://www..nl/contact.html |   |   |   | Apache error
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #5 /var/www/vhosts/.nl/httpdocs/core/components/formit/src/FormIt/Request.php(253): Sterc\\FormIt\\Request->postProcess(), referer: https://www..nl/contact.html |   |   |   | Apache error
2019-08-03 15:39:12 | Warning | 80.101.212.253 |   | mod_fcgid: stderr: #6 /var/www/vhosts/ in /var/www/vhosts/.nl/httpdocs/core/cache/includes/elements/modsnippet/37.include.cache.php on line 114, referer: https://www..nl/contact.html

It looks like problems with various function calls. Array's passed, functions not available that are expected The site is pretty basic. About 15 plugins.

The snippet:

`[[!FormIt?
       &hooks= spam,email
       &emailFrom= info@.nl 
       &emailTpl= contact-form-emailTpl
       &emailTo= info@.nl,[[+frmEmail]]
       &emailSubject= Contactbericht - via Contactformulier
       &validate= frmNaam:required,
                  frmEmail:email:required,
                  frmBericht:required:stripTags
        &frmNaam.vTextRequired= U heeft uw naam niet ingevuld.
        &frmEmail.vTextRequired= U heeft uw email-adres niet ingevuld.
        &frmEmail.TextEmailInvalid= U heeft een fout in uw email-adres gemaakt.
        &frmBericht.vTextRequired= U heeft geen bericht ingevuld.
        &validationErrorMessage= <div class="bg-danger text-danger form-danger-adjustment">
  <h4 class="text-danger">De volgende fouten zijn opgetreden:</h4>
    <ul>
        [[+errors]]
    </ul>
    [[!+fi.error.recaptchav3_error]]
</div>
        &successMessage= <div class="bg-success text-success form-success-adjustment">
  <h4 class="text-success">Het formulier is succesvol verstuurd! U ontvangt hiervan ook een email.</h4>
</div>
]]`

I also have a similar problem on another site. I do get the response page, but the recaptchav3 seems to be ignored.

So I guess RecaptchaV2 and FormIt don't work together pretty good or I am doing something wrong. If so, please point me in the right direction. If you need more information, please let me know.

sepiariver commented 5 years ago

Hi @medusadelft

The README has details on how to use RecaptchaV2 with FormIt: https://github.com/sepiariver/recaptchav2/blob/master/README.md#usage-examples

A couple of notes from your posted code:

  1. The RecaptchaV2 (V3 in this case) hook is not being added to the hooks property.
  2. This may be an issue with the github markdown but Snippet property values must be delimited with back-ticks

I’m guessing the main issue here is the lack of the required hook.

sepiariver commented 5 years ago

Sorry @MedusaDelft I just noticed you mentioned you had tried it with the hook in place. What is the Snippet with ID 37 please? And also what is on line 114 of that Snippet?

MedusaDelft commented 5 years ago

Yes, I have tried it with the hook in place. I forgot to add it, as I removed it so the form is working again. The backticks I removed.

ID 37 (37.include) starts with:

<?php
/**
 * recaptchav3 hook for use with MODX form processors
 *
 * Based on https://github.com/google/recaptcha
 *
 * @copyright Copyright (c) 2014, Google Inc.
 * @link      http://www.google.com/recaptcha
 *
 * Ported to MODX by YJ Tso @sepiariver
 *

Line 114 is the foreach:

// Hook pass/fail
if ($resp != null && $resp->isSuccess()) {
    return true;
} else {
    $msg = '';
    if ($props['display_resp_errors']) {
        foreach ($resp->getErrorCodes() as $error) {
            $msg .= $error . "\n";
        }
    }
    if (empty($msg)) $msg = $recaptcha_err_msg;
    $hook->addError('recaptchav3_error', $msg);
    $modx->log(modX::LOG_LEVEL_DEBUG, print_r($resp, true));
    return false;
}

Sorry for the confiusion I caused with the missing hook in the formit snippet.

sepiariver commented 5 years ago

Thanks @medusadelft that was very helpful. I’ve updated the Snippet here: https://github.com/sepiariver/recaptchav2/blob/master/core/components/recaptchav2/elements/snippets/recaptchav3.snippet.php

Would you mind please trying the new code in the Snippet ID 37?

Note: I think you are getting a null or otherwise invalid response from Recaptcha. This fix shouldn’t change that, but will prevent the horrible server error when it happens.