Repository navigation
Description
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
- Set up two accounts on one registry. Account A can only pull
ns-a/*, and account B can only pushns-b/*(Harbor robot accounts work well for this, as inoras copycan not handle separate authentication for same registry #1892). oras cp <reg>/ns-a/img:tag <reg>/ns-b/img:tag --from-username A --from-password … --to-username B --to-password …- Run it with
--debug. Requests for the destination reuse the token cached for the source, or the other way round.
Proposed fix
- Remove the
sharedClientglobal andSet/Get/ClearSharedClient. - Scope the cache per command invocation and per credential identity, for example a map keyed by
(host, credential fingerprint)owned byoption.Commonor 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
FromandToclients 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.
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--tocredential 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.goand its test.cp,backupandrestoreall go through it. - Risk: low.
Set/Get/ClearSharedClientare 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
- Delete
sharedClient, its lock, andSet/Get/ClearSharedClient(remote.go:49-79). - Add an invocation-scoped, mutex-guarded
map[identityKey]auth.Cache, owned byoption.Commonor placed in the command context, soFromandTocan 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-configfiles separate.
- static creds:
authClientlooks up or creates the cache by that key, instead of reusing the global.- Mirror the fix in feat: migrate from oras-go v2 to v3 #2002 with a
ClientBuilder.CacheFactorykeyed by the same identity.
Edge cases
- Same host and same creds on both sides: should share the cache.
- One side anonymous.
--registry-configdiffering between--fromand--to.
Tests to add
From/Toon 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
cpsends the--tocredentials 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 inauthClient.cmd/oras/internal/option/remote.go:504:Remote.Credential()(flag-only).cmd/oras/root/cp.go:829-849:getMountPointcredential check, which the shared cache undermines.
Activity