Skip to content

Approval prompt: bound Steps: lines (and every prompt line) so a long argument cannot push [y/N] off-screen - #93

Open
cuihtlauac wants to merge 6 commits into
mainfrom
fix/85-steps-bound
Open

cuihtlauac wants to merge 6 commits into
mainfrom
fix/85-steps-bound

Conversation

@cuihtlauac

@cuihtlauac cuihtlauac commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Summary

Fixes #85. Part of #89 (Phase 0).

Only the Command: line of the approval prompt was length-bounded (G4.3 / F3). A privileged exec ["echo", <900 KB>] printed the whole argument on Steps: line 0 and pushed the [y/N] question off-screen. The Resolves: block and line (a long absolute argv[0], up to 256 entries) and the metadata lines were unbounded too.

This PR implements the approved plan (the 🤖 Approved plan (Claude) — final comment on #85). It changes the display only: validation, execution, Response and the audit log do not change.

  • src/tui.rs: new constants MAX_DISPLAY_LINE_CHARS (160), MAX_DISPLAY_FIELD_CHARS (256) and MAX_DISPLAY_RESOLVES (32), and new helpers split_at_chars, clip and clip_styled. clip_styled counts the daemon's own CSI escapes as zero width, writes reset before the marker, and is byte-identical when the line fits. truncate_for_display keeps its exact output and now sits on split_at_chars.
    • Steps: lines (text plus # note) are clipped to 160 visible chars. The gutter and indent are outside the budget.
    • Resolves: block: the name is clipped before the dedup key and the pad width. Paths are clipped in resolve_detail, including the opaque and canonicalize failed arms. The block is capped at 32 entries, followed by … N more binaries hidden.
    • Resolves: single line: branches are chosen on the unclipped strings, and the strings are shown clipped.
    • Metadata: From:, Requester:, Client:, Time:, ID:, Reason: and the session name in the a line are clipped to 256. The lines from Client: through Command: move to write_request_meta, and Env: moves to write_env_line. Both are writer-generic, so they can be unit-tested. For short values prompt_tty writes the same bytes as before.
    • steps_shell_text and command_summary do not change. The bash-oracle properties still check the full texts.
  • Docs: Sn4.3 now covers every prompt line and is moved to Rung 2, with the small-terminal / column-width / tab residual. Also updated: the threat-model row and attack-tree leaf, an F3 note in docs/security-audit.md, a long-argument row in the docs/testing.md UI table, a changelog entry, and Done: #85. in backlog.md. The changelog and backlog edits only add lines.

Test evidence

  • cargo clippy --all-targets --all-features -- -D warnings (the CI command): clean.
  • cargo test --no-fail-fast: all suites pass. Lib: 118 passed, including 6 new tui tests. pty_ui_golden: 4 passed.
    • On the first full run, concurrency::burst_connections_above_cap_get_busy_response failed once. This is the known flaky burst test. It passed when the whole suite was run again (13/13), and this PR does not touch that code.
  • New unit tests (src/tui.rs):
    • clip_bounds_and_marks
    • clip_styled_ignores_escapes_and_resets (includes the tab case)
    • steps_line_is_bounded_for_huge_argument
    • resolves_block_bounds_long_names_and_count
    • resolves_line_bounds_long_absolute_path
    • meta_and_env_lines_are_bounded
  • New property: prop_prompt_lines_are_bounded_and_prefix_faithful, 300 iterations, both styles.
    • Mutation check: I removed the clip_styled call in write_steps and the property failed at once. I then restored it.
  • New pty test: huge_argument_prompt_is_bounded. It sends a 900 KB argument and checks that every line is ≤ 1152 visible chars, the block is under 16 KiB, the question is the last line, the status is Denied, and the shim log is empty.
  • Goldens: I ran UPDATE_GOLDEN=1 cargo test --test pty_ui_golden only to create long-argument.{plain,color}.txt. The 10 existing golden files are byte-identical: git status showed no change to them after the regeneration.
  • cargo fmt --check: it fails on origin/main already, across most files (the repo is not rustfmt-formatted, and CI does not run it). I did not reformat anything.
  • Kani: not run. src/proofs.rs is not touched, and none of the functions it proves changed (it proves no tui function). cargo-kani 0.67.0 is installed.

Deviations from plan

  1. Test 4(a), the id row width. The plan asked that the short id row stay under 80 chars. That cannot hold together with the plan's own step 3: the pad width is the maximum of the clipped names, so id is padded to about 185 chars (160 + marker). The test asserts what the plan intends instead: the padding follows the clipped name, not the 2 009-char one. The row is ≤ MAX_DISPLAY_LINE_CHARS + MARKER_MAX + 64.
  2. STEPS_BOUND indent term (test-only constant). The plan gave the indent as 2·(MAX_PLAN_DEPTH+1). A cd/export scope (( cd D && ( body ) )) adds two parenthesis levels per plan level, so the bound used is 2·(2·MAX_PLAN_DEPTH+3). It is still a constant.
  3. Property generator. The plan asked for a leaf generator variant. Instead, a lengthen pass runs over the plans that random_any produces. It makes an argv element or a cd/redirect/stdin path 0..3 000 chars long (1 in 4), and sets a resolvable long argv[0] "/" + "./"×k + "bin/echo" (1 in 8, k < 1 500). This leaves random_any, and so the random sequences of the two existing properties, unchanged.
  4. Test 6 inputs. Besides reason, session and requester, the test also makes host and version 10 000 chars long, so that every clipped metadata field is exercised.

Nits

Plan review (#85 (comment)):

  • P4: applied as guidance. backlog.md gets a separate Done: #85. line after the Phase 0 paragraph instead of an edit to the shared prose line.
  • P5: applied as guidance. Env: uses clip(&joined, MAX_DISPLAY_CMD_CHARS), so it no longer says "full command not shown".
  • P6: dropped: already filed as Prompt: '⚠ invalid plan' line embeds an unbounded request-derived error #90.
  • P7: applied as guidance. The clip_styled doc comment says that the hidden count is in visible chars (escapes excluded), and that the clipped tail of a Steps: line may hold the chain operator, a group's ( or ), or a redirection (the last two added for C2).
  • P8: applied as guidance. Test 3 strips escapes (the dim gutter and the root colour) and the 0 gutter before it checks sudo echo AAAA, and it runs under both styles.
  • P9: applied (cosmetic, but the line was being written). The Resolves: cap marker is … N more binaries hidden, without the "see Steps:/Command:" hint.

Code review:

  • C4: applied in chore: review nits. The long_echo_path doc gives the length as 2 * reps + 9 bytes (2 009 for reps = 1_000); the helper is new in this PR.

Follow-ups

Review

Manual test before merge

Run this with your own live daemon, built from this branch:

  1. In a terminal, build and start the branch daemon in the foreground, as in docs/testing.md: cargo build --release, then run target/release/sudo-proxy (the same flags you normally use).
  2. From another terminal, send a privileged request (the default) with a 200 KB argument. The plan is read from stdin, because one 200 KB argv element is over the kernel's 128 KiB per-argument limit:
    { printf '{"op":"exec","argv":["echo","'; head -c 200000 /dev/zero | tr '\0' A; printf '"]}'; } | target/release/sudo-request --plan-file -
    
  3. On the daemon terminal, check that:
    • Command: ends with … [198986 more chars hidden — full command not shown].
    • Steps: line 0 reads 0 sudo echo AAAA… and ends with … [199850 more chars hidden] in bold, with the light-red colour reset before the marker (no colour bleed).
    • The whole prompt fits in a few screen rows, and Execute as root? [y/N] is visible with the Command: line still on screen.
  4. Answer n. The client reports denied.
  5. Optionally, run it once more with NO_COLOR=1 set in the daemon's environment. The same markers should appear, with no escape codes.

🤖 Generated with Claude Code

@cuihtlauac cuihtlauac changed the title Bound each Steps: line (and every other prompt line) like Command: Approval prompt: bound Steps: lines (and every prompt line) so a long argument cannot push [y/N] off-screen Nov 20, 2026

@cuihtlauac cuihtlauac left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🤖 Code review (Fable) — round 1

Verdict: approve-with-nits

Reviewed the whole diff of PR #93 against the 🤖 Approved plan (Claude) — final comment on #85. Independently re-ran on the branch: cargo clippy --all-targets --all-features -- -D warnings (clean), cargo test --lib tui:: (34 passed, the 6 new unit tests and the new property included), cargo test --test pty_ui_golden (4 passed). The diff adds exactly two golden files (long-argument.{plain,color}.txt); the 10 existing goldens are untouched. The cargo fmt --check statement is accurate: the branch shows rustfmt diffs across tests/common/pty.rs, src/protocol.rs, src/server.rs, src/executor.rs, src/mcp.rs and others, so the repo is not rustfmt-formatted and nothing was reformatted here. Kani is not needed (src/proofs.rs untouched, no proved function changed); the PR says so explicitly.

The implementation follows the plan point by point: split_at_chars is the single truncation rule behind truncate_for_display and clip; clip_styled copies CSI escapes at zero width, writes reset before the marker, is byte-identical when the line fits, and the hidden count is in visible chars (P7); Steps: lines are clipped text+note with the gutter and indent outside the budget; the Resolves: block clips the name before the dedup key and the pad width, caps at 32 with the … N more binaries hidden marker (P9), and the single-line form branches on the unclipped strings; the metadata lines moved into writer-generic write_request_meta/write_env_line with the same bytes for short values (pinned by the literal expectation in test 6); Env: uses clip(…, MAX_DISPLAY_CMD_CHARS) (P5). steps_shell_text and command_summary are untouched, and the two bash-oracle properties still pass.

Deviations from plan — all accepted

  1. Test 4(a), the id row width. Correct reading of the plan: step 3 pads to the widest clipped name (160 + marker), so "under 80 chars" was never achievable. The replacement assertion (id row ≤ MAX_DISPLAY_LINE_CHARS + MARKER_MAX + 64) still proves the plan's intent, that the 2 009-char name does not set the width.
  2. STEPS_BOUND indent term. Verified against lay_scope (src/tui.rs:263-275): a cd/export scope is Group { Chain [ head, Group(body) ] }, so each plan level adds up to two nesting levels. 2·(2·MAX_PLAN_DEPTH+3) is a sound constant; the plan's 2·(MAX_PLAN_DEPTH+1) was not.
  3. lengthen pass instead of a generator variant. Good choice: random_any is unchanged, so the random sequences of the two existing properties are unchanged, and the pass still produces the three-string Resolves: rows the plan wanted (1 in 8 resolvable long argv[0]).
  4. Test 6 also lengthens host and version. Strictly more coverage, no cost.

Findings

Should-fix

  • C2 — src/tui.rs:488 and docs/assurance-case.md:116. The P7 note on clip_styled says the clipped tail of a Steps: line "may hold the chain operator or a group's ( opener". It may also hold a redirection suffix (> path, 2>&1, < path) and the group closer ) with its redirection: steps_emit joins redir into the leaf text (src/tui.rs:927) and appends close = join_suffix(")", redir) to the group's last line (:962) before write_steps clips the text (:900). Add redirections to that doc comment and one sentence to the Sn4.3 residual ("a long argument can push the line's redirection, operator or parenthesis into the hidden tail; Command: still carries them under 1024 chars"), with a pointer to the follow-up issue from C1. Doc-only; no code change.

  • C3 — PR body, "Follow-ups". The plan's "Out of scope (follow-ups to file, not done here)" names three items to file: src/gui.rs format_prompt_text unbounded and plan-blind; a total prompt-height budget plus column-width (wide glyphs, tabs) counting; display_banner printing command_summary untruncated. The PR lists them as "not filed by this PR". Per the Phase 0 rules, the author opens the issues (Part of #89, linking the plan comment) and lists the numbers in the PR. One combined "prompt bounds, remaining paths" issue is fine if that reads better; C1 below can go in with it or on its own.

Follow-up

  • C1 — src/tui.rs:900, :927, :962. A redirection can disappear into a Steps: line's hidden tail. Scenario: a privileged {"op":"redirect","stream":"stdout","target":{"file":"/etc/sudoers"},"body":{"op":"exec","argv":["echo","A"×1100]}}. Steps: line 0 is sudo echo AAAA…… [N more chars hidden] with > /etc/sudoers in the cut tail; Command: is also cut at 1024 chars before the redirection; Resolves: shows only echo. No prompt line names the target file, although both markers say text is hidden. This is the F3 trade-off the plan accepts (operators deny anything with a marker), and Command: already behaved this way, so it is not a regression against the plan's intent. A better clip policy would keep the structural suffix (operator, (/), redirections) out of the budget and clip only the command text, or clip each argv word separately; that changes the StepLine shape and the prefix-fidelity property, and belongs with the Phase 1 render-from-Compiled work in #89. File it as its own issue or fold it into the C3 batch.

Nit

  • C4 — src/tui.rs:2325. long_echo_path(reps) is documented as "2 009 bytes long", which holds only for reps = 1_000; the property calls it with k < 1_500. Say "/ + ./×reps + bin/echo; 2 009 bytes at 1 000, under PATH_MAX" when next touching the line.

Nits from the plan review

P4, P5, P7, P8 and P9 were followed as described in the PR; P6 is correctly left to #90. No disagreement.

Manual test

The PR's "Manual test before merge" matches the plan's. One addition worth doing while at it: after step 3, press nothing and let the 60 s timeout expire once, to confirm the clipped prompt does not change the timeout/deny path (→ Timeout, client reports the timeout).

cuihtlauac and others added 6 commits November 21, 2026 22:25
Only the Command: line was length-bounded (G4.3 / F3). A privileged
exec of a 900 KB argument printed it whole on Steps: line 0 and pushed
the [y/N] question off-screen; Resolves: names and paths and the
metadata fields were unbounded too.

Clip each Steps: line to 160 visible chars (clip_styled skips the
daemon's own colour escapes and resets before the marker), each
Resolves: name and path to 160, each metadata field to 256, the Env:
line to 1024, and the Resolves: block to 32 entries, each with an
explicit hidden-chars marker. truncate_for_display keeps its output and
shares split_at_chars with clip. The metadata and Env: lines move into
writer-generic helpers so they are unit-testable; prompt_tty writes the
same bytes for short values. steps_shell_text and command_summary are
unchanged.

Tests: clip unit tests, Steps:/Resolves:/metadata bounds, and the
property prop_prompt_lines_are_bounded_and_prefix_faithful.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
Add the long-argument UI plan (a 600-char argument, privileged) with its
two goldens: Command: shows it whole, Steps: line 0 is clipped with the
marker. The ten existing goldens are byte-identical.

huge_argument_prompt_is_bounded sends a 900 KB argument through a real
daemon on a pty and checks that every prompt line is bounded, the block
is under 16 KiB, the question is the last line, and a denial runs
nothing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
The clip_styled doc comment and the Sn4.3 residual now name redirections
and the ) closer alongside the chain operator and ( opener, and point at
the follow-up issues #97 (structure outside the budget) and #98 (height
and column width).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
C4: long_echo_path doc gives the length as a function of reps.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
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.

Approval prompt: Steps: lines are not length-bounded, so a long argument can push [y/N] off-screen

1 participant