Skip to content

feat(auth): add basic HTTP authentication - #1210

Closed
FlorentinDUBOIS wants to merge 3 commits into
feat/url-rewritefrom
feat/basic-auth
Closed

FlorentinDUBOIS wants to merge 3 commits into
feat/url-rewritefrom
feat/basic-auth

Conversation

@FlorentinDUBOIS

Copy link
Copy Markdown
Collaborator

Summary

  • Basic HTTP authentication with SHA256 password hashes
  • WWW-Authenticate challenge response (401) with configurable realm
  • Per-cluster authorized_hashes and www_authenticate configuration
  • Authorization header extraction and validation in editor.rs
  • required_auth wired through Frontend -> RouteResult -> auth check flow

Changes

  • command.proto: authorized_hashes (field 10) and www_authenticate (field 11) on Cluster
  • config.rs: authorized_hashes and www_authenticate on FileClusterConfig and HttpClusterConfig
  • editor.rs: Extract Authorization: Basic header, decode base64, SHA256 hash password, store as authorization_found
  • answers.rs: WWW_AUTHENTICATE template variable for 401 responses (header-only, elide if empty)
  • mod.rs: Auth check in cluster_id_from_request - compare hash against authorized_hashes
  • router/mod.rs: required_auth field on Frontend and RouteResult
  • e2e: Auth test covering no-auth 401, wrong-credentials 401, correct-credentials 200

Depends on

Test plan

  • Existing e2e tests pass (126 pass, 9 ignored)
  • Auth e2e test: 401 without credentials
  • Auth e2e test: 401 with wrong credentials
  • Auth e2e test: 200 with correct credentials
  • cargo build --workspace -- 0 errors
  • cargo clippy --workspace -- no new warnings
  • cargo +nightly fmt -- clean

Add basic HTTP authentication with SHA256 password hashes:
- Extract and verify Authorization header in editor.rs
- SHA256 hash comparison against per-cluster authorized_hashes
- WWW-Authenticate challenge response (401) with configurable realm
- New proto fields: authorized_hashes, www_authenticate on Cluster
- Route required_auth through Frontend -> RouteResult -> auth check
- E2e test covering: no-auth 401, wrong-credentials 401, correct 200

Signed-off-by: Florentin Dubois <[email protected]>
Vec::contains uses PartialEq with early exit, which leaks timing
information about the hash value. Replace with subtle::ConstantTimeEq
to prevent timing side-channel attacks on basic auth credentials.

Signed-off-by: Florentin Dubois <[email protected]>
The sozu-lib crate depends on subtle (workspace dependency) for
constant-time credential comparison, but Cargo.lock was missing
the subtle entry in sozu-lib's dependency list. This caused
`cargo vendor --locked` to fail in the Docker build CI job.

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Apr 28, 2026
…chema

Lay the wire schema for the bundled feature work that brings PR #1206..#1210
onto current main. Wave 1 of a multi-wave implementation; subsequent waves
land the runtime engine, mux integration, auth helper, e2e tests, and docs.

Cluster gains four fields: per-cluster `answers` (status code → template body)
at field 9, `https_redirect_port` at 10, `authorized_hashes` at 11, and
`www_authenticate` realm at 12. RequestHttpFrontend gains eight: `redirect`
policy at 8, `required_auth` at 9, `redirect_scheme` at 10, `redirect_template`
at 11, `rewrite_host`/`rewrite_path`/`rewrite_port` at 12-14, and a repeated
`headers` list at 15. Listener configs gain a parallel `answers` map at 31
(HttpListenerConfig) and 43 (HttpsListenerConfig); the legacy
`CustomHttpAnswers http_answers` field is preserved on the wire so existing
state files round-trip — the runtime will read both for one minor.

New top-level enums RedirectPolicy / RedirectScheme / HeaderPosition and a
Header message land alongside. An empty `Header.val` deletes the named header
(HAProxy `del-header` parity).

prost stops auto-deriving Hash/Eq once a message holds a map field, so the
build script gains explicit `#[derive(Hash, Eq)]` for Cluster,
HttpListenerConfig, HttpsListenerConfig, UpdateHttpListenerConfig, and
UpdateHttpsListenerConfig. Existing initializers grow a `..Default::default()`
fallthrough so this commit is a pure additive scaffolding step — no runtime
behaviour changes yet.

Signed-off-by: Florentin Dubois <[email protected]>
FlorentinDUBOIS added a commit that referenced this pull request Apr 28, 2026
Introduce the constant-time comparison primitive that Wave 3a will plug
into `apply_route_decision`. The new module owns three small surfaces:

- `extract_authorization_header` walks the front kawa once, case-
  insensitively matches `Authorization`, and returns the raw value bytes.
  Handles elided headers gracefully and never panics on hostile input.
- `canonicalize_basic_credentials` decodes a `Basic <token>` value,
  base64-decodes the token, splits on the first `:`, hashes the password
  with SHA-256, and emits the canonical `<user>:<hex(sha256)>` form that
  matches every entry in `Cluster.authorized_hashes`. Caps the decoded
  payload at 4 KiB so a hostile peer can't force the worker to allocate
  unbounded transient buffers per failed attempt.
