Skip to content

Bound ASCII integer parsing in Parser and charge UTF-8 bytes in _event_byte_cost() #23

Done

Description

@tony

Summary

Parser._dispatch_osc() in src/libvt/parser.py (#2) parses OSC command numbers with int(head) if head.isdigit() else -1, which raises ValueError (Exceeds the limit (4300 digits) for integer string conversion) on untrusted OSC headers over 4,300 ASCII digits while accepting non-ASCII isdigit() bytes in downstream str.isdigit() checks (terminal.py, tmux.py) and unbounded int() strings in graphics.py. Meanwhile, _event_byte_cost() in src/libvt/terminal.py charges a flat 64 bytes for TitleChanged, WorkingDirectoryChanged, WindowRenamed, ProgramStatusChanged, and UnhandledSequence, letting max_events (4,096) retain hundreds of megabytes of string payloads despite Limits.max_event_bytes.

Reproduction

  1. Feed an OSC sequence with a 4,301-digit command prefix into Terminal.feed():
uv run python -c 'from libvt import Terminal; vt = Terminal(rows=2, cols=10); vt.feed(b"\x1b]" + b"9" * 4301 + b";x\x07")'

Expected

  • Terminal.feed() and Parser.feed() never raise ValueError or other implementation exceptions on arbitrary untrusted byte streams (matching libghostty-vt terminal.h at 11ed660d9 untrusted-input semantics when Policy.strict=False).
  • Integer fields parse strictly ASCII digits 0x30..0x39 with bounded accumulators: OSC command codes clamp/reject above 65535 (matching xterm do_osc), Kitty APC G i= and I= accept full unsigned 32-bit IDs up to 4,294,967,295 (10 ASCII digits), and OSC 133;D exit codes parse bounded signed ASCII integers.
  • _event_byte_cost() accounts for UTF-8 byte lengths of variable-size strings and dictionaries across every Event variant so unread _events memory stays bounded by Limits.max_event_bytes.

Actual

ValueError: Exceeds the limit (4300 digits) for integer string conversion crashes Terminal.feed().

Environment

Versions
  • libvt: #2 (0.1.0a0)
  • Python: 3.15.1
  • OS: Linux x86_64

Proposal

Invariants

The invariants below are load-bearing; everything under Intent is open.

  • With Policy.strict=False (default), Parser.feed(), Parser.feed_to(), and Terminal.feed() never raise ValueError, OverflowError, IndexError, or KeyError for any bytes input.
  • Only ASCII digits 0x30..0x39 (plus leading - where signed values are defined by spec) are accepted as protocol numeric digits; Unicode superscripts and non-ASCII decimal characters never match.
  • Cumulative UTF-8 payload bytes across buffered Terminal events never exceed Limits.max_event_bytes.

Intent (non-binding)

  • Add small ASCII byte/string integer helpers with explicit ceilings (parse_ascii_uint(raw, max_value=...) and parse_ascii_int(...)) shared by parser.py, terminal.py, graphics.py, and tmux.py.
  • Measure UTF-8 string/control byte footprints across TitleChanged, WindowRenamed, WorkingDirectoryChanged, ClipboardWrite, ProgramStatusChanged, KittyGraphic (control + payload), SixelGraphic, Passthrough, and UnhandledSequence in _event_byte_cost().

Not doing

  • Raising sys.set_int_max_str_digits() process-wide.

References

Activity

  1. added
    bug
    on Nov 23, 2026
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

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions