Reject terminal stdin for `gh auth login --with-token` by BagToad · Pull Request #14679 · cli/cli · GitHub
Skip to content

Reject terminal stdin for gh auth login --with-token - #14679

Draft
BagToad wants to merge 1 commit into
trunkfrom
bagtoad/auth-login-with-token-tty-error
Draft

BagToad wants to merge 1 commit into
trunkfrom
bagtoad/auth-login-with-token-tty-error

Conversation

@BagToad

@BagToad BagToad commented Oct 14, 2026

Copy link
Copy Markdown
Member

Fixes #9789

Description

gh auth login --with-token reads a token from standard input until the input ends. A file or a pipe ends on its own, but a terminal does not. The login command never checked where standard input came from. So in a terminal without a piped token, it printed nothing and waited for Ctrl+D. It looked like it had hung. A token pasted while it waited also stayed visible on screen.

Following the acceptance criteria in the issue, --with-token now fails right away when standard input is a terminal. The new check runs before anything reads standard input:

if tokenStdin {
	// New: fail before io.ReadAll waits on a terminal
	if opts.IO.IsStdinTTY() {
		return cmdutil.FlagErrorf("`--with-token` must be used non-interactively, ...")
	}
	defer opts.IO.In.Close()
	token, err := io.ReadAll(opts.IO.In)
	// ...
}

The error shows the redirect form. It also points to interactive gh auth login, which already accepts a pasted token without showing it on screen:

`--with-token` must be used non-interactively, for example `gh auth login --with-token < mytoken.txt`
To paste a token interactively, run `gh auth login` instead.

How did you test this change?

I built gh from trunk at d6b82b1 and from this branch at 2de1e08. I recorded each build running gh auth login --with-token in a terminal. Both runs used an empty config directory and no token environment variables.

Before, the command printed nothing and waited until I pressed Ctrl+C.

Before: gh auth login --with-token prints nothing and waits until Ctrl+C

After, the command printed the new error and the usage right away. It exited with status 1.

After: gh auth login --with-token prints the new error and the usage

I also ran these by hand in a terminal with an empty config directory:

  • gh auth login --with-token --hostname example.com printed the same error and exited with status 1.
  • gh auth login --with-token | cat printed the same error. Only stdout was piped, so standard input was still the terminal.
  • gh auth login --web --with-token still reported the --web conflict first.
  • Piping and redirecting a fake token behaved the same on both builds. Each read the token and failed validation with HTTP 401: Bad credentials.
  • gh auth login --hostname github.com --git-protocol ssh --skip-ssh-key still offered Paste an authentication token, which the new hint points to.

Key points

  • This changes behavior for anyone who pasted a token and pressed Ctrl+D. The issue thread accepted that, since --with-token is meant for non-interactive input.
  • The check looks at standard input only, not CanPrompt(). The wait happens whenever standard input is a terminal, even when stdout is piped or prompts are disabled.
  • It returns a flag error, like other commands that refuse to read a terminal, such as gh gist create. That is why the usage follows the error.
  • I did not add a hidden token prompt to --with-token. The issue thread preferred an error over prompting in this flag, and interactive gh auth login already covers pasting a token.

Notes for reviewers

Authorship and follow-up

Who wrote this:

  • A human wrote it.
  • An agent wrote it under close human direction.
  • An agent wrote it independently, and no human has guided the implementation beyond the initial prompt.

Who answers review comments:

  • @BagToad will read and reply directly.
  • An agent will draft replies and @username will read them before they are posted.
  • Nobody has explicitly committed to replying.

`gh auth login --with-token` reads standard input until EOF. In an
interactive terminal it printed nothing and appeared to hang, and a
pasted token stayed visible on screen. It now fails fast with a flag
error that shows the redirect form and points to `gh auth login` for
pasting a token interactively.

Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 27852a81-0ebb-4088-b113-49705d817080
Copilot AI balanced review requested due to automatic review settings October 14, 2026 05:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Regression coverage does not verify the intended behavior when stdin is a terminal but stdout is piped.

1 open finding
What changed in this PR

Rejects terminal stdin for gh auth login --with-token, preventing apparent hangs and visible token entry.

Changes:

  • Adds an immediate, actionable flag error for terminal stdin.
  • Updates constructor tests for the new behavior and exact errors.
File Description
pkg/​cmd/​auth/​login/​login.go Rejects terminal stdin before reading the token.
pkg/​cmd/​auth/​login/​login_test.go Updates TTY and conflict test expectations.

🧠 Review effort: Balanced


💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +70 to +71

This branch has not been deployed

No deployments
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.

gh auth login --with-token hangs

2 participants