Repository navigation
test(auth): pin the order of the scope date and token expiry checks - #949
Conversation
crates/auth/src/tests.rs held 613 lines. Lines 293 to 613 move to a new file, crates/auth/src/scope_tests.rs, which lib.rs declares beside mod tests. They hold nine tests of the request timestamp and credential scope checks and of the order of the checks. The helpers that only these tests use move with them: provider_without_credentials, make_date_stale, SCOPE_DATE_MESSAGE, yesterday, signed_with_scope and refusal. The helpers that both files use stay in tests.rs and become pub(super): MockCredentialStore and its fields, make_headers_with_auth, SECRET, signed_headers and provider. rustfmt wraps the signature of signed_headers, which is now longer than 100 columns. No test body, assertion or name changes. The moved lines are byte-identical to lines 293 to 613 of the old tests.rs. Before and after the move, cargo test for extenddb-auth lists and passes the same 515 unit tests, nine of them now under scope_tests. tests.rs now has 296 lines and scope_tests.rs has 331.
DynamoDB compares the credential scope date before it checks whether a session token has expired. Measured on 2026-11-10 in us-east-1 with signed ListTables requests and temporary credentials, two sends per case and the same answer each time: - expired token, scope date of the X-Amz-Date day: 400 ExpiredTokenException, "The security token included in the request is expired" - expired token, scope date one day earlier: 400 InvalidSignatureException, "Date in Credential scope does not match YYYYMMDD from ISO-8601 version of date from HTTP. " (the message ends with a space) ExtendDB answers both cases the same way. authenticate() runs check_scope before lookup_credential, and the lookup checks the session expiry. The new test in scope_tests.rs checks that order for both cases. It reads the session through CachedCredentialStore, so the ExpiredTokenException type and message come from the expiry check in CachedCredentialStore::lookup_credential. The same request with a matching scope date authenticates through a CachedCredentialStore built the same way when the session ends an hour from now. The expiry is the only fault in the matching case. The measurement recorded the error type after the "#" only, so the test does not compare the prefix. signed_with_scope_and_token is signed_with_scope with an optional session token, sent and signed. It sorts the signed header list, so x-amz-security-token comes before x-amz-target, as in the measured requests. signed_with_scope calls it with no token, so its existing callers sign the same requests as before. credential() in tests.rs becomes pub(super) for the new test.
The SigV4 section of the security model manual says its steps run in the order listed. It listed the access key lookup before the X-Amz-Date check and the credential scope check. BuiltinAuthProvider runs validate_timestamp, then check_scope, then lookup_credential, and its doc comment gives that as the service's order. The list now follows the code. The X-Amz-Date check is step 3, the scope check is step 4 and the key lookup is step 5. The scope step names the date comparison that check_scope makes. The lookup step says that the lookup checks the session expiry and refuses an expired session at once with ExpiredTokenException, before the signature check. The storage layer checks the expiry on a cache miss, and CachedCredentialStore checks it on a cache hit. The lookup step also says that the lookup reads whether the key is active, and that step 9 applies that decision. Step 9 now refers to steps 5 and 6 for the deferred decisions. DynamoDB checks the scope date before the session expiry (measured on 2026-11-10 in us-east-1). With an expired session token, a scope date one day earlier answers InvalidSignatureException with the scope date message. The same token with a matching scope date answers ExpiredTokenException. The order of the X-Amz-Date, scope and key lookup checks was measured on 2026-10-23, as the comment above validate_timestamp records.
crates/auth/src/lib.rs keeps the test_support module from main (#1003) and the scope_tests module from this branch. crates/auth/src/tests.rs keeps the cache-backed provider and provider_over helpers from main, and the move of the scope and check order tests to scope_tests.rs from this branch. Main's change to provider_without_credentials, in a moved test helper, goes to scope_tests.rs with the move.
Main (#1003) removed the store type parameter and the region argument of BuiltinAuthProvider::new. The provider now takes a CachedCredentialStore, which holds the region, and CachedCredentialStore::new also takes a worker count. Main's tests build each provider through provider_over in tests.rs. provider_with_a_session_expiring_at now builds its provider through provider_over. The session still reaches the expiry check of CachedCredentialStore::lookup, which runs after check_scope in authenticate. That check refuses a session past expires_at with ExpiredTokenException, with or without a cached entry.
|
#1346 fixes #1297. It has the text of this PR for steps 3, 4, 5 and 9 of |
navareamol
left a comment
There was a problem hiding this comment.
I think the branch does not build against current main. After merging main (3534c9e), crates/auth/src/scope_tests.rs line 12 use axum::http::{HeaderMap, HeaderValue}; fails with E0433, because #1009 removed axum from crates/auth/Cargo.toml and tests.rs now imports from http. With use http::{HeaderMap, HeaderValue}; the merged tree passes 534 lib, 14 sigv4_differential and 3 doc tests here. Can you rebase on main and change that import?
The new test uses CachedCredentialStore::pass_through, which skips the cache. Can the description say that the expiry refusal in this test comes from the lookup after the loader returns the mock session, not from a cache hit?
The description says scope_tests.rs holds 429 lines and tests.rs 296 after the move. At the head they hold 419 and 304 after the merge of main, and "The three commits" is now five. Can you update those numbers?
I confirmed the new test fails when the lookup error is returned before the scope check (533 pass, 1 fails), and fails with the cache expiry check off (2 fail). I did not measure the live service rows in the table.
I will approve once the import is fixed on a rebased branch.
#1009 removed axum from the dependencies of extenddb-auth. After the merge of main, the lib tests stopped building at the axum import of HeaderMap and HeaderValue (E0433). scope_tests.rs now imports the two types from http, as tests.rs does on main.
|
Thanks for checking it against main. I merged main at 3534c9e (7ff08b7) and reproduced the E0433. d0c9d26 imports The description now says the test reads the session through |
navareamol
left a comment
There was a problem hiding this comment.
I approve. The import is fixed in scope_tests.rs, CI is 65 of 65 green on the rebased branch, and the description now names the pass_through store and the current line and commit counts.
Main is at 5252eda. The last merge of main was at 80490c6, and main has four commits since then: #864, #895, #929 and #949. They change CI workflows, the auth and fuzz crates, a PostgreSQL test and the security manual. None of them changes a file that this branch changes, so git merged them with no conflict.
Changes since the last review
Two commits sit on top of 8052d0d. 7ff08b7 merges
mainat 3534c9e. d0c9d26 importsHeaderMapandHeaderValueinscope_tests.rsfromhttp, because #1009 removedaxumfrom the dependencies ofextenddb-auth.What
The branch has seven commits, two of them merges of
main. None changes an answer that ExtendDB gives.crates/auth/src/tests.rsto the newcrates/auth/src/scope_tests.rs, with no change to a test. tests.rs keeps the shared helpers, nowpub(super). tests.rs held 613 lines before the move and 296 after it. After the merges ofmain, tests.rs holds 304 lines and scope_tests.rs 419.scope_date_is_compared_before_the_session_token_expiryto scope_tests.rs, which then held 429 lines. The test sends a temporary key with a signed session token. The store holds that session. The provider reads it throughCachedCredentialStore::pass_through, which skips the cache. So the expiry refusal comes from the lookup after the loader returns the session, not from a cache hit.docs/manuals/10-security-model.mdin the order ofcrates/auth/src/lib.rs: theX-Amz-Datecheck, then the credential scope check, then the key lookup. Step 4 now names the scope date, and step 5 names the session expiry refusal. Step 9 points at steps 5 and 6.Why
Follow-up to #355 and #512. #512 moved the scope date check ahead of the key lookup. As a result, an expired session token with a stale scope date gets the scope date refusal. No test pinned that order, and the security model listed the key lookup first.
Measured on Amazon DynamoDB in us-east-1 on 2026-11-10, two sends of each request, with temporary credentials:
X-Amz-DateX-Amz-DateThe order of the
X-Amz-Date, scope and key lookup checks rests on the 2026-10-23 measurements that the comment abovevalidate_timestampincrates/auth/src/lib.rsrecords.Merges of main
407632b54mergesmainata60c2b9f3(#1003).crates/auth/src/lib.rskeeps main'stest_supportmodule and this branch'sscope_testsmodule.crates/auth/src/tests.rskeeps main'sproviderandprovider_over. The block that this branch moves stays inscope_tests.rs, with main's change toprovider_without_credentials.The follow-up
8052d0d26builds the provider of the new test throughprovider_over, because #1003 changed the constructors ofBuiltinAuthProviderandCachedCredentialStore. On main the scope date check still runs before the session expiry check, whichCachedCredentialStore::lookupnow makes. At8052d0d26,cargo +1.97.0 test --locked -p extenddb-auth --libgives 534 passed.7ff08b757mergesmainat3534c9e4dwithout a conflict. #1009 removedaxumfromcrates/auth/Cargo.toml, so after that merge theaxumimport ofHeaderMapandHeaderValueinscope_tests.rsstopped the lib tests from building (E0433).d0c9d2644imports the two types fromhttp, astests.rsdoes on main.#1079 adds the path and the query string to
AuthProvider::authenticate. Whichever of #949 and #1079 merges second must adapt theauthenticatecalls inscope_tests.rsto those arguments.Testing done
At d0c9d26, after the merge of
mainat 3534c9e:cargo +1.97.0 test -p extenddb-auth: 534 unit tests, 14sigv4_differentialtests and 3 doc tests passed.cargo +1.97.0 clippy -p extenddb-auth --all-targets -- -D warningsandcargo +1.97.0 fmt -p extenddb-auth -- --check: clean.The runs below used the same three patches on bf5ea8f, before they moved onto main 89b9427. Main's commits between the two change neither
crates/authnor the manual. The merge of #1003 came after these runs.cargo +1.97.0 test --locked -p extenddb-auth: 516 unit tests, 14sigv4_differentialtests and 3 doc tests passed. The base and the move alone each give 515, 14 and 3.check_scope, 515 passed and 1 failed. The failure is the new test's stale scope case, which got ExpiredTokenException where it expects InvalidSignatureException.cached_session_past_expires_at_returns_expired_token.cargo +1.97.0 clippy --locked --all-targets -- -D warningswith the two feature sets of CI: no warnings.cargo +1.97.0 fmt --all -- --check: clean.#, because the probe recorded only that part. The prefix that the service sends is not measured.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, or public CLI surface, an RFC has been accepted or is linked
below. Otherwise, an ADR captures the decision (link below).
Only the auth crate ran locally, and CI runs the workspace tests. Clippy ran with
-D warnings, without the pedantic group.ADR / RFC: n/a
Breaking changes
None.
By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.