Problem/Motivation

Since version 1.2.1, and perhaps also 2.0.0-rc1, which solved issue 3621734, it is no longer possible to have multiple ALTCHA captcha fields in the same webform. Same issue did not occur with version 1.2.0.
This is usually used if you use field groups in webform, whereas only some of the field groups are shown if certain conditions are met.
When multiple ALTCHA captha fields exists in same webform, the captcha validation always fails.

The reason seems to be that "src/SolutionVerificationBase.php" checks for consumed solutions on line 83, which is added on line 90, and if it is used, it returns false.
If multiple ALTCHA captha fields exists in same webform the same key is validated multiple times, and therefore fails from the second run and forward.

Steps to reproduce

- Use Drupal 11.4.10
- Install drupal/webform
- Install drupal/altcha (version 1.2.1)
- Create a webform with multiple ALTCHA based captcha fields.
- Put a checkmark in all catcha fields.
- Submit the form.
- Observe the validation failure message.

Proposed resolution

Maybe allow the consumed solution to be valid until all captcha validations has run through.
An idea is to keep all the keys, validated within the request, in an array, and if the key exists in that array it skips the validation on line 83.

Remaining tasks

User interface changes

API changes

Data model changes

Issue fork altcha-3631706

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

flemming.fridthjof created an issue. See original summary.

flemming.fridthjof’s picture

Issue summary: View changes

Updated reason and proposed resolution, as the reason was a bit more complex.

flemming.fridthjof’s picture

siddhika_27’s picture

Assigned: Unassigned » siddhika_27

Working on it.

siddhika_27’s picture

Assigned: siddhika_27 » Unassigned

Hi,
I tried reproducing this issue on a fresh Drupal 11.4 site using ALTCHA version 1.2.1 and Webform.
I tested multiple ALTCHA CAPTCHA fields in the same Webform, including fields inside conditional field groups. In both scenarios, all CAPTCHA fields validated successfully, and the form was submitted without any errors.
Could you please share any additional configuration details or reproduction steps that might help me reproduce this issue?
Thank you!

flemming.fridthjof’s picture

It was quite easy for me to reproduce the issue.
I just add the captcha field twice, with different field IDs, to the same webform together with a text field, and when I submit the form the validation fails.

Here is a screenshot of the validation fail:
Proof of validation issue

I've also attached my webform configuration to this issue as "webform.webform.altcha_multiple_captcha_test.yml"

Hope this helps in reproducing the issue.

siddhika_27’s picture

Assigned: Unassigned » siddhika_27
Status: Active » Needs review

I've updated Merge Request !28 with a proposed fix for this issue.

Initially, I looked into handling the consumed solution state inside SolutionVerificationBase.php (and the verification class layer), but that ended up interfering with PHPUnit tests because state was persisting across test assertions.

I think moving the static solution cache directly into altcha_captcha_validation() in altcha.module using drupal_static() is the best fit here. Since form validation runs on a per-request basis, caching the verified result inside the validation callback allows multiple CAPTCHA elements on the same form to validate cleanly during a single request, while keeping service states completely clean across tests and subsequent requests.

I tested this locally with multiple ALTCHA fields on the same form, and everything is now validating smoothly as wanted. Let me know what you think or if any tweaks are needed!
Thank you!

flemming.fridthjof’s picture

I've tested git@git.drupal.org:issue/altcha-3631706.git branch 3631706-multiple-altcha-captcha on my site, whereas the bug initially was found, and it works now.
Multiple ALTCHA captchas within the same webform is validated and accepted.
Great work.