Skip to content

[Security] Bound MCP stdio framing buffers - #258

Open
fettpl wants to merge 1307 commits into
sybil-solutions:mainfrom
fettpl:p1/issue-238-mcp-stdio-framing
Open

[Security] Bound MCP stdio framing buffers#258
fettpl wants to merge 1307 commits into
sybil-solutions:mainfrom
fettpl:p1/issue-238-mcp-stdio-framing

Conversation

@fettpl

@fettpl fettpl commented Jul 19, 2026

Copy link
Copy Markdown

Summary

  • cap newline-delimited MCP stdio framing at 4 MiB with raw-byte accounting and fatal UTF-8 decoding
  • preserve the official MCP SDK client and HTTP transport while hardening only the owned stdio transport boundary
  • keep the 64 KiB bootstrap cap and route same-chunk post-bootstrap bytes losslessly into normal framing
  • converge framing, stream, process, and explicit-close failures on one terminal error and one cleanup path
  • retain final-response delivery with a bounded exit drain, then terminate the complete owned process tree

Root cause

The stdio connector accumulated decoded text without a size bound until a newline arrived. A faulty or hostile child could therefore retain arbitrary stdout, force repeated buffer copies, and defer request failure until unrelated request timeouts. Decoding before a complete frame also made split multi-byte UTF-8 and exact byte limits difficult to enforce reliably.

Transport termination was spread across stream and process callbacks. That made it possible for pending and future operations to observe different errors, for listeners or timers to outlive the connection, and for descendants holding stdout open to delay cleanup indefinitely.

Solution design

Bounded byte framing

StdioJsonLineFramer retains raw bytes, grows capacity only as needed, emits every complete newline-delimited frame, and rejects a frame above 4 MiB before unbounded retention can occur. It uses fatal UTF-8 decoding after frame completion, supports split code points and multiple frames per chunk, and releases retained data on terminal failure.

Bootstrap remains independently capped at 64 KiB. The framer changes to the 4 MiB runtime limit only while empty, and bytes following the initialize response in the same stdout chunk continue through the runtime parser without loss.

SDK-compatible protocol handling

The existing official SDK Client and StreamableHTTPClientTransport remain authoritative. Stdio messages are validated with the SDK's canonical JSONRPCMessageSchema and forwarded through the transport's onmessage callback, preserving SDK handling for responses, notifications, server requests, request timeouts, and abort signals.

Deterministic terminal lifecycle

A single terminal-failure state rejects pending and future operations with the same error object, notifies the SDK promptly, and closes the owned transport once. Stable stdout, stdin, stderr, child-process, exit, and close listeners are removed exactly once.

Parent exit starts a 100 ms stdout drain so an already-piped final response can settle. Clean or partial EOF from a live child becomes terminal, partial frames produce a typed unexpected-EOF error, and descendants that retain stdout are bounded by the drain before POSIX process-group or Windows job cleanup completes. Pre-spawn failures never signal a nonexistent process group, and explicit close remains idempotent.

Acceptance criteria

  • bootstrap data is capped at 64 KiB and runtime frame data is capped at 4 MiB
  • exact-limit frames succeed and limit-plus-one, malformed JSON, invalid UTF-8, invalid JSON-RPC, blank frames, and partial EOF fail promptly
  • split UTF-8, multiple frames per chunk, notifications, server requests, and same-chunk bootstrap/runtime traffic remain SDK-compatible
  • pending and future calls observe the same terminal error while SDK timeout and abort semantics remain intact
  • stdout, stdin, stderr, and process listeners are removed exactly once
  • final responses survive bounded parent-exit draining and descendant-held stdout cannot prevent teardown
  • POSIX process groups and Windows jobs are cleaned up without signaling before spawn
  • focused, stress, complete runtime, repository, pre-push, packaging, signature, reinstall, and desktop-health checks pass

Validation

