Skip to content

Fix: sanitize embedded control bytes in log() (#599) - #607

Merged
fdaviddpt merged 2 commits into
mainfrom
fix/599
Sep 6, 2026
Merged

fdaviddpt merged 2 commits into
mainfrom
fix/599

Conversation

@fdaviddpt

Copy link
Copy Markdown
Contributor

What

log() (scripts/log.sh) wrote its message argument verbatim into the daily log
file, with no newline handling. Several call sites in
scripts/save-session.sh embed the first 80 bytes of a model's reply straight
into a log line -- untrusted text since #593 -- and an embedded newline
landed at column 0 of the log file, reading as a second, forged log entry to
anything parsing the log.

Approach

Sanitize once, inside log() itself, rather than at each of the (at least
three) call sites that embed such text, so the class is closed everywhere
log() is used rather than only at the newest call site.

Follow-up commit: self-review (Explore + oss:auditor) found that
tr '[:cntrl:]' under the caller's own UTF-8 locale errors on a byte
sequence truncated mid-multibyte-character -- exactly what save-session.sh's
head -c 80 cut of a model reply produces whenever the 80-byte offset lands
inside a multibyte character. tr then exits nonzero and truncates its own
output at the bad byte, and because save-session.sh runs under set -e, the
message=$(...) assignment's nonzero exit aborted the whole script on an
otherwise ordinary non-English model reply. LC_ALL=C makes tr classify
every byte 0-255 on its own, identically on GNU and BSD, so the
embedded-newline/CR/tab class this fix exists to close is still caught while
a non-ASCII byte that is not itself a control code passes through untouched.

Verification

  • Observed: tests/test_log_newline_sanitization_599.py pins both --
    sanitization of embedded control bytes, and
    test_truncated_multibyte_reply_does_not_abort_or_lose_the_tail (red without
    LC_ALL=C: script aborts, content lost; green with it).

Changelog

changelog.d/599.fixed.md added.

Closes #599

This PR was assembled by a maintainer tick from two already-committed and
pushed commits (a developer lane dispatched in a prior tick whose handback
never opened the pull request) -- the commit messages above are that
developer's own record of the work and its self-review.

🤖 Generated with Claude Code

https://claude.ai/code/session_01P9WexZb39yUjQgJ53Zmf1c

[AI-generated]

log() (scripts/log.sh) wrote its message argument verbatim into the daily
log file, with no newline handling. Several call sites in
scripts/save-session.sh embed the first 80 bytes of a model's reply
straight into a log line -- untrusted text since #593 -- and an embedded
newline landed at column 0 of the log file, reading as a second, forged
log entry to anything parsing the log.

Flattened once, inside log() itself, rather than at each of the (at
least three) call sites that embed such text, so the class is closed
everywhere log() is used rather than only at the newest call site.

Co-Authored-By: Max <noreply>
…ollow-up)

Self-review (Explore + oss:auditor) on the first #599 commit found that
`tr '[:cntrl:]'` under the caller's own UTF-8 locale errors on a byte
sequence truncated mid-multibyte-character -- exactly what save-session.sh's
`head -c 80` cuts of a model reply produce whenever the 80-byte offset lands
inside a multibyte character. `tr` then prints "Illegal byte sequence",
exits nonzero, and truncates its own output at the bad byte, silently
dropping everything logged after it -- and because save-session.sh runs
under `set -e`, the `message=$(...)` assignment's nonzero exit aborts the
whole script on an otherwise ordinary non-English model reply (legitimate
since #593).

`LC_ALL=C` makes tr classify every byte 0-255 on its own, identically on
GNU and BSD, so the embedded-newline/CR/tab class this fix exists to close
is still caught while a non-ASCII byte that is not itself a control code
passes through untouched instead of erroring. New test
test_truncated_multibyte_reply_does_not_abort_or_lose_the_tail pins this:
red without LC_ALL=C (script aborts, content lost), green with it.

Co-Authored-By: Max <noreply>
@fdaviddpt
fdaviddpt merged commit b05a330 into main Sep 6, 2026
16 checks passed
@fdaviddpt
fdaviddpt deleted the fix/599 branch September 6, 2026 23:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant