Skip to content

Commit 1c98d05

Browse files
Update server.py
1 parent 2bf1a63 commit 1c98d05

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

server.py

+18-6
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def create_email(to_email, identifier, text, all_attachments, reference=''):
8181
subject = f'Secure Form Submission {identifier}'
8282
if reference:
8383
subject = f'{reference} {subject}'
84-
84+
8585
message = Mail(
8686
from_email=FROMEMAIL,
8787
to_emails=to_email,
@@ -108,15 +108,21 @@ def validate_recaptcha(recaptcha_response):
108108
Validates the ReCaptcha response.
109109
"""
110110
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
111116
if not recaptcha.verify(response=recaptcha_response):
112117
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)
116121
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))
118123
raise
119124

125+
120126
def send_email(message):
121127
"""
122128
Sends the email using SendGrid and logs detailed information for debugging.
@@ -163,6 +169,7 @@ def send_email(message):
163169
def index():
164170
return render_template('index.html', notice='', hascaptcha=not Config.DEBUG_MODE, attachments_number=Config.NUMBER_OF_ATTACHMENTS, recaptcha_sitekey=RECAPTCHASITEKEY)
165171

172+
166173
@app.route('/submit-encrypted-data', methods=['POST'])
167174
@limiter.limit("5 per minute")
168175
def submit():
@@ -172,7 +179,11 @@ def submit():
172179

173180
# Validate ReCaptcha unless in debug mode
174181
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
176187

177188
# Extract fields from JSON data
178189
message = data['message']
@@ -219,6 +230,7 @@ def submit():
219230
logging.error(f"Internal error: {str(e)}")
220231
return jsonify({'status': 'failure', 'message': error_message})
221232

233+
222234
@app.errorhandler(413)
223235
def error413(e):
224236
return render_template('413.html'), 413

0 commit comments

Comments
 (0)