Use constant-time comparison for admin basic auth credentials#528
Open
ChristianPavilonis wants to merge 4 commits intomainfrom
Open
Use constant-time comparison for admin basic auth credentials#528ChristianPavilonis wants to merge 4 commits intomainfrom
ChristianPavilonis wants to merge 4 commits intomainfrom
Conversation
aram356
requested changes
Mar 20, 2026
Collaborator
aram356
left a comment
There was a problem hiding this comment.
@ChristianPavilonis Please resolve conflicts
Replace standard string equality with subtle::ConstantTimeEq for username and password checks in enforce_basic_auth. This prevents timing side-channel attacks that could leak credential prefix information through response latency differences. Closes #447
aa6560d to
6de0972
Compare
aram356
approved these changes
Mar 20, 2026
Collaborator
aram356
left a comment
There was a problem hiding this comment.
Summary
Solid security hardening — SHA-256 + ct_eq + bitwise AND is the right approach to close timing side-channels on credential length, prefix, and username existence. One non-blocking concern about the Redacted::PartialEq impls.
Non-blocking
🤔 thinking
Redacted::PartialEqstill uses standard==— accidental misuse risk: The commit history shows a TODO warning was added in commit 2 then removed in commit 3. Whileauth.rsnow correctly bypassesPartialEqvia.expose()+ SHA-256 +ct_eq, a future contributor could writehandler.username == inputand reintroduce the timing vulnerability. Consider keeping the TODO comment or adding a doc comment onRedacted::PartialEqnoting it is NOT constant-time and should not be used for security-sensitive comparisons. (crates/trusted-server-core/src/redacted.rs:65)
🌱 seedling
- Pre-compute expected credential hashes at settings load time: Currently SHA-256 digests of expected credentials are computed on every request. For admin endpoints this is negligible, but if constant-time auth is ever added to higher-traffic paths, pre-computing hashes in
Handlerwould avoid redundant work. Not for this PR.
⛏ nitpick
- PR checklist references
tracingbut project useslog: The checklist item says "Usestracingmacros (notprintln!)" but CLAUDE.md specifieslogcrate macros. The code correctly useslog::warn!— just a template inaccuracy.
CI Status
- fmt: PASS
- clippy: PASS
- rust tests: PASS
- js tests: PASS
- integration tests: PASS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
==string equality withsubtle::ConstantTimeEqfor username and password checks inenforce_basic_auth, preventing timing side-channel attacks that could leak credential prefix informationct_eqon raw byte slices short-circuits when lengths differ, which would leak credential lengthChoice) regardless of where the first mismatch occursChanges
Cargo.tomlsubtle = "2.6"to workspace dependencies (already a transitive dep viasha2/digest,ed25519-dalek,chacha20poly1305, etc. — no new crate downloaded, no binary size change)crates/common/Cargo.tomlsubtle = { workspace = true }crates/common/src/auth.rs==comparison with SHA-256 +ConstantTimeEq; use bitwise&onChoiceto prevent early exit; add partial-match tests; switch to sharedcreate_test_settings()helpercrates/common/src/redacted.rsCloses
Closes #447
Test plan
cargo test --workspacecargo clippy --all-targets --all-features -- -D warningscargo fmt --all -- --checkcd crates/js/lib && npx vitest runcd crates/js/lib && npm run formatcd docs && npm run formatcargo build --bin trusted-server-fastly --release --target wasm32-wasip1fastly compute serveChecklist
unwrap()in production code — useexpect("should ...")tracingmacros (notprintln!)