Skip to content

fix: escape control bytes in client stderr diagnostics - #8892

Merged
oferchen merged 3 commits into
masterfrom
fix/escape-client-stderr-control-bytes
Nov 27, 2026
Merged

oferchen merged 3 commits into
masterfrom
fix/escape-client-stderr-control-bytes

Conversation

@oferchen

@oferchen oferchen commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

What rsync 3.5.2 does

rwrite() hands every FERROR/FWARNING/FINFO line to filtered_fwrite() before it reaches the terminal:

  • A client without -8 opens ic_chck = iconv_open(cs, cs) (rsync.c:96-98). rwrite() then validates the line in the locale codeset (log.c:463-496). Each rejected byte is written as \#ooo (log.c:487), and the valid characters go through filtered_fwrite(f, convbuf, len, 0, ..) (log.c:476).
  • A -8 client or a server takes filtered_fwrite(f, buf, len, !allow_8bit_chars, ..) (log.c:499).
  • filtered_char_len() (log.c:276-293) escapes C0 except TAB, DEL, 0x80-0x9F, and the backslash of a literal \#ddd (log.c:288-291). With use_isprint it also escapes every byte isprint() rejects.
  • rwrite() peels one trailing newline and writes it raw (log.c:460).
  • A refused filter rule is echoed through rprintf(FERROR, ...): filter_rule_err() at exclude.c:133-136 and the invalid modifier line at exclude.c:1376. So a VT in the rule prints as \#013, both in the '%c' detail and in the echoed rule.

What oc did

oc's daemon log file already escaped its lines (LogFileWriter, mirroring logit() at log.c:143), and so did lines forwarded from a server in MSG frames. Lines the client printed itself were not escaped:

  • A refused --filter rule (write_refusal).
  • Every Message written through the client stderr MessageSink.
  • The pre-formatted cause lines of a ClientError report.
  • The local-copy link_stat ... failed / file has vanished operand lines, which the engine printed with eprintln!.

The fix

It reuses the existing escaper in logging::escape. There is no second escaper.

  • logging::escape:
    • may_need_escaping() is the fast-path test escape_for_output already ran, now split out so sinks can skip the copy.
    • write_terminal_line() / eprint_terminal_line() write one escaped stderr line. A clean line goes out with no copy.
  • MessageSink:
    • A terminal_escaping flag, set on the client stderr sink. A message whose text is clean keeps the vectored, allocation-free render. Only a text with something to escape is rendered into a buffer and escaped once.
    • write_escaped_line() for pre-formatted lines.
    • The style is read at write time, because -8 and the locale are parsed after the sink is built.
  • write_refusal takes an EscapeStyle:
    • Client: the sink's style.
    • Server argv: terminal_style(), as log.c:499 applies on a server before multiplexing.
    • Daemon: passthrough(). This is deliberate. The daemon's refusal lines also go to its log sink, which escapes on its own as upstream's logit() does (log.c:143). Escaping here as well would print \#134#013, so each line is still escaped exactly once.
  • emit_error_report sends its cause and program lines through write_escaped_line. The remote shell's own stderr is still written as received.

Measured on Linux, stderr cause line (od -c) and exit code

cell rsync 3.5.2 oc master oc with this change
--filter=$'+\vfoo' invalid modifier '\#013' at position 1 in filter rule: +\#013foo, exit 1 raw \v twice, exit 1 invalid modifier '\#013' at position 1 in filter rule: +\#013foo, exit 1
--filter=$'foo\vbar' Unknown filter rule: foo\#013bar, exit 1 raw \v, exit 1 Unknown filter rule: foo\#013bar, exit 1
missing operand no\001pe link_stat ".../no\#001pe" failed, exit 23 raw \001 on both lines, exit 23 link_stat ".../no\#001pe" failed on both lines, exit 23
same, forwarded from a remote server \#001, exit 23 \#001, exit 23 \#001, exit 23 (unchanged)
missing operand no\tpe raw TAB, exit 23 raw TAB, exit 23 raw TAB, exit 23
operand café\001, LC_ALL=C caf\#303\#251\#001 raw caf\#303\#251\#001
operand café\001, -8 or C.UTF-8 caf + raw é + \#001 raw caf + raw é + \#001

