Skip to content

馃敀 Fix authentication bypass in admin routes - #44

Merged
iloveitaly merged 1 commit into
masterfrom
fix-admin-auth-bypass-17224035668587722108
Feb 20, 2026
Merged

馃敀 Fix authentication bypass in admin routes#44
iloveitaly merged 1 commit into
masterfrom
fix-admin-auth-bypass-17224035668587722108

Conversation

@iloveitaly

Copy link
Copy Markdown
Owner

This PR fixes a critical security vulnerability in app/routes/admin.py where unauthorized users could bypass admin checks if request.state.user was not set.

Changes:

  • Modified require_admin dependency to explicitly check for user existence using getattr(request.state, "user", None).
  • Replaced the silent bypass with an explicit ImpossibleStateError if the user is missing, enforcing the assumption that upstream middleware handles authentication.
  • Maintained HTTPException(401) for authenticated users who are not admins.

Testing:

  • Verified the fix using a reproduction script (verify_fix.py, now deleted) which simulated requests with request.state.user as None, missing, non-admin, and admin.
  • Confirmed that ImpossibleStateError is raised for missing users and 401 for non-admins.
  • Ran linting checks (just py_lint).

PR created automatically by Jules for task 17224035668587722108 started by @iloveitaly

The admin route protection logic (`require_admin`) previously checked `if (admin_user := request.state.user) and ...`.
This check evaluated to `False` if `request.state.user` was `None` (falsy), causing the `HTTPException` to be skipped and allowing unauthorized access.

This commit changes the logic to:
1. Safely retrieve the user using `getattr(request.state, "user", None)`.
2. Explicitly raise `ImpossibleStateError` if the user is missing or `None`. This asserts that upstream authentication middleware must have successfully populated the user.
3. If the user exists, verify the admin role and raise `401 Unauthorized` if the check fails.

This fails closed on missing user data, treating it as an internal server error (impossible state) rather than an unauthorized request, which aligns with the expectation that admin routes are behind strict authentication middleware.

Co-authored-by: iloveitaly <150855+iloveitaly@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
Contributor

馃憢 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 馃憖 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@iloveitaly
iloveitaly merged commit dbb93bd into master Feb 20, 2026
2 of 3 checks passed
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.

1 participant