Skip to content

bug: cp/backup/restore share one auth cache across clients with different credentials #2276

Closed

Description

@TerryHowe

What is the problem?

cmd/oras/internal/option/remote.go keeps a package-global sharedClient (lines 49-79). In authClient (lines ~431-470), the first *auth.Client built becomes the "shared" client, and every later client reuses its auth.Cache, whatever credentials that later client was configured with.

auth.Cache is keyed by registry host (and scope). It is not keyed by credential. So in one invocation of oras cp, oras backup or oras restore, where --from-* and --to-* point at the same registry with different credentials, the two clients share cached Basic tokens and bearer tokens. The destination client can end up authenticating with the source's token, or the other way round.

This also works against getMountPoint (cmd/oras/root/cp.go:842-845). That function deliberately avoids cross-repo mounts unless the from/to credentials match, but the shared cache mixes the credentials anyway. It is a likely contributor to #1892.

The global also makes tests order-dependent: remote_test.go has to call ClearSharedClient().

Steps to reproduce

  1. Set up two accounts on one registry. Account A can only pull ns-a/*, and account B can only push ns-b/* (Harbor robot accounts work well for this, as in oras copy can not handle separate authentication for same registry #1892).
  2. oras cp <reg>/ns-a/img:tag <reg>/ns-b/img:tag --from-username A --from-password … --to-username B --to-password …
  3. Run it with --debug. Requests for the destination reuse the token cached for the source, or the other way round.

Proposed fix

  • Remove the sharedClient global and Set/Get/ClearSharedClient.
  • Scope the cache per command invocation and per credential identity, for example a map keyed by (host, credential fingerprint) owned by option.Common or passed through the context. Clients that share a credential can still share a cache, which was the original intent of feat: save context between calls #1923.
  • Add a unit test that builds From and To clients for the same host with different static credentials and checks that the caches are distinct.

oras-go v3 note

This still applies on the v3 migration branch (#2002, remote.go:52-80). v3's auth.Cache is still keyed by registry/resource. v3's remote.ClientBuilder.CacheFactory func(registry string) auth.Cache makes the fix natural: the CLI should supply a factory keyed by registry plus credential, rather than one global cache.

Related: #1892, #1923, #2008


Evaluation

Confirmed. Introduced by #1923 (e5f2178b) and shipped in v1.3.4 and v1.3.5.

Why it causes wrong-identity requests

oras-go v2 auth.Client.Do reads a cached token before calling the client's own Credential func:

  • Basic tokens are cached per host with an empty key (registry/remote/auth/client.go:401). So the second client always sends the first client's Basic token.
  • Re-auth happens only on a 401 (client.go:419). A 403, which Harbor returns for a denied push, goes straight back to the caller, and the --to credential is never tried.
  • Bearer tokens are keyed by scope string. They collide only when the scopes overlap, for example the same repo on both sides or mount scopes.

Effort / risk

  • Effort: Small to Medium. The change is contained to cmd/oras/internal/option/remote.go and its test. cp, backup and restore all go through it.
  • Risk: low. Set/Get/ClearSharedClient are internal. A correct fix costs at most an extra token round-trip when the identities differ, and keeps same-credential sharing (the intent of feat: save context between calls #1923).
  • Tests: TestRemote_authClient_SharedCache (remote_test.go:161) currently asserts the buggy behaviour and needs to be rewritten.

Suggested approach

  1. Delete sharedClient, its lock, and Set/Get/ClearSharedClient (remote.go:49-79).
  2. Add an invocation-scoped, mutex-guarded map[identityKey]auth.Cache, owned by option.Common or placed in the command context, so From and To can both reach it. The cache is already host-keyed internally, so the identity alone is enough for the key:
    • static creds: (username, sha256(secret)). Hash the secret rather than keying on plaintext.
    • store creds: ("store", configPaths...). This keeps different --from-registry-config / --to-registry-config files separate.
  3. authClient looks up or creates the cache by that key, instead of reusing the global.
  4. Mirror the fix in feat: migrate from oras-go v2 to v3 #2002 with a ClientBuilder.CacheFactory keyed by the same identity.

Edge cases

  • Same host and same creds on both sides: should share the cache.
  • One side anonymous.
  • --registry-config differing between --from and --to.

Tests to add

  • From/To on the same host with different static creds: the caches are distinct.
  • Same creds: the cache is shared.
  • An httptest registry that 403s the wrong Basic user. It proves cp sends the --to credentials to the destination.

Related code

  • cmd/oras/internal/option/remote.go:49-79: the global and its accessors.
  • cmd/oras/internal/option/remote.go:431-470: cache reuse and first-client registration in authClient.
  • cmd/oras/internal/option/remote.go:504: Remote.Credential() (flag-only).
  • cmd/oras/root/cp.go:829-849: getMountPoint credential check, which the shared cache undermines.

Activity

  1. added
    bug
    help wanted
    on Oct 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bughelp wanted

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions