Skip to content

Structured parse diagnostics on ProtocolError #91

Description

@EONRaider

Target: 2.0.0 (Tier 2 — from codec to platform)

Strictness is the library's security story: we raise where scapy
silently fills defaults. But every exception carries only a formatted
string. Verified — the full attribute set on a raised
TruncatedHeaderError is args, add_note, with_traceback. There is
no offset, no field, no protocol, no expected/actual.

Ethernet TruncatedHeaderError "Ethernet header needs 14 bytes, buffer holds 4"
IPv4 InvalidFieldError "IPv4 IHL must be at least 5, got 0"

Everything useful is in the prose. A fuzzing harness, a conformance
suite or a protocol-validation tool that wants to know where a parse
failed has to regex our error messages.

Why this is a differentiator, not just a nicety

Scapy mostly does not raise at all. dpkt raises bare UnpackError /
NeedData with nothing attached. For the audience we most want —
people parsing untrusted input — this is the difference between a
library that rejects bad input and one that explains it. No
competitor offers it.

What to do

Add structured attributes to ProtocolError and populate them at every
raise site. Candidates: offset (byte offset into the buffer handed to
decode), protocol (the class), field (name), expected,
actual.

Design questions:

  • Offsets are per-decode()-call, since each layer receives its own
    buffer slice. An offset absolute to the original frame needs the walker
    (Ship decode_frame() — the chain walker belongs in the library #88) to add its cursor. Decide whether the exception carries a
    relative offset that decode_frame rebases, or whether both are
    exposed.
  • Keep the message strings as they are — they are good, and tests
    assert on some of them.

Acceptance criteria

  • Every ProtocolError raised from src/ carries at least
    protocol and, where meaningful, offset and field.
  • Existing message text is unchanged.
  • decode_frame rebases relative offsets to the whole frame, or
    documents why it does not.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions