`gh gist create` silently corrupts files that are not valid UTF-8
Describe the bug
gh gist create accepts files whose contents are not valid UTF-8, and uploads them with the invalid bytes silently replaced by U+FFFD. The gist is created, the command exits 0, and nothing warns that the contents that arrived are not the contents on disk.
Gist contents are sent as a JSON string. encoding/json does not reject invalid UTF-8, it rewrites every offending byte to U+FFFD, so anything that is not valid UTF-8 is quietly mangled in transit.
The binary check that is supposed to catch this classifies contents by MIME type:
// pkg/cmd/gist/shared/shared.go
func IsBinaryContents(contents []byte) bool {
isBinary := true
for mime := mimetype.Detect(contents); mime != nil; mime = mime.Parent() {
if mime.Is("text/plain") {
isBinary = false
break
}
}
return isBinary
}MIME sniffing and "survives a JSON round trip" are not the same question, so this misses text-like encodings. latin-1 and UTF-16 both sniff as text/plain, pass the check, and are then corrupted by the encoder.
Affected version
gh version 2.98.0
https://github.com/cli/cli/releases/tag/v2.98.0Also present on trunk at a255baf7.
Steps to reproduce the behavior
Create a file that is text, but not UTF-8. Any file saved as ISO-8859-1 with an accented character will do:
printf 'caf\xe9 r\xe9sum\xe9\n' > latin1.txtCreate a gist from it:
gh gist create latin1.txtThe command succeeds and prints a gist URL.
Read the file back and compare it to the original:
gh gist view <gist-id> --raw | xxd | head xxd latin1.txt | head
Expected vs actual behavior
Expected: either the file is uploaded unchanged, or gh refuses it and says why. Silently changing the contents is the one outcome that should not happen.
Actual: each 0xe9 byte is replaced by 0xef 0xbf 0xbd (U+FFFD, the replacement character). The 12-byte file above is stored as 18 bytes:
sent on the wire: {"files":{"latin1.txt":{"content":"caf� r�sum�\n"}}}
on disk: 63 61 66 e9 20 72 e9 73 75 6d e9 0a "café résumé\n"
in gist: 63 61 66 ef bf bd 20 72 ef bf bd 73 75 6d ef bf bd 0aThe original bytes are not recoverable from the gist afterwards.
Notes
There is a second, opposite symptom of the same check, already reported as #9761: most ASCII control characters sniff as binary, so a plain text file containing one is rejected with binary file not supported even though the API accepts it and the same gist can be created in the browser.
So the current check is wrong in both directions — it rejects contents the API handles fine, and accepts contents it cannot carry losslessly. Checking utf8.Valid instead answers the question that actually matters, and lines up exactly with what survives the round trip.
Incidentally, the existing test fixture for "binary" contents in pkg/cmd/gist/shared/shared_test.go is a run of 239, 191, 189 bytes — that is U+FFFD repeated, so the JPEG it was taken from had already been through this same lossy conversion before it was captured as test data.
I have a fix for both directions and am happy to open a PR.
Source: cli/cli