fix(auth): gate cache deny on non-null user_id to prevent cross-user pollution#5388
fix(auth): gate cache deny on non-null user_id to prevent cross-user pollution#5388truecallerabreham wants to merge 1 commit into
Conversation
…pollution When user_id is None (anonymous/unauthenticated requests), the outer exception handlers in auth_user call set_cache with a deny value using a generic cache key (empty user segment). Since all anonymous users share the same cache key, one user's auth failure can cause all subsequent anonymous requests to also fail auth for up to 5 minutes. This fix adds a user_id is not None guard before writing deny entries to the cache, so anonymous failures are not cached globally. Closes Agenta-AI#5387
|
@truecallerabreham is attempting to deploy a commit to the agenta projects Team on Vercel. A member of the Team first needs to authorize it. |
|
|
|
Hi @truecallerabreham, thanks for opening a pull request. 🙏 This PR was automatically closed because it does not yet meet our contribution requirements:
We ask for this so every change is documented and demonstrably tested before review. How to get it reopened See the Contributing guide and Creating your first PR. If you think this was closed in error, leave a comment and a maintainer will take a look. |
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Plus Run ID: 📒 Files selected for processing (1)
Disabled knowledge base sources:
📝 WalkthroughSummary by CodeRabbit
WalkthroughThe bearer-token authentication middleware now guards deny-cache writes in both unauthorized and unexpected exception handlers, avoiding cache updates when no user ID was resolved. ChangesAuthentication deny-cache handling
Estimated code review effort: 2 (Simple) | ~5 minutes ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Summary
When
user_idisNone(anonymous/unauthenticated requests), the outer exception handlers inauth_usercallset_cachewith a deny value using a generic cache key (empty user segment). Since all anonymous users share the same cache key, one user's auth failure can cause all subsequent anonymous requests to also fail auth for up to 5 minutes.This fix adds a
user_id is not Noneguard before writing deny entries to the cache, so anonymous failures are not cached globally.Changes
api/oss/src/middlewares/auth.py: Addedif user_id is not None:guards aroundset_cachecalls in both theUnauthorizedExceptionhandler and the genericExceptionhandler. The_denylogging call is preserved unconditionally so failures are still recorded.api/oss/tests/pytest/unit/middlewares/test_cache_deny.py: Added regression tests verifying that deny entries are only cached whenuser_idis known, and not cached for anonymous users.Demo
Code diff showing the two guarded
set_cachecalls:Impact
user_idis set, the cache deny behavior is unchangedTesting
test_cache_deny_not_written_when_user_id_is_none— verifiesset_cacheis NOT called with deny whenuser_idis Nonetest_cache_deny_written_when_user_id_is_set— verifiesset_cacheIS called with deny whenuser_idis setcaching._packfunction: whenuser_idis None, the cache key becomescache:p:{project}:u:{12 dashes}:{namespace}:{key}, which is identical for all anonymous users