Skip to content

In Codespaces, GITHUB_TOKEN seems to be used instead of GH_TOKEN #6238

@dmalan

Description

@dmalan

Describe the bug

Even though GH_TOKEN should have higher precedence than GITHUB_TOKEN, per #2388, it seems as though gh (including 2.15.0) somehow uses the latter within Codespaces. By default, a codespace has a very-restricted GITHUB_TOKEN that has access (only) to the repo for which the codespace was created. But even if you create less-restricted PAT with repo and read:org scopes (that should thus have access to all of one's repos) and store that value as a codespace secret called GH_TOKEN, thereby exposing it in the codespace as an environment variable, gh appears to use GITHUB_TOKEN when trying to clone those other repos, even though it reports being logged in with GH_TOKEN, per the logs below.

Steps to reproduce the behavior

  1. Create a private repo with just a README.md.
  2. Create a public repo with just a README.md.
  3. Create a PAT with repo and read:org scope.
  4. Store PAT as a codespace secret called GH_TOKEN in public repo.
  5. Create a codespace for public repo.
  6. In codespace, execute
    gh auth status
    
    which should confirm that GH_TOKEN is in use.
  7. In codespace, execute
    gh repo clone https://github.com/{owner}/{repo}.git
    
    where {owner}/{repo} is the private repo, which should fail.
  8. In codespace, execute:
    export GITHUB_TOKEN=$GH_TOKEN
    
  9. In codespace, execute
    gh repo clone https://github.com/{owner}/{repo}.git
    
    which should succeed.

Expected vs actual behavior

Clone should succeed using GH_TOKEN alone, without modifying GITHUB_TOKEN.

Logs

# Confirm that gh is using GH_TOKEN
@dmalan ➜ /workspaces/public (main) $ GH_DEBUG=1 gh auth status
* Request at 2022-09-11 16:40:03.732200409 +0000 UTC m=+0.015436667
* Request to https://api.github.com/
* Request took 54.412286ms
* Request at 2022-09-11 16:40:03.786785597 +0000 UTC m=+0.070021755
* Request to https://api.github.com/graphql
* Request took 100.683283ms
github.com
  ✓ Logged in to github.com as dmalan (GH_TOKEN)
  ✓ Git operations for github.com configured to use https protocol.
  ✓ Token: *******************

# Try to clone with gh
@dmalan ➜ /workspaces/public (main) $ GH_DEBUG=1 gh repo clone https://github.com/dmalan/private.git
* Request at 2022-09-11 16:42:36.891859527 +0000 UTC m=+0.014884457
* Request to https://api.github.com/graphql
* Request took 166.385245ms
[git clone https://github.com/dmalan/private.git]
Cloning into 'private'...
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/dmalan/private.git/': The requested URL returned error: 403
exit status 128

# Try to clone with git
@dmalan ➜ /workspaces/public (main) $ GH_DEBUG=1 git clone https://github.com/dmalan/private.git
Cloning into 'private'...
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/dmalan/private.git/': The requested URL returned error: 403

# Overwrite GITHUB_TOKEN with GH_TOKEN
@dmalan ➜ /workspaces/public (main ✗) $ export GITHUB_TOKEN=$GH_TOKEN

# Clone with gh
@dmalan ➜ /workspaces/public (main) $ GH_DEBUG=1 gh repo clone https://github.com/dmalan/private.git
* Request at 2022-09-11 16:43:30.332738503 +0000 UTC m=+0.015444697
* Request to https://api.github.com/graphql
* Request took 169.280648ms
[git clone https://github.com/dmalan/private.git]
Cloning into 'private'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

# Remove clone
@dmalan ➜ /workspaces/public (main ✗) $ rm -rf private/

# Re-clone with git
@dmalan ➜ /workspaces/public (main) $ GH_DEBUG=1 git clone https://github.com/dmalan/private.git
Cloning into 'private'...
remote: Enumerating objects: 3, done.
remote: Counting objects: 100% (3/3), done.
remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
Receiving objects: 100% (3/3), done.

CC @aprilla @rongxin-liu

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingdiscussFeature changes that require discussion primarily among the GitHub CLI team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions