Repository navigation
fix: escape control characters in API error messages - #94
Conversation
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 <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. WalkthroughThe change sanitizes errors from the GitHub CLI and GraphQL responses before returning them. The sanitizer normalizes CRLF to LF and escapes control characters other than newline and tab. Tests cover sanitization and error messages. ChangesTerminal error sanitization
Estimated code review effort: 2 (Simple) | ~8 minutes Change: Bug fix Suggested reviewers: Merge Risk:
|
Problem
parseMergedPRResponseputerrors[0].messagefrom thegh api graphqlresponse into its error, andlookupMergedPRsprinted that withprintWarning. JSON\uXXXXescapes are decoded, so a malicious or compromised host could send ESC and other control characters and rewrite what the terminal shows (move the cursor, erase lines, change the title). Since #93 gh is only called for GitHub hosts or hosts gh is logged in to, so the severity is low.Fix
sanitizeForTerminal, which shows control characters (C0 including ESC, DEL, C1) as Go escapes such as\x1b. Everything else, including non-ASCII text, quotes and backslashes, is left as it is, so ordinary messages read the same as before. Invalid UTF-8 bytes (e.g. a raw 0x9b) become U+FFFD.gh api graphqlcall too:commandOutputwraps gh's stderr, and gh prints the server's error message there. That error is no longer wrapped with%w; nothing unwraps it (the only consumer printserr.Error()).Decisions worth a look:
No README change: normal output is the same.
Testing
TestParseMergedPRResponse: a response whose error message contains ESC, BEL and C1 CSI yields an error string with no control characters.TestSanitizeForTerminal: table of plain, non-ASCII, multi-line, ESC/OSC, CR, NUL, DEL, C1 and invalid-UTF-8 inputs.mise run testandmise run lintpass locally on Linux. Not run locally on macOS or Windows; the new tests don't touch the filesystem or spawn processes.🤖 Generated with Claude Code
Summary by CodeRabbit