@@ -111,27 +111,25 @@ def create_email(to_email, identifier, text, all_attachments, reference=''):
111111
112112 return msg
113113
114- def validate_recaptcha ( recaptcha_response ):
114+ def validate_turnstile ( turnstile_response ):
115115 """
116- Validates the ReCaptcha response using Google 's API.
116+ Validates the Turnstile response using Cloudflare 's API.
117117 """
118- secret_key = os .getenv ('RECAPTCHASECRETKEY ' )
118+ secret_key = os .getenv ('TURNSTILE_SECRET_KEY ' )
119119 payload = {
120120 'secret' : secret_key ,
121- 'response' : recaptcha_response
121+ 'response' : turnstile_response
122122 }
123- response = requests .post ('https://www.google .com/recaptcha/api /siteverify' , data = payload )
123+ response = requests .post ('https://challenges.cloudflare .com/turnstile/v0 /siteverify' , data = payload )
124124 result = response .json ()
125125
126126 # Log the validation result
127- logging .info (f"ReCaptcha validation response: { result } " )
127+ logging .info (f"Turnstile validation response: { result } " )
128128
129129 if not result .get ('success' ):
130- raise ValueError ('ReCaptcha verification failed.' )
131-
132- # Check action and score thresholds for additional security
133- if result .get ('score' , 1.0 ) < 0.5 :
134- raise ValueError ('ReCaptcha score is too low, indicating potential abuse.' )
130+ error_codes = result .get ('error-codes' , [])
131+ logging .error (f"Turnstile verification failed with error codes: { error_codes } " )
132+ raise ValueError ('Turnstile verification failed.' )
135133
136134def send_email (message ):
137135 """
@@ -187,11 +185,11 @@ def get_forwarded_address():
187185 return get_remote_address ()
188186
189187# Validate required environment variables
190- required_env_vars = ['RECAPTCHASITEKEY ' , 'RECAPTCHASECRETKEY ' , 'AWS_ACCESS_KEY_ID' , 'AWS_SECRET_ACCESS_KEY' , 'AWS_REGION' , 'SES_FROM_EMAIL' ]
188+ required_env_vars = ['TURNSTILE_SITE_KEY ' , 'TURNSTILE_SECRET_KEY ' , 'AWS_ACCESS_KEY_ID' , 'AWS_SECRET_ACCESS_KEY' , 'AWS_REGION' , 'SES_FROM_EMAIL' ]
191189validate_env_vars (required_env_vars )
192190
193- RECAPTCHASITEKEY = os .environ ['RECAPTCHASITEKEY ' ]
194- RECAPTCHASECRETKEY = os .environ ['RECAPTCHASECRETKEY ' ]
191+ TURNSTILE_SITE_KEY = os .environ ['TURNSTILE_SITE_KEY ' ]
192+ TURNSTILE_SECRET_KEY = os .environ ['TURNSTILE_SECRET_KEY ' ]
195193AWS_ACCESS_KEY_ID = os .environ ['AWS_ACCESS_KEY_ID' ]
196194AWS_SECRET_ACCESS_KEY = os .environ ['AWS_SECRET_ACCESS_KEY' ]
197195AWS_REGION = os .environ ['AWS_REGION' ]
@@ -222,7 +220,7 @@ def get_forwarded_address():
222220
223221@app .route ('/' , methods = ['GET' ])
224222def index ():
225- return render_template ('index.html' , notice = '' , hascaptcha = True , attachments_number = Config .NUMBER_OF_ATTACHMENTS , recaptcha_sitekey = RECAPTCHASITEKEY )
223+ return render_template ('index.html' , notice = '' , hascaptcha = True , attachments_number = Config .NUMBER_OF_ATTACHMENTS , turnstile_sitekey = TURNSTILE_SITE_KEY )
226224
227225@app .route ('/submit-encrypted-data' , methods = ['POST' ])
228226@limiter .limit ("3 per minute" )
@@ -231,14 +229,14 @@ def submit():
231229 # Parse JSON data from request
232230 data = request .get_json ()
233231
234- # Validate ReCaptcha
235- recaptcha_response = data .get ('g-recaptcha -response' , '' )
236- if not recaptcha_response :
237- logging .warning (f"Missing ReCaptcha response. Potential bypass attempt detected from IP: { request .remote_addr } " )
238- return jsonify ({'status' : 'failure' , 'message' : 'Missing ReCaptcha token' }), 400
232+ # Validate Turnstile
233+ turnstile_response = data .get ('cf-turnstile -response' , '' )
234+ if not turnstile_response :
235+ logging .warning (f"Missing Turnstile response. Potential bypass attempt detected from IP: { request .remote_addr } " )
236+ return jsonify ({'status' : 'failure' , 'message' : 'Missing Turnstile token' }), 400
239237
240238 try :
241- validate_recaptcha ( recaptcha_response )
239+ validate_turnstile ( turnstile_response )
242240 except ValueError as e :
243241 return jsonify ({'status' : 'failure' , 'message' : str (e )}), 400
244242
0 commit comments