Skip to content

show: escape the author, the subject and the file paths of a commit - #9

Open
fcy222fcy wants to merge 1 commit into
feder-cr:mainfrom
fcy222fcy:show-escape-server-text
Open

fcy222fcy wants to merge 1 commit into
feder-cr:mainfrom
fcy222fcy:show-escape-server-text

Conversation

@fcy222fcy

Copy link
Copy Markdown

Summary

graft show (text output) printed the commit's author, subject and file paths raw. These strings come from other clients: a commit whose subject or file name carries a terminal escape sequence would act on the terminal of whoever reads the output — move the cursor, erase lines above it, or forge which files a commit changed.

Every other command already routes server-reported text through displayText (search, review, conflicts, log, the repo commands — see display_test.go and review_escape_test.go). show did not. This PR wraps the author, the subject and the file paths of the text branch of show in displayText, and adds a regression test.

The --json output is unchanged (JSON encoding already escapes control characters), so consumers of the superfan format see no difference.

Problem

// cmd/graft/show.go, before
fmt.Fprintf(out, "commit %s\nAuthor: %s\n\n%s\n", d.OID, d.Author, d.Subject)
for _, f := range d.Files {
    fmt.Fprintf(out, "+%d -%d\t%s\n", f.Additions, f.Deletions, f.Path)
}

A commit created by another client with, say, subject fix\x1b[1A\x1b[2Kforged and path ledger/a\x1b[2J.txt prints those bytes raw; the escape sequences erase the commit/Author lines above and can rewrite the file list on screen. search and review show already guard against exactly this for the same reason.

Verification

  • Reproduced outside the change: rendering the current format string with a commit whose author is Ada\x1b[2K <ada@example.com>, subject fix\x1b[1A\x1b[2Kforged and path ledger/a\x1b[2J.txt leaves raw 0x1b bytes in the output; the same rendering through displayText prints Ada\x1b[2K <ada@example.com>, fix\x1b[1A\x1b[2Kforged and ledger/a\x1b[2J.txt with no raw escape left.
  • New TestCLIShowEscapesServerText fails on the old code (raw \x1b reaches the terminal) and passes with the fix, following the pattern of TestCLIReviewShowEscapesServerText.
  • TestShowJSONIsOneObjectOfTheSuperfanFormat and TestShowJSONListsAreNeverNull are unaffected: only the text branch changed.

graft show prints the author, the subject and the file paths of a commit
as they are. These strings come from other clients: a commit whose
subject carries a terminal escape sequence would move the cursor of the
terminal of whoever reads it and overwrite the lines above, for example
to forge which files a commit changed.

Every other command already shows such text through displayText: search,
review, conflicts, log and the repo commands do. show did not. Route the
author, the subject and the paths of show through displayText too, and
check the escaping with a regression test.
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