Optionally enable CAPTCHAs for every webform automatically

Created on 5 November 2024, about 2 months ago

Problem/Motivation

After we merged 📌 Add the friendlycaptcha module Active , I got a cool idea - we could use the recipe system's createForEach action to create a CAPTCHA for every webform, and they would all be enabled or disabled based on whether the person applying the recipe opts into using CAPTCHAs. I think this is a great idea - if someone adds a new webform, all they'd need to do to get a CAPTCHA on it is rerun the drupal_cms_forms recipe.

Feature request
Status

Active

Component

Track: Contact form

Created by

🇺🇸United States phenaproxima Massachusetts

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @phenaproxima
  • 🇺🇸United States phenaproxima Massachusetts
  • Pipeline finished with Canceled
    about 2 months ago
    Total: 64s
    #329691
  • First commit to issue fork.
  • Pipeline finished with Failed
    about 2 months ago
    Total: 609s
    #329700
  • Pipeline finished with Failed
    about 2 months ago
    Total: 706s
    #329701
  • 🇦🇺Australia pameeela

    Per https://www.drupal.org/node/3035729 we should be using the Captcha element instead of points. This is mainly because the form ID changes depending on where the form is used, and we are likely to add it to a node via LB -- so we won't know the form ID.

    This approach is also nice because it is quite obvious how it works, since the element is visible in the form builder.

    I also want to check with the Contact form track lead on why they didn't include Captcha in their recipe. Should have done that earlier.

  • 🇺🇸United States phenaproxima Massachusetts

    I actually tried adding a CAPTCHA by using a Webform element, and it just will not work for a number of technical reasons.

    Or, rather - it will work if we add it to the form...but then there's no way to make it optional using a recipe input. This seems to be due to the way Webform is architected. There might be a way out if Webform supported certain config actions, but I'm not holding my breath for that. (Although we could patch Webform to add support for the necessary actions; chances are it would be just one line in a few places.)

    My question is why exactly would we attach the form to a node? Is it just for visibility in the /admin/content list?

  • 🇦🇺Australia pameeela

    why exactly would we attach the form to a node?

    A few reasons, for one: so we can add content to the page, without having to use a webform element. Having it in the list of nodes is also nice! But I wouldn't do it just for that. Not that it's a huge insight but benchmarking shows that almost no one has a page called Contact with just a form on it. They want to add some text, perhaps their business info etc. And doing that using the form itself just kinda sucks. You also don't get regular alias behaviour, or menu settings.

    I get that it's not possible to do this optionally. I do think in that case we can either or enable Captcha by default OR have a separate recipe to enable Captcha; and then a tour for adding it to the form. It's much easier to show a user how to add it to the form than to deal with the Captcha config and endpoints.

  • 🇺🇸United States pfrilling Minster, OH

    I added a config action to the Webform project here https://www.drupal.org/project/webform/issues/3490313#comment-15877907 Add Config Action to enable captcha form elements Active that will add a `captcha` form element. With this, we'll be able to add the captcha points as a form element.

    I have some code to work with that plugin going locally. I'll work on getting it pushed for further testing.

  • 🇺🇸United States phenaproxima Massachusetts

    I don't think we should proceed with this as proposed.

    Why? Because the MR breaks the optionality of having a CAPTCHA. It will always add a CAPTCHA to every webform. That's not the desired behavior.

    I would suggest that we do not use a new config action for this, and instead create CAPTCHA points automatically for every webform, and set their status to what the ${captcha} input specifies. Recipes already have the tools for this; see the createForEachIfNotExists config action from core: https://www.drupal.org/node/3481714 .

  • Pipeline finished with Failed
    24 days ago
    Total: 676s
    #353255
  • Pipeline finished with Failed
    24 days ago
    Total: 730s
    #353254
  • Pipeline finished with Failed
    24 days ago
    Total: 1131s
    #353278
  • Pipeline finished with Success
    24 days ago
    Total: 2371s
    #353279
  • 🇺🇸United States pfrilling Minster, OH

    I believe the intent is to move the contact form into a layout builder section of a basic page so that additional content can be included along with the form. Assuming that is the direction, the form id will never be consistent and will always include a node id. Inserting the captcha as a form element instead of a captcha point will allow the captcha to be present without needing to know a node id.

    The other option would be to have the captcha point utilize the base form id, which should include all derivatives of the contact form placed in a layout builder section.

    I did a quick test of the latter option by adding the base form id `webform_submission_contact_form_form` as a captcha point and placing the contact form in a layout builder section on a node. When browsing that node anonymously, the captcha did show.

    So, either of the above options should work. I guess it might come down to editor experience at that point... does it make sense to have a captcha element on each form like any other form field or does it make sense to route an editor to another configuration page to add a captcha point?

  • 🇺🇸United States phenaproxima Massachusetts

    Let's go with the second option for now, but I'll seek input from @pameeela about this.

  • 🇦🇺Australia pameeela

    I prefer using the form element, it is the recommended approach and is much more intuitive in my opinion. The editor can clearly see it and remove it if they want, or add it to new forms based on this example.

  • 🇺🇸United States phenaproxima Massachusetts

    If that's the preferred approach, then we need to get the necessary config action(s) into a tagged release of Webform. This is very unlikely to happen in time for Drupal CMS v1.0.0. We should probably postpone this issue on Add Config Action to enable captcha form elements Active .

  • 🇦🇺Australia pameeela

    Only if it's not on by default? But I think the consensus is that it could/should be.

    So can we update the form to add this, and worry about making it optional later?

  • 🇦🇺Australia pameeela

    I updated the MR with this, but I'm not able to change the branch to 1.x.

  • Pipeline finished with Failed
    6 days ago
    Total: 55s
    #369618
  • Pipeline finished with Failed
    6 days ago
    Total: 55s
    #369619
  • Pipeline finished with Failed
    6 days ago
    Total: 53s
    #369626
  • Pipeline finished with Failed
    6 days ago
    Total: 65s
    #369625
  • Pipeline finished with Failed
    6 days ago
    Total: 62s
    #369966
  • 🇺🇸United States pfrilling Minster, OH

    I changed the branch to 1.x. I also changed the status back to needs work to get the build errors addressed.

  • Pipeline finished with Failed
    5 days ago
    Total: 563s
    #370347
  • 🇦🇺Australia pameeela

    After rebase there are e2e test fails but the tests just need updating.

  • 🇺🇸United States phenaproxima Massachusetts

    I personally have no strong opinion about whether we use a CAPTCHA. The final word on it belongs to you.

  • 🇺🇸United States phenaproxima Massachusetts
  • 🇺🇸United States pfrilling Minster, OH

    I updated the tests to pass without actually getting a successful submission. Not sure how to get Cypress to bypass and/or pass the Captcha element?

  • Pipeline finished with Success
    4 days ago
    Total: 449s
    #371631
  • 🇩🇪Germany jurgenhaas Gottmadingen

    Not sure how to get Cypress to bypass and/or pass the Captcha element?

    As the purpose of the captcha is to prevent bypass, the user should just that. It should not be possible to submit the form.

    In customer projects we also sometimes need to get the form submit. For that we disable the captcha temporarily.

    That way we can test both.

  • 🇮🇳India rohan_singh India

    rohan_singh changed the visibility of the branch 3485552-captchas-everywhere to hidden.

  • 🇦🇺Australia pameeela

    I'm happy for this to be merged once the tests are satisfactory, not sure if that is now or not but putting it here either way!

  • 🇺🇸United States phenaproxima Massachusetts

    One question, one probable loss of test coverage that should be adjusted instead, and one nitpick. Otherwise this looks good to me!

    Also updating the issue summary to reflect the final MR.

  • 🇺🇸United States pfrilling Minster, OH

    I addressed the MR feedback. This is ready to review again.

  • Pipeline finished with Success
    4 days ago
    Total: 340s
    #372520
  • 🇺🇸United States phenaproxima Massachusetts

    Thanks @pfrilling. That looks better - but we had a miscommunication (I'm sorry about that!) regarding the test coverage.

  • 🇺🇸United States pfrilling Minster, OH

    I believe I addressed everything. Please review again.

  • Pipeline finished with Canceled
    4 days ago
    Total: 275s
    #372553
  • 🇺🇸United States phenaproxima Massachusetts

    Zero complaints remaining. Nice work. I'll fend off any random failures and merge this as soon as the pipeline is green.

  • 🇺🇸United States phenaproxima Massachusetts
  • Pipeline finished with Skipped
    4 days ago
    #372565
  • 🇺🇸United States phenaproxima Massachusetts

    Great work here, thanks for sticking with it through that unexpectedly lengthy review process. Merged into 1.x, thanks!

Production build 0.71.5 2024