Skip to content

fix(auth): Digest authentication replay protection (stateless signed nonces) - #504

Merged
mathieucarbou merged 1 commit into
mainfrom
fix/digest_auth
Nov 12, 2026
Merged

mathieucarbou merged 1 commit into
mainfrom
fix/digest_auth

Conversation

@mathieucarbou

@mathieucarbou mathieucarbou commented Nov 4, 2026 •

Copy link
Copy Markdown
Member

Digest challenges now include server-side anti-replay protections, with no server-side per-nonce storage (server stays stateless):

  • the challenge nonce embeds its issuing time and is signed with a per-instance secret (HMAC-MD5): forged, foreign or expired nonces are rejected; expiry issues a challenge with stale=true (RFC 7616 sec. 3.3) so compliant clients re-authenticate silently;
  • the opaque is derived from the instance secret and identifies the server;
  • the header uri field is bound to the effective request URI (RFC 7230 sec. 5.5 raw request-target, path + query), taken before decoding and rewrites: a captured header cannot be replayed on another endpoint nor with a different query string.

AsyncWebServerRequest::authenticateDigest() keeps its signature: NULL nonce/opaque/uri arguments mean automatic (library) validation, values take precedence. authenticate() defaults to the full stateless check.

New request API: rawUri() (effective request URI), authStale().
New server API: setDigestNonceTimeout(), setDigestSecret().
New build flag: ASYNCWEBSERVER_DIGEST_NONCE_TTL (default 60s).

Consolidated hex digit tables in literals.h (T_hex_chars, lowercase).

Example: examples/arduino/Auth gains endpoints for digest-with-hash and test_digest.sh which verifies replay protection live.

test_digest.sh can be used to verify the fix and launch tests

@mathieucarbou
mathieucarbou force-pushed the fix/digest_auth branch 2 times, most recently from 6480fb9 to f054c44 Compare November 5, 2026 10:37

@willmmiles willmmiles left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR comes at a substantial RAM cost for all clients, whether or not they use the digest feature. I think we should avoid penalizing the normal case (no authentication) to support a less-common feature.

Comment thread examples/arduino/Auth/test_digest.sh Resolved
Comment thread src/ESPAsyncWebServer.h Resolved
Comment thread src/ESPAsyncWebServer.h Resolved
mathieucarbou added a commit that referenced this pull request Nov 5, 2026
The per-instance digest secret, nonce TTL and opaque String were always
allocated, costing RAM for every user even without Digest auth. Move them
into a DigestState struct behind a single pointer, initialized on first
use from _digestState() (no lock/atomic: first use happens from setup()
or the async_tcp task).

- setDigestSecret() derives the opaque before applying the secret: on
  allocation failure the previous secret/opaque pair stays intact, and
  the first-use path avoids the double secret/opaque initialization.
- On init failure the state is discarded so the next use retries.
- digestOpaque() returns an empty String if Digest auth was never used.

Closes review: #504 (discussion r4483301272)

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Comment thread examples/arduino/Auth/test_digest.sh Resolved
@mathieucarbou
mathieucarbou marked this pull request as draft November 6, 2026 15:27
@mathieucarbou
mathieucarbou force-pushed the fix/digest_auth branch 2 times, most recently from 510b3a1 to 115e054 Compare November 6, 2026 16:00
@mathieucarbou
mathieucarbou marked this pull request as ready for review November 6, 2026 16:02
@mathieucarbou
mathieucarbou marked this pull request as draft November 10, 2026 09:08
@mathieucarbou

Copy link
Copy Markdown
Member Author

@willmmiles : I have updated the PR as discussed in Discord and rebased it. I have also simplified a little the code and also limited the exposed public API to facilitate further rework.

@mathieucarbou
mathieucarbou marked this pull request as ready for review November 10, 2026 10:11
@mathieucarbou
mathieucarbou force-pushed the fix/digest_auth branch 3 times, most recently from 16798ed to 9e78e4a Compare November 11, 2026 23:45

@willmmiles willmmiles left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some nitpicks - nothing blocking.

Comment thread src/ESPAsyncWebServer.h Resolved
Comment thread src/ESPAsyncWebServer.h Resolved
Comment thread src/WebAuthentication.h
// Extracts one parameter value from a Digest header (e.g. getDigestParam(header, "nonce", out)).
// Quote-aware: values in quoted form are returned without their quotes.
// Returns false when the parameter is absent; an empty value yields an empty out string.
bool getDigestParam(const char *header, const char *name, String &out);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wish we could return std::optional<std::string_view>. I still advise avoiding output parameters - it's particularly bad in this case since we always end up zeroing the String twice (once in the default constructor, once immediately after the function starts).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wish we could return std::optional<std::string_view>. I still advise avoiding output parameters - it's particularly bad in this case since we always end up zeroing the String twice (once in the default constructor, once immediately after the function starts).

there are several states outputted. One way would be to use optional also but I don't know if we can use -std=gnu++17 for all platforms.

I will leave it as-is for now.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We cannot - we're stuck with C++11 as floor support for the foreseeble future, I think. Hence the wish. ;) I'm very tempted to consider including polyfills for useful newer C++ features in that bad_alloc-safe containers library.

…nonces)

Digest challenges now include server-side anti-replay protections, with no
server-side per-nonce storage (server stays stateless):

- the challenge nonce embeds its issuing time and is signed with a
  per-instance secret (HMAC-MD5): forged, foreign or expired nonces are
  rejected; expiry issues a challenge with stale=true (RFC 7616 sec. 3.3)
  so compliant clients re-authenticate silently;
- the opaque is derived from the instance secret and identifies the
  server;
- the header uri field is bound to the effective request URI
  (RFC 7230 sec. 5.5 raw request-target, path + query), taken before
  decoding and rewrites: a captured header cannot be replayed on another
  endpoint nor with a different query string.

AsyncWebServerRequest::authenticateDigest() keeps its signature: NULL
nonce/opaque/uri arguments mean automatic (library) validation, values
take precedence. authenticate() defaults to the full stateless check.

New request API: rawUri() (effective request URI), authStale().
New server API: setDigestNonceTimeout(), setDigestSecret().
New build flag: ASYNCWEBSERVER_DIGEST_NONCE_TTL (default 60s).

Consolidated hex digit tables in literals.h (T_hex_chars, lowercase).

Example: examples/arduino/Auth gains endpoints for digest-with-hash and
test_digest.sh which verifies replay protection live.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@mathieucarbou
mathieucarbou merged commit d889f0b into main Nov 12, 2026
37 checks passed
@mathieucarbou
mathieucarbou deleted the fix/digest_auth branch November 12, 2026 10:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants