@@ -194,24 +194,54 @@ async function verifyCaptcha(turnstileToken, ip) {
194194 if ( DEBUG_MODE ) {
195195 logger . warn ( 'Verify captcha for %s' , ip ) ;
196196 } else {
197+ if ( ! turnstileSecret || turnstileSecret . trim ( ) === '' ) {
198+ logger . error ( 'TURNSTILE_SECRET is not configured' ) ;
199+ throw new Error ( 'Turnstile secret key is not configured' ) ;
200+ }
201+
202+ if ( ! turnstileToken || turnstileToken . trim ( ) === '' ) {
203+ throw new Error ( 'Turnstile token is required' ) ;
204+ }
205+
197206 const url = `https://challenges.cloudflare.com/turnstile/v0/siteverify` ;
198207 const formData = `secret=${ encodeURIComponent (
199208 turnstileSecret
200209 ) } &response=${ encodeURIComponent (
201210 turnstileToken
202211 ) } &remoteip=${ encodeURIComponent ( ip ) } `;
203212
204- const response = await ( await fetch ( url , {
205- method : 'POST' ,
206- headers : {
207- 'Content-Type' : 'application/x-www-form-urlencoded' ,
208- } ,
209- body : formData ,
210- } ) ) . json ( ) ;
213+ let response ;
214+ try {
215+ const fetchResponse = await fetch ( url , {
216+ method : 'POST' ,
217+ headers : {
218+ 'Content-Type' : 'application/x-www-form-urlencoded' ,
219+ } ,
220+ body : formData ,
221+ } ) ;
222+ response = await fetchResponse . json ( ) ;
223+ } catch ( error ) {
224+ logger . error ( { error } , 'Failed to verify Turnstile token' ) ;
225+ throw new Error ( 'Failed to verify Turnstile token' ) ;
226+ }
211227
212228 if ( ! response . success ) {
213229 const errors = response [ 'error-codes' ] || [ 'unknown' ] ;
214- throw new Error ( `Turnstile verification failed: ${ errors . join ( ) } ` ) ;
230+ logger . warn (
231+ { errorCodes : errors , ip } ,
232+ 'Turnstile verification failed'
233+ ) ;
234+
235+ // Provide more specific error messages
236+ if ( errors . includes ( 'invalid-input-secret' ) ) {
237+ throw new Error (
238+ 'Turnstile verification failed: Invalid secret key. Please check TURNSTILE_SECRET configuration.'
239+ ) ;
240+ }
241+
242+ throw new Error (
243+ `Turnstile verification failed: ${ errors . join ( ', ' ) } `
244+ ) ;
215245 }
216246 }
217247}
0 commit comments