Skip to content

feat(pcap): read tshark JSON packets through one envelope owner with an adjustable expansion bound - #10788

Merged
morluto merged 3 commits into
mainfrom
refactor/tshark-json-reader
Nov 15, 2026
Merged

morluto merged 3 commits into
mainfrom
refactor/tshark-json-reader

Conversation

@morluto

@morluto morluto commented Nov 15, 2026 •

Copy link
Copy Markdown
Owner

Summary

Two readers decoded tshark -T json: src/pcap/PcapOutput.ts (pcap inventory) and src/captures/parsePacketOutput.ts (selected packet fields). They now share one envelope owner, src/pcap/TsharkJsonPackets.ts. Plan W13 step 5 (report 16, "two tshark -T json readers").

Problem and expected behavior

  • The capture reader called JSON.parse directly, so truncated producer output escaped as a bare SyntaxError. A non-array root or a packet without _source.layers only failed later as a schema error on a path like [0, "_source", "layers", …].
  • PcapOutput checked _source.layers in three places: the selected-field pass, the DNS join and the inventory loop.

Expected: one reader owns the export envelope (malformed JSON, the packet array, each packet's _source.layers, dropping _index/_score) and returns typed failures. Each consumer interprets only the layers.

Change and scope

  • parseTsharkJsonPackets(text) is synchronous. It returns Result<layers[], malformed-json | malformed-packet>.

  • readTsharkJsonPackets(text, budget, signal) adds pcap's byte bound, its expansion estimate (charged across acquisitions that share one budget) and cancellation, then parses with parseTsharkJsonPackets.

  • PcapOutput maps the failures to the error kinds it already returned: AnalysisResourceConstraintError, AnalysisProtocolError and AnalysisCancelledError. Its three local envelope checks are removed.

  • parsePacketOutput returns Result<packets, { reason, cause? }> for every refusal: the envelope, the field shape, unordered frames and frames over the limit. TsharkCaptureProvider builds the AnalysisOutputError with capturedOutput, so the producer's stdout and stderr stay attached. Before this, unordered and out-of-limit refusals on main dropped them, and so did the first commit of this PR for envelope refusals (review thread). The capture read stays synchronous and is not signal-aware, so a late abort still keeps complete Evidence (captureProvider.test.ts "post-run integrity checks").

  • Guard: tshark-json-envelope in scripts/verify/architecture-guards.mjs allows _source.layers reads only in the owner, with a zero baseline.

  • Capture expansion budget (coordinator decision, 8b38c62): the capture path now charges the same expansion estimate through readTsharkJsonPackets under a new runtime setting capture.packet_json_expansion_bytes (REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES, src/config/capturePacketJsonExpansion.ts). Measured bound: the estimate charges at most 130 bytes per UTF-16 code unit and the producer text is one Node string (buffer.constants.MAX_STRING_LENGTH = 536,870,888 on Node 24.22.1), so no output callers can select today is charged more than 69,793,215,440 bytes (65.0 GiB). The default and maximum are 137,438,953,472 bytes (128 GiB), floor 1 MiB, so nothing that parses today is refused; the budget refuses only when lowered. Representative -e output was charged 16.9 (pretty-printed, five fields) and 28.2 (compact, one field) bytes per code unit; a dense [[],[],...] worst case 87.3. Over budget, the provider returns an AnalysisResourceConstraintError whose limit names the setting, so details.refusal is adjustable_budget; the producer output stays attached. A malformed launch seed is refused as a ConfigurationError before the capture is read. The read stays unsignalled, so a late abort still keeps complete Evidence.

Not included:

  • The pcap path's fixed 128 MiB expansion allowance is unchanged.

Contribution scope: not applicable (refactor within existing providers).

Contract and boundary impact

  • Semantic owner and earliest changed stage: src/pcap/TsharkJsonPackets.ts now produces the parsed tshark envelope. PcapOutput.parseTsharkJsonOutput and parsePacketOutput consume its layers.
  • Contribution scope fit and linked maintainer decision: not applicable.
  • CLI and MCP/tool-catalog contract: one new runtime analysis setting, capture.packet_json_expansion_bytes, so update_analysis_settings gains one branch (two const members; advertisedDialectBaseline.ts records 83/1857). Otherwise unchanged schemas. Error kinds are unchanged. inspect_packet_capture refusals now carry capturedOutput in every case. Reasons change: envelope failures read TShark emitted malformed JSON. and TShark packet N lacks _source.layers., and field-shape failures read TShark packet fields do not match the selected-field shape. with the Zod error as cause. Pcap root and packet messages now say packet root must be an array and packet N lacks _source.layers.
  • Provider, bridge, target-format, or platform compatibility: unchanged.
  • Evidence, artifact, provenance, or reconstruction contract: unchanged.
  • Target selection, authority, upstream behavior, and cleanup details: unchanged.
  • Generated metadata, package, or installation impact: none.

Expected effects:

  • Read existing files or captured data.
  • Launch or attach to a process.
  • Inject instrumentation or run scripts/code.
  • Change process memory, registers, files, or configuration.
  • Contact network services.
  • Recover protected code or work around protections.
  • Documentation or usability only; no target interaction.
  • Not sure yet; investigation or maintainer guidance needed.

Evidence and regression coverage

  • Tests added or updated:
    • New src/pcap/TsharkJsonPackets.test.ts: layer order and metadata dropping, typed failures for truncated JSON, a non-array root and missing layers, the first malformed packet named, byte bound checked before the estimate, expansion charged across shared acquisitions, abort reason returned.
    • tests/boundary/captures/packetOutput.test.ts: assertions now use the Result shape. Three new cases cover the malformed envelope reasons. Schema issue paths are now relative to the layers.
    • tests/boundary/captures/captureProvider.test.ts: "keeps the producer output of a refused %s packet reply" (invalid JSON, unordered frames) asserts the reason and capturedOutput.stdout. Both cases fail on main and on this PR's first commit.
  • Base reproduction: on main, the new provider cases fail. Unordered frames lose capturedOutput, and malformed JSON reports the raw SyntaxError message.
    • Capture expansion budget: packetOutput.test.ts "refuses output whose expansion estimate exceeds the selected budget"; captureProvider.test.ts "refuses a packet reply over the selected JSON expansion budget with the typed setting refusal" (setting, effective value, source, remediation naming the setting, producer output kept, and the same reply admitted at the default) and "refuses a malformed JSON expansion setting before reading the capture".
  • Remaining proof gaps: no real TShark run. The fake-producer boundary suites below cover both providers.

Boundary regressions covered: malformed input (truncated JSON, wrong root, missing layers), capacity (byte and expansion budget), cancellation during the estimate, and late abort after the producer completed.

  • Observed, derived, and inferred claims remain distinguishable.
  • Artifact identity, source provenance, and failed attempts remain preserved.
  • Unsupported, incomplete, unavailable, or uncertain outcomes remain visible.

Validation performed

  • npm run test:local -- src/pcap/TsharkJsonPackets.test.ts src/pcap/PcapOutput.test.ts src/pcap/PcapConfiguration.test.ts: 92 passed
  • npm run test:focused -- tests/boundary/captures/packetOutput.test.ts tests/boundary/captures/captureProvider.test.ts tests/boundary/pcap/pcapProvider.test.ts tests/boundary/pcap/pcapProviderCancellation.test.ts: 77 passed, 39 skipped (platform-gated)
  • After the review fix: npm run test:focused -- tests/boundary/captures/packetOutput.test.ts tests/boundary/captures/captureProvider.test.ts: 69 passed. Against main's and c490f89's capture sources, the two new provider cases fail.
  • node scripts/verify/architecture-guards.mjs: passed. With main's PcapOutput.ts it reports src/pcap/PcapOutput.ts: 12 (baseline 0) for the new guard.
  • npm run check:fast: passed
  • npx oxfmt --check src/pcap src/captures tests/boundary/captures scripts/verify: passed
  • After the expansion setting (8b38c62): npm run test:focused -- tests/boundary/captures/packetOutput.test.ts tests/boundary/captures/captureProvider.test.ts tests/boundary/mcp/advertisedDialect.test.ts src/pcap/TsharkJsonPackets.test.ts: passed; npm run test:focused -- tests/acceptance/analysisSettingsMcp.test.ts src/application/Doctor.test.ts src/setupEnvironment.test.ts tests/boundary/filesystem/clientConfigurationEnvironment.test.ts src/application/AnalysisSettings.test.ts tests/acceptance/setup/registrationRuntimeEnvironmentCli.test.ts src/domain/analysisSettingAdmission.test.ts: passed; npm run check:fast, npm run docs:check, npx oxfmt --check src tests docs: passed. The bound was measured with a standalone copy of the estimator on Node 24.22.1.

Compatibility, safety, and release

  • Breaking changes or migration steps: none. Error kinds are unchanged; only the message texts listed above change.
  • Real Hopper/Ghidra, browser, or OS coverage: not applicable; no real TShark run.
  • Package or release metadata impact: none.
  • Security, privacy, process, or containment review: no new process or file access. The pcap byte and expansion budgets are unchanged. The capture path gains an expansion check whose default cannot refuse any output Node can hold.

Author readiness

Human owner:

  • I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • I declare this PR ready for initial maintainer review.

Before merge

  • This PR addresses a concrete problem or an agreed enhancement.
  • Material scope and authority questions are resolved, and any required maintainer scope decision covers the final diff.
  • The PR has one focused outcome and the title follows type(scope): outcome.
  • Related issue is linked, or the reason for not linking one is stated above.
  • Tests cover changed observable behavior and meaningful failure paths.
  • Owning docs, contracts, and generated metadata are updated where needed.
  • User-visible CLI/MCP changes include representative output.
  • I checked the final diff for secrets, unrelated cleanup, and unsupported claims.

PcapOutput and the packet-capture reader each decoded tshark -T json and checked the packet envelope. The capture reader let SyntaxError escape and accepted any shape until its field schema ran. src/pcap/TsharkJsonPackets.ts now owns the envelope (malformed JSON, the packet array, each packet's _source.layers) as typed failures, and the pcap byte and expansion budget. Both consumers map those failures to their existing error kinds. An architecture guard keeps _source.layers reads in the owner.
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-15T06:34:53.302233Z 8b38c62 New commits
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 5/5 Tier: plus

[Medium impact] The PR appears safe to merge; no new actionable issues were found.

Summary

Both TShark readers now use TsharkJsonPackets.ts to check the JSON envelope and return packet layers.

  • Both packet readers now share one TShark JSON envelope reader.
  • Packet inspection can limit JSON expansion with a runtime setting.

Diagram

%%{init: {'theme': 'neutral'}}%%
flowchart TD
  P[PcapOutput: fixed shared budget] --> R[readTsharkJsonPackets]
  C[parsePacketOutput: selected budget] --> R
  R --> B[Check bytes and estimated expansion]
  B --> E[parseTsharkJsonPackets]
  E --> L[Ordered packet layers]
  L --> I[Pcap inventory and DNS join]
  L --> F[Selected-field checks]
  F --> T[TsharkCaptureProvider attaches captured output]
Loading

Reviews (3) · Last reviewed commit: "feat(captures): bound packet-capture JSO..." · Reviewed by Greptile

Comment thread src/captures/parsePacketOutput.ts Resolved
parsePacketOutput threw AnalysisOutputError itself, so the provider's failure mapping returned it without the captured stdout and stderr, both for envelope refusals and for unordered or out-of-limit frames. It now returns a Result with the reason and cause, and the provider builds the AnalysisOutputError with capturedOutput.
…setting

The capture path parsed TShark's selected-field JSON without the expansion
estimate the pcap reader charges. It now reads through readTsharkJsonPackets
under capture.packet_json_expansion_bytes (REA_CAPTURE_PACKET_JSON_EXPANSION_BYTES).
The estimate charges at most 130 bytes per code unit and the output is one
Node string, so no admissible output reaches 69,793,215,440 bytes; the default
and maximum are 128 GiB, so the budget refuses only when lowered. A refusal is
an adjustable_budget resource constraint naming the setting and keeps the
producer output; a malformed launch seed is refused before the capture is read.
@morluto morluto changed the title refactor(pcap): read tshark JSON packets through one envelope owner feat(pcap): read tshark JSON packets through one envelope owner with an adjustable expansion bound Nov 15, 2026
@morluto
morluto merged commit c06a0b9 into main Nov 15, 2026
56 of 58 checks passed
@morluto
morluto deleted the refactor/tshark-json-reader branch November 15, 2026 08:17
@morluto morluto added enhancement New feature or request provider: pcap Analysis provider / engine: pcap provider: tshark Analysis provider / engine: tshark labels Nov 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request provider: pcap Analysis provider / engine: pcap provider: tshark Analysis provider / engine: tshark

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant