Skip to content

Commit 4000db2

Browse files
Use timezone aware comparisons for cert validity (#50)
* Use timezone aware comparisons for cert validity * Ran format * Switch to backward compatible timezone.utc * make format --------- Co-authored-by: Alex Cottner <acottner@mozilla.com>
1 parent 0f14801 commit 4000db2

2 files changed

Lines changed: 19 additions & 18 deletions

File tree

src/autograph_utils/__init__.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,15 @@ async def verify_x5u(self, url):
332332

333333
now = _now()
334334
for cert in certs:
335-
if cert.not_valid_before > cert.not_valid_after:
335+
if cert.not_valid_before_utc > cert.not_valid_after_utc:
336336
raise BadCertificate(
337-
f"not_before ({cert.not_valid_before}) after "
338-
f"not_after ({cert.not_valid_after})"
337+
f"not_before ({cert.not_valid_before_utc}) after "
338+
f"not_after ({cert.not_valid_after_utc})"
339339
)
340-
if now < cert.not_valid_before:
341-
raise CertificateNotYetValid(cert.not_valid_before)
342-
if now > cert.not_valid_after:
343-
raise CertificateExpired(cert.not_valid_after)
340+
if now < cert.not_valid_before_utc:
341+
raise CertificateNotYetValid(cert.not_valid_before_utc)
342+
if now > cert.not_valid_after_utc:
343+
raise CertificateExpired(cert.not_valid_after_utc)
344344

345345
# Verify chain of trust.
346346
chain = certs[::-1]

tests/test_autograph_utils.py

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
import datetime
77
import os.path
8+
from datetime import timezone
89
from unittest import mock
910

1011
import aiohttp
@@ -89,7 +90,7 @@ def cache():
8990
def now_fixed():
9091
with mock.patch("autograph_utils._now") as m:
9192
# A common static time used in a lot of tests.
92-
m.return_value = datetime.datetime(2019, 10, 23, 16, 16)
93+
m.return_value = datetime.datetime(2019, 10, 23, 16, 16, tzinfo=timezone.utc)
9394
# Yield the mock so someone can change the time if they want
9495
yield m
9596

@@ -107,8 +108,8 @@ def mock_cert(real_cert):
107108
"""
108109

109110
mock_cert = mock.MagicMock(wraps=real_cert)
110-
mock_cert.not_valid_before = real_cert.not_valid_before
111-
mock_cert.not_valid_after = real_cert.not_valid_after
111+
mock_cert.not_valid_before_utc = real_cert.not_valid_before_utc
112+
mock_cert.not_valid_after_utc = real_cert.not_valid_after_utc
112113
mock_cert.signature = real_cert.signature
113114
mock_cert.tbs_certificate_bytes = real_cert.tbs_certificate_bytes
114115
mock_cert.signature_hash_algorithm = real_cert.signature_hash_algorithm
@@ -180,21 +181,21 @@ async def test_verify_signature_bad_numbers(aiohttp_session, mock_with_x5u, cach
180181

181182

182183
async def test_verify_x5u_expired(aiohttp_session, mock_with_x5u, cache, now_fixed):
183-
now_fixed.return_value = datetime.datetime(2022, 10, 23, 16, 16, 16)
184+
now_fixed.return_value = datetime.datetime(2022, 10, 23, 16, 16, 16, tzinfo=timezone.utc)
184185
s = SignatureVerifier(aiohttp_session, cache, DEV_ROOT_HASH)
185186
with pytest.raises(autograph_utils.CertificateExpired) as excinfo:
186187
await s.verify(SIGNED_DATA, SAMPLE_SIGNATURE, FAKE_CERT_URL)
187188

188-
assert excinfo.value.detail == "Certificate expired on 2021-07-05 21:57:15"
189+
assert excinfo.value.detail == "Certificate expired on 2021-07-05 21:57:15+00:00"
189190

190191

191192
async def test_verify_x5u_too_soon(aiohttp_session, mock_with_x5u, cache, now_fixed):
192-
now_fixed.return_value = datetime.datetime(2010, 10, 23, 16, 16, 16)
193+
now_fixed.return_value = datetime.datetime(2010, 10, 23, 16, 16, 16, tzinfo=timezone.utc)
193194
s = SignatureVerifier(aiohttp_session, cache, DEV_ROOT_HASH)
194195
with pytest.raises(autograph_utils.CertificateNotYetValid) as excinfo:
195196
await s.verify(SIGNED_DATA, SAMPLE_SIGNATURE, FAKE_CERT_URL)
196197

197-
assert excinfo.value.detail == "Certificate is not valid until 2016-07-06 21:57:15"
198+
assert excinfo.value.detail == "Certificate is not valid until 2016-07-06 21:57:15+00:00"
198199

199200

200201
async def test_verify_x5u_screwy_dates(aiohttp_session, mock_with_x5u, cache, now_fixed):
@@ -204,16 +205,16 @@ async def test_verify_x5u_screwy_dates(aiohttp_session, mock_with_x5u, cache, no
204205
CERT_LIST[0], backend=default_backend()
205206
)
206207
bad_cert = mock_cert(leaf_cert)
207-
bad_cert.not_valid_before = leaf_cert.not_valid_after
208-
bad_cert.not_valid_after = leaf_cert.not_valid_before
208+
bad_cert.not_valid_before_utc = leaf_cert.not_valid_after_utc
209+
bad_cert.not_valid_after_utc = leaf_cert.not_valid_before_utc
209210
with mock.patch("autograph_utils.x509.load_pem_x509_certificate") as x509:
210211
x509.return_value = bad_cert
211212
with pytest.raises(autograph_utils.BadCertificate) as excinfo:
212213
await s.verify(SIGNED_DATA, SAMPLE_SIGNATURE, FAKE_CERT_URL)
213214

214215
assert excinfo.value.detail == (
215-
"Bad certificate: not_before (2021-07-05 21:57:15) "
216-
"after not_after (2016-07-06 21:57:15)"
216+
"Bad certificate: not_before (2021-07-05 21:57:15+00:00) "
217+
"after not_after (2016-07-06 21:57:15+00:00)"
217218
)
218219

219220

0 commit comments

Comments
 (0)