@@ -81,7 +81,7 @@ def create_email(to_email, identifier, text, all_attachments, reference=''):
81
81
subject = f'Secure Form Submission { identifier } '
82
82
if reference :
83
83
subject = f'{ reference } { subject } '
84
-
84
+
85
85
message = Mail (
86
86
from_email = FROMEMAIL ,
87
87
to_emails = to_email ,
@@ -108,15 +108,21 @@ def validate_recaptcha(recaptcha_response):
108
108
Validates the ReCaptcha response.
109
109
"""
110
110
try :
111
+ if not recaptcha_response :
112
+ logging .error ('No ReCaptcha response provided.' )
113
+ raise ValueError ('ReCaptcha verification failed: No response provided.' )
114
+
115
+ # Perform the verification
111
116
if not recaptcha .verify (response = recaptcha_response ):
112
117
logging .error ('ReCaptcha verification failed for response: %s' , recaptcha_response )
113
- raise ValueError ('Error: ReCaptcha verification failed! ' )
114
- else :
115
- logging .info ('ReCaptcha verification succeeded' )
118
+ raise ValueError ('ReCaptcha verification failed. ' )
119
+
120
+ logging .info ('ReCaptcha verification succeeded for response: %s' , recaptcha_response )
116
121
except Exception as e :
117
- logging .error ('ReCaptcha validation encountered an error : %s' , str (e ))
122
+ logging .error ('Error during ReCaptcha validation : %s' , str (e ))
118
123
raise
119
124
125
+
120
126
def send_email (message ):
121
127
"""
122
128
Sends the email using SendGrid and logs detailed information for debugging.
@@ -163,6 +169,7 @@ def send_email(message):
163
169
def index ():
164
170
return render_template ('index.html' , notice = '' , hascaptcha = not Config .DEBUG_MODE , attachments_number = Config .NUMBER_OF_ATTACHMENTS , recaptcha_sitekey = RECAPTCHASITEKEY )
165
171
172
+
166
173
@app .route ('/submit-encrypted-data' , methods = ['POST' ])
167
174
@limiter .limit ("5 per minute" )
168
175
def submit ():
@@ -172,7 +179,11 @@ def submit():
172
179
173
180
# Validate ReCaptcha unless in debug mode
174
181
if not Config .DEBUG_MODE :
175
- validate_recaptcha (data ['g-recaptcha-response' ])
182
+ recaptcha_response = data .get ('g-recaptcha-response' , '' )
183
+ try :
184
+ validate_recaptcha (recaptcha_response )
185
+ except ValueError as e :
186
+ return jsonify ({'status' : 'failure' , 'message' : str (e )}), 400
176
187
177
188
# Extract fields from JSON data
178
189
message = data ['message' ]
@@ -219,6 +230,7 @@ def submit():
219
230
logging .error (f"Internal error: { str (e )} " )
220
231
return jsonify ({'status' : 'failure' , 'message' : error_message })
221
232
233
+
222
234
@app .errorhandler (413 )
223
235
def error413 (e ):
224
236
return render_template ('413.html' ), 413
0 commit comments