Skip to content

Commit defece2

Browse files
authored
Add ability to disable root hash check (#43)
1 parent f21a56a commit defece2

2 files changed

Lines changed: 13 additions & 3 deletions

File tree

src/autograph_utils/__init__.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ class SignatureVerifier:
260260
:params ClientSession session: An aiohttp session, used to retrieve x5us.
261261
:params Cache cache: A cache used to store results for x5u verification.
262262
:params bytes root_hash: The expected hash for the first
263-
certificate in a chain. This should not be encoded in any
263+
certificate in a chain. Disabled if ``None``. This should not be encoded in any
264264
way. Hashes can be decoded using decode_mozilla_hash.
265265
:params SubjectNameCheck subject_name_check: Predicate to use to
266266
validate cert subject names. Defaults to
@@ -344,8 +344,9 @@ async def verify_x5u(self, url):
344344

345345
# Verify chain of trust.
346346
chain = certs[::-1]
347-
root_hash = chain[0].fingerprint(SHA256())
348-
if root_hash != self.root_hash:
347+
348+
# Check root certificate hash if specified
349+
if self.root_hash and self.root_hash != (root_hash := chain[0].fingerprint(SHA256())):
349350
raise CertificateHasWrongRoot(expected=self.root_hash, actual=root_hash)
350351

351352
current_cert = chain[0]

tests/test_autograph_utils.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,15 @@ async def test_verify_wrong_root_hash(aiohttp_session, mock_with_x5u, cache, now
264264
)
265265

266266

267+
async def test_root_hash_is_ignored_if_none(aiohttp_session, mock_with_x5u, cache, now_fixed):
268+
s = SignatureVerifier(
269+
aiohttp_session,
270+
cache,
271+
root_hash=None,
272+
)
273+
await s.verify_x5u(FAKE_CERT_URL) # not raising
274+
275+
267276
async def test_verify_broken_chain(aiohttp_session, mock_aioresponses, cache, now_fixed):
268277
# Drop next-to-last cert in cert list
269278
broken_chain = CERT_LIST[:1] + CERT_LIST[2:]

0 commit comments

Comments
 (0)