-
-
Notifications
You must be signed in to change notification settings - Fork 0
Fixed captcha on login screen. #139
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
68 changes: 68 additions & 0 deletions
68
web/modules/custom/do_base/js/do_base.recaptcha_v3_guard.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| /** | ||
| * @file | ||
| * Prevents form submission until the reCAPTCHA v3 token is populated. | ||
| * @param {Object} Drupal - The Drupal object. | ||
| * @param {Function} once - The once function. | ||
| */ | ||
|
|
||
| (function doBaseRecaptchaV3Guard(Drupal, once) { | ||
| /** | ||
| * Enables all submit buttons in a form. | ||
| * | ||
| * @param {NodeList} submits - The submit buttons to enable. | ||
| */ | ||
| function enableSubmits(submits) { | ||
| submits.forEach(function enableBtn(btn) { | ||
| btn.disabled = false; | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
| * Watches a token input and enables submit buttons when the token is set. | ||
| * | ||
| * @param {HTMLElement} tokenInput - The reCAPTCHA token hidden input. | ||
| * @param {NodeList} submits - The submit buttons to control. | ||
| */ | ||
| function watchToken(tokenInput, submits) { | ||
| // Poll for the token value since hidden input value changes do not | ||
| // reliably fire DOM events. | ||
| const poll = setInterval(function checkToken() { | ||
| if (tokenInput.value) { | ||
| clearInterval(poll); | ||
| enableSubmits(submits); | ||
| } | ||
| }, 200); | ||
|
|
||
| // Safety timeout: re-enable buttons after 5 seconds regardless so the | ||
| // form is never permanently locked. | ||
| setTimeout(function safetyTimeout() { | ||
| clearInterval(poll); | ||
| enableSubmits(submits); | ||
| }, 5000); | ||
| } | ||
|
|
||
| Drupal.behaviors.doBaseRecaptchaV3Guard = { | ||
| attach: function attachGuard(context) { | ||
| once('recaptcha-v3-guard', '.recaptcha-v3-token', context).forEach( | ||
| function processToken(tokenInput) { | ||
| const form = tokenInput.closest('form'); | ||
| if (!form) { | ||
| return; | ||
| } | ||
|
|
||
| const submits = form.querySelectorAll('[type="submit"]'); | ||
| if (!submits.length) { | ||
| return; | ||
| } | ||
|
|
||
| // Disable submit buttons until the token is ready. | ||
| submits.forEach(function disableBtn(btn) { | ||
| btn.disabled = true; | ||
| }); | ||
|
|
||
| watchToken(tokenInput, submits); | ||
| }, | ||
| ); | ||
| }, | ||
| }; | ||
| })(Drupal, once); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace Drupal\do_base\Hook; | ||
|
|
||
| use Drupal\Core\Hook\Attribute\Hook; | ||
|
|
||
| /** | ||
| * Form alter hooks for do_base module. | ||
| */ | ||
| final class FormAlterHook { | ||
|
|
||
| /** | ||
| * Implements hook_form_alter(). | ||
| * | ||
| * Attaches the reCAPTCHA v3 guard library to forms that use reCAPTCHA v3 | ||
| * to prevent submission before the token is populated. | ||
| */ | ||
| #[Hook('form_alter')] | ||
| public function alter(array &$form): void { | ||
| if (!isset($form['captcha'])) { | ||
| return; | ||
| } | ||
|
|
||
| $captcha_type = $form['captcha']['#captcha_type'] ?? ''; | ||
| if (str_contains($captcha_type, 'recaptcha_v3')) { | ||
| $form['#attached']['library'][] = 'do_base/recaptcha_v3_guard'; | ||
| } | ||
| } | ||
|
|
||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Preserve existing
modifier_classvalues instead of always overwriting (Line 35).The comment says to allow existing classes, but Line 35 always forces
modifier_classtoFALSE, which can wipe module-provided modifier classes. Consider only forcingFALSEwhen no value is present.🛠️ Suggested fix
function drevops_preprocess_input__hidden(array &$variables): void { // Hidden inputs should not have classes as classes are used for presentation. // But some contributed modules use classes on hidden inputs and target them // with JavaScript, so we need to allow classes if they are already set. - $variables['modifier_class'] = FALSE; + if (empty($variables['modifier_class'])) { + $variables['modifier_class'] = FALSE; + } }📝 Committable suggestion
🤖 Prompt for AI Agents