Skip to content
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

fix(recaptcha): add safeguards against duplicate recaptcha rendering #3624

Merged
merged 1 commit into from
Dec 13, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions src/other-scripts/recaptcha/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ const isV3 = 'v3' === newspack_recaptcha_data.version;
const siteKey = newspack_recaptcha_data.site_key;
const isInvisible = 'v2_invisible' === newspack_recaptcha_data.version;

/**
* Destroy hidden reCAPTCHA v3 token fields to avoid unnecessary reCAPTCHA checks.
*/
function destroy( forms = [] ) {
if ( isV3 ) {
const formsToHandle = forms.length
? forms
: [ ...document.querySelectorAll( 'form[data-newspack-recaptcha]' ) ];

formsToHandle.forEach( form => {
removeHiddenField( form );
} );
}
}

/**
* Refresh the reCAPTCHA v3 token for the given form and action.
*
Expand Down Expand Up @@ -140,6 +155,12 @@ function renderWidget( form, onSuccess = null, onError = null ) {
return;
}

// Don't render widget if the button is currently rendering recaptcha.
if ( button.hasAttribute( 'data-recaptcha-processing' ) ) {
return;
}
button.setAttribute( 'data-recaptcha-processing', 'true' );

// Callback when reCAPTCHA passes validation.
const successCallback = () => {
onSuccess?.()
Expand All @@ -160,8 +181,8 @@ function renderWidget( form, onSuccess = null, onError = null ) {
clearInterval( refreshIntervalId );
}
const message = retryCount < 3
? wp.i18n.__( 'There was an error with reCAPTCHA. Please try again.', 'newspack-plugin' )
: wp.i18n.__( 'There was an error with reCAPTCHA. Please reload the page and try again.', 'newspack-plugin' );
? wp.i18n.__( 'There was an error connecting with reCAPTCHA. Please try submitting again.', 'newspack-plugin' )
: wp.i18n.__( 'There was an error connecting with reCAPTCHA. Please reload the page and try again.', 'newspack-plugin' );
if ( onError ) {
onError( message );
} else {
Expand All @@ -170,20 +191,25 @@ function renderWidget( form, onSuccess = null, onError = null ) {
alert( message );
}
},
'expired-callback': () => {
refreshWidget( button );
},
} );

button.setAttribute( 'data-recaptcha-widget-id', widgetId );
const refreshIntervalId = setInterval( () => refreshWidget( button ), 120000 ); // Refresh widget every 2 minutes.

button.addEventListener( 'click', e => {
e.preventDefault();
e.stopImmediatePropagation();
// Skip reCAPTCHA verification if the button has a data-skip-recaptcha attribute.
if ( button.hasAttribute( 'data-skip-recaptcha' ) ) {
successCallback();
} else {
grecaptcha.execute( widgetId );
}
} );
button.removeAttribute( 'data-recaptcha-processing' );
} );
}

Expand All @@ -205,30 +231,19 @@ function render( forms = [], onSuccess = null, onError = null ) {
: [ ...document.querySelectorAll( 'form[data-newspack-recaptcha]' ) ];

formsToHandle.forEach( form => {
if ( form.hasAttribute( 'data-recaptcha-rendered' ) ) {
return;
}
if ( isV3 ) {
addHiddenField( form );
}
if ( isV2 ) {
renderWidget( form, onSuccess, onError );
}
form.setAttribute( 'data-recaptcha-rendered', 'true' );
} );
}

/**
* Destroy hidden reCAPTCHA v3 token fields to avoid unnecessary reCAPTCHA checks.
*/
function destroy( forms = [] ) {
if ( isV3 ) {
const formsToHandle = forms.length
? forms
: [ ...document.querySelectorAll( 'form[data-newspack-recaptcha]' ) ];

formsToHandle.forEach( form => {
removeHiddenField( form );
} );
}
}

/**
* Invoke only after reCAPTCHA API is ready.
*/
Expand Down
Loading