SEP-1846: Resolve a request's Bearer credential once per request - #1397
Open
marcuscruz-percona wants to merge 5 commits into
Open
SEP-1846: Resolve a request's Bearer credential once per request#1397marcuscruz-percona wants to merge 5 commits into
marcuscruz-percona wants to merge 5 commits into
Conversation
Every mutating request authenticated the same credential twice: the unsafe-method role gate resolves the caller in its body, and the route's own authentication dependency resolves it again. FastAPI's per-request dependency cache keys on the declared callable, so the gate's direct call is invisible to it. Split the resolution into `resolve_current_user` plus a memoizing `get_current_user` that keeps the public name, so every `CurrentUser` alias and test dependency override is untouched. The cache is keyed on the credential, holds successes only, and lives under a namespaced key in the request scope rather than in `request.state`, whose backing dict a request inherits as a shallow copy of the ASGI lifespan state. Under Casdoor one resolution is two HTTP round-trips, so this halves them on every write. No authorization outcome changes.
marcuscruz-percona
requested review from
peter-o-addo and
yyyyyyyan
as code owners
August 21, 2026 17:29
Contributor
There was a problem hiding this comment.
Pull request overview
Adds request-scoped credential memoization to avoid duplicate authentication round-trips while preserving existing dependency interfaces.
Changes:
- Splits user resolution from the cached dependency.
- Propagates request context through SEP authentication.
- Adds cross-service cache and authentication coverage.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
app/api/deps.py |
Adds request-scoped user caching. |
app/sep/deps.py |
Forwards requests to the cached resolver. |
tests/app/api/test_deps.py |
Tests cache isolation and failures. |
tests/app/api/test_role_gate.py |
Verifies unsafe routes authenticate independently. |
tests/app/inventory/test_role_gate.py |
Checks Inventory round-trip counts. |
tests/app/tasks/test_role_gate.py |
Checks Tasks round-trip counts. |
tests/app/sep/api/test_router.py |
Checks SEP round-trip counts. |
tests/app/sep/test_deps.py |
Verifies request forwarding. |
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
A cache hit returned the user without re-setting the log context variable the resolution it replaces would have written, so a request resolving two credentials attributed its later logs to whichever one resolved last rather than to the user actually returned. No production path presents two credentials in one request — the Bearer header is the only source — so this closes a latent inconsistency in the memoizer rather than a live misattribution, and makes it correct without resting on that caller invariant.
Name the inner resolver authenticate_bearer_token. The name now carries what its docstring was instructing callers about, so the paragraph telling them to declare IsAuthenticatedDep instead goes, and the eight tests plus the class the earlier rename left naming get_current_user are swept to the symbol they exercise. Trim get_current_user's docstring to its own contract: how the role gate resolves the caller is asserted by that gate's docstring, and the provider round-trip arithmetic belongs in the PR. Key the cache on a digest of the credential rather than on the credential itself. Neither harm this forecloses is reachable today - the Authorization header bytes already sit in the request scope, and a per-request dict only ever holds tokens its own caller presented - but a secret is no longer a dict key, and the local no longer infers Any. Read the log identity through ContextFilter rather than app.core.log's private context variables, which no test outside tests/app/core reaches. Guard the two corners the acceptance criteria left untested: that the shared instance reaches the second consumer unrewritten, and that a safe method costs the one resolution its route declares rather than gaining the gate's.
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||||||||||||||||||||
The admin gate test asserted != 403 across three routers. PUT /periodic/1 returned 500 under it - the route resolves its target through the celery-beat session, which nothing overrode, so it raised on a missing table before reaching the handler - and the assertion passed on that, as it would on a 401 or any other failure on the way in. Override the beat session in the client fixture so the route answers from the in-memory beat tables, and give the parametrized table the concrete status each route returns for an empty body. Removing the override now fails with assert 500 == 404 rather than passing.
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
require_minimum_role_for_unsafe_methodsresolves the caller in its body (a direct call FastAPI's per-request dependency cache can't see, since that cache keys on the declared callable), then the route's own authentication dependency resolves the same token again. Under Casdoor one resolution is two HTTP round-trips — introspect, then fetch the user — so every write paid four where two would do, and the two resolutions could disagree if the caller's role changed between them.authenticate_bearer_tokenplus a memoizingget_current_userthat keeps the public name, so all ~158app/references and everydependency_overrides[get_current_user]registration are untouched; the production change is two call sites. The cache is keyed on the credential, so a resolution is never served to a caller presenting a different one, and holds successes only, so a refusal is re-derived rather than remembered. The name states what the resolver does — authenticate, unconditionally, on every call — so nothing has to tell callers to declareIsAuthenticatedDepinstead of reaching for it.request.state; the cache lives under a namespaced key in the request scope instead. A request inheritsscope["state"]as a shallow copy of the ASGI lifespan state (uvicorn/protocols/http/h11_impl.py), so a cache found there could be reached by every later request as soon as anything published a key of the same name. The invariant the ticket asks for — the cache never outlives its request — holds under the scope key and would not hold underrequest.state; a test pins that nothing is written to the inherited namespace.Authorizationheader bytes already sit inrequest.scope["headers"]on every request, and a per-request dict only ever holds tokens its own caller presented — so this is hygiene that keeps a secret from being a dict key, not a fix for a live hole.GET /healthunauthenticated.Coverage the round-trip claim previously lacked: provider round-trips are now counted per service,
GET /healthis asserted to resolve nothing on each of the three, andtests/app/api/test_role_gate.pypins that every unsafe inventory/tasks route authenticates itself as well — the second consumer the deduplication is measured against, without which the round-trip counts would read as green for the wrong reason. That last test is kept knowing it fail-locks a future deliberately-unauthenticated unsafe route; editing it with a stated reason is the right friction for that change.Two further guards close the corners the acceptance criteria left untested: that the instance the gate and the route now share reaches the second consumer unrewritten, and that a safe method costs the one resolution its route declares rather than gaining the gate's (previously covered only by the gate's own unit tests, never end to end).
One correction to the ticket, recorded there as well: AC 5 cites
tests/app/api/test_admin_gate.pyandTestRequireAdminForUnsafeMethods. Both were renamed by the ordered-role-gate work, totests/app/api/test_role_gate.pyandTestRequireMinimumRoleForUnsafeMethods. Judged against the real equivalents the criterion holds — no assertion changed; the one altered line in that class is a setup call the resolver rename forced.One weak assertion found while working here and fixed rather than left noted:
tests/app/tasks/test_role_gate.pyasserted!= 403across three routers, andPUT /periodic/1was returning 500 under it — the route resolves its target through the celery-beat session, which nothing overrode, so it raised on a missing table before reaching the handler, and the assertion passed on that as it would on any other failure on the way in. The client fixture now overrides the beat session and the parametrized table names the concrete status each route answers with; removing the override fails withassert 500 == 404instead of passing. Separate commit, no app code, and outside AC 5's cited gate suites.Tested
make test— 10234 passed, 424 skippedmake lint— all checks passedmake run-pre-commit— all hooks passedassert 2 == 1; after the change each is 1request.statename madeget_current_userreturn the seeded object without validating itChecklist
make test)make run-pre-commit)make makemigrations)changelog.d/if the change is user-facing (make changelog-add), or confirmed N/A (internal-only change, or a same-release-cycle fix for an unreleased sibling ticket)