WebServer: digest auth keeps one server-wide nonce, so any second client (or any unauthenticated request) logs everyone out; no stale=true

Author: questkingCreated Sep 19, 2026Updated Sep 24, 2026
LabelsArea: LibrariesIDE: Arduino IDE

Board

ESP32 Dev Module (ESP32-D0WD-V3 rev 3.1, WROOM-32E) — not board specific

Device Description

Plain dev board, nothing attached.

Hardware Configuration

Nothing connected.

Version

v3.3.7 (tested). The code in question is unchanged in v3.3.12 and master (libraries/WebServer/src/WebServer.cpp, requestAuthentication() / authenticate()).

Type

Bug

IDE Name

Arduino IDE / arduino-cli

Operating System

macOS

Flash frequency

80 MHz

PSRAM enabled

no

Upload speed

460800

Description

WebServer keeps one digest nonce/opaque pair for the whole server (_snonce, _sopaque), and requestAuthentication(DIGEST_AUTH, ...) replaces it on every 401 it sends — to anybody. authenticate() then only accepts that single latest nonce, and a request with an older nonce is answered with a plain 401 (no stale=true).

Consequences:

  1. Any second client logs the first one out. Two browsers (PC + phone), a browser plus a script, or even a single unauthenticated request from anyone on the network (every curl --digest / python-requests call starts with one) invalidates the nonce every logged-in browser holds. Their next request gets 401 → because it is not marked stale=true the browser treats it as "wrong password" and shows the login dialog again.
  2. The login dialog can become impossible to satisfy. If the page polls in the background (fetch()/XHR every few seconds, very common for device UIs), each of those requests is rejected too and each rejection replaces the nonce again. The response the browser computes from the password the user just typed is for a nonce that has already been replaced → 401 → dialog again, in a loop. Users report "it keeps asking for the password although it is correct; opening a new tab works".
  3. An unauthenticated attacker on the LAN can keep every user logged out by just requesting a protected URL in a loop (cheap DoS of the UI).

This is what our users hit on a device UI that polls a status endpoint; we traced it to the library and confirmed it with the minimal sketch below: with two logged-in clients, 20 of 20 requests carrying valid credentials are rejected.

Expected: several clients can be logged in at the same time; a valid response for an outdated nonce is answered with stale=true (RFC 7616 §3.3) so the browser retries silently instead of prompting.

Possible fix (what we ended up doing in our application instead of authenticate()/requestAuthentication()): stateless nonces, e.g. nonce = <issue time> "-" HMAC/SHA-256(secret made at boot, issue time). authenticate() recomputes the signature and checks the age instead of comparing against one stored string, so any number of clients hold valid nonces at once and no per-client state is needed. If the digest response is correct but the nonce is expired/unknown, send the new challenge with stale=true. While there: authenticate() also never compares the uri= parameter with the URI actually requested, so a captured Authorization header for one URL is accepted for any other URL (same method) while the nonce lives.

Sketch

cpp
#include <WiFi.h>
#include <WebServer.h>

WebServer server(80);

void setup() {
  Serial.begin(115200);
  WiFi.persistent(false);
  WiFi.begin("your-ssid", "your-password");
  while (WiFi.status() != WL_CONNECTED) delay(200);
  Serial.println(WiFi.localIP());
  server.on("/", []() {
    if (!server.authenticate("admin", "esp32")) return server.requestAuthentication(DIGEST_AUTH, "test", "Login required");
    server.send(200, "text/plain", "ok");
  });
  server.begin();
}

void loop() { server.handleClient(); delay(2); }

Debug Message

$ python digest_repro.py <esp32-ip>
requests with valid credentials rejected with 401 (no stale=true): 20 of 20
two clients only, alternating: 20 of 20 rejected

Other Steps to Reproduce

digest_repro.py (two sessions that keep their nonce, like two browsers, plus one request without credentials):

python
# pip install requests ; python digest_repro.py <esp32-ip>
import sys, requests
from requests.auth import HTTPDigestAuth
url = "http://%s/" % sys.argv[1]
def client():
    s = requests.Session(); s.auth = HTTPDigestAuth("admin", "esp32"); return s
A, B = client(), client()          # two logged-in clients (think: two browser tabs / a PC and a phone)
assert A.get(url).status_code == 200 and B.get(url).status_code == 200
rejected = total = 0
for i in range(10):
    requests.get(url)              # anybody without credentials -> 401 -> the server makes a new nonce
    for c in (A, B):
        r = c.get(url); total += 1
        # a request that carried VALID credentials and still got 401 without stale=true
        # is what makes a browser pop up the login dialog again
        for h in list(r.history) + [r]:
            if h.status_code == 401 and "Authorization" in h.request.headers and "stale" not in h.headers.get("WWW-Authenticate", ""):
                rejected += 1
print("requests with valid credentials rejected with 401 (no stale=true): %d of %d" % (rejected, total))
# without the stranger: two clients alone also knock each other out
rejected = 0
for i in range(10):
    for c in (A, B):
        r = c.get(url)
        rejected += sum(1 for h in list(r.history) + [r] if h.status_code == 401 and "Authorization" in h.request.headers)
print("two clients only, alternating: %d of 20 rejected" % rejected)

By hand: open http://<ip>/ in a browser and log in; run curl http://<ip>/ from another machine; reload the browser tab → login dialog. With a page that polls in the background the dialog then keeps re-appearing even with the right password.

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.