remotes/docker: requests are only re-authenticated on 401, so registries that deny with 403 fail permanently even when credentials are available
Description
I hit this through moby (https://github.com/moby/moby/issues/53340) pushing to JFrog Artifactory with the containerd image store, but I'm fairly confident the root cause lives here in core/remotes/docker.
Requests only get re-authenticated on 401. If a request goes out without an Authorization header and the registry just says 403, that's the end of it — retryRequest hands the response straight back, the authorizer never gets to see a challenge, and the push fails even though valid credentials are sitting right there.
What I think happens on Artifactory:
dockerAuthorizer.Authorizedoesn't attach anything until a handler exists, and handlers only come fromAddResponses, which only runs out ofretryRequeston a 401.- The push does
HEAD /v2/<repo>/blobs/<digest>first. Those succeed anonymously (the registry allows anonymous pulls), so nothing ever primes the authorizer. - The
POST /v2/<repo>/blobs/uploads/then goes out with no credentials, comes back 403 instead of a 401 challenge, and that's that. Artifactory's request log showsDENIED LOGIN client: NAfor the upload — the request literally carried no auth at all.
The error on the client side is just unexpected status: 403 Forbidden, which doesn't give you much to work with. Related: when the basic auth handler exists but was set up with empty creds it returns no basic auth credentials, which is another kind of confusing way of saying "we never found your credentials".
Repro
You need a registry that allows anonymous reads but answers the unauthenticated POST /v2/<repo>/blobs/uploads/ with 403 instead of a 401 challenge. Artifactory does this (some versions even send 403 with a WWW-Authenticate: Basic header, which feels like it's asking for auth that the client then never sends). Configure real credentials, push, done.
Expected
The upload should end up authenticated one way or another — either creds/challenge applied before the first request, or the 403 treated as "you should authenticate now" and retried once.
Actual
The upload POST goes out anonymous, dies with 403, and no request carrying credentials is ever made.
Versions
Looks unchanged to me between v2.2.6 (what Docker 29.7.1 vendors) and v2.4.0 — retryRequest still only looks at 401. Also, this doesn't seem Artifactory-specific to me; any registry or proxy that 403s pre-auth mutating requests should hit the same dead end. For what it's worth, the old Docker distribution client path never had this problem since it pinged /v2/ and attached creds preemptively.
Possible fix
How about retryRequest treating a 403 as retryable once (within the existing maxAttempts budget) when either the response has a WWW-Authenticate header, or the request went out without an Authorization header (so the failure is definitely pre-auth)? Then do the same AddResponses dance as for 401, and fall back to the current behavior if no creds come out of it. It can't loop since it's bounded, and it only touches requests that are already failing.
Alternatively (or additionally), prime the challenge from GET /v2/ for hosts that have creds configured before the first request, so the registry never sees an anonymous mutating request at all. Moby is doing roughly this as a workaround in https://github.com/moby/moby/pull/53784, but that doesn't help ctr/nerdctl/BuildKit and friends.
Source: containerd/containerd