DEV Community

kozhevniko
kozhevniko

Posted on

WWW-Authenticate challenge order is security policy

WWW-Authenticate challenge order is security policy

An HTTP 401 response tells a client that authentication is required. The WWW-Authenticate header tells it which schemes the server accepts. That header often contains more than one challenge. The client chooses among them, and the server should not assume the choice is the strongest option it advertised.

RFC 9110 requires an origin server to send at least one challenge in a 401 response. The field can appear multiple times and can contain multiple comma-separated challenges. Parsing is not trivial, and the standard notes that some clients fail when they encounter an unknown scheme. A compatibility workaround is to list a widely supported scheme first. That workaround can conflict with a security policy that wants a stronger scheme selected whenever possible.

The practical answer is to separate compatibility from exposure. If Basic authentication is supported only for a legacy client, scope it to a separate listener or path with a documented owner and a removal date. Keep Basic off the public listener. Do not advertise it broadly just because one old client fails to parse a Bearer challenge.

Credential confidentiality depends on the scheme and transport. Basic authentication encodes the user name and password with a reversible encoding. TLS protects the connection, but the scheme itself does not provide a proof that survives a leaked credential. Bearer tokens have a different failure mode: possession is authority, so leakage and replay are central risks. Advertise the scheme that matches the credential model, and make the protected space obvious through a precise realm.

Multiple realms can make the challenge harder to reason about. A user may be prompted more than once, or a client may cache credentials for the wrong scope. Use stable, distinct realms for distinct protection spaces. Do not use a realm as a secret. It is visible to the client and should describe the resource, not reveal internal hostnames or account structure.

Proxy behavior also matters. RFC 9110 says a proxy forwarding a response must not modify WWW-Authenticate. A reverse proxy that rewrites challenges can break client negotiation or make a stronger origin scheme look like Basic to the browser. Test the actual response through every ingress path. A browser developer tool is useful here because it shows the final scheme list, status code, and redirect chain that the application sees.

Log authentication failures with the selected scheme, not just the user name. A spike in Basic attempts against an endpoint that normally uses Bearer is worth investigating. Rate-limit repeated failures and avoid distinguishing unknown users from wrong credentials. For high-value actions, require a fresh authentication event rather than accepting an old session.

The challenge is part of the protocol contract. Keep it small, deliberate, and consistent with the credential and transport controls behind it.

References

Top comments (0)