Repository navigation
fix: escape control characters in ledger strings on human-readable output - #2710
Merged
Merged
Conversation
…tput Ledger strings (narrations, payees, metadata, comments, document paths) reached the terminal raw in check diagnostics (message and miette snippet), query tables and report text. They are now escaped with rustledger_plugin::untrusted's rule (#2525), as are price fetch-failure lines. Data output keeps the value: CSV, PRINT, -f beancount and format write it as is; JSON escapes DEL, C1, bidi controls and line separators as \uXXXX, which decode to the same string. extract neutralizes control characters in imported text (tab/newline to a space, others to U+FFFD) with a warning naming the row and field, and `rledger lint control-chars` reports the ones already in a ledger (#2538). Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr
Contributor
📊 Benchmark Results
vs base, measured in this run
A difference counts only when it clears the combined spread of the two measurements, per shape (2σ: What do these numbers mean?
|
Contributor
✅ Compatibility Test Results
What's tested
Full results available in workflow artifacts |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
Member
Author
|
Deep-review rounds (lane V6)
Converged after round 2. Red non-required checks:
|
This was referenced Oct 20, 2026
…trol-chars # Conflicts: # CHANGELOG.md
…trol-chars # Conflicts: # CHANGELOG.md
…trol-chars # Conflicts: # CHANGELOG.md
This was referenced Oct 21, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What changed
This PR implements the decision on #2538, plus the
pricefetch-failure follow-up from its second comment.1. Human-readable output always escapes; data formats keep the value. I reused
rustledger_plugin::untrusted(#2525), so\u{1b}shows up visibly instead of acting on the terminal.checktext diagnostics:escape_untrusted_sourcekeeps\n,\tand\r(which miette reads as line terminators) and maps byte offsets, so label spans still cover the same characters.file:line:collines are escaped, and so are the load-error, option-warning and LINT-XFER lines.reporttext:sanitize_displayused to replace control characters with spaces. It now escapes them, sojournalnarration and payee,balanceslot labels, andbudget/returnslabels all get the same rule. Budget column widths now use the escaped width.Diagnostic::to_line) are escaped too.Messages::emit_asescapes the terminal line, which coversPRICE_FETCH_FAILED. The plain-format currency from a price source is escaped too. Recorded diagnostics (ag-rledger) keep the raw text.PRINT,query -f beancountandrledger format. They emit ledger text, and beancount strings have no escape syntax for these characters. A test pins thatPRINTandformatstill emit them byte for byte.returnsCSV no longer space-replaces its labels.\uXXXX. serde emitted these raw. The escaped form decodes to the same string, so the value is unchanged.check/query/lintJSON goes through a newcmd::to_json_pretty.reportJSON goes throughrustledger_core::format::escape_json.rustledger_core::format::is_display_unsafe, anduntrusted::is_unsafedelegates to it.2. Ingest:
extractneutralizes control characters in imported text: CSV cells, OFX fields, and the entries a WASM importer returns. The fields covered are payee, narration, string metadata (directive and posting), note comment and event value.Row 1: control characters in narration replaced .... OFX rows are named by their FITID; WASM importer rows readEntry N.3.
rledger lint control-chars(also inag-rledger lint) reports control characters already in a ledger's strings, with file:line and field. Tab, newline and the\rof a\r\nare not reported. It has text and JSON output. It is a lint, not acheckwarning, becausebean-checkaccepts these characters (same reasoning as #1446).Overlap note for the parallel BQL lane: the only query-writer change is in
crates/rustledger/src/cmd/query/output.rs(write_textescaping andwrite_jsonviato_json_pretty). Nothing inrustledger-querychanged.No CACHE_VERSION bump: parser output is unchanged.
Proposed, not implemented: a parser warning
Should the parser warn on control characters in strings? I propose no:
bean-checkaccepts them.\b/\fnow decode to real control characters on purpose (Parser:\band\fin strings are not decoded; beancount decodes them #2672).checkreports.If you do want one, it should be opt-in, either
check --lint control-chars(alongside--lint transfers) or an option.How tested
crates/rustledger/tests/control_chars_output_test.rs(7 tests, end to end through the binary):checktext: message and snippet are escaped.check -f json: the value is kept and is valid JSON with no raw C1.report journal: text is escaped, CSV is raw, JSON is valid and keeps the value.PRINTandformatemit the characters byte-identically.extractreplaces the characters and warns, once per row.lint control-charsfinds them.escape_untrusted_source: offset mapping, line terminators kept, escape lengths.escape_json: DEL/C1/bidi.to_json_pretty: round trip.neutralize_directive.cargo test -p rustledger-core -p rustledger-plugin -p rustledger-importer -p rustledger --all-features: 2515 passed. The one failure in the first run was in my own new test, which used the wrong flag; that test passes after the fix.cargo clippy --workspace --all-features --all-targets -D warnings-p rustledger --no-default-features --features mimalloc,ag-rledger)cargo doc -D warningsfor the 4 changed cratescargo fmtCloses #2538
🤖 Generated with Claude Code
https://claude.ai/code/session_012nNGPA44Dt32NyxWem26xr