Skip to content

fix(builtins): byte-exact tee, split, cut and xxd -r; sort -o refuses non-UTF-8 in place - #2945

Merged
chaliy merged 4 commits into
mainfrom
claude/project-thread-cs5tvz
Nov 18, 2026
Merged

chaliy merged 4 commits into
mainfrom
claude/project-thread-cs5tvz

Conversation

@chaliy

@chaliy chaliy commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

Requested by Михайло · project thread

Follow-up to #2935. A differential probe ran 60 commands through bashkit and real bash on a file containing Latin-1 and invalid bytes. It found 5 more byte-oriented builtins that decode their input as text and replace invalid bytes with �.

What changed

  • tee now passes stdin through as bytes, both to stdout and to its files. Before, both were rewritten to U+FFFD.
  • split (-b, -l, -n) works on bytes, so the pieces concatenate back to the exact input. -l also keeps \r on CRLF lines; it used to drop it.
  • cut -b now selects bytes. Before, it was an alias of -c. cut -c and cut -f keep invalid bytes as they are: -c counts each invalid byte as one character.
  • xxd -r outputs raw bytes. Before, it returned lossy UTF-8.
  • sort -o FILE FILE used to silently destroy a non-UTF-8 FILE by rewriting it through a lossy decode. It now refuses with exit 2, the same way sed -i does (L-SED-003). Sorting into a different file is unchanged.

Text tools (sort, sed, awk, ...) still decode their input with replacement by design (L-STREAM-001). That row now lists which builtins are byte-exact.

Before / After

On printf 'K\366ln,\377\376\000z\nb,K\303\266ln\200\r\n' > in.bin:

$ tee o.bin < in.bin | od -c | head -1
before: K 357 277 275 l n , 357 277 275 357 277 275 \0 z \n      (also in o.bin)
after:  K 366 l n , 377 376 \0 z \n                              (same as bash)
$ cut -b1-3 in.bin | od -c | head -1
before: K 357 277 275 l \n        after: K 366 l \n
$ printf 'b\n\366\na\n' > f; sort -o f f; echo $?
before: 0, f now holds U+FFFD   after: sort: cannot rewrite f: not a valid UTF-8 text file / 2, f unchanged

A new test, byte_tools_match_real_bash_on_non_utf8_input, compares stdout byte for byte with real bash for tee, split -b/-l, cut -b/--complement/-c/-f.

Risk

  • Low to medium. cut -b on multibyte UTF-8 now cuts bytes, as GNU does, so it can split a character. cut -c still counts characters.
  • A script that ran sort -o f f on a non-UTF-8 file now gets exit 2 instead of a silently damaged file.

Checklist

  • Tests added or updated: 3 integration tests (one differential against real bash). The existing unit tests for cut, split, tee, sort and xxd pass, 242 of them.
  • Backward compatibility considered. Knowledge updated: L-STREAM-001 and the log.

A differential probe on a Latin-1 + invalid-byte file found byte tools
decoding through text: tee rewrote output and file to U+FFFD, split
wrote damaged pieces (and -l dropped \r), cut -b was an alias of -c,
xxd -r returned lossy UTF-8. All now work on bytes. sort -o FILE FILE
refuses a non-UTF-8 FILE instead of destroying it, like sed -i.

Refs #2935
@chaliy chaliy self-assigned this Nov 18, 2026
@bashkit-pr-reviewer

bashkit-pr-reviewer Bot commented Nov 18, 2026 •

Copy link
Copy Markdown

Review summary — fix(builtins): byte-exact tee, split, cut and xxd -r; sort -o refuses non-UTF-8 in place

Verdict (3rd push, 57c8790a): Clean — no findings. Both earlier findings are now resolved.

Findings:

  • ✅ Resolved: sort-inplace-guard-unnormalized-path (P1) — resolve_path on both sides.
  • ✅ Resolved: sort-inplace-guard-symlink-alias (P2) — guard now canonicalizes via the new crate::fs::canonicalize (component walk + lstat/read_link with ELOOP hop cap, lexical fallback for unresolvable paths). Verified the helper fails safe: a missing -o target can't alias an existing input, and a link loop fails the subsequent read too, so fallback never enables a destructive rewrite. Test sort_in_place_guard_sees_through_path_aliases extended to link in both directions (-o link f, -o f link): 5× rc=2, file byte-identical.

This push: sortuniq.rs guard rework + test extension only; cut/tee/split/xxd hunks unchanged from the reviewed version.

Could not review: per project rules no build/test run (test expectations read against the implementation instead); check-then-act TOCTOU between canonicalize and write noted but not filed — same race exists in GNU sort's stat/open and the VFS is session-local.

@bashkit-pr-reviewer bashkit-pr-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Byte-exact conversions (tee/split/cut/xxd -r) look correct; one guard bypass in the new sort -o protection, detailed inline.

Comment thread crates/bashkit/src/builtins/sortuniq.rs Resolved
./f, d/../f and /cwd/f aliases of the input bypassed the refusal.

@bashkit-pr-reviewer bashkit-pr-reviewer Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Re-review of the new push: the lexical-alias bypass is fixed and regression-tested; one residual symlink-alias bypass noted inline (P2).

Comment thread crates/bashkit/src/builtins/sortuniq.rs Resolved
ln -s f link; sort -o link f bypassed the lexical comparison.
@chaliy
chaliy merged commit 518edda into main Nov 18, 2026
68 checks passed
@chaliy
chaliy deleted the claude/project-thread-cs5tvz branch November 18, 2026 21:35
chaliy added a commit that referenced this pull request Nov 19, 2026
… unix (#2955)

Two CI-green fixes.

Nine heapless 0.7.17 unsoundness advisories (RUSTSEC-2026-0574 through 0582)
were published on 2026-11-18. Cargo scans run `--deny unsound` over every
discovered lockfile, so `scripts/audit-lockfiles.sh` failed on both the
workspace lock and `crates/bashkit/fuzz/Cargo.lock`. The advisory database is
fetched at job time, so this went red with nothing in the tree changed.

postcard 1.1.3 is its newest release and requires `heapless ^0.7`, so no bump
reaches the patched 0.9.4. But heapless is an optional postcard dependency and
postcard's `default = ["heapless-cas"]` exists only to enable it; both of our
manifests asked for `features = ["alloc"]` while leaving default features on,
pulling heapless in by accident. Only `to_allocvec`, `from_bytes` and
`to_slice` are used here, all of which need `alloc` alone. Declaring postcard
with `default-features = false` drops heapless and its chain
(atomic-polyfill, critical-section, hash32, spin, lock_api, rustc_version)
from both lockfiles, so the unsound code leaves the build instead of having
its advisories suppressed.

That also satisfies the recorded removal condition for RUSTSEC-2023-0089
(atomic-polyfill), now absent from every discovered lockfile, so the
suppression is retired from deny.toml and the threat model.

`bashkit-monty` is a fork held at upstream Monty 1.5.0 outside
`bashkit fork:` markers, so the postcard divergence is marked with its reason
and a removal condition. knowledge/operations/dependencies.md described a
marker as covering a dependency's version; the check compares the whole
dependency spec, so a feature or default-features divergence is markable too,
and postcard is the first of that kind.

Separately, `byte_tools_match_real_bash_on_non_utf8_input` landed in #2945
without the platform gate every other host-bash oracle in the file carries,
and the Windows nightly went red on its first run afterwards. On
windows-latest `bash` is the WSL launcher, so the failure was in the test's
own `assert!(expected.status.success())` precondition, before any Bashkit
output was compared. Gated off unix as knowledge/operations/testing.md
requires. Windows keeps byte-exactness coverage through the non-oracle tests
in the same file.

Verified: audit-lockfiles.sh exits 0 with no denied warnings across all six
cargo lockfiles, `cargo deny check licenses sources` reports licenses and
sources ok, `cargo vet --locked` succeeds, and a workflow_dispatch of the
Windows nightly on the branch passes with the oracle reported ignored.
@chaliy chaliy mentioned this pull request Nov 19, 2026
2 tasks done
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.

1 participant