Skip to content

feat(foundation): reassemble TLS records split across TCP segments (#2938) - #3138

Merged
JarryShaw merged 1 commit into
developfrom
feat/2938-tls-record-reassembly
Nov 27, 2026
Merged

JarryShaw merged 1 commit into
developfrom
feat/2938-tls-record-reassembly

Conversation

@JarryShaw

Copy link
Copy Markdown
Owner

Please follow the guide below

  • Put an x into every box [ ] that applies to your pull request (like this: [x])

  • Use the Preview tab to see how your pull request will look

  • Searched for similar pull requests

  • Followed the coding style (make pylint, make mypy, make isort)

  • make test passes, and a test case covers the change

  • Described the user-visible change below and left its changelog entry to the maintainer, keeping docs/source/changelog/ and CHANGELOG.md out of the diff, if the change is user-visible: N/A — centralised

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

  • Also tick if it regenerates constant or registry tables, members keeping their numeric values (the const label)


Description of your pull request and other information

Closes #2938.

flowchart TD
    SEG["a TCP segment on a port TCP binds to TLS"] -->|"toolkit <engine>.tls_reassembly: TCP adapter's record"| RE["reassembly.tls.TLS: one octet stream per direction"]
    RE -->|"TLS.record_header (RFC 9846 §5.1)"| REC["every record, COMPLETE or PARTIAL/TIMEOUT"]
    REC -->|"packet, deferred: TLS.analyze_record"| PKT["TLS layer, hello extensions decoded"]
Loading
  • reassembly=['tls'] on pcap, pcapng, dpkt, scapy and pypcapfile; ReassemblyData.tls; analysis.tls_record. A stream starts at its SYN, or at a segment whose payload opens a record header; out-of-order octets wait, the first received win (RFC 9293 §3.4, §3.10); FIN/RST close it.
  • Every record of the stream is reported, single-segment ones too, since a boundary is only known from the record before it. Incomplete records get packet=None, as DTLS's do.
  • TCP reassembly's _unwrap moves to pcapkit.utilities.seqnum.unwrap, now that TLS reads it too.
  • test_tls_record_reassembly_2938_unit and test_seqnum_2938_unit fail on an untouched origin/develop export; pins moved for the new tls built-in field and toolkit __all__.

@JarryShaw JarryShaw added feat Pull requests that add a new capability (feat: subject prefix) review: running A cross-review is in flight against the current head - no verdict yet labels Nov 27, 2026
@JarryShaw JarryShaw added this to the PyPCAPKit v1.9 milestone Nov 27, 2026
@JarryShaw JarryShaw linked an issue Nov 27, 2026 that may be closed by this pull request
@JarryShaw JarryShaw moved this to In review in PyPCAPKit Nov 27, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

Review verdict: needs changes at 1a777e2b2, from a cross-review on Sonnet.

  1. TLS.record_header doesn't cap the declared length. On a stream with no SYN, a first segment that opens with 17 03 03 ff ff locks the stream onto a 65535-octet record. That swallows the real record behind it and, with no default timeout, never releases. Reproduced. Fix: reject any length above 2¹⁴ + 2048, the largest record any TLS version allows. Add a test for exactly this case. Then reword the resync warning in the reassembler docs, which overstates how safe the resync is.

Confirmed:

  • Split, coalesced and out-of-order records, a split header, zero-length and maximum-size records, the 2³² wrap, and overlap keep-first all cut correctly.
  • FIN and RST close the stream.
  • Parsing stays on TLS.
  • unwrap moved, with no copy and no re-export.
  • All five engines are wired.
  • The new modules are at 100% coverage, and four mutations are caught.

For whichever of #3137 and this PR lands second: pick one order for the ReassemblyData fields and their pin, merge both _KT constraints, and use one way of noting the new fields. The fix goes to the original worker, and this head's CI is cancelled.

@JarryShaw JarryShaw added review: needs-changes Cross-review at the current head says changes are required; see the verdict comment and removed review: running A cross-review is in flight against the current head - no verdict yet labels Nov 27, 2026
@JarryShaw
JarryShaw force-pushed the feat/2938-tls-record-reassembly branch from 1a777e2 to ed1e6fe Compare November 27, 2026 08:53
@JarryShaw
JarryShaw force-pushed the feat/2938-tls-record-reassembly branch from ed1e6fe to 61a31fc Compare November 27, 2026 09:13
@JarryShaw JarryShaw added review: running A cross-review is in flight against the current head - no verdict yet and removed review: needs-changes Cross-review at the current head says changes are required; see the verdict comment labels Nov 27, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

Re-review verdict: good to go at 61a31fcd9, from a delta re-review on Sonnet.

  • Length cap: the false-lock reproduction (a stray 17 03 03 ff ff with no SYN) now leaves the real record behind it whole. A header declaring exactly 2¹⁴ + 2048 is still read, and one octet more raises record_overflow. A bogus header that fits under the cap can still mislead the stream, which the reassembler's warning now says.
  • First-record path: the cap now holds there too, for TLS and legacy DTLS. DTLS 1.3 unified headers are capped at 2¹⁴ + 256. Every RFC citation was checked against the texts.
  • make(): it refuses an oversized fragment. The new tests fail on the old tree (15 of 18). tests/project passes (2481), and the only failures elsewhere are runtime tests that need captures not present in the worktree, which fail on develop too.
  • SSLv2 fallback test: the moved fixture still checks a header that declares more than the buffer holds.
  • Nit: the RFC 9146 §4 citation for tls12_cid is loose, because the bound actually comes through DTLS 1.2.

For whichever of this PR and #3137 lands second: reconcile the ReassemblyData field order and its pin, the _KT constraints, and how the new fields are noted.

@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 Nov 27, 2026
@github-actions

github-actions Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Coverage: 94.81% (unit tier, Python 3.15, 8e9cbf361, Unit Tests run success)

Package Statements Missed Branches Partial Cover
pcapkit (top level) 110 4 20 2 95.38%
pcapkit/const 21794 212 3100 185 98.32%
pcapkit/corekit 3059 83 1004 40 96.73%
pcapkit/dumpkit 423 3 148 5 98.60%
pcapkit/foundation 4118 24 1326 26 99.08%
pcapkit/interface 103 4 34 3 94.89%
pcapkit/protocols 26422 159 6970 129 99.13%
pcapkit/toolkit 1016 26 360 8 96.80%
pcapkit/utilities 596 7 162 5 98.42%
pcapkit/vendor 6214 2320 1412 245 59.36%

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

@JarryShaw

Copy link
Copy Markdown
Owner Author

Ready to merge at 61a31fcd9. The verdict is good to go and every required check is green. The KTLO legs carry no annotations, and only GraalPy (experimental, non-blocking) is still running. #3137 is also ready and touches the same files, so whichever of the two lands second needs a rebase. I will do that rebase.

@JarryShaw

Copy link
Copy Markdown
Owner Author

Resolve conflicts

@JarryShaw

Copy link
Copy Markdown
Owner Author

On it: the original worker is already rebasing this onto develop, keeping #3137's order for the reassembly fields and its _KT and field-note conventions, then adding TLS. A rebase note follows with the old and new heads and the range-diff.

…2938)

