Skip to content

test(auth): pin the order of the scope date and token expiry checks - #949

Merged
LeeroyHannigan merged 7 commits into
mainfrom
test/sigv4-scope-before-expired-token
Nov 25, 2026
Merged

LeeroyHannigan merged 7 commits into
mainfrom
test/sigv4-scope-before-expired-token

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 11, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

Two commits sit on top of 8052d0d. 7ff08b7 merges main at 3534c9e. d0c9d26 imports HeaderMap and HeaderValue in scope_tests.rs from http, because #1009 removed axum from the dependencies of extenddb-auth.

What

The branch has seven commits, two of them merges of main. None changes an answer that ExtendDB gives.

  • cd0ad41 moves the scope and check-order tests of crates/auth/src/tests.rs to the new crates/auth/src/scope_tests.rs, with no change to a test. tests.rs keeps the shared helpers, now pub(super). tests.rs held 613 lines before the move and 296 after it. After the merges of main, tests.rs holds 304 lines and scope_tests.rs 419.
  • ea5a90b adds scope_date_is_compared_before_the_session_token_expiry to 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 through CachedCredentialStore::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.
    • The session expired one minute ago, and the scope date is one day early: InvalidSignatureException with the scope date message.
    • The same session, with a matching scope date: ExpiredTokenException.
    • A session that expires in one hour, with a matching scope date: the request authenticates.
  • 101857a puts the SigV4 step list of docs/manuals/10-security-model.md in the order of crates/auth/src/lib.rs: the X-Amz-Date check, 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:

Session token Scope date Answer
valid the day of X-Amz-Date 200
valid one day earlier 400 InvalidSignatureException "Date in Credential scope does not match YYYYMMDD from ISO-8601 version of date from HTTP. "
expired the day of X-Amz-Date 400 ExpiredTokenException "The security token included in the request is expired"
expired one day earlier 400 InvalidSignatureException with the scope date message

The order of the X-Amz-Date, scope and key lookup checks rests on the 2026-10-23 measurements that the comment above validate_timestamp in crates/auth/src/lib.rs records.

Merges of main

407632b54 merges main at a60c2b9f3 (#1003). crates/auth/src/lib.rs keeps main's test_support module and this branch's scope_tests module. crates/auth/src/tests.rs keeps main's provider and provider_over. The block that this branch moves stays in scope_tests.rs, with main's change to provider_without_credentials.

The follow-up 8052d0d26 builds the provider of the new test through provider_over, because #1003 changed the constructors of BuiltinAuthProvider and CachedCredentialStore. On main the scope date check still runs before the session expiry check, which CachedCredentialStore::lookup now makes. At 8052d0d26, cargo +1.97.0 test --locked -p extenddb-auth --lib gives 534 passed.

7ff08b757 merges main at 3534c9e4d without a conflict. #1009 removed axum from crates/auth/Cargo.toml, so after that merge the axum import of HeaderMap and HeaderValue in scope_tests.rs stopped the lib tests from building (E0433). d0c9d2644 imports the two types from http, as tests.rs does on main.

#1079 adds the path and the query string to AuthProvider::authenticate. Whichever of #949 and #1079 merges second must adapt the authenticate calls in scope_tests.rs to those arguments.

Testing done

At d0c9d26, after the merge of main at 3534c9e:

  • cargo +1.97.0 test -p extenddb-auth: 534 unit tests, 14 sigv4_differential tests and 3 doc tests passed.
  • cargo +1.97.0 clippy -p extenddb-auth --all-targets -- -D warnings and cargo +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/auth nor the manual. The merge of #1003 came after these runs.

  • cargo +1.97.0 test --locked -p extenddb-auth: 516 unit tests, 14 sigv4_differential tests and 3 doc tests passed. The base and the move alone each give 515, 14 and 3.
  • The move: lines 293 to 613 of the old tests.rs equal scope_tests.rs after its 10-line header, byte for byte.
  • Negative controls, each on a clean committed tree:
    • With the lookup's error return moved above check_scope, 515 passed and 1 failed. The failure is the new test's stale scope case, which got ExpiredTokenException where it expects InvalidSignatureException.
    • With the expiry check of the credential cache turned off, 514 passed and 2 failed: the new test and cached_session_past_expires_at_returns_expired_token.
  • cargo +1.97.0 clippy --locked --all-targets -- -D warnings with the two feature sets of CI: no warnings. cargo +1.97.0 fmt --all -- --check: clean.
  • The test compares the error type after the #, because the probe recorded only that part. The prefix that the service sends is not measured.
  • CI at 101857a: 63 of 63 checks passed.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, 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.

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.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

#1346 fixes #1297. It has the text of this PR for steps 3, 4, 5 and 9 of docs/manuals/10-security-model.md, byte for byte. So the two PRs merge in either order without a conflict in that file. This PR came first. #1346 also fixes the auth error causes in the quickstart and in the first CRUD reference of .agents/skills/extenddb.

@navareamol navareamol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

Thanks for checking it against main. I merged main at 3534c9e (7ff08b7) and reproduced the E0433. d0c9d26 imports HeaderMap and HeaderValue from http, as tests.rs does. On the merged tree cargo +1.97.0 test -p extenddb-auth gives 534 lib, 14 sigv4_differential and 3 doc tests passed, and clippy with -D warnings and fmt are clean on the crate.

The description now says the test reads the session through CachedCredentialStore::pass_through. So the expiry refusal comes from the lookup after the loader returns the mock session, not from a cache hit. It gives the line counts at the head (scope_tests.rs 419, tests.rs 304) and the commit count (seven, two of them merges of main).

@navareamol navareamol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

@navareamol
navareamol added this pull request to the merge queue Nov 25, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to no response for status checks Nov 25, 2026
@LeeroyHannigan
LeeroyHannigan added this pull request to the merge queue Nov 25, 2026
Merged via the queue into main with commit 5252eda Nov 25, 2026
65 checks passed
@LeeroyHannigan
LeeroyHannigan deleted the test/sigv4-scope-before-expired-token branch November 25, 2026 13:12
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
Main is at 5252eda, four commits after the last merge (#864, #895,
#929 and #949). No file conflicted. Main and this branch change no
common file.
LeeroyHannigan added a commit that referenced this pull request Nov 25, 2026
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.
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