From 6b8313f9c78881510eb0ac6748264d9787134eef Mon Sep 17 00:00:00 2001 From: him0 Date: Sat, 21 Nov 2026 04:03:42 +0900 Subject: [PATCH] fix: escape control characters in API error messages A GraphQL error message, or the server error that gh quotes on stderr, was printed to the terminal as received, so a response could carry ANSI escape sequences that rewrite what the terminal shows. Control characters in both are now shown as escapes such as \x1b. Co-Authored-By: Claude Mythos 5.2 --- main.go | 26 ++++++++++++++++++++++++-- main_test.go | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+), 2 deletions(-) diff --git a/main.go b/main.go index d40ecd2..17bfe2f 100644 --- a/main.go +++ b/main.go @@ -19,6 +19,7 @@ import ( "strconv" "strings" "time" + "unicode" "github.com/mattn/go-isatty" ) @@ -1443,7 +1444,8 @@ func findMergedPRsWithGh(cfg runConfig, remote Remote, base string, heads []stri return nil, fmt.Errorf("gh api graphql: timed out after %s", ghTimeout) } if err != nil { - return nil, fmt.Errorf("gh api graphql: %w", err) + // gh's stderr quotes the server's error body. + return nil, fmt.Errorf("gh api graphql: %s", sanitizeForTerminal(err.Error())) } return parseMergedPRResponse(output, heads) } @@ -1506,7 +1508,7 @@ func parseMergedPRResponse(output []byte, heads []string) (map[string][]mergedPR return nil, fmt.Errorf("unexpected gh api graphql output: %w", err) } if len(resp.Errors) > 0 { - return nil, fmt.Errorf("GraphQL error: %s", resp.Errors[0].Message) + return nil, fmt.Errorf("GraphQL error: %s", sanitizeForTerminal(resp.Errors[0].Message)) } if resp.Data.Repository == nil { return nil, errors.New("repository not found") @@ -1530,6 +1532,26 @@ func parseMergedPRResponse(output []byte, heads []string) (map[string][]mergedPR return prs, nil } +// sanitizeForTerminal makes text that came from a server safe to print: control +// characters (C0 including ESC, DEL and C1) are shown as Go escapes such as +// \x1b, so a response can't move the cursor, recolor or erase what the terminal +// shows. Line breaks and tabs are kept, and so is every other character, so an +// ordinary message reads as it was sent. Bytes that aren't valid UTF-8 become +// U+FFFD. +func sanitizeForTerminal(s string) string { + s = strings.ReplaceAll(s, "\r\n", "\n") + var b strings.Builder + for _, r := range s { + if unicode.IsControl(r) && r != '\n' && r != '\t' { + quoted := strconv.QuoteRune(r) + b.WriteString(quoted[1 : len(quoted)-1]) + continue + } + b.WriteRune(r) + } + return b.String() +} + // gitHubRepo identifies a repository on a GitHub host (github.com or GitHub // Enterprise Server). type gitHubRepo struct { diff --git a/main_test.go b/main_test.go index f5fcfe9..a1b2f97 100644 --- a/main_test.go +++ b/main_test.go @@ -12,6 +12,7 @@ import ( "slices" "strings" "testing" + "unicode" ) // TestMain isolates every git invocation in this package (including the ones @@ -966,6 +967,47 @@ func TestParseMergedPRResponse(t *testing.T) { t.Errorf("%s: want error", name) } } + + // A message from the server can't carry escape sequences to the terminal. + _, err = parseMergedPRResponse([]byte(`{"errors":[{"message":"\u001b[2K\u001b[1Aall good\u009b31m\u0007"}]}`), heads) + if err == nil { + t.Fatal("GraphQL error with escape sequences: want error") + } + if want := `GraphQL error: \x1b[2K\x1b[1Aall good\u009b31m\a`; err.Error() != want { + t.Errorf("parseMergedPRResponse() error = %q, want %q", err.Error(), want) + } + if i := strings.IndexFunc(err.Error(), unicode.IsControl); i >= 0 { + t.Errorf("parseMergedPRResponse() error keeps a control character at %d: %q", i, err.Error()) + } +} + +func TestSanitizeForTerminal(t *testing.T) { + tests := []struct { + name string + in string + want string + }{ + {"plain", "Something went wrong", "Something went wrong"}, + {"non-ASCII", "リポジトリが見つかりません — café ✓", "リポジトリが見つかりません — café ✓"}, + {"quotes and backslashes", `Could not resolve "a\b"`, `Could not resolve "a\b"`}, + {"lines and tabs", "first\n\tsecond\r\nthird", "first\n\tsecond\nthird"}, + {"ESC sequence", "\x1b[31mred\x1b[0m", `\x1b[31mred\x1b[0m`}, + {"OSC title", "\x1b]0;title\a", `\x1b]0;title\a`}, + {"carriage return", "real\rfake", `real\rfake`}, + {"backspace and NUL", "a\b\x00", `a\b\x00`}, + {"DEL", "a\x7f", `a\x7f`}, + {"C1 CSI", "\u009b31m", `\u009b31m`}, + {"raw C1 byte", "\x9b31m", "\ufffd31m"}, + } + for _, tt := range tests { + got := sanitizeForTerminal(tt.in) + if got != tt.want { + t.Errorf("%s: sanitizeForTerminal(%q) = %q, want %q", tt.name, tt.in, got, tt.want) + } + if i := strings.IndexFunc(strings.NewReplacer("\n", "", "\t", "").Replace(got), unicode.IsControl); i >= 0 { + t.Errorf("%s: sanitizeForTerminal(%q) keeps a control character: %q", tt.name, tt.in, got) + } + } } func TestSSHResolvedHost(t *testing.T) {