Repository navigation
Fix invisible reCAPTCHA when multiple widgets are disabled #3727
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| import assert from 'node:assert/strict'; | ||
| import { readFileSync } from 'node:fs'; | ||
| import { test } from 'node:test'; | ||
| import { runInNewContext } from 'node:vm'; | ||
|
|
||
| const source = readFileSync( new URL( '../../js/formidable.js', import.meta.url ), 'utf8' ); | ||
|
|
||
| /** | ||
| * Submit a form with an invisible reCAPTCHA and record what it does. | ||
| * | ||
| * @param {string} widgetClass The widget's rendering class. | ||
| * @param {string|void} widgetId The ID assigned by Formidable's explicit renderer. | ||
| * @param {Function} getResponse Returns the current response from reCAPTCHA. | ||
| * @param {Object} proForm Optional Pro form methods. | ||
| * @return {Array} Calls made to reCAPTCHA and the immediate submission handler. | ||
| */ | ||
| function submitForm( widgetClass, widgetId, getResponse, proForm ) { | ||
| const calls = []; | ||
| const widget = { | ||
| classList: { contains: name => name === widgetClass }, | ||
| dataset: { rid: widgetId } | ||
| }; | ||
| const form = { | ||
| classList: { contains: () => false }, | ||
| querySelector: selector => selector.includes( `.${ widgetClass }[data-size="invisible"]` ) ? widget : null | ||
| }; | ||
| function jQuery() { | ||
| return { ready() {}, hasClass: () => true, querySelectorAll: () => [] }; | ||
| } | ||
| const context = { | ||
| window: {}, | ||
| document: { body: { classList: { contains: () => false } } }, | ||
| jQuery, | ||
| grecaptcha: getResponse && { | ||
| getResponse: id => { | ||
| calls.push( [ 'getResponse', id ] ); | ||
| return getResponse( id ); | ||
| }, | ||
| reset: id => calls.push( [ 'reset', id ] ), | ||
| execute: id => calls.push( [ 'execute', id ] ) | ||
| }, | ||
| frmProForm: proForm | ||
| }; | ||
|
|
||
| runInNewContext( source, context ); | ||
| const { frmFrontForm } = context.window; | ||
| context.frmFrontForm = frmFrontForm; | ||
| frmFrontForm.submitFormNow = () => calls.push( [ 'submit' ] ); | ||
| frmFrontForm.submitFormManual( { preventDefault() {} }, form ); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
|
||
| return calls; | ||
| } | ||
|
|
||
| test( 'Invisible reCAPTCHA executes the auto-rendered widget without an ID', () => { | ||
| const calls = submitForm( 'g-recaptcha', undefined, () => '' ); | ||
|
|
||
| assert.deepEqual( calls, [ [ 'getResponse', undefined ], [ 'reset', undefined ], [ 'execute', undefined ] ] ); | ||
| } ); | ||
|
|
||
| test( 'Invisible reCAPTCHA executes the explicitly rendered widget by ID with Pro', () => { | ||
| const calls = submitForm( 'frm-g-recaptcha', '0', () => '', { goingToPreviousPage: () => false } ); | ||
|
|
||
| assert.deepEqual( calls, [ [ 'getResponse', '0' ], [ 'reset', '0' ], [ 'execute', '0' ] ] ); | ||
| } ); | ||
|
|
||
| test( 'An explicit invisible widget without an ID cannot run the default widget', () => { | ||
| const calls = submitForm( 'frm-g-recaptcha', undefined, () => '' ); | ||
|
|
||
| assert.deepEqual( calls, [ [ 'submit' ] ] ); | ||
| } ); | ||
|
|
||
| test( 'An unavailable reCAPTCHA API allows the submission to reach server validation', () => { | ||
| assert.deepEqual( submitForm( 'g-recaptcha' ), [ [ 'submit' ] ] ); | ||
| } ); | ||
|
|
||
| test( 'An auto-rendered widget that is not ready allows the submission to reach server validation', () => { | ||
| const calls = submitForm( 'g-recaptcha', undefined, () => { | ||
| throw new Error( 'No reCAPTCHA clients exist.' ); | ||
| } ); | ||
|
|
||
| assert.deepEqual( calls, [ [ 'getResponse', undefined ], [ 'submit' ] ] ); | ||
| } ); | ||
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.
Having empty functions hurts readability, and is considered a code-smell. There's almost always a way to avoid using them. If you must use one, consider adding a comment to inform the reader of its purpose.