Tests

  • crates/cli/tests/client_stderr_control_byte_escaping.rs:
    • Three oracle cells against rsync 3.5.2: VT in a refused modifier, the same under -8, and a control byte in an unknown rule. Each compares the cause line byte for byte, plus the exit code.
    • One cell for a control byte in a missing operand. It checks the escaping and exit 23, not the whole line, because the surrounding wording still differs (see below).
  • MessageSink unit tests, one per byte class:
    • C0 escaped and TAB raw.
    • DEL and high/C1 bytes.
    • The literal \#ddd guard.
    • An embedded newline staying on one line.
    • A raw trailing newline.
    • write_escaped_line.
  • Unit tests for write_refusal (VT) and write_terminal_line.

Mutation: with the escaping switched off at each new site (the client sink flag, the write_refusal style, MessageSink::escape_style, write_terminal_line), all 12 new tests fail: 4/4 cli, 6/6 logging-sink, 1/1 core, 1/1 logging.

Multi-line causes

Escaping showed that oc folded some multi-line diagnostics into one line. Each such cause is now written one line per upstream rprintf call:

  • Unexpected local arg: %s and If arg is a remote file/dir, prefix it with a colon (:). are two rprintf(FERROR, ...) calls in 3.5.2 (main.c:1591-1592). oc built one cause with an embedded newline, so escaping printed \#012 between the two parts. A ClientError cause now holds one line per upstream call (cause_lines()), and the report writes each line through the escaper. A newline inside the operand itself is still escaped as \#012, as log.c:288-291 does.

  • clap's parse error spans several lines. oc writes it line by line, so its own line breaks stay raw and only bytes inside each line are escaped. Upstream prints its option error as one popt line through option_error() (options.c:2087-2090, options.c:933), then the trailer (main.c:1916-1917). The wording differs, and this PR does not change it:

    • 3.5.2: rsync: --version=extra: option does not take an argument, then rsync error: syntax or usage error (code 1) at main.c(1917) [client=3.5.2]
    • oc: rsync error: syntax or usage error: unexpected value 'extra' for '--version...' found; no more were expected, a blank line, Usage: rsync --version... [args]..., then (code 1) [client=...]
  • The unknown-option refusal and the failed --log-file open line were raw writes. They now go through the escaper too. Measured: 3.5.2 prints rsync: --bo\#001gus: unknown option, and oc now prints the same line.

  • A daemon that sends free text and then closes before acknowledging a module listing used to be reported as one message, with the received lines joined by newlines. Escaping would have printed \#012 between them. Upstream prints each received line with its own rprintf (clientserver.c:428-429 for @ERROR, 438-439 for MOTD and listing lines). Each received line is now its own escaped stderr line, followed by the existing daemon refused module listing trailer. The trailer no longer repeats the lines. The connect-failure lines on that path also go through the escaper now.

New tests in crates/cli/src/frontend/tests/stderr_lines.rs. Each one fails with the split reverted:

  • the local-after-remote refusal prints 3 lines, and a newline in the operand prints as \#012
  • a parse error keeps its lines
  • an unknown option is escaped
  • a daemon refusal prints one stderr line per received line, with a control byte escaped (plus a core module_list test that the refusal keeps one cause line per received line)

Gates (Linux host, on master 0c57d58)

gate exit
cargo fmt --all -- --check 0
python3 tools/ci/check_rustfmt_all.py 0
clippy on logging, logging-sink, core, cli, daemon, engine (--all-targets --all-features -D warnings) 0
cargo check --workspace --all-features --all-targets --target x86_64-pc-windows-gnu 0
cargo clippy --workspace --all-features --all-targets --target x86_64-pc-windows-gnu --no-deps -- -D warnings 0
cargo run --locked -p xtask -- no-placeholders 0
tools/ci/citation_drift_audit.py --ratchet 0
new oracle file vs rsync 3.5.2 (OC_RSYNC_UPSTREAM_COMPAT=1) 0 (4/4)
cli --lib (all frontend unit tests) 0 (3142/3142)
cli stderr_lines + clap under LC_ALL=C and LC_ALL=en_US.UTF-8 0 (7/7 each)
cli summary/filters/argv_filters/refusal/filter_rule units 0 (67/67)
cli module (module listing) 0 (20/20)
core module_list 0 (269/269)
daemon /list/ 0 (118/118)
cli client_output_locale_escaping, upstream_compat_filter_rule_parse 0 (31/31)
bin ssh_transport 0 (15/15)
test-support daemon_pull_every_operand 0 (10/10)
core filter_args, client::error 0 (357/357)
core client::remote::invocation 0 (322/322)
logging-sink message_sink 0 (63/63)
logging escape 0 (50/50)
engine orchestration/link_stat/vanished 0 (20/20)
daemon /argv_filters/, /daemon_filter/ 0 (39/39)

