Skip to content

Commit f15ec94

Browse files
committed
fix(foundation): validate a registrar's code before registering it (#2186)
Problem: the enum-keyed registrars register first and then log code.name, so an int, None or str code raised AttributeError after the protocol's table (and schema table) had already been written. Fix: a _check_code() helper rejects a code that is not an enum member with RegistryError before any registry is touched. Applied to the fifteen method-name registrars and to register_linktype, register_pcap, register_pcapng, register_ethertype and register_transtype, which share the pattern; Raises: sections added to each. Test: tests/foundation/registry/test_registrar_code_check_unit.py calls each registrar with 250, None and '250' and checks RegistryError is raised and every registry matches its snapshot.
1 parent 4c3d301 commit f15ec94

2 files changed

Lines changed: 268 additions & 0 deletions

File tree

‎pcapkit/foundation/registry/protocols.py‎

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,23 @@ def register_protocol_code(protocol: 'Type[ProtocolBase]', code: 'Any') -> 'None
536536
destination.__name__, key)
537537

538538

539+
def _check_code(code: 'Any') -> 'None':
540+
"""Reject a registration key that is not an enum member.
541+
542+
The registrars typed on an enum key call this before touching any
543+
registry, so a bad ``code`` leaves every registry as it was (:issue:`2186`).
544+
545+
Args:
546+
code: Registration key handed to the registrar.
547+
548+
Raises:
549+
RegistryError: If ``code`` is not an enum member.
550+
551+
"""
552+
if not isinstance(code, (enum.Enum, aenum.Enum)):
553+
raise RegistryError(f'code must be an enum member, not {code!r}')
554+
555+
539556
###############################################################################
540557
# Top-Level Registries
541558
###############################################################################
@@ -571,7 +588,13 @@ def register_linktype(code: 'LinkType', module: 'str | ModuleDescriptor[Protocol
571588
* :func:`pcapkit.foundation.registry.protocols.register_pcap`
572589
* :func:`pcapkit.foundation.registry.protocols.register_pcapng`
573590
591+
Raises:
592+
RegistryError: If ``code`` is not an enum member, or ``module``
593+
is not a :class:`~pcapkit.protocols.protocol.ProtocolBase`
594+
subclass.
595+
574596
"""
597+
_check_code(code)
575598
if isinstance(module, str):
576599
module = cast('ModuleDescriptor[ProtocolBase]', ModuleDescriptor(module, class_))
577600

@@ -609,7 +632,13 @@ def register_pcap(code: 'LinkType', module: 'str | ModuleDescriptor[ProtocolBase
609632
:class:`~pcapkit.protocols.protocol.ProtocolBase` subclass
610633
class\_: class name
611634
635+
Raises:
636+
RegistryError: If ``code`` is not an enum member, or ``module``
637+
is not a :class:`~pcapkit.protocols.protocol.ProtocolBase`
638+
subclass.
639+
612640
"""
641+
_check_code(code)
613642
if isinstance(module, str):
614643
module = cast('ModuleDescriptor[ProtocolBase]', ModuleDescriptor(module, class_))
615644

@@ -646,7 +675,13 @@ def register_pcapng(code: 'LinkType', module: 'str | ModuleDescriptor[ProtocolBa
646675
:class:`~pcapkit.protocols.protocol.ProtocolBase` subclass
647676
class\_: class name
648677
678+
Raises:
679+
RegistryError: If ``code`` is not an enum member, or ``module``
680+
is not a :class:`~pcapkit.protocols.protocol.ProtocolBase`
681+
subclass.
682+
649683
"""
684+
_check_code(code)
650685
if isinstance(module, str):
651686
module = cast('ModuleDescriptor[ProtocolBase]', ModuleDescriptor(module, class_))
652687

@@ -688,7 +723,13 @@ def register_ethertype(code: 'EtherType', module: 'str | ModuleDescriptor[Protoc
688723
:class:`~pcapkit.protocols.protocol.ProtocolBase` subclass
689724
class\_: class name
690725
726+
Raises:
727+
RegistryError: If ``code`` is not an enum member, or ``module``
728+
is not a :class:`~pcapkit.protocols.protocol.ProtocolBase`
729+
subclass.
730+
691731
"""
732+
_check_code(code)
692733
if isinstance(module, str):
693734
module = cast('ModuleDescriptor[ProtocolBase]', ModuleDescriptor(module, class_))
694735

@@ -730,7 +771,13 @@ def register_transtype(code: 'TransType', module: 'str | ModuleDescriptor[Protoc
730771
:class:`~pcapkit.protocols.protocol.ProtocolBase` subclass
731772
class\_: class name
732773
774+
Raises:
775+
RegistryError: If ``code`` is not an enum member, or ``module``
776+
is not a :class:`~pcapkit.protocols.protocol.ProtocolBase`
777+
subclass.
778+
733779
"""
780+
_check_code(code)
734781
if isinstance(module, str):
735782
module = cast('ModuleDescriptor[ProtocolBase]', ModuleDescriptor(module, class_))
736783

@@ -758,7 +805,12 @@ def register_ipv4_option(code: 'IPv4_OptionNumber', meth: 'str | tuple[IPv4_Opti
758805
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
759806
It should be a subclass of :class:`pcapkit.protocols.schema.internet.ipv4.Option`.
760807
808+
Raises:
809+
RegistryError: If ``code`` is not an enum member, or ``meth``
810+
is a name with no matching ``_read_opt_*`` method.
811+
761812
"""
813+
_check_code(code)
762814
if isinstance(meth, str) and not hasattr(IPv4, f'_read_opt_{meth}'):
763815
raise RegistryError('method must be a valid IPv4 option parser function')
764816

@@ -783,7 +835,12 @@ def register_hip_parameter(code: 'HIP_Parameter', meth: 'str | tuple[HIP_Paramet
783835
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the parameter.
784836
It should be a subclass of :class:`pcapkit.protocols.schema.internet.hip.Parameter`.
785837
838+
Raises:
839+
RegistryError: If ``code`` is not an enum member, or ``meth``
840+
is a name with no matching ``_read_param_*`` method.
841+
786842
"""
843+
_check_code(code)
787844
if isinstance(meth, str) and not hasattr(HIP, f'_read_param_{meth}'):
788845
raise RegistryError('method must be a valid HIP parameter parser function')
789846

@@ -808,7 +865,12 @@ def register_hopopt_option(code: 'IPv6_Option', meth: 'str | tuple[HOPOPT_Option
808865
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
809866
It should be a subclass of :class:`pcapkit.protocols.schema.internet.hopopt.Option`.
810867
868+
Raises:
869+
RegistryError: If ``code`` is not an enum member, or ``meth``
870+
is a name with no matching ``_read_opt_*`` method.
871+
811872
"""
873+
_check_code(code)
812874
if isinstance(meth, str) and not hasattr(HOPOPT, f'_read_opt_{meth}'):
813875
raise RegistryError('method must be a valid HOPOPT option parser function')
814876

@@ -833,7 +895,12 @@ def register_ipv6_opts_option(code: 'IPv6_Option', meth: 'str | tuple[IPv6_Opts_
833895
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
834896
It should be a subclass of :class:`pcapkit.protocols.schema.internet.ipv6_opts.Option`.
835897
898+
Raises:
899+
RegistryError: If ``code`` is not an enum member, or ``meth``
900+
is a name with no matching ``_read_opt_*`` method.
901+
836902
"""
903+
_check_code(code)
837904
if isinstance(meth, str) and not hasattr(IPv6_Opts, f'_read_opt_{meth}'):
838905
raise RegistryError('method must be a valid IPv6-Opts option parser function')
839906

@@ -858,7 +925,12 @@ def register_ipv6_route_routing(code: 'IPv6_Routing', meth: 'str | tuple[IPv6_Ro
858925
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the routing data.
859926
It should be a subclass of :class:`pcapkit.protocols.schema.internet.ipv6_route.RoutingType`.
860927
928+
Raises:
929+
RegistryError: If ``code`` is not an enum member, or ``meth``
930+
is a name with no matching ``_read_data_type_*`` method.
931+
861932
"""
933+
_check_code(code)
862934
if isinstance(meth, str) and not hasattr(IPv6_Route, f'_read_data_type_{meth}'):
863935
raise RegistryError('method must be a valid IPv6-Route routing data parser function')
864936

@@ -883,7 +955,12 @@ def register_mh_message(code: 'MH_Packet', meth: 'str | tuple[MH_PacketParser, M
883955
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the message type.
884956
It should be a subclass of :class:`pcapkit.protocols.schema.internet.mh.Packet`.
885957
958+
Raises:
959+
RegistryError: If ``code`` is not an enum member, or ``meth``
960+
is a name with no matching ``_read_msg_*`` method.
961+
886962
"""
963+
_check_code(code)
887964
if isinstance(meth, str) and not hasattr(MH, f'_read_msg_{meth}'):
888965
raise RegistryError('method must be a valid MH message type parser function')
889966

@@ -908,7 +985,12 @@ def register_mh_option(code: 'MH_Option', meth: 'str | tuple[MH_OptionParser, MH
908985
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
909986
It should be a subclass of :class:`pcapkit.protocols.schema.internet.mh.Option`.
910987
988+
Raises:
989+
RegistryError: If ``code`` is not an enum member, or ``meth``
990+
is a name with no matching ``_read_opt_*`` method.
991+
911992
"""
993+
_check_code(code)
912994
if isinstance(meth, str) and not hasattr(MH, f'_read_opt_{meth}'):
913995
raise RegistryError('method must be a valid MH option parser function')
914996

@@ -933,12 +1015,17 @@ def register_mh_extension(code: 'MH_CGAExtension', meth: 'str | tuple[MH_Extensi
9331015
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the CGA extension.
9341016
It should be a subclass of :class:`pcapkit.protocols.schema.internet.mh.CGAExtension`.
9351017
1018+
Raises:
1019+
RegistryError: If ``code`` is not an enum member, or ``meth``
1020+
is a name with no matching ``_read_ext_*`` method.
1021+
9361022
.. versionchanged:: 1.5.0
9371023
9381024
``schema`` is keyword-only, as in the other ``register_*`` functions
9391025
that take one (:issue:`516`).
9401026
9411027
"""
1028+
_check_code(code)
9421029
if isinstance(meth, str) and not hasattr(MH, f'_read_ext_{meth}'):
9431030
raise RegistryError('method must be a valid MH CGA extension function')
9441031

@@ -1198,7 +1285,12 @@ def register_tcp_option(code: 'TCP_Option', meth: 'str | tuple[TCP_OptionParser,
11981285
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
11991286
It should be a subclass of :class:`pcapkit.protocols.schema.transport.tcp.Option`.
12001287
1288+
Raises:
1289+
RegistryError: If ``code`` is not an enum member, or ``meth``
1290+
is a name with no matching ``_read_mode_*`` method.
1291+
12011292
"""
1293+
_check_code(code)
12021294
if isinstance(meth, str) and not hasattr(TCP, f'_read_mode_{meth}'):
12031295
raise RegistryError('method must be a TCP option parser function')
12041296

@@ -1223,7 +1315,12 @@ def register_tcp_mp_option(code: 'TCP_MPTCPOption', meth: 'str | tuple[TCP_MPOpt
12231315
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
12241316
It should be a subclass of :class:`pcapkit.protocols.schema.transport.tcp.MPTCP`.
12251317
1318+
Raises:
1319+
RegistryError: If ``code`` is not an enum member, or ``meth``
1320+
is a name with no matching ``_read_mptcp_*`` method.
1321+
12261322
"""
1323+
_check_code(code)
12271324
if isinstance(meth, str) and not hasattr(TCP, f'_read_mptcp_{meth}'):
12281325
raise RegistryError('method must be a MPTCP option parser function')
12291326

@@ -1336,7 +1433,12 @@ def register_http_frame(code: 'HTTP_Frame', meth: 'str | tuple[HTTP_FrameParser,
13361433
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the frame.
13371434
It should be a subclass of :class:`pcapkit.protocols.schema.application.httpv2.FrameType`.
13381435
1436+
Raises:
1437+
RegistryError: If ``code`` is not an enum member, or ``meth``
1438+
is a name with no matching ``_read_http_*`` method.
1439+
13391440
"""
1441+
_check_code(code)
13401442
if isinstance(meth, str) and not hasattr(HTTPv2, f'_read_http_{meth}'):
13411443
raise RegistryError('method must be a frame parser function')
13421444

@@ -1366,7 +1468,12 @@ def register_pcapng_block(code: 'PCAPNG_BlockType', meth: 'str | tuple[PCAPNG_Bl
13661468
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the block.
13671469
It should be a subclass of :class:`pcapkit.protocols.schema.misc.pcapng.BlockType`.
13681470
1471+
Raises:
1472+
RegistryError: If ``code`` is not an enum member, or ``meth``
1473+
is a name with no matching ``_read_block_*`` method.
1474+
13691475
"""
1476+
_check_code(code)
13701477
if isinstance(meth, str) and not hasattr(PCAPNG, f'_read_block_{meth}'):
13711478
raise RegistryError('method must be a block parser function')
13721479

@@ -1391,7 +1498,12 @@ def register_pcapng_option(code: 'PCAPNG_OptionType', meth: 'str | tuple[PCAPNG_
13911498
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the option.
13921499
It should be a subclass of :class:`pcapkit.protocols.schema.misc.pcapng.Option`.
13931500
1501+
Raises:
1502+
RegistryError: If ``code`` is not an enum member, or ``meth``
1503+
is a name with no matching ``_read_option_*`` method.
1504+
13941505
"""
1506+
_check_code(code)
13951507
if isinstance(meth, str) and not hasattr(PCAPNG, f'_read_option_{meth}'):
13961508
raise RegistryError('method must be a option parser function')
13971509

@@ -1417,7 +1529,12 @@ def register_pcapng_record(code: 'PCAPNG_RecordType', meth: 'str | tuple[PCAPNG_
14171529
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the name resolution record.
14181530
It should be a subclass of :class:`pcapkit.protocols.schema.misc.pcapng.NameResolutionRecord`.
14191531
1532+
Raises:
1533+
RegistryError: If ``code`` is not an enum member, or ``meth``
1534+
is a name with no matching ``_read_record_*`` method.
1535+
14201536
"""
1537+
_check_code(code)
14211538
if isinstance(meth, str) and not hasattr(PCAPNG, f'_read_record_{meth}'):
14221539
raise RegistryError('method must be a name resolution record parser function')
14231540

@@ -1442,7 +1559,12 @@ def register_pcapng_secrets(code: 'PCAPNG_SecretsType', meth: 'str | tuple[PCAPN
14421559
schema: :class:`~pcapkit.protocols.schema.schema.Schema` class for the decryption secrets.
14431560
It should be a subclass of :class:`pcapkit.protocols.schema.misc.pcapng.DSBSecrets`.
14441561
1562+
Raises:
1563+
RegistryError: If ``code`` is not an enum member, or ``meth``
1564+
is a name with no matching ``_read_secrets_*`` method.
1565+
14451566
"""
1567+
_check_code(code)
14461568
if isinstance(meth, str) and not hasattr(PCAPNG, f'_read_secrets_{meth}'):
14471569
raise RegistryError('method must be a decryption secrets parser function')
14481570

0 commit comments

Comments
 (0)