Skip to content

feat: read the --token flag value from standard input with a dash - #773

Merged
mwbrooks merged 6 commits into
mainfrom
mwbrooks-token-stdin
Nov 12, 2026
Merged

mwbrooks merged 6 commits into
mainfrom
mwbrooks-token-stdin

Conversation

@mwbrooks

Copy link
Copy Markdown
Member

Changelog

The --token flag now reads the token from standard input when its value is -, which keeps tokens out of shell history and process listings. This works for both the global --token flag and slack auth login --token.

Summary

This pull request lets developers and coding agents pass a token to the CLI on standard input, so a secret never has to appear in shell history or ps output.

slack auth login --token - < token.txt
op read "op://vault/slack/token" | slack api auth.test --token -
  • One helper (auth.ResolveTokenInput) handles both flags, using the same - convention as slack blocks preview --blocks -.
  • A terminal on standard input or empty input errors with missing_input instead of hanging or sending an empty token.
  • Suggested by the anc.dev agent CLI scorecard as an easy win.

Testing

  • echo "xoxp-fake" | slack api auth.test --token - sends the piped token and returns invalid_auth.
  • echo "xoxp-fake" | slack auth login --token - validates the piped token and fails with invalid_auth.
  • printf '' | slack api auth.test --token - errors with "No token was provided on standard input".
  • slack auth login --token - from an interactive terminal errors right away instead of waiting on input.

Notes

  • Standard input can only be read once, so --token - and --blocks - can't both be used on one command.
  • A follow-up PR adds an environment variable for the token, after we settle its name.

Requirements

@mwbrooks mwbrooks added this to the Next Release milestone Nov 11, 2026
@mwbrooks mwbrooks added enhancement M-T: A feature request for new functionality changelog Use on updates to be included in the release notes semver:minor Use on pull requests to describe the release version increment labels Nov 11, 2026
@mwbrooks mwbrooks self-assigned this Nov 11, 2026
@codecov

codecov Bot commented Nov 11, 2026

Copy link
Copy Markdown

Codecov Report

โŒ Patch coverage is 91.66667% with 2 lines in your changes missing coverage. Please review.
โœ… Project coverage is 78.99%. Comparing base (024c7de) to head (61d453c).

Files with missing lines Patch % Lines
cmd/auth/stdin.go 92.30% 1 Missing โš ๏ธ
cmd/root.go 75.00% 1 Missing โš ๏ธ
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #773      +/-   ##
==========================================
+ Coverage   78.95%   78.99%   +0.03%     
==========================================
  Files         241      242       +1     
  Lines       18365    18387      +22     
==========================================
+ Hits        14501    14524      +23     
+ Misses       3864     3863       -1     

โ˜” View full report in Codecov by Harness.
๐Ÿ“ข Have feedback on the report? Share it here.

๐Ÿš€ New features to boost your workflow:
  • โ„๏ธ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@mwbrooks
mwbrooks marked this pull request as ready for review November 11, 2026 06:09
@mwbrooks
mwbrooks requested a review from a team as a code owner November 11, 2026 06:09

@srtaalej srtaalej 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.

Nice addition! Followed the testing steps and all four behave as described: piped tokens return invalid_auth for both api auth.test and auth login (so no double-read of stdin between the global and login --token flags), empty input errors with missing_input, and a pty on stdin errors immediately with the remediation. go test and make lint pass, and I confirmed with a throwaway test that InitConfig resolves --token - into Config.TokenFlag (with whitespace trimmed).

Holding off on approving for a couple of small things inline, mainly the generated docs.

Comment thread internal/config/flags.go Outdated
// TODO - next semver MAJOR can consider a new shorthand flag, right now -t and -T are used by other commands
cmd.PersistentFlags().StringVarP(&c.TeamFlag, "team", "w", "", "select workspace or organization by team name or ID")
cmd.PersistentFlags().StringVarP(&c.TokenFlag, "token", "", "", "set the access token associated with a team")
cmd.PersistentFlags().StringVarP(&c.TokenFlag, "token", "", "", "set the access token associated with a team\n use \"-\" to read the token from standard input")

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.

The --token help text changes here (and in auth login) aren't reflected in docs/reference yet. Running slack docgen ./docs/reference updates 90 command pages (the global flags block on every page, plus the new auth login example). Could we regenerate them in this PR?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Regenerated in commit d1f86f1, thanks! ๐Ÿ™‡

Comment thread cmd/root.go Outdated
}

// Read the token from standard input when requested
token, err := auth.ResolveTokenInput(clients, clients.Config.TokenFlag)

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.

The global flag path is the headline use case (op read ... | slack api auth.test --token -), but only ResolveTokenInput and the auth login local flag are tested. Would be good to have a test that InitConfig swaps Config.TokenFlag from - to the stdin value, and returns the missing_input error, so a refactor that moves or drops this call gets caught.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added Test_InitConfig in commit a1bf205, great catch! It checks the stdin swap and the missing_input error.

Comment thread cmd/auth/stdin.go Outdated
if clients.IO.IsStdinTTY() {
return "", slackerror.New(slackerror.ErrMissingInput).
WithMessage("No token was provided on standard input").
WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("auth login --token - < token.txt", false))

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.

nit: this remediation always suggests auth login --token - < token.txt, but it's also shown for the global flag (e.g. slack api auth.test --token - from a terminal), where it points at the wrong command. Maybe something command-agnostic like Pipe a token into the command, e.g. echo $TOKEN | slack <command> --token -, or omit the command example?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reworded in commit a1bf205, thanks! The hint is now --token - < token.txt for every command.

@WilliamBergamin WilliamBergamin 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.

๐Ÿ” @mwbrooks thanks for this! Reusing the - convention from blocks preview and failing fast on a terminal makes keeping tokens out of shell history really easy.
Left a few comments, nothing blocking. The --token - plus --blocks - clash and a test for the global path are the ones I'd look at first ๐Ÿ™

Comment thread cmd/root.go Outdated
}

// Read the token from standard input when requested
token, err := auth.ResolveTokenInput(clients, clients.Config.TokenFlag)

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.

๐Ÿงช suggestion (non-blocking): I think the global --token - path is the one slack api and sandbox rely on, but the new tests only cover login's local flag and the helper. What do you think about a TestExecuteContext-style case that runs api auth.test --token - with mocked stdin and asserts the resolved Config.TokenFlag? That would also cover the error return just below.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added Test_InitConfig in commit a1bf205, thanks! It covers the stdin value and the error return.

Comment thread cmd/root.go Outdated
}

// Read the token from standard input when requested
token, err := auth.ResolveTokenInput(clients, clients.Config.TokenFlag)

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.

๐Ÿง  issue (non-blocking): Since this reads stdin before any command runs, echo '[...]' | slack blocks preview --blocks - --token - uses the blocks JSON as the token and then fails with "No blocks were provided", which points at the wrong flag. I tried it with a local build. Could we error up front when both flags are -, naming the conflict? The PR notes mention the limit, so this would just make it loud.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Fixed in commit a1bf205, great catch! --blocks - --token - now errors with mismatched_flags and names both flags. Thanks for trying it on a local build! ๐Ÿ™‡

Comment thread cmd/auth/stdin.go Outdated
if clients.IO.IsStdinTTY() {
return "", slackerror.New(slackerror.ErrMissingInput).
WithMessage("No token was provided on standard input").
WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("auth login --token - < token.txt", false))

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.

๐Ÿ“š suggestion (non-blocking): This remediation always suggests auth login --token - < token.txt, even when the global flag was used on slack api or sandbox. Maybe a command-neutral hint reads better there?

Suggested change
WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("auth login --token - < token.txt", false))
WithRemediation("Pipe a token into the command, e.g. %s", style.Commandf("--token - < token.txt", false))

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Reworded in commit a1bf205, thanks!

Comment thread cmd/auth/stdin.go
// ResolveTokenInput returns the token read from standard input when the value
// is "-" and otherwise returns the value unchanged. Reading from standard input
// keeps the token out of shell history and process listings.
func ResolveTokenInput(clients *shared.ClientFactory, value string) (string, error) {

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.

๐Ÿชž suggestion (non-blocking): This is almost the same shape as resolveBlocksInput and readStdinBlocks in cmd/blocks/preview.go (TTY check, ReadInAll, empty check, missing_input). What do you think about one shared helper, maybe on iostreams, that both flags call with their own messages? Totally fine as a follow-up too.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Good idea! I'll keep this PR small and do the shared helper as a follow-up. Thanks!

Comment thread cmd/auth/stdin.go Outdated
"github.com/slackapi/slack-cli/internal/style"
)

// stdinTokenValue is the --token value that reads the token from standard input

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.

๐Ÿช“ nitpick (non-blocking): I don't think we need this comment, stdinTokenValue already says it. Same for the // Read the token from standard input when requested line in root.go, which restates the call below it.

Suggested change
// stdinTokenValue is the --token value that reads the token from standard input

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Removed both in commit a1bf205, thanks!

Comment thread cmd/auth/stdin.go
}
token, err := clients.IO.ReadInAll()
if err != nil {
return "", slackerror.Wrap(err, slackerror.ErrMissingInput)

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.

๐Ÿงช suggestion (non-blocking): The ReadInAll error branch isn't hit by a test yet. A reader that returns an error in Test_ResolveTokenInput would pin the missing_input wrap.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Added a read error case in commit a1bf205, thanks!

Comment thread cmd/auth/stdin.go
if value != stdinTokenValue {
return value, nil
}
if clients.IO.IsStdinTTY() {

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.

๐Ÿ’ฏ praise: Checking IsStdinTTY first so an interactive --token - fails right away instead of hanging is a really nice touch.

@zimeg zimeg left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

๐Ÿ” @mwbrooks LGTM! Reading --token - keeps the token out of shell history and the debug log, which records the full command, and /dev/null on standard input errors right away instead of hanging ๐ŸŽ

๐Ÿ“ quibble: The --blocks help text reads (use - to read from standard input), so perhaps we match that wording for both --token flags while the docs are regenerated?

- Error when --token - and --blocks - both read standard input
- Use a command-neutral remediation and match the --blocks help wording
- Test the global flag path in InitConfig and the stdin read error
- Remove comments that restate the code
@mwbrooks
mwbrooks requested a review from a team as a code owner November 12, 2026 22:18
@mwbrooks

Copy link
Copy Markdown
Member Author

@zimeg Reworded both --token flags in commit a1bf205 to match --blocks with (use - to read from standard input), and the regenerated docs in d1f86f1 include it. Thanks for the nudge! ๐Ÿ™‡

@mwbrooks

Copy link
Copy Markdown
Member Author

Thanks @WilliamBergamin, @zimeg, and @srtaalej for the careful reviews! Catching the --blocks - clash on a local build and pushing for the InitConfig test made this a lot more solid. ๐Ÿ™‡

@mwbrooks
mwbrooks merged commit 67c36d3 into main Nov 12, 2026
13 checks passed
@mwbrooks
mwbrooks deleted the mwbrooks-token-stdin branch November 12, 2026 22:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

changelog Use on updates to be included in the release notes enhancement M-T: A feature request for new functionality semver:minor Use on pull requests to describe the release version increment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants