cli / cli

GitHub’s official command line tool
https://cli.github.com
MIT License
37.01k stars 5.69k forks source link

Strip whitespace from given token #9731

Open agkphysics opened 4 days ago

agkphysics commented 4 days ago

Describe the feature or problem you’d like to solve

gh should strip whitespace from a token given in GH_TOKEN.

Proposed solution

This will allow cases where the token is stored in a file or string with a trailing newline.

Additional context

Example

$ GH_TOKEN="gho_<valid token>" gh status
Assigned Issues                                 │ Assigned Pull Requests                          
Nothing here ^_^                                │ Nothing here ^_^                                
                                                │                                                 
Review Requests                                 │ Mentions                                        
Nothing here ^_^                                │ Nothing here ^_^                                
                                                │                                                 
Repository Activity
...

$ GH_TOKEN="gho_<valid token>\n" gh status
could not load events: failed to get current username: non-200 OK status code: 401 Unauthorized body: "{\"message\":\"Bad credentials\",\"documentation_url\":\"https://docs.github.com/graphql\",\"status\":\"401\"}"
williammartin commented 4 days ago

Hey @agkphysics, I can't really see any problem with this off the top of my head but I'm curious what your workflow was that you ended up in this situation? Like, you describe having the token in a file but I'm not sure how you would get that token without the newline being stripped by the shell e.g. GH_TOKEN=$(cat token_file)

Cheers!

agkphysics commented 4 days ago

@williammartin So in my case it comes from a GitHub secret which contains the token for a custom bot which we use for CI. I noticed that a workflow was failing because of a trailing newline in the token when trying to use gh. But I guess I should ask the maintainer to remove the newline from the secret.