Skip to content

protocol: widen the forbidden prompt-character class to Cc/Cf/Zl/Zp/DICP (#86) - #96

Open
cuihtlauac wants to merge 6 commits into
mainfrom
fix/86-dangerous-chars
Open

cuihtlauac wants to merge 6 commits into
mainfrom
fix/86-dangerous-chars

Conversation

@cuihtlauac

@cuihtlauac cuihtlauac commented Nov 20, 2026 •

Copy link
Copy Markdown
Member

Fixes #86
Part of #89

Summary

Implements the approved plan for #86 ("🤖 Approved plan (Claude) — final").

  • One principled class. dangerous(c) ⇔ c ≠ TAB ∧ c ∈ Cc ∪ Cf ∪ Zl ∪ Zp ∪ Default_Ignorable_Code_Point, held as protocol::DANGEROUS_RANGES (27 sorted, disjoint ranges built from the Unicode 16.0.0 UCD) and looked up by is_dangerous_char (binary search). has_dangerous_chars is now s.chars().any(is_dangerous_char), so every validated field and plan leaf gets the wider class with no other edit. The audit safe() escaper and the --log-json escape_for_terminal reuse the same predicate.
  • Resolves paths. canonicalize output (and, as defence in depth, the resolved path and the zenity shown path) is printed through the new escape_dangerous. A same-UID caller could otherwise put ESC/newline on the prompt through an absolute symlink's target name, which never goes through validation. Comparisons keep the raw strings, so benign paths render byte-identically.
  • Env-key error no longer echoes the raw key (consistency only, see P4).
  • MCP [P3]. sanitize_description strips the class from description before it becomes reason, so "Restart nginx ⚠️" (U+FE0F) keeps working. The daemon gate stays strict. The execute tool description says so.
  • Kani. Both harnesses now run over the whole char domain, with a three-part spec: (a) string fold == char class, (b) independently written must-reject list (incl. U+2065), (c) must-accept TAB and printable ASCII. Exact membership is discharged by the exhaustive oracle test.
  • Docs. Assurance case G3.3/Sn3.3/G4.1/Sn4.1 (statuses stay [discharged]), threat model (control row + 3.4 residual), docs/security.md, REVIEWING.md C6, docs/usage.md audit escapes, README note on MCP description, docs/security-audit.md F1 line, changelog entry, and the has_dangerous_chars misses DEL/C1, soft hyphen, U+061C, U+2028/2029, U+2060-2064 and U+FEFF #86 clause struck from the Phase 0 backlog line. All line-local / append-only.

Newly rejected at the daemon (each gets the existing "contains forbidden control/bidi characters" error; the fix is to drop the character): soft hyphen, U+FE0F emoji presentation in reason from non-MCP clients, Arabic/Syriac text with U+061C, U+0600–0605 or U+070F, a leading BOM, tag characters.

Test evidence

Command Result
cargo test (debug, full suite) all pass: lib 117, validation 24, audit 12, pty_ui_golden 3, … 0 failures
cargo test --test pty_ui_golden without UPDATE_GOLDEN pass; no golden changed
cargo clippy --all-targets --all-features -- -D warnings clean
cargo clippy --all-targets --no-default-features -- -D warnings clean
cargo deny check advisories ok, bans ok, licenses ok, sources ok
cargo kani (cargo-kani 0.67.0, local) 4/4 harnesses verified: has_dangerous_chars_matches_spec_per_char (≈6 s), plan_run_leaf_matches_dangerous_char_spec (≈25 s), plus the two unchanged ymd_hms_to_epoch harnesses
cargo fmt --check fails, but on main too (the tree is not rustfmt-clean and CI does not run fmt). Per-file rustfmt --check hunk counts on the touched files are equal or lower than on main, so this PR adds no new fmt diffs

New tests:

  • protocol::tests::dangerous_class_matches_unicode_oracle: all 1,114,112 chars vs unicode-general-category (=1.1.0, Unicode 16.0.0, asserted via UNICODE_VERSION) plus DICP_16_0, copied verbatim (unmerged, incl. Cn U+2065, U+FFF0–FFF8, U+E0000, U+E0002–E001F, U+E0080–E00FF, U+E01F0–E0FFF) from DerivedCoreProperties-16.0.0.txt.
  • dangerous_ranges_sorted_and_disjoint, dangerous_char_regression_table, escape_dangerous_escapes_class_keeps_rest.
  • tests/validation.rs: newly_forbidden_chars_rejected (9 chars × argv/reason/session/env value/plan Exec/plan Chdir, also asserts the prompter is never called), legitimate_non_ascii_accepted, and dangerous_env_key_rejected tightened (no raw key in the message).
  • tests/audit.rs: widened_class_is_rejected_and_escaped_in_record (U+2028 and U+00AD are recorded as \u{2028} / \u{ad}).
  • mcp::tests::description_is_sanitised_of_forbidden_chars.
  • tui::tests::resolves_escapes_dangerous_symlink_target (exercises both resolve_detail and write_resolves_line).
  • server.rs property test: DANGEROUS extended, env keys from a valid-by-construction alphabet, benign alphabet adds é 漢 →, and a new accepted >= 2000 of 5000 floor (observed: 2575 accepted / 2425 rejected).

Deviations from plan

  1. Test file name. In the Resolves test, the file name is "evil\x1b[2J\nExecute? [y|N]" instead of "…[y/N]". A file name cannot contain /. Nothing else changes.
  2. is_dangerous_char visibility. It is pub(crate), not pub. src/mcp.rs is a module of the library crate, so pub(crate) is enough, which is what the plan's "the visibility the crate layout needs" asks for.
  3. DANGEROUS_RANGES keeps TAB. The table holds U+0000–001F as one Cc range, and is_dangerous_char exempts TAB, matching the rule c ≠ TAB ∧ …. The plan text allowed either form.
  4. MCP filter sites. src/mcp.rs:73/:88 only pass the already-built reason on, so the filter is applied once, where description becomes reason.
  5. MCP tool docs. The execute tool is documented in README.md ("Run commands"), not in docs/usage.md, so the one-line note went there.
  6. Unicode version. The plan's Cf list matches Unicode 16.0.0, the version the pinned oracle implements. The table is 27 ranges, not "about 35", after merging adjacent ranges (e.g. U+2028–202E, U+2060–206F, U+FFF0–FFFB, U+E0000–E0FFF).

Nits

Plan review:

  • P4: applied as guidance. The changelog and the code comment describe dropping the key echo as a consistency change, not a security fix.
  • P5: applied as guidance. The resolve_detail/write_resolves_line doc comments and the gui comment say that only the canonical path is caller-influenced and that wrapping the others is defence in depth.
  • P6: applied as guidance. The pinned message "contains forbidden control/bidi characters" is unchanged.
  • P7: applied as guidance. The lookup is a partition_point binary search, and the choice and the resulting unwind(8) are explained in the is_dangerous_char and harness comments.
  • P8: applied as guidance. unicode-general-category = "=1.1.0" (Apache-2.0) is a dev-dependency, Unicode 16.0.0 is cited, and the test asserts UNICODE_VERSION == (16, 0, 0). cargo deny check passes.
  • P9: applied as guidance. The Resolves test uses style_for(true) and an absolute symlink target, and asserts that the -> arm is taken.
  • P10: applied. The residual is appended to the existing 3.4 (F1 residual) bullet in docs/threat-model.md, so it stays one bullet.

Code review:

  • C1: applied. PR title now describes the change instead of the bug report.
  • C2: applied in "chore: review nits" (changelog.md rule written with ≠ ∧ ∈ ∪, as in the DANGEROUS_RANGES doc comment).
  • C3: dropped: pre-existing property shared with the audit safe() and --log-json escapers; no security effect (a dangerous character is always escaped, so a name can only look scarier, never tamer), and the reviewer expected a drop.
  • C4: dropped: the wording and anchor of the 3.4 residual were dictated by the plan (P10); the reviewer asked to drop it.

Follow-ups

Review

Manual check before merging

Run the branch daemon in a terminal and send, with sudo-request --plan or a direct socket request:

  • a reason containing U+00AD or U+2028, which must be rejected with the validation error;
  • an echo 'café 漢字 — →', which must prompt normally;
  • an absolute symlink whose target name contains ESC and a newline, used as argv[0], which must show the target on the Resolves: line with \u{1b}/\u{a} escapes and no redraw.

From the MCP server, execute with description "Restart nginx ⚠️" must prompt with Reason: Restart nginx ⚠.

🤖 Generated with Claude Code

@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 #96 (5 commits, fix/86-dangerous-chars) against the issue's "🤖 Approved plan (Claude) — final" comment. No blocker, no should-fix. The plan is implemented item by item, every deviation is justified, and the deferred nits P4–P10 are handled as the batch rules require (P4–P9 as implementation guidance, P10 applied).

What I verified independently

  • cargo test (debug, full suite, run twice): all green, including dangerous_class_matches_unicode_oracle, the property test and the pty goldens (no golden changed; working tree clean afterwards).
  • cargo clippy --all-targets -- -D warnings with --all-features and with --no-default-features: clean.
  • cargo deny check: advisories/bans/licenses/sources ok with the new pinned dev-dependency.
  • cargo kani (cargo-kani 0.67.0, run locally): 4/4 harnesses verified; has_dangerous_chars_matches_spec_per_char 6.7 s, plan_run_leaf_matches_dangerous_char_spec 35 s, both over the whole char domain with unwind(8).
  • DICP_16_0 (src/protocol.rs tests) compared entry by entry against DerivedCoreProperties-16.0.0.txt fetched from unicode.org: identical, unmerged, 27 lines including the Cn ones (U+2065, U+FFF0–FFF8, U+E0000, U+E0002–E001F, U+E0080–E00FF, U+E01F0–E0FFF). So the oracle test is not derived from the table, as [P1] demanded.
  • DANGEROUS_RANGES by inspection: the 21 Cf ranges of Unicode 16.0.0, Cc, Zl/Zp and every DICP range are present, adjacent ranges coalesced (U+2028–202E, U+2060–206F, U+FFF0–FFFB, U+E0000–E0FFF); sorted and disjoint. The exhaustive test is the real check and it passes.
  • Formatting: the tree is not rustfmt-clean on main and CI does not run cargo fmt. Per-file hunk counts, branch vs main: src/protocol.rs 36/36, src/tui.rs 89/89, src/server.rs 19/21, src/mcp.rs 17/17, src/gui.rs 6/6, src/audit.rs 1/1, src/proofs.rs 0/1, tests/audit.rs 0/0, tests/validation.rs 9/9 (all pre-existing lines). The implementer's claim holds: no new fmt diff.
  • resolve_command (src/executor.rs) does not canonicalize, so the P5 statement in the resolve_detail / write_resolves_line / gui.rs comments (only the canonical path is caller-influenced, the rest is defence in depth) is accurate.
  • shquote (src/tui.rs) accepts only ASCII alphanumerics and _./:=@%+,- unquoted, so the new threat-model residual's claim that non-ASCII tokens are single-quoted is correct.
  • sanitize_description is applied at the single site where description becomes reason; the other description uses in src/mcp.rs (lines 418, 527) are the host description of update_host, unrelated. Deviation 4 is right.

Deviations from plan (all accepted)

  1. [y|N] file name: a file name cannot contain /; the test still exercises ESC and newline, which is the point.
  2. pub(crate) for is_dangerous_char: src/mcp.rs is a module of the library crate (src/lib.rs:18), so this is exactly "the visibility the crate layout needs".
  3. TAB kept in the table and exempted in is_dangerous_char: matches the rule as written; the oracle test models the same exemption.
  4. One filter site: verified above.
  5. README instead of docs/usage.md: the execute tool is documented in README "Run commands"; the sentence is the plan's.
  6. 27 ranges instead of "about 35": the plan's count was an estimate and the coalescing is what it asked for.
  7. #95 filed for the zenity Pango-markup note: the plan's "Out of scope" section asked for exactly that.

Findings

Blockers

None.

Should-fix

None.

Follow-up

None.

Nits

  • C1 (nit) — PR title. The title is the issue's title ("has_dangerous_chars misses DEL/C1, …"), which reads as a bug report rather than a change once it lands as the merge commit subject. Suggest something like "protocol: widen the forbidden prompt-character class to Cc/Cf/Zl/Zp/DICP (#86)". Cosmetic; only if convenient.
  • C2 (nit) — changelog.md:12: Cc u Cf u Zl u Zp uses an ASCII u for the set union while the rest of the file already uses non-ASCII punctuation (—, →); ∪ as in the plan and in the DANGEROUS_RANGES doc comment would read better. Drop if not editing the line.
  • C3 (nit, pre-existing, no action expected) — src/protocol.rs:713 escape_dangerous: a backslash is not escaped, so a symlink target literally named x\u{1b}y prints the same as one containing a raw ESC. No security effect (a dangerous character is always escaped, so a name can look scarier than it is, never tamer), and the audit safe() and --log-json escapers have had the same property since before this PR. Recording it so it is on the record; "dropped" is the expected outcome.
  • C4 (nit) — docs/threat-model.md:174: the new sentence under 3.4 ("look-alike reason") argues from shquote, which applies to argv tokens, not to reason (printed as is). The plan dictated both the wording and the anchor (P10), so this is on the plan, not the implementer; a future touch of that bullet could say "in argv, shquote …". Drop.

Manual test before merging

The PR's "Manual check before merging" list is the right one (reject U+00AD/U+2028 in reason; echo 'café 漢字 — →' prompts normally; an absolute symlink with ESC + newline in its target name shows escaped on Resolves:; MCP execute with "Restart nginx ⚠️" prompts with Reason: Restart nginx ⚠). I have nothing to add to it.

@cuihtlauac cuihtlauac changed the title has_dangerous_chars misses DEL/C1, soft hyphen, U+061C, U+2028/2029, U+2060-2064 and U+FEFF protocol: widen the forbidden prompt-character class to Cc/Cf/Zl/Zp/DICP (#86) Nov 20, 2026
cuihtlauac and others added 6 commits November 21, 2026 22:25
…ICP (#86)

has_dangerous_chars only knew C0, three zero-width/bidi ranges, and
nothing else: DEL, C1 (NEL, CSI, OSC), soft hyphen, ALM, LS/PS, word
joiner and invisible operators, BOM, tag characters and variation
selectors all reached the approval prompt.

Replace the hand-listed ranges with one table-driven class,
  dangerous(c) <=> c != TAB && c in Cc u Cf u Zl u Zp u
                   Default_Ignorable_Code_Point,
built from the Unicode 16.0.0 UCD (DANGEROUS_RANGES, binary-searched by
is_dangerous_char), and reuse it in the audit-record and --log-json
escapers. An exhaustive unit test checks the table against a
General_Category oracle (unicode-general-category =1.1.0, a pinned
test-only dev-dependency) plus a verbatim copy of the DICP list.

The env-key error no longer echoes the raw key; this is for consistency
(the key only reaches the response, and the audit copy is escaped), not
a security fix. Validation messages are otherwise unchanged.

Tests: oracle and table-shape tests, a regression table, integration
tests over every newly covered character in argv/reason/session/env
value/plan Exec/plan Chdir, a non-ASCII accept test, an audit-record
escape test, and a property test whose env keys are now valid by
construction and whose alphabet includes multibyte text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
The Resolves: text printed canonicalize() output raw. A same-UID caller
can exec an absolute symlink whose target name holds ESC or a newline;
only argv[0] is validated, so that name reached the prompt verbatim.
Print every displayed path through escape_dangerous. Only the canonical
path is caller-influenced; escaping the resolved path (tui) and the
gui's shown path is defence in depth. Comparisons keep the raw strings,
so benign paths render byte-identically and the pty goldens are
unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
With the widened class, an agent's description such as "Restart nginx"
followed by a warning-sign emoji with U+FE0F would make the daemon
reject the request. sanitize_description removes every
is_dangerous_char character before the description becomes reason; the
daemon gate stays strict. The execute tool description says so.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
)

Drop the < 0x2100 bound (tag characters sit at U+E0000). The per-char
harness now proves (a) has_dangerous_chars(encode(c)) ==
is_dangerous_char(c) for every char, (b) an independently written list
of the code points #86 names is dangerous, (c) TAB and printable ASCII
are not. Exact membership is left to the exhaustive oracle unit test.
unwind 8 covers the 4-byte decode and the binary search.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
Assurance case G3.3/G4.1 and their solutions, threat model (control row
and the 3.4 residual), security.md, REVIEWING.md C6, the audit-escape
note in usage.md, the MCP description note in README, a line under
security-audit F1, a changelog entry, and the #86 clause struck from the
Phase 0 backlog line.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
C2: use set-theory symbols in the changelog rule, as in the
DANGEROUS_RANGES doc comment.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Cuihtlauac ALVARADO <cuihtlauac@tarides.com>
@cuihtlauac
cuihtlauac force-pushed the fix/86-dangerous-chars branch from 535cfa2 to 8c65706 Compare November 22, 2026 05:35
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.

has_dangerous_chars misses DEL/C1, soft hyphen, U+061C, U+2028/2029, U+2060-2064 and U+FEFF

1 participant