- add pcapkit.foundation.reassembly.tls.TLS, registered as 'tls': each TCP
  direction is one octet stream, followed from its SYN or from a segment
  whose payload opens a record header, and cut into records by their
  headers (RFC 9846 section 5.1); out-of-order octets wait and the first
  received win (RFC 9293 sections 3.4 and 3.10); a FIN or RST closes it
- report every record of the stream, COMPLETE, or PARTIAL/TIMEOUT with no
  packet when a hole or the stream's end cuts it
- add TLS.record_header and TLS.analyze_record, and the public wrapper
  pcapkit.foundation.analysis.tls.tls_record
- refuse a TLS or legacy DTLS record declaring more than 2^14 + 2048 octets
  (RFC 5246 section 6.2.3), and a DTLS 1.3 unified-header one more than
  2^14 + 256 (RFC 9147 section 4), in parse and make, first or further
- add tls_reassembly adapters for pcap, pcapng, dpkt, scapy and pypcapfile,
  keeping the TCP adapter's record of a segment on a port TCP binds to TLS
- add ReassemblyData.tls; Deferred admits the TLS record header
- move TCP reassembly's _unwrap to pcapkit.utilities.seqnum.unwrap, which
  both reassemblers read
- document the reassembler, its analysis and the toolkit adapters

Tests: new TLS reassembly and seqnum tests fail on origin/develop; tests/project,
tests/foundation, tests/toolkit, tests/protocols/application pass.
@JarryShaw
JarryShaw force-pushed the feat/2938-tls-record-reassembly branch from 61a31fc to 8e9cbf3 Compare November 27, 2026 16:54
@JarryShaw

Copy link
Copy Markdown
Owner Author

Rebased 61a31fcd9 → 8e9cbf361, onto develop at 974e1f48b, which now includes #3137 (SCTP), #3143 and #3147.

@JarryShaw JarryShaw added review: running A cross-review is in flight against the current head - no verdict yet and removed review: good-to-go Cross-review at the current head says ready; CI state is separate labels Nov 27, 2026
@JarryShaw

Copy link
Copy Markdown
Owner Author

Re-review verdict: good to go at 8e9cbf361, from a delta re-review on Sonnet covering the conflict resolution.

  • ReassemblyData: fields run …, dtls, sctp, tls, and tls takes a field-level versionadded:: 1.9.0, as sctp does. That meets the one-versionadded-per-block rule.
  • _KT and Deferred: _KT holds both constraints, and Deferred carries a single 1.9.0 note citing feat(foundation): reassemble SCTP user messages #2935 and feat(foundation): reassemble TLS records split across TCP segments #2938.
  • strict docstrings: they keep feat(foundation): reassemble SCTP user messages (#2935) #3137's wording, and the appended TLS text matches TLS.submit.
  • Wiring: engines, Extractor, all.py and the toolkit only add tls. The count of sctp mentions across all 57 changed files equals develop's, so no SCTP work was lost.
  • Pins: 2847, 2814, the 2935 SCTP pin and 1570 all place tls after sctp.
  • Tests: tests/foundation/reassembly and the touched modules pass (417), as do tests/project, the root suites, tests/toolkit and the TLS/DTLS record files (2841). The rest of tests/protocols was cut short for host load. Its only protocol change is tls/protocol.py, whose tests pass, and CI covers the rest.

@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 Nov 27, 2026
@JarryShaw
JarryShaw merged commit 4d7dea2 into develop Nov 27, 2026
75 checks passed
@github-project-automation github-project-automation Bot moved this from In review to Done in PyPCAPKit Nov 27, 2026
@JarryShaw
JarryShaw deleted the feat/2938-tls-record-reassembly branch November 27, 2026 21:42
@JarryShaw JarryShaw removed the review: good-to-go Cross-review at the current head says ready; CI state is separate label Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

feat Pull requests that add a new capability (feat: subject prefix)

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

feat(foundation): reassemble TLS records split across TCP segments

1 participant