Skip to content

fix(const): reject an out-of-range port in AppType.register (#1792) - #1818

Merged
JarryShaw merged 1 commit into
mainfrom
fix/1792-apptype-register-range
Oct 16, 2026
Merged

JarryShaw merged 1 commit into
mainfrom
fix/1792-apptype-register-range

Conversation

@JarryShaw

Copy link
Copy Markdown
Owner

Please follow the guide below

What is the purpose of your pull request?

Tick the commit type your subject line carries.

  • fix — corrects a defect
  • feat — adds a feature
  • perf — changes performance, not behaviour
  • refactor — changes neither behaviour nor performance
  • test — tests only
  • docs — documentation only
  • ci — workflows or build tooling
  • release — bumps the version or rolls up a distribution
  • chore — anything else

Summary

TCP.register(70000, 'x') minted a member, and so did -1, after which AppType.get(70000, proto=tcp) returned it, although get rejects an out-of-range port (#758). EnumRegistry.register validates through the _validate_value hook, but AppType neither overrode it nor called it.

  • Fix: AppType._validate_value rejects anything that is not an int in 0–65535, the check _missing_ makes. register calls it, in the same order as EnumRegistry.register. The change is identical in the vendor template and the generated pcapkit/const/reg/apptype/apptype.py.
  • Exception: per registry-protocol.rst, the hook raises EnumValueError, which derives ValueError, with the same message get uses for an out-of-range port. Owner note: as a pcapkit BaseError it is loud (CRITICAL log, excepthooks outside devmode), like EnumLookup.get's miss. The registry's other register refusals are still builtin ValueError.
  • Tests: tests/const/test_const_apptype_register_range_1792_unit.py fails on main across TCP, UDP, SCTP and DCCP. A mutant raising builtin ValueError fails it too. tests/const, tests/vendor, tests/corekit and tests/project pass.
  • No versionchanged: AppType had no register in 1.4.1.post2. The same gap in the other registries is fix(corekit): registries' register accepts values their get rejects #1816.

Closes #1792

AppType.register neither called nor overrode _validate_value, so
TCP.register(70000, 'x') minted a member that AppType.get(70000,
proto='tcp') then returned, although get raises ValueError for a port
outside 0-65535.

- AppType overrides _validate_value with the check _missing_ makes and
  raises EnumValueError, as the registry-protocol convention asks of an
  override, with the message get raises, so register and get refuse the
  same ports in the same words. register calls it after its duplicate
  check, as EnumRegistry.register does. Template and generated module
  changed identically, including the EnumValueError import.
- EnumLookup._validate_value's docstring no longer says AppType skips it.
- Re-point the line citations the change shifts.
- tests/const/test_const_apptype_register_range_1792_unit.py covers TCP,
  UDP, SCTP and DCCP; it fails on main.

Closes #1792
@JarryShaw JarryShaw added the fix Pull requests that fix a defect (fix: subject prefix) label Oct 16, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

Verdict: GOOD TO GO at 38f55c043. A Sonnet review and two re-checks (author: Opus). On all four transports register now rejects an out-of-range port with EnumValueError and get's message, and 65535 still mints. Template parity holds over the whole file. The test fails on main and against a builtin-ValueError mutant, and the const, vendor, corekit and project suites pass. The loud-BaseError consequence is noted in the body for the owner.

@JarryShaw JarryShaw added the review: good-to-go Cross-review at the current head says ready; CI state is separate label Oct 16, 2026
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Oct 16, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage: 89.60% (unit tier, Python 3.15, 38f55c043, Unit Tests run success)

Package Statements Missed Branches Partial Cover
pcapkit (top level) 104 3 20 3 95.16%
pcapkit/const 18868 954 2370 868 91.06%
pcapkit/corekit 2462 69 780 35 96.55%
pcapkit/dumpkit 276 3 100 3 98.40%
pcapkit/foundation 2915 164 1016 34 93.59%
pcapkit/interface 138 7 52 5 93.68%
pcapkit/protocols 17197 224 4580 189 98.09%
pcapkit/toolkit 872 177 298 8 77.18%
pcapkit/utilities 512 2 158 4 99.10%
pcapkit/vendor 4417 2342 1010 157 43.38%

Per-file detail: the coverage-html artifact of this run.

@JarryShaw

Copy link
Copy Markdown
Owner Author

Is this check needed for all other enums as well? I dont think register validate the input range at all.

@JarryShaw

Copy link
Copy Markdown
Owner Author

Yes, every other registry has the same gap, and you're right that register doesn't range-check anything today. EnumRegistry.register does call the _validate_value hook (pcapkit/corekit/enum.py:530), but the base implementation accepts everything and no generated registry overrides it.

I measured this just now on main: none of the 127 EnumRegistry subclasses define _validate_value, and Hardware.register(70000, 'x') mints a member that Hardware.get(70000) then returns.

This PR fixes AppType only (#1792). The rest is tracked in #1816, which is blocked until this PR merges because both touch the AppType const file and its generator. The proposed fix for #1816 goes in the vendor generators:

  • emit a _validate_value override from the same range check each generator already writes into _missing_;
  • regenerate the const files;
  • add one test that every registry's register and get reject the same out-of-range value.

My recommendation is to merge this one as it stands and then do #1816. If you'd rather have it all in one PR, I can widen this one instead.

@JarryShaw
JarryShaw merged commit 5ba693c into main Oct 16, 2026
56 checks passed
@JarryShaw
JarryShaw deleted the fix/1792-apptype-register-range branch October 16, 2026 13:19
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Oct 16, 2026
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Oct 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

fix Pull requests that fix a defect (fix: subject prefix)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

fix(const): AppType.register accepts a port outside 0–65535

1 participant