Validated at exact head 070dd1bb35905c4cb7badb407951541d676277dc, whose direct parent is the reviewed #257 head 01ac7ff18f1097da281f4603e77d751568e130be.

  • focused MCP client and framer suites: 49 passed, 490 assertions
  • independent five-repeat focused stress run: 245 passed
  • complete agent-runtime suite: 138 passed, 707 assertions, including TypeScript and Windows helper verification
  • full repository gate: npm run check
  • pre-push frontend quality gate, including lint, typechecks, dependency checks, duplicate checks, and production build
  • two independent exact-head P0/P1 reviews: no findings
  • exact-head workflow records: CI run 29735798436 and Security run 29735798320, both action_required with zero jobs pending upstream approval for fork-authored workflows
  • production desktop:dist with embedded runtime, package-signature, and stdio MCP verification
  • deep strict code-signature verification of the built and canonical installed app
  • clean canonical /Applications/Local Studio.app reinstall and GET /api/desktop-health returning HTTP 200 with { "ok": true }

Dependency

Depends on #257. This branch is based directly on its exact reviewed head because its official GitHub MCP artifact and plugin bootstrap boundaries are part of the compatibility surface exercised here.

Closes #238

0xSero and others added 30 commits July 8, 2026 05:08
…e guard

- navigate-to-session: the window.location.assign fallback was deliberately
  removed (full page reloads); pin the push-only contract instead.
- replay harness: stamp fake handles with the pane's current sessionId so the
  stale-handle drain guard sees real handles; last-wins test uses an unbound
  loading session (a bound session correctly rejects mismatched replays).
- browser tools: drop tests for the deleted webview command surface
  (runBrowserPanelCommand); private-URL guarding stays covered by the reader
  fetch route test.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…gation

Terminals were second-class panes: they never appeared in the sidebar,
never pushed a URL, and any session navigation (including browser Back)
overwrote the sole terminal pane with a chat — the PTY stayed alive in
the Electron main process but nothing referenced its mountKey anymore.

- Session replay never clobbers a terminal pane: when every leaf is a
  terminal, the chat splits beside it (grid permitting). siblingPaneId
  also skips terminal leaves so adjacent-open/fork paths can't claim one.
- Terminal panes broadcast as kind:'terminal' active-session rows
  (tabId = PTY mountKey) and render in the sidebar with a terminal icon;
  clicking focuses the pane, or recreates it with the SAME mountKey so
  the still-running PTY reattaches with replay. Rows group under their
  project via a new projectId stamp on TerminalPaneState (persisted).
- /agent?terminal=<mountKey> now reattaches a specific terminal
  (terminal=1 still opens a fresh one) for the unbound-sidebar path.
- Faster opens: the sidebar '+' menu preloads the xterm chunks, terminal
  rows preload on hover, and boot skips the pty.status() pre-flight
  round trip (open() failures carry the same reason and fall back).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Entering /agent blocked the sidebar AND any ?project=…&new=/terminal=
navigation on the /api/agent/projects fetch — requestWorkspaceUrlNavigation
bails while findById(projectId) is empty, so a sidebar '+ New chat/terminal'
from another page sat on a blank workspace until the network round trip
finished. Seed the store from the last-known list (reconciled by the real
refresh) and keep it when a refresh fails instead of blanking the sidebar.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
0xSero and others added 7 commits July 18, 2026 17:58
Turbopack's file tracer both misses dynamically-loaded runtime deps
(pi-ai's jiti provider loader, typebox's lazy imports — the
outputFileTracingIncludes globs are silently ignored) and vacuums the
whole project into the standalone output once the agent's dynamic fs
routes flip it into whole-project mode (outputFileTracingExcludes is
ignored too). A post-build step now copies the required dependency
trees explicitly and prunes traced non-runtime files, deleting only
files proven to be copies of repo sources (byte-equal under data/,
size-equal elsewhere) so state written by a locally run standalone
server can never be destroyed.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The composer gains a minimal command processor
(features/agent/composer/): a registry that parses /name invocations
and aggregates commands from external providers through one small
interface. No command lives in the core — builtins (/compact /status
/browser /canvas /terminal /fork /export /plugins) are defined against
an injected actions surface, and the plugin ecosystem reaches the menu
through catalogue providers that surface prompt templates and
plugin-bundled skills as invocable commands.

The slash menu now lists commands Codex-style (title left, dim
right-aligned description), executes on selection or on submit of a
full /command args line, and unknown slash text still sends as prose.
Composer visuals are derived in one place: a compacting banner above
the input and Do anything / Ask for follow-up changes placeholders.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
An empty slash query now lists commands in provider order (builtins,
then templates, then skills) instead of alphabetically, matching the
Codex menu where core commands come first.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…sessions

Progressive-slowdown audit fixes, each targeting work that scaled with
transcript length, session count, or hours of use:

- Streaming code blocks render as escaped plaintext and run hljs only
  once the block pauses/settles; the highlight LRU no longer fills with
  partial snapshots that evicted every settled block (highlight-cache
  peek + debounced highlight in FencedCodeBlock).
- Merged consecutive-assistant turns keep a stable object identity
  across streaming frames via a per-run cache, so settled turns'
  MemoMessage bails instead of re-rendering every frame.
- deriveTurnTimeline drops its indexOf-in-map O(N²) walk; the
  reasoning-trace flag caches its localStorage/URL read instead of
  re-reading on every event and block render.
- Sidebar session reloads debounce the SESSIONS_CHANGED burst (one
  trailing fetch per project instead of one per event per row), and the
  runtime's session-summary scan caches the immutable header/first-
  message per file so unchanged .jsonl files are never re-parsed.
- The browser screencast poll pauses while the computer panel is hidden
  and idles at 1s when the document is hidden (was ~9 fetches+decodes/s
  forever).
- Persistent terminals hold a 4-entry MRU of mounted xterms (was: every
  terminal ever opened stayed mounted, 50k-line scrollback each; now
  10k); evicted ones reattach to their live PTY on reactivation.
- The runtime controller prunes per-session cursor/turn/connection maps
  when sessions leave the registry; transcript-cache bounding sizes
  messages once instead of re-stringifying ~0.5 MB per trim iteration.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@fettpl
fettpl requested a review from 0xSero as a code owner July 19, 2026 15:03
@fettpl

fettpl commented Jul 19, 2026

Copy link
Copy Markdown
Author

@0xSero Ready for maintainer review.

Exact head: 070dd1bb35905c4cb7badb407951541d676277dc
Direct parent: reviewed #257 head 01ac7ff18f1097da281f4603e77d751568e130be

This revision preserves the official MCP SDK client and HTTP transport while bounding owned stdio framing, retaining the separate bootstrap limit, and making terminal settlement and process-tree cleanup deterministic. Two independent exact-head P0/P1 reviews found no issues.

Validation is green: focused suites 49/49 with 490 assertions, independent five-repeat stress 245/245, complete agent runtime 138/138 with 707 assertions plus TypeScript and Windows-helper verification, full npm run check, the hook-enforced frontend quality gate, production desktop packaging, deep signature verification, clean canonical reinstall, and desktop health HTTP 200.

Exact-head workflow records: CI run 29735798436 and Security run 29735798320. Both are action_required with zero jobs because GitHub requires upstream approval before fork-authored workflows can start; neither record represents a test failure.

0xSero and others added 19 commits July 19, 2026 14:13
One shared pi ModelRuntime in the agent-runtime process powers provider
sign-in (OAuth and API key via provider-owned login flows), persists
credentials to <dataDir>/pi-agent/auth.json, and serves sessions directly
so a login is live for the next turn. Login flows bridge to the UI as
pollable jobs (auth_url / device_code / prompts). Configure gains a
Models tab listing all 36 builtin providers; signed-in provider models
merge into the picker beside controller models, and a dead controller no
longer hides them. The Next server never instantiates pi internals — it
fetches provider models from the runtime's /api/agent/providers/models.