Remaining differences

  • The missing-operand report is worded differently from 3.5.2. oc prints a bare link_stat ... line, then an rsync error: link_stat ... (code 23) trailer. 3.5.2 prints rsync: [sender] link_stat ..., then some files/attrs were not transferred ... (code 23). That is a separate text-parity gap; this change only fixes the escaping.
  • Other raw eprintln! diagnostics in the local-copy engine carry names that are not escaped yet. Examples: symlink has no referent, opendir, directory has vanished, and the skip notices. They can move to eprint_terminal_line() one by one.
  • The same applies to the remaining raw stderr writes of an oc --server, and to the copy of a daemon's refusal on its own stderr.
  • Stdout escaping is untouched. The escaping of valid UTF-8 names on stdout is tracked separately.

Closes #8884

rsync 3.5.2 passes every diagnostic line through filtered_fwrite()
before it reaches the terminal. A client without -8 first checks the
line against the locale codeset via ic_chck (rsync.c:96-98,
log.c:463-496, filtered_fwrite(.., 0, ..) at log.c:476). A -8 client
or a server calls filtered_fwrite(.., !allow_8bit_chars, ..) at
log.c:499. filtered_char_len() escapes C0 except tab, DEL, 0x80-0x9f
and a literal \#ddd as \#ooo (log.c:288-291). A refused filter rule
is echoed through rprintf(FERROR) (exclude.c:133-136, exclude.c:1376),
so a VT in the rule prints as \#13.

oc printed these client lines raw: filter refusals, messages written
through the client stderr MessageSink, the pre-formatted cause lines
of a client error report, and the local-copy link_stat and vanished
operand lines.

The client stderr MessageSink now escapes with the process terminal
style, reusing logging::escape. A message with nothing to escape keeps
the copy-free vectored render. write_refusal takes the escape style.
The daemon passes passthrough, because its log sink already escapes
each line once, as logit() does (log.c:143).
@oferchen oferchen added this to the 0.6.5 milestone Nov 27, 2026
@github-actions github-actions Bot added the bug Something isn't working label Nov 27, 2026
rsync 3.5.2 prints "Unexpected local arg: %s" and the colon hint with
two rprintf(FERROR, ...) calls (main.c:1591-1592). rwrite() keeps only
each call's trailing newline raw (log.c:460) and escapes a newline
inside the text as \#12 (log.c:288-291). oc built one cause with an
embedded newline, so the new terminal escaping folded the two lines
into one.

A ClientError cause now holds one line per upstream call, and the
report writes each through the escaper. clap's multi-line parse error
is written line by line in the same way. The unknown-option refusal
and the failed --log-file open line now go through the escaper too,
as option_error() prints the token with rprintf (options.c:933).
A daemon that sends free text and then closes before acknowledging a
module listing was reported as one message, with the received lines
joined by newlines. The terminal escaping then printed them as \#12.
rsync 3.5.2 prints each line it receives with its own rprintf()
(clientserver.c:428-429 for @error, 438-439 for MOTD and listing
lines).

The refusal now carries the received lines as cause lines. The listing
report writes each one through the escaper, then the existing "daemon
refused module listing" trailer. The connect-failure lines on that
path go through the escaper too.
@oferchen
oferchen merged commit 56aca1c into master Nov 27, 2026
90 checks passed
@oferchen
oferchen deleted the fix/escape-client-stderr-control-bytes branch November 27, 2026 21:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Client stderr diagnostics print control bytes raw; rsync 3.5.2 escapes them

1 participant