- `check_authorized_hashes` compares the canonical form against every
  entry in the cluster's hash list using `subtle::ConstantTimeEq`,
  iterating the entire slice on every call (no early exit) so the time
  spent does not leak which entry matched or whether any did at all.
  Mitigates timing side-channel attacks on the credential boundary
  (CWE-208 family, specifically the credential-comparison flavour PR
  #1210's review fix `171a31ce` was hardening).

`check_basic` is the convenience entry point the mux will call:
header → canonicalise → constant-time compare. Empty cluster lists
short-circuit to `false` so a frontend with `required_auth = true` on
a cluster with no `authorized_hashes` reliably returns 401.

Workspace gains `base64 = ^0.22.1` and `subtle = ^2.6.1`; only `sozu-lib`
pulls them in, so the command-lib and e2e crates stay unaware. `Cargo.lock`
refresh is included so `cargo vendor --locked` keeps the Docker build
green.

10 unit tests cover round-trip canonicalisation, scheme case-insensitivity,
non-Basic schemes, malformed base64, missing colon, oversized payload,
matching at any list position, wrong-password rejection, and empty list.

Signed-off-by: Florentin Dubois <[email protected]>
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
…chema

Lay the wire schema for the bundled feature work that brings PR sozu-proxy#1206..sozu-proxy#1210
onto current main. Wave 1 of a multi-wave implementation; subsequent waves
land the runtime engine, mux integration, auth helper, e2e tests, and docs.

Cluster gains four fields: per-cluster `answers` (status code → template body)
at field 9, `https_redirect_port` at 10, `authorized_hashes` at 11, and
`www_authenticate` realm at 12. RequestHttpFrontend gains eight: `redirect`
policy at 8, `required_auth` at 9, `redirect_scheme` at 10, `redirect_template`
at 11, `rewrite_host`/`rewrite_path`/`rewrite_port` at 12-14, and a repeated
`headers` list at 15. Listener configs gain a parallel `answers` map at 31
(HttpListenerConfig) and 43 (HttpsListenerConfig); the legacy
`CustomHttpAnswers http_answers` field is preserved on the wire so existing
state files round-trip — the runtime will read both for one minor.

New top-level enums RedirectPolicy / RedirectScheme / HeaderPosition and a
Header message land alongside. An empty `Header.val` deletes the named header
(HAProxy `del-header` parity).

prost stops auto-deriving Hash/Eq once a message holds a map field, so the
build script gains explicit `#[derive(Hash, Eq)]` for Cluster,
HttpListenerConfig, HttpsListenerConfig, UpdateHttpListenerConfig, and
UpdateHttpsListenerConfig. Existing initializers grow a `..Default::default()`
fallthrough so this commit is a pure additive scaffolding step — no runtime
behaviour changes yet.

Signed-off-by: Florentin Dubois <[email protected]>
jrcribb pushed a commit to jrcribb/sozu that referenced this pull request Aug 10, 2026
Introduce the constant-time comparison primitive that Wave 3a will plug
into `apply_route_decision`. The new module owns three small surfaces:

- `extract_authorization_header` walks the front kawa once, case-
  insensitively matches `Authorization`, and returns the raw value bytes.
  Handles elided headers gracefully and never panics on hostile input.
- `canonicalize_basic_credentials` decodes a `Basic <token>` value,
  base64-decodes the token, splits on the first `:`, hashes the password
  with SHA-256, and emits the canonical `<user>:<hex(sha256)>` form that
  matches every entry in `Cluster.authorized_hashes`. Caps the decoded
  payload at 4 KiB so a hostile peer can't force the worker to allocate
  unbounded transient buffers per failed attempt.
- `check_authorized_hashes` compares the canonical form against every
  entry in the cluster's hash list using `subtle::ConstantTimeEq`,
  iterating the entire slice on every call (no early exit) so the time
  spent does not leak which entry matched or whether any did at all.
  Mitigates timing side-channel attacks on the credential boundary
  (CWE-208 family, specifically the credential-comparison flavour PR
  sozu-proxy#1210's review fix `171a31ce` was hardening).

`check_basic` is the convenience entry point the mux will call:
header → canonicalise → constant-time compare. Empty cluster lists
short-circuit to `false` so a frontend with `required_auth = true` on
a cluster with no `authorized_hashes` reliably returns 401.

Workspace gains `base64 = ^0.22.1` and `subtle = ^2.6.1`; only `sozu-lib`
pulls them in, so the command-lib and e2e crates stay unaware. `Cargo.lock`
refresh is included so `cargo vendor --locked` keeps the Docker build
green.

10 unit tests cover round-trip canonicalisation, scheme case-insensitivity,
non-Basic schemes, malformed base64, missing colon, oversized payload,
matching at any list position, wrong-password rejection, and empty list.

Signed-off-by: Florentin Dubois <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants