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
parent
cded1e0272
commit
94b4eb08a2
|
@ -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')
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue