From 81d98f4457958d1c365673d1b4759b0f0640597c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oleg=20H=C3=B6fling?= Date: Thu, 7 Nov 2024 00:38:20 +0100 Subject: [PATCH] fix(admissions): allow profession_oids field being none (#11908) * fix: allow profession_oids to be none Signed-off-by: oleg.hoefling * chore: provide explicit type hints for profession oids in hash calculation Signed-off-by: oleg.hoefling * chore: remove unused ignore in profession info init test Signed-off-by: oleg.hoefling * fix(profession info): simplify profession oids handling in hash calculation Signed-off-by: oleg.hoefling --------- Signed-off-by: oleg.hoefling --- src/cryptography/x509/extensions.py | 27 ++++++++++++++++----------- tests/x509/test_x509_ext.py | 24 +++++++++++++++++++++++- 2 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/cryptography/x509/extensions.py b/src/cryptography/x509/extensions.py index 202101208dad..1709862c9869 100644 --- a/src/cryptography/x509/extensions.py +++ b/src/cryptography/x509/extensions.py @@ -2227,7 +2227,7 @@ def __init__( self, naming_authority: NamingAuthority | None, profession_items: typing.Iterable[str], - profession_oids: typing.Iterable[ObjectIdentifier], + profession_oids: typing.Iterable[ObjectIdentifier] | None, registration_number: str | None, add_profession_info: bytes | None, ) -> None: @@ -2242,14 +2242,15 @@ def __init__( "Every item in the profession_items list must be a str" ) - profession_oids = list(profession_oids) - if not all( - isinstance(oid, ObjectIdentifier) for oid in profession_oids - ): - raise TypeError( - "Every item in the profession_oids list must be an " - "ObjectIdentifier" - ) + if profession_oids is not None: + profession_oids = list(profession_oids) + if not all( + isinstance(oid, ObjectIdentifier) for oid in profession_oids + ): + raise TypeError( + "Every item in the profession_oids list must be an " + "ObjectIdentifier" + ) if registration_number is not None and not isinstance( registration_number, str @@ -2276,7 +2277,7 @@ def profession_items(self) -> list[str]: return self._profession_items @property - def profession_oids(self) -> list[ObjectIdentifier]: + def profession_oids(self) -> list[ObjectIdentifier] | None: return self._profession_oids @property @@ -2309,11 +2310,15 @@ def __eq__(self, other: object) -> bool: ) def __hash__(self) -> int: + if self.profession_oids is None: + profession_oids = None + else: + profession_oids = tuple(self.profession_oids) return hash( ( self.naming_authority, *tuple(self.profession_items), - *tuple(self.profession_oids), + profession_oids, self.registration_number, self.add_profession_info, ) diff --git a/tests/x509/test_x509_ext.py b/tests/x509/test_x509_ext.py index fa47c277a4d5..b29a45664484 100644 --- a/tests/x509/test_x509_ext.py +++ b/tests/x509/test_x509_ext.py @@ -6443,7 +6443,7 @@ def test_invalid_init(self): x509.ProfessionInfo( None, None, # type:ignore[arg-type] - None, # type:ignore[arg-type] + None, None, None, ) @@ -6493,6 +6493,10 @@ def test_eq(self): info2 = x509.ProfessionInfo(None, [], [], None, None) assert info1 == info2 + info1 = x509.ProfessionInfo(None, [], None, None, None) + info2 = x509.ProfessionInfo(None, [], None, None, None) + assert info1 == info2 + info1 = x509.ProfessionInfo( x509.NamingAuthority( x509.ObjectIdentifier("1.2.3"), "https://example.com", "spam" @@ -6566,6 +6570,7 @@ def test_ne(self): info8 = x509.ProfessionInfo(None, [], [], "spam", None) info9 = x509.ProfessionInfo(None, [], [], None, b"\x01\x02\x03") info10 = x509.ProfessionInfo(None, [], [], None, None) + info11 = x509.ProfessionInfo(None, [], None, None, None) assert info1 != info2 assert info1 != info2 @@ -6577,6 +6582,7 @@ def test_ne(self): assert info1 != info8 assert info1 != info9 assert info1 != info10 + assert info1 != info11 assert info1 != object() def test_repr(self): @@ -6590,6 +6596,16 @@ def test_repr(self): "add_profession_info=None)>" ) + info = x509.ProfessionInfo(None, [], None, None, None) + assert repr(info) == ( + "" + ) + info = x509.ProfessionInfo( x509.NamingAuthority( x509.ObjectIdentifier("1.2.3"), "https://example.com", "spam" @@ -6659,6 +6675,10 @@ def test_hash(self): info7 = x509.ProfessionInfo( x509.NamingAuthority(None, None, None), [], [], None, None ) + info8 = x509.ProfessionInfo( + x509.NamingAuthority(None, None, None), [], None, None, None + ) + info9 = x509.ProfessionInfo(None, [], None, None, None) assert hash(info1) == hash(info2) assert hash(info1) != hash(info3) @@ -6666,6 +6686,8 @@ def test_hash(self): assert hash(info1) != hash(info5) assert hash(info1) != hash(info6) assert hash(info1) != hash(info7) + assert hash(info1) != hash(info8) + assert hash(info1) != hash(info9) class TestAdmission: