From 04b2d1e4723e88dc2a2fdf82c88c7dbfad730088 Mon Sep 17 00:00:00 2001 From: Ben Bangert Date: Tue, 19 Jul 2016 13:31:35 -0700 Subject: [PATCH] fix: catch InvalidToken exceptions from fernet Closes #530 --- autopush/tests/test_web_validation.py | 14 ++++++++++++++ autopush/web/validation.py | 3 ++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/autopush/tests/test_web_validation.py b/autopush/tests/test_web_validation.py index 4ca2558b..2cd1df6a 100644 --- a/autopush/tests/test_web_validation.py +++ b/autopush/tests/test_web_validation.py @@ -5,6 +5,7 @@ from boto.dynamodb2.exceptions import ( ItemNotFound, ) +from cryptography.fernet import InvalidToken from jose import jws from marshmallow import Schema, fields from mock import Mock @@ -343,6 +344,19 @@ def throw_item(*args, **kwargs): eq_(cm.exception.errno, 102) + def test_invalid_fernet_token(self): + schema = self._makeFUT() + + def throw_item(*args, **kwargs): + raise InvalidToken + + schema.context["settings"].parse_endpoint.side_effect = throw_item + + with assert_raises(InvalidRequest) as cm: + schema.load(self._make_test_data()) + + eq_(cm.exception.errno, 102) + def test_invalid_uaid_not_found(self): schema = self._makeFUT() schema.context["settings"].parse_endpoint.return_value = dict( diff --git a/autopush/web/validation.py b/autopush/web/validation.py index 16209523..19d340b0 100644 --- a/autopush/web/validation.py +++ b/autopush/web/validation.py @@ -7,6 +7,7 @@ from boto.dynamodb2.exceptions import ( ItemNotFound, ) +from cryptography.fernet import InvalidToken from marshmallow import ( Schema, fields, @@ -194,7 +195,7 @@ def extract_subscription(self, d): version=d["api_ver"], ckey_header=d["ckey_header"], ) - except InvalidTokenException: + except (InvalidTokenException, InvalidToken): raise InvalidRequest("invalid token", errno=102) return result