Skip to content

fix: strip terminal control sequences from server error text - #161

Open
tas50 wants to merge 1 commit into
mainfrom
fix/sanitize-server-message
Open

tas50 wants to merge 1 commit into
mainfrom
fix/sanitize-server-message

Conversation

@tas50

@tas50 tas50 commented Nov 27, 2026

Copy link
Copy Markdown
Collaborator

ServerMessage() and Error() passed server text through unchanged, so a misbehaving server could inject terminal escape sequences, carriage returns or bidi overrides into a caller's output.

Every place the library turns server-supplied text into an error string now sanitizes it:

  • newErrorResponse: each JSON error entry, depsolver message, top-level message, and the raw body fallback (sanitized before the 512-byte cap, so escapes don't count against it). Messages that sanitize to empty are dropped.
  • transferErrorMessages: S3/bookshelf Code: Message and the raw body.
  • redirectError: the Location, parsed or raw.
  • Cookbook checksum mismatch: the manifest checksum is now %q-quoted.

sanitizeServerText removes ESC sequences (CSI; OSC, DCS, SOS, PM, APC ended by BEL or ST, or cut short by a new ESC; two-byte and nF escapes; an unterminated sequence runs to the end), replaces other C0/C1 controls and DEL with a space (newline and tab kept), and drops U+202A-U+202E, U+2066-U+2069, U+200E, U+200F and U+061C. Invalid UTF-8 becomes U+FFFD rather than panicking.

Fixes #158

Test plan

  • go test ./... -race -count=2
  • cd integration && go test -race ./...
  • gofmt -l, go vet, staticcheck clean in both modules
  • New code at 100% line coverage

ServerMessage and Error returned the server's text as is, so a
misbehaving server could clear the screen, retitle the window, hide
text, overwrite output with a carriage return, or reorder it with a
bidi override in any caller that prints the error.

newErrorResponse now sanitizes every message, the raw body fallback
included (before it is capped): ESC sequences (CSI, OSC and the other
string sequences, two-byte escapes) are removed, other C0/C1 controls
except newline and tab become a space, and Unicode bidi controls are
dropped. Invalid UTF-8 becomes U+FFFD. The same applies to bookshelf
transfer errors and to the Location named in a redirect error, and a
cookbook checksum mismatch now quotes the manifest's checksum.

Fixes #158

Signed-off-by: Tim Smith <tsmith84@proton.me>
@tas50
tas50 requested review from damacus and ramereth December 1, 2026 01:28
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.

Strip control characters from ServerMessage()

1 participant