From a1a21f46c0ab36fc49b087b2d70751ed23c94724 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 19 Oct 2014 19:11:30 -0700 Subject: Fixed an issue in fernet where the wrong exception would occur on an all-nulls input. Also switched a few tests to not generate a key --- cryptography/fernet.py | 2 +- tests/test_fernet.py | 9 +++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/cryptography/fernet.py b/cryptography/fernet.py index cdb9bdca..153f398b 100644 --- a/cryptography/fernet.py +++ b/cryptography/fernet.py @@ -90,7 +90,7 @@ class Fernet(object): except (TypeError, binascii.Error): raise InvalidToken - if six.indexbytes(data, 0) != 0x80: + if not data or six.indexbytes(data, 0) != 0x80: raise InvalidToken try: diff --git a/tests/test_fernet.py b/tests/test_fernet.py index 0683d950..0b4e3e87 100644 --- a/tests/test_fernet.py +++ b/tests/test_fernet.py @@ -86,15 +86,20 @@ class TestFernet(object): f.decrypt(token.encode("ascii"), ttl=ttl_sec) def test_invalid_start_byte(self, backend): - f = Fernet(Fernet.generate_key(), backend=backend) + f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) with pytest.raises(InvalidToken): f.decrypt(base64.urlsafe_b64encode(b"\x81")) def test_timestamp_too_short(self, backend): - f = Fernet(Fernet.generate_key(), backend=backend) + f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) with pytest.raises(InvalidToken): f.decrypt(base64.urlsafe_b64encode(b"\x80abc")) + def test_non_base64_token(self, backend): + f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) + with pytest.raises(InvalidToken): + f.decrypt(b"\x00") + def test_unicode(self, backend): f = Fernet(base64.urlsafe_b64encode(b"\x00" * 32), backend=backend) with pytest.raises(TypeError): -- cgit v1.2.3 From 461c8d286d3cff4b5f5ed962669defa9710e7605 Mon Sep 17 00:00:00 2001 From: Alex Gaynor Date: Sun, 19 Oct 2014 22:33:46 -0700 Subject: eep, stray space --- cryptography/fernet.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cryptography/fernet.py b/cryptography/fernet.py index 153f398b..a8e0330e 100644 --- a/cryptography/fernet.py +++ b/cryptography/fernet.py @@ -90,7 +90,7 @@ class Fernet(object): except (TypeError, binascii.Error): raise InvalidToken - if not data or six.indexbytes(data, 0) != 0x80: + if not data or six.indexbytes(data, 0) != 0x80: raise InvalidToken try: -- cgit v1.2.3