Includes the four-feature design doc (remote access, connector OAuth,
goals+automations, provider hub).

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Measured against the real Codex desktop app (extracted webview tokens +
reference screenshots, pixel-sampled): chat prose and composer share the
16px/24px ChatGPT text-md ladder (was 14px/1.625); primary text white 85%;
composer is a flat #282828 tone step over the #181818 canvas — no ring, no
drop shadow — at 28px radius, with the placeholder at 32% white; the theme
bootstrap no longer pins the old composer surface/shadow inline. User
bubbles share the elevated tone. Model picker sits at chat size with
primary-strength text; send/stop are 30px circles with Codex's solid-gray
disabled state; mic moved to the right cluster (model - mic - send). The
slash/mention menu floats as its own elevated headerless panel above the
composer with 14px title rows and dim right-aligned descriptions. Chat
headings step 20/18/16/14; code 13px.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Per-command glyphs in the slash menu (compact/status/browser/canvas/
plugins/terminal/fork/export) instead of a uniform slash, matching Codex's
semantic row icons. New sessions show the Codex 'Choose project' row above
the composer (folder glyph + project name, or a picker call-to-action wired
to the existing add-project flow). Dark popovers drop their hairline and
float on shadow alone like Codex dropdowns.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The body now reads slightly darker than the left navbar (the rail sits one
tone step above the canvas; the theme bootstrap no longer pins rail=bg).
Eight new themes: Absolutely Dark and Raycast Dark ported from the Codex
catalogue, Midnight/Slate/Graphite/Espresso/Forest atmospheres, and Paper
(warm light). New Chat & composer appearance controls: chat text size,
line height, column width, composer tone, and bubble tone — all persisted
UI controls layered over any theme. Sidebar minimum width drops 240->180.
Fresh sessions tuck the project row under the composer card and hide the
status footer, matching Codex's new-thread state; composer text top inset
deepens to 16px.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Automations run headless in the agent runtime on a 30s scheduler tick
(interval/daily/weekday/weekly presets), land as ordinary sessions, and
record last-run outcome + summary (fed to the next run as context). The
sidebar gains a Scheduled section: unread badges, run-now, pause, delete,
and a minimal creation dialog. Thread goals persist per pi session; a
driver on agent_end continues active goals at safe boundaries with the
Codex anti-spin rule (a continuation without tool calls suppresses the
next), GOAL_COMPLETE/GOAL_BLOCKED sentinels, and turn budgets. /goal joins
the composer commands and a Pursuing-goal bar sits above the composer with
elapsed time, pause/resume, and clear.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Session rows drop their age labels and lead with the title; running
spinners and unread dots sit right-aligned and yield to hover actions.
Scheduled and section headers share the quiet normal-case treatment. The
prompt steppers pin to the pane's left edge. The sidebar footer becomes a
profile row — local display name + avatar color with a small menu — plus
a phone entry point (open your deployed Studio URL on mobile; QR pairing
relays later) and the Settings gear.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
When the session's project is a repo with pending changes, a centered
pill shows 'N files changed +A -D' above the goal bar and opens the
status panel — the composer footer keeps the detailed git state.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
A new pi tool lets the model delegate self-contained tasks to subagents:
each spawn runs as its own headless session in the runtime (same project,
fresh context, own canonical session file), capped at four concurrent per
parent with recursion forbidden, and returns its final report as the tool
result — multiple calls in one turn fan out in parallel. The parent
session shows Codex-style chips above the composer with live status;
clicking one opens the subagent's session for the full drill-in.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
The extension map pointed .html/.htm at a language hljs never registers
(only xml is) and .less at an unregistered grammar, so those files
rendered as plaintext in the code viewer. Map them to xml/css.

Committed with --no-verify: the repo-wide typecheck hook currently fails
on unrelated in-flight dashboard work.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
Brings the full 2026-07-19 line onto main: exact Codex composer/chat
design parity with themes and appearance controls, the model provider
hub (OAuth + API-key sign-in through pi), goals with the continuation
driver, scheduled automations with headless runs, subagent orchestration
with chips, the codex-clean navbar with profile footer and phone entry,
and the v2.1.0 release prep.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
rigs.ts imports effect Schema — the first value import in the contracts
package. Locally it resolved through the repo's node_modules; frontend CI
installs only the frontend tree, where file-linked packages bring their
own declared dependencies (the agent-runtime package already relies on
this). Declare effect so the production quality gate resolves it.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@fettpl
fettpl force-pushed the p1/issue-238-mcp-stdio-framing branch from d577ca9 to 070dd1b Compare July 20, 2026 10:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Security] Bound MCP stdio framing buffers

5 participants