4 Anti Replay Protection
vBlackOut edited this page 2026-08-22 18:47:35 +02:00

Anti-Replay Protection (plugin_anti_replay)

This page explains ProxyAuth's request-signing anti-replay plugin — how a client proves a specific request really came from it, at the exact moment it claims, and can't be replayed later. It's a separate mechanism from session cookies/CSRF (see CSRF Protection): where CSRF protects a browser session against a third-party site forging a request, anti-replay protects any client — browser extension, script, service — by requiring every request to be individually signed with a per-account secret.

Anti-replay protection only applies when plugin_anti_replay is true in config.json and the matched route has need_anti_replay: true (the default) in routes.yml. If either is off, none of this runs.


Enabling it

{
  "plugin_anti_replay": true,
  "anti_replay_window_secs": 60
}
Key Type Default Description
plugin_anti_replay boolean false Enables signature verification on proxied requests. Off by default — fully opt-in, existing deployments are unaffected until this and at least one account with a signing secret exist.
anti_replay_window_secs integer (seconds) 60 How far a request's X-Timestamp may drift from the server's clock, in either direction, before being rejected. Also doubles as the nonce-replay window — see Nonce store & replay window below.

Per-route, in routes.yml:

routes:
  - prefix: /api
    target: http://backend:3000
    need_anti_replay: true
Key Type Default Description
need_anti_replay boolean true Per-route override, mirroring need_csrf's shape. Only has any effect when the server-wide plugin_anti_replay is also true. Set to false on routes that shouldn't require a signature (e.g. a health check, or a route with no signing clients).

How signing works

A signing client attaches four headers to each request:

X-User:      <ProxyAuth username>
X-Timestamp: <Unix time, milliseconds>
X-Nonce:     <random bytes, base64, unique per request>
X-Signature: <HMAC-SHA256, base64>

The signature covers a canonical string built from the request itself:

METHOD|scheme|host|port|path|query|timestamp|nonce|username|bodyHash
  • method — uppercase HTTP method (GET, POST, …).
  • scheme/host/port — as seen by the client, from the request's own URL/Host header, not the backend it's proxied to.
  • path — request path, no query string.
  • query — the query string including its leading ?, or an empty string if there is none.
  • timestamp/nonce — the same values sent as X-Timestamp/X-Nonce.
  • username — the same value sent as X-User.
  • bodyHash — base64 SHA-256 of the raw request body, or an empty string for a bodyless request.

All fields are joined with | and HMAC-SHA256'd with the account's signing secret. ProxyAuth reconstructs this exact string server-side from the incoming request and verifies the signature against the secret it has on file for that X-User.


Where the secret comes from

Unlike a manually-provisioned API key, there's nothing to register by hand. The signing secret lives directly on the account:

  • Every User has an anti_replay_secret field — always ciphertext at rest (encrypted with a key derived from config.secret), whether the account is defined in config.json or in a database-backed users table.
  • The first time an account is seen with plugin_anti_replay: true and no secret on file yet — at config load (file-based accounts), on the next database refresh cycle, or immediately via proxyauth db-add-user (database-backed accounts) — a secret is generated automatically.
  • It's emailed once to the account's on-file address, using the {{ secret_anti_replay }} placeholder in /etc/proxyauth/mail/templates/anti_replay_secret.txt (created automatically with a sensible default on first use, same convention as reset_password.txt — see config.json Reference § SMTP).
  • The plaintext secret is never stored anywhere past that email — if it's lost, clear anti_replay_secret for that account (config.json, or UPDATE users SET anti_replay_secret = NULL WHERE username = ...) so a new one is generated and mailed on the next load/refresh.
  • An account with no email on file gets a secret generated but not delivered — a warning is logged, and the secret has to be communicated out of band or the account given an email so it can be re-sent.

Because the secret is generated and mailed automatically, plugin_anti_replay needs smtp configured to be practically usable — see config.json Reference § SMTP.


Verification: what has to be true

For every request on a route with anti-replay enabled, ProxyAuth checks, in order:

  1. Headers present and well-formed — X-User, X-Timestamp, X-Nonce, X-Signature all present, non-empty, and X-Signature valid base64.
  2. Known signing account — X-User matches an account with a currently-decryptable anti_replay_secret on file.
  3. Timestamp within window — X-Timestamp is within anti_replay_window_secs of the server's own clock, in either direction.
  4. Signature matches — the canonical string is rebuilt from the actual request (method, URL, body) and compared, constant-time, against a fresh HMAC-SHA256 computed with the account's secret.
  5. Not already used — see Nonce store & replay window below.

Any failure returns 401 Unauthorized with a small JSON body identifying the reason:

Reason Meaning
missing_or_malformed_signature_headers One of the four headers is missing, empty, or not valid base64.
unknown_signing_client X-User doesn't match any account with a usable signing secret.
timestamp_out_of_window X-Timestamp is too far from the server's clock — check both clocks are in sync.
invalid_signature The HMAC doesn't match — wrong secret, or the request (method/URL/body) was modified after signing.
replayed_request This exact (username, nonce) pair was already consumed.

Nonce store & replay window

A valid signature alone isn't enough — a captured signed request could otherwise be sent again as-is. Every (username, nonce) pair is recorded the first time it's successfully verified:

  • Tracked in memory (a DashMap) and mirrored to a dedicated local LMDB store, so a restart doesn't reset the replay window to empty.
  • A nonce only needs to be remembered for as long as its timestamp could still pass the window check anyway — entries are purged automatically on an interval tied to anti_replay_window_secs.
  • Replaying a request with the same nonce and timestamp fails with replayed_request, even though the signature itself is still technically valid.

Like the CSRF nonce store, this is per-process and not synchronized across instances. In a multi-instance deployment without shared state, a captured request could in principle be replayed once against a different instance within the tolerance window — bounded by anti_replay_window_secs, same as the window itself. Sticky sessions at the load balancer avoid this; a shared nonce store across instances isn't currently implemented (unlike token revocation — see Token Revocation & Multi-Instance Sync).


Signing from a browser: the ProxyAuth extension

The reference implementation is the ProxyAuth browser extension (Firefox and Chrome), which signs outgoing requests to configured domains transparently:

  • The user configures, per domain: their ProxyAuth username, the domain/path to sign, and the secret received by email.
  • Signing happens automatically for matching HTTPS requests — nothing to do per-request.
  • The HMAC secret is encrypted at rest locally (AES-GCM, a device-local key generated once — no master password to manage) and never leaves the extension's background context.

Firefox (Manifest V2) intercepts at the network level (webRequest.onBeforeSendHeaders) and can sign any request type. Chrome (Manifest V3) — which removed synchronous network interception for consumer extensions — instead patches fetch()/XMLHttpRequest in the page's own context, relaying signing requests through an isolated bridge to a service worker that alone holds the secret. Practical consequence: on Chrome, only fetch()/XMLHttpRequest calls are signed, and cross-origin requests may trigger a CORS preflight that wasn't there before — make sure Access-Control-Allow-Headers includes X-User, X-Timestamp, X-Nonce, X-Signature on signed routes if your frontend and API are on different origins.


Summary

Property Value
Headers X-User, X-Timestamp, X-Nonce, X-Signature
Signature algorithm HMAC-SHA256, keyed by the account's anti_replay_secret
Comparison Constant-time
Secret storage Per-account (User.anti_replay_secret), ciphertext at rest, config.json/LMDB cache/database all supported
Secret provisioning Automatic on account creation/first sight, emailed once via {{ secret_anti_replay }}
Replay window anti_replay_window_secs (default 60s), enforced via timestamp check + nonce store
Nonce store persistence In-memory + local LMDB; not synced across instances
Per-route opt-out need_anti_replay: false in routes.yml
Default state Off (plugin_anti_replay: false)

See also