fix(admin): swallow get_admin_user / get_admin_config exceptions in template filter (#754) - #755
fix(admin): swallow get_admin_user / get_admin_config exceptions in template filter (#754)#755SAY-5 wants to merge 5 commits into
Conversation
…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>
for more information, see https://pre-commit.ci
Signed-off-by: SAY-5 <say.apm35@gmail.com>
|
Heads-up on the failing (see pre-commit.ci run)
|
|
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 |
Signed-off-by: SAY-5 <say.apm35@gmail.com>
|
Bumped pyupgrade pin to v3.21.2, v3.19.1 crashes on Python 3.13 ( |
| - id: end-of-file-fixer | ||
| - id: trailing-whitespace | ||
| - repo: https://github.com/asottile/pyupgrade | ||
| rev: v3.19.1 |
|
|
||
| # 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 |
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