Return 401 when token is invalid (#714)

In some rare instances (a race condition perhaps?) a
`cryptography.fernet.InvalidToken` exception is thrown resulting in
a broken connection.

This change gracefully returns a 401 error instead.
main
gdm85 2022-04-18 21:06:44 +02:00 committed by GitHub
parent cded1e0272
commit 94b4eb08a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 10 additions and 3 deletions

View File

@ -28,7 +28,8 @@ from flask import jsonify, make_response, request, redirect, render_template, \
send_file, session, url_for, g send_file, session, url_for, g
from requests import exceptions, get from requests import exceptions, get
from requests.models import PreparedRequest from requests.models import PreparedRequest
from cryptography.fernet import Fernet from cryptography.fernet import Fernet, InvalidToken
from cryptography.exceptions import InvalidSignature
# Load DDG bang json files only on init # Load DDG bang json files only on init
bang_json = json.load(open(app.config['BANG_FILE'])) or {} bang_json = json.load(open(app.config['BANG_FILE'])) or {}
@ -460,8 +461,14 @@ def imgres():
def element(): def element():
element_url = src_url = request.args.get('url') element_url = src_url = request.args.get('url')
if element_url.startswith('gAAAAA'): if element_url.startswith('gAAAAA'):
try:
cipher_suite = Fernet(g.session_key) cipher_suite = Fernet(g.session_key)
src_url = cipher_suite.decrypt(element_url.encode()).decode() src_url = cipher_suite.decrypt(element_url.encode()).decode()
print(src_url)
except (InvalidSignature, InvalidToken) as e:
return render_template(
'error.html',
error_message=str(e)), 401
src_type = request.args.get('type') src_type = request.args.get('type')