gh api emits invalid JSON (bad \\u escape) when a body contains a backslash before 'u', and --jq fails on it
gh api emits invalid JSON on stdout, and --jq fails, when a response body contains a backslash immediately before u
Summary
gh api re-serializes the response body instead of passing the server's bytes through. When a
string value contains a backslash immediately followed by the letter u, the re-serialization
produces an invalid \\\uXXXX escape, so the output cannot be parsed by any JSON consumer —
including gh's own --jq and --template.
gh exits 0 while writing this unparseable output, so a script that checks only the exit code
will treat a failed read as a successful one.
This is not specific to --jq; plain gh api redirected to a file is equally corrupt.
Reproduction
$ gh --version
gh version 2.23.0 (2023-02-27 Debian 2.23.0+dfsg1-1)
$ gh api repos/OWNER/REPO/pulls/147 > gh.json
$ gh api repos/OWNER/REPO/pulls/147 --jq .number
invalid character 'X' in \\\u hexadecimal character escape--jq .number should print 147.
Comparing against the server's own bytes with the same User-Agent:
$ gh api repos/OWNER/REPO/pulls/147 | wc -c
21852
$ curl -sH "Authorization: token $TOKEN" -H "Accept: application/vnd.github+json" \
-H "User-Agent: gh/2.23.0" \
https://api.github.com/repos/OWNER/REPO/pulls/147 | wc -c
21851gh emits exactly one surplus byte, at offset 2333. Deleting it makes the two outputs
byte-for-byte identical over the whole common length:
>>> a = open('gh.json','rb').read(); b = open('curl.json','rb').read()
>>> i = next(i for i in range(min(len(a),len(b))) if a[i]!=b[i])
>>> a[:i] + a[i+1:] == b
TrueThe defect
At the first differing byte:
gh: ...\r \t`, `\\\u00XX`), and
curl: ...\r \t`, `\\\u00XX`), andThe source text is the JCS (RFC 8785) escape list, where the sequence means "a literal backslash
followed by the characters u00XX". The server encodes it correctly as \\\\u00XX (an escaped
backslash, then ordinary characters).
gh decodes the body, then re-encodes it one backslash short, emitting \\\u00XX. A JSON parser
then reads \\\u00 as the start of a \\\u escape, finds XX is not hex, and fails:
json.decoder.JSONDecodeError: Invalid \\\uXXXX escape: line 1 column 2335Trigger: parity of the backslash run, not the literal text
The trigger is not the presence of the text u00XX. It is the parity of the run of
backslashes immediately preceding the letter u. This is why the bug looks intermittent across
otherwise similar bodies:
| body containing | backslash run before u |
result |
|---|---|---|
..., \\\u00XX (even run) |
even | breaks |
| same construct elsewhere | even | works |
A second affected object with the same literal text and an even run parses fine, which is what makes this look content-dependent when it is really parity-dependent.
Why this matters more than a parsing nit
gh exiting 0 on output that no JSON parser can read inverts the usual failure signal. Any wrapper
that checks $? records a successful read of an empty/garbled value. That is how a repository
with 100+ merged pull requests can be reported as having none, cleanly, with exit status 0.
Expected behavior
gh api should emit the server's response bytes unmodified when no transformation is requested, and
any re-serialization should produce valid JSON.
Workaround
Read through curl, or validate the bytes and fail loudly rather than retrying.
Environment
gh version 2.23.0 (2023-02-27 Debian 2.23.0+dfsg1-1)- Behavior appears User-Agent dependent (the same
curlrequest with and without gh'sUser-Agentis what makes the byte comparison above meaningful), which is why the trigger has been hard to pin down from the JSON side alone.
Source: cli/cli