Skip to content

fix: escape control characters in API error messages - #94

Merged
him0 merged 1 commit into
mainfrom
fix/sanitize-api-error-output
Nov 20, 2026
Merged

him0 merged 1 commit into
mainfrom
fix/sanitize-api-error-output

Conversation

@him0

@him0 him0 commented Nov 20, 2026 •

Copy link
Copy Markdown
Owner

Problem

parseMergedPRResponse put errors[0].message from the gh api graphql response into its error, and lookupMergedPRs printed that with printWarning. JSON \uXXXX escapes 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

  • Add 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.
  • Apply it to the GraphQL error message.
  • Apply it to the error of a failed gh api graphql call too: commandOutput wraps 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 prints err.Error()).

Decisions worth a look:

  • Line feeds and tabs are kept (CRLF becomes LF; a lone CR is escaped), because gh's stderr can legitimately span lines. A server can therefore still add extra plain-text lines to the warning, but can't overwrite or erase existing output.
  • Only control characters (Unicode Cc) are handled. Format characters such as bidi overrides are not, since stripping them would also affect legitimate text.
  • git's own stderr is unchanged. Other warnings print local git output or local names, not API responses.

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 test and mise run lint pass locally on Linux. Not run locally on macOS or Windows; the new tests don't touch the filesystem or spawn processes.
  • The gh stderr path has no automated test and was not exercised against a real failing server.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Bug Fixes
    • Error messages from GraphQL requests now display terminal control characters safely, while preserving ordinary text, newlines, and tabs.

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>
@coderabbitai

coderabbitai Bot commented Nov 20, 2026 •

Copy link
Copy Markdown

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

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

Changes

Terminal error sanitization

Layer / File(s) Summary
Sanitizer and error handling
main.go, main_test.go
sanitizeForTerminal normalizes CRLF, escapes control characters other than newline and tab, and preserves other runes. GitHub CLI and GraphQL errors pass through the sanitizer. Tests cover escaped controls, preserved text and whitespace, and replacement of a raw C1 byte.

Estimated code review effort: 2 (Simple) | ~8 minutes

Change: Bug fix

Suggested reviewers: c-w-z

Merge Risk: Minimal · up to 6b831

The change escapes terminal control characters in API error text so server-provided messages cannot rewrite terminal output. It has automated tests for the GraphQL path and the sanitizer. Merge risk is low.

Pre-merge checks: Passed 4

Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title "fix: escape control characters in API error messages" matches the main change. The PR adds sanitizeForTerminal and applies it to GraphQL error messages and failed gh api graphql errors, esc…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "fix: escape control characters in API error messages" matches the main change. The PR adds sanitizeForTerminal and applies it to GraphQL error messages and failed gh api graphql errors, escaping C0, DEL, and C1 control characters so server text cannot rewrite terminal output. The title is specific, short, and uses a conventional fix: prefix.

Finishing Touches

Generate unit tests (beta)

  • Commit to this branch
  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 35738b1a-7b4c-4df3-8cae-814ceb06a1b4
Commits

Reviewing files that changed from the base of the PR and between 03e2162 and 6b8313f.

Files selected for processing: 2

  • main.go
  • main_test.go

Included review availability: This review used your included allowance. Your plan provides up to 1 included review per hour; 0 remain after this review.

@him0
him0 merged commit cc6330c into main Nov 20, 2026
6 checks passed
@him0
him0 deleted the fix/sanitize-api-error-output branch November 20, 2026 19:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant