Skip to content

Escape bidi, C1 and DEL characters in JSON and SARIF output - #150

Open
ammartahamohamedy wants to merge 2 commits into
prooflinedev:mainfrom
ammar-taha-dev:fix/json-output-terminal-controls
Open

ammartahamohamedy wants to merge 2 commits into
prooflinedev:mainfrom
ammar-taha-dev:fix/json-output-terminal-controls

Conversation

@ammartahamohamedy

Copy link
Copy Markdown

Fixes #149

Problem

serde_json escapes only U+0000–U+001F, so JSON and SARIF output wrote untrusted server text (PR titles, author names, finding text, environment names) with bidi controls, C1 controls and DEL raw to the terminal. The text renderer and export already keep them out.

Fix

  • text::json_safe writes each character terminal_safe replaces, other than JSON whitespace (space, tab, newline, carriage return), as a JSON \u escape. Outside its strings a serialized document holds only ASCII, so these characters are always inside a string, where the escape decodes to the same character. The output stays valid JSON with the same values.
  • tools::pretty and tools::compact wrap serde_json with it, and every JSON print site uses them: tools::json (reviews, history, run, routing, guidance, fix-prompt, config, plan, scan --json), review/findings/ci review --json, --sarif on stdout and the ci review --sarif file, review --no-wait --json, review --schema, stop --json, ci deploy --json and coverage upload --json.
  • It's lossless, so it applies whether or not stdout is a terminal. Scripts reading the JSON get the same values.
  • Unchanged: the MCP bridge (frames for an agent, not a terminal), agent install's config files, the credential store and the update cache.

Tests (written first; the new tests failed before the change)

Test What it checks
raw_json_commands_escape_terminal_controls_and_keep_the_values (new, tests/http.rs) reviews, routing and fix-prompt print no raw bidi, C1 or DEL, and their stdout parses to the served document
ci_review_sarif_file_escapes_terminal_controls_and_keeps_the_values (new, tests/http.rs) The file ci review --sarif writes holds none raw and keeps the environment name. identity_console now delegates to named_identity_console, which takes the environment name
json_and_sarif_output_escape_terminal_controls_and_keep_the_values (new, tests/sarif.rs) findings --json and --sarif escape every kind (U+202E, U+2066, U+2069, U+061C, U+200F, U+009B, U+007F), the JSON parses to the server's review, and the SARIF still validates against the schema with the same environment name
json_strings_escape_every_terminal_control_and_keep_their_value, json_without_terminal_controls_is_unchanged, json_whitespace_between_tokens_stays_raw (new, src/text.rs unit) Compact and pretty output round-trip, a backslash before a control stays escaped, the pretty layout keeps its lines, JSON with nothing to escape is unchanged, and tabs and carriage returns between tokens stay raw

Mutation checks: writing the SARIF file with serde_json again fails only the ci review --sarif test; dropping the bidi check from json_safe fails the three integration tests and the round-trip unit test; escaping tabs and carriage returns again fails the whitespace unit test.

End to end, offline, with the built binary: the issue's repro lists U+202E U+202C U+009B U+009B U+007F on main and none with this change.

Checks

  • cargo fmt --check: ✅
  • cargo clippy --locked -- -D warnings: clean on the lib, the bins and every integration test. --all-targets stops at the 2 errors main already has on Windows (src/agent/tests.rs).
  • cargo test --locked: the new tests pass. Otherwise, the same 12 Windows failures as on main (tests/mcp.rs ×7, agent_install ×2, update, local_patch, hooks), none in a file this change touches.
  • cargo deny check: no dependency changes.
  • Fork CI: check and windows green on ammar-taha-dev/proofline-cli#5 (commit fa2af96).

Proofline review log

Fork review PR: ammar-taha-dev/proofline-cli#5. Rounds capped at 2.

Round Finding What I made of it Outcome
1 Warning, edge case: json_safe rewrites carriage returns and tabs as \u escapes even when they are legal whitespace between JSON tokens. Recommended: turn CR into spaces, keep tabs Right about the function, not about the impact today: callers pass only serde_json output, which never holds a raw tab or CR. But json_safe takes any &str, so it should leave JSON whitespace alone Fixed differently from the recommendation: space, tab, newline and carriage return all stay raw, and the test covers it (written first, failed before the change). json_safe only escapes, so it shouldn't reformat. Valid JSON can hold a raw tab or CR only between tokens, so leaving them is lossless, while turning CR into a space would change the caller's bytes for nothing. The doc comment is shorter and says so
2 None ("Proofline review finished: no new findings") Nothing to act on No changes; review closed at round 2

The prompts below are the ones that drove this implementation; the investigation, reproduction and fix scope were done before them and aren't included.

This branch has not been deployed

No deployments
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.

JSON and SARIF output print bidi, C1 and DEL characters from server text raw

1 participant