Skip to content

fix(corekit): range-check ports in PortEnumField, and document NumberField's masking (#2314) - #2335

Merged
JarryShaw merged 1 commit into
mainfrom
fix/2314-port-field-range
Oct 26, 2026
Merged

JarryShaw merged 1 commit into
mainfrom
fix/2314-port-field-range

Conversation

@JarryShaw

Copy link
Copy Markdown
Owner

What is the purpose of your pull request?

  • fix — bug fix

Description

Closes #2314, per the ruling (take the recommendation, with the ports on the port enum field).

@JarryShaw JarryShaw added this to the PyPCAPKit v1.5 milestone Oct 26, 2026
@JarryShaw JarryShaw added bug Issues reporting a defect (set by the bug report template; a default, not an assessment) fix Pull requests that fix a defect (fix: subject prefix) review: running A cross-review is in flight against the current head - no verdict yet labels Oct 26, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

Review: GOOD TO GO at ec62c5872 (Sonnet).

  • Fix: TCP, UDP and SCTP all declare their ports as PortEnumField, so the range check covers all three. 65536 and -1 raise FieldValueError, while 0 and 65535 pack byte-exact.
  • Unchanged: NumberField masking (fix(corekit): a NumberField whose bit_length is narrower than its octets drops the high bits #1488). Parsed ports are always in range, make() already rejected bad ports through _make_port, and AppType members still convert to their port first.
  • Fail-without-fix: on origin/main, 12 failed (FieldValueError not raised). On the head, tests/corekit, tests/protocols/transport and the contract tests gave 1110 passed.
  • Non-blocking:
    • SCTP is covered only through the shared field, with no SCTP subtest.
    • PortEnumField.pre_process gained a Raises: entry but no versionchanged note.

@JarryShaw JarryShaw added review: good-to-go Cross-review at the current head says ready; CI state is separate and removed review: running A cross-review is in flight against the current head - no verdict yet labels Oct 26, 2026
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Oct 26, 2026
…Field's masking (#2314)

Problem: a TCP or UDP schema built directly packed an out-of-range port by
masking it to 16 bits (srcport=70000 -> 0x1170, -1 -> 0xffff), because
PortEnumField.pre_process handed a bare int to NumberField's mask unchecked.

Fix: the UDP, TCP and SCTP port fields already use PortEnumField (#1516,
#1526), so the fix is the range check there: its pre_process raises
FieldValueError for a port outside the field's width. NumberField keeps
masking as the documented schema contract (#1488); the NumberField and
Schema docstrings say direct construction does not range-check and name
where the checks live.

Test: test_tcp_udp_schema_port_range_unit.py rejects -1/65536/70000 on both
ports of both schemas, and round-trips ports 0 and 65535 byte-exact.
@JarryShaw
JarryShaw force-pushed the fix/2314-port-field-range branch from ec62c58 to 841014d Compare October 26, 2026 22:32
@JarryShaw

Copy link
Copy Markdown
Owner Author

Rebased ec62c5872 → 841014d29 onto main bfe184bd6. The patch-id is identical, so the GOOD TO GO verdict carries over to the new head.

The old head's Project tests failed on test_no_test_calls_unparse_outside_a_version_gate. That was not this PR. It came from main's #2306 scan test, which called ast.unparse outside a version gate; bfe184bd6 fixes it on main, and CI is re-running on the new head.

@github-actions

Copy link
Copy Markdown
Contributor

Coverage: 91.56% (unit tier, Python 3.15, 841014d29, Unit Tests run success)

Package Statements Missed Branches Partial Cover
pcapkit (top level) 104 4 20 2 95.16%
pcapkit/const 19599 881 2618 867 92.11%
pcapkit/corekit 2591 74 818 37 96.51%
pcapkit/dumpkit 282 3 102 3 98.44%
pcapkit/foundation 3023 23 1044 25 98.82%
pcapkit/interface 138 4 52 3 96.32%
pcapkit/protocols 17437 218 4586 185 98.15%
pcapkit/toolkit 869 36 298 10 95.20%
pcapkit/utilities 504 7 146 5 98.15%
pcapkit/vendor 4432 1988 1018 167 51.87%

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

@JarryShaw
JarryShaw merged commit b7b02c0 into main Oct 26, 2026
61 checks passed
@JarryShaw
JarryShaw deleted the fix/2314-port-field-range branch October 26, 2026 23:42
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Oct 26, 2026
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Oct 26, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Issues reporting a defect (set by the bug report template; a default, not an assessment) fix Pull requests that fix a defect (fix: subject prefix)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

design(corekit): NumberField wraps out-of-range values, e.g. Schema_UDP(srcport=70000)

1 participant