Skip to content

fix(admin): swallow get_admin_user / get_admin_config exceptions in template filter (#754) - #755

Open
SAY-5 wants to merge 5 commits into
jowilf:mainfrom
SAY-5:fix/safe-admin-user-filter-754
Open

fix(admin): swallow get_admin_user / get_admin_config exceptions in template filter (#754)#755
SAY-5 wants to merge 5 commits into
jowilf:mainfrom
SAY-5:fix/safe-admin-user-filter-754

Conversation

@SAY-5

@SAY-5 SAY-5 commented Apr 24, 2026

Copy link
Copy Markdown

Summary

When `StaticFiles` returns 404 for a missing asset (e.g. a crawler hitting `/admin/statics/js/vendor/tabler.min-1.1.0.js.map` before the source map exists on disk), `admin_app`'s `exception_handler` rebuilds the response through `_render_error` → `error.html` → `layout.html`. `layout.html` invokes the `get_admin_user` filter. Provider implementations typically read `request.session['name']` directly, so an unauthenticated crawler has no session and raises `KeyError`, turning the 404 into a 500 in the auth layer.

Stack trace from the issue matches this path: the 500 originates in the user's `get_admin_user` via the Jinja filter call on the rendered error page.

Fix

Wrap `get_admin_user` / `get_admin_config` in a small `_safe_filter` helper that returns `None` on any exception, so an unauthenticated render path gracefully falls back to the anonymous layout. Provider contracts are unchanged for authenticated requests.

Fixes #754

Test plan

  • `python3 -c 'import ast; ast.parse(open("starlette_admin/base.py").read())'`, syntax clean
  • Logic trace: authenticated request → filter delegates to provider callable (unchanged); unauthenticated request that ends up in `error.html` → provider raises → filter returns `None` → `layout.html`'s `{% set current_user = (request | get_admin_user) %}` gets `None` and renders the anonymous layout

SAY-5 and others added 3 commits April 24, 2026 00:40
…emplate filter

When StaticFiles returns 404 for a missing asset (e.g. a crawler
requesting /admin/statics/js/vendor/tabler.min-1.1.0.js.map before
the source map exists), admin_app's exception_handler runs
_render_error → error.html → layout.html, which invokes the
get_admin_user filter. Typical provider implementations read
request.session['name'] directly, so unauthenticated requests
crash with KeyError — turning a 404 into a 500 in the auth layer
(issue jowilf#754).

Wrap get_admin_user / get_admin_config with a _safe_filter helper
that returns None on any exception, so an unauthenticated render
path gracefully falls back to the anonymous layout.

Fixes jowilf#754

Signed-off-by: SAY-5 <say.apm35@gmail.com>
Signed-off-by: SAY-5 <say.apm35@gmail.com>
@SAY-5

SAY-5 commented Apr 29, 2026

Copy link
Copy Markdown
Author

Heads-up on the failing pre-commit.ci - pr check, it's not the patch in this PR. The pre-commit.ci runner has moved to Python 3.14 (runner-image:2026-04-26-14f680d), and pyupgrade v3.19.1 (the version pinned in .pre-commit-config.yaml) hits a Python 3.14 incompatibility in _fix_tokens:

File ".../pyupgrade/_main.py", line 297, in _fix_tokens
    tokenize.cookie_re.match(token.src)
TypeError: cannot use a bytes pattern on a string-like object

(see pre-commit.ci run)

pyupgrade v3.21.2 is the current release. Locally with the current config (pre-commit run --all-files) every hook passes, pyupgrade, ruff, black, the rest. Happy to push a one-line bump (pyupgrade rev: v3.19.1 → v3.21.2) on this branch if that's the right place, but it's a separate concern from the get_admin_user filter fix so leaving it for your call. The patch itself in starlette_admin/base.py and the auto-fix from pre-commit.ci[bot] (2febcc4) are both clean.

@SAY-5

SAY-5 commented May 2, 2026

Copy link
Copy Markdown
Author

The failing pre-commit.ci check is unrelated to this change: it's pyupgrade v3.19.1 crashing on pre-commit.ci's Python 3.14 runner with TypeError: cannot use a bytes pattern on a string-like object. The autoupdate in #750 should resolve it once merged. Local pre-commit run --all-files passes on the current branch.

@SAY-5

SAY-5 commented May 5, 2026

Copy link
Copy Markdown
Author

Bumped pyupgrade pin to v3.21.2, v3.19.1 crashes on Python 3.13 (tokenize.cookie_re became a bytes pattern). Should unblock pre-commit.ci across the repo.

Comment thread .pre-commit-config.yaml
- id: end-of-file-fixer
- id: trailing-whitespace
- repo: https://github.com/asottile/pyupgrade
rev: v3.19.1

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

can you remove this

Comment thread starlette_admin/base.py
Comment on lines +249 to +265

# Wrap provider callbacks in a safe filter so an unauthenticated
# request reaching the layout template (e.g. the exception handler
# rendering error.html for a missing static asset before the auth
# redirect fires, issue #754) doesn't crash get_admin_user's
# session lookup and turn a 404 into a 500.
def _safe_filter(fn):
if fn is None:
return None

def _wrapped(request):
try:
return fn(request)
except Exception:
return None

return _wrapped

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

can we move this into helpers

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.

Bug: Auth layer incorrectly triggers on static assets ending with .map

2 participants