From 76d7e68370019c28d55dfbebc0fce85e96839c8c Mon Sep 17 00:00:00 2001 From: Claude Sonnet 5 Date: Sun, 4 Oct 2026 09:11:50 +0000 Subject: fix(resources/views): don't crash on an invalid (not just missing) token resources_authorisation()'s except _HTTPException branch only set `resp` when err_msg["error"] == "missing_authorization", leaving it unset for any other token-related _HTTPException -- in particular "invalid_token", raised for a malformed or otherwise invalid bearer token (as opposed to no Authorization header at all). `return resp` then raised UnboundLocalError, a 500, instead of the same anonymous public-only fallback this endpoint already gives a request with no token. Reproduced: POST /auth/resource/authorisation with "Authorization: Bearer not-a-real-token" raised UnboundLocalError before this change, and now returns the same {"public-read": ...} shape a request with no Authorization header gets. Co-Authored-By: Claude Sonnet 5 Claude-Session: https://claude.ai/code/session_01XDDpXvZBQat9uSgDeEMJ8d --- gn_auth/auth/authorisation/resources/views.py | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/gn_auth/auth/authorisation/resources/views.py b/gn_auth/auth/authorisation/resources/views.py index 2737916..3cc2404 100644 --- a/gn_auth/auth/authorisation/resources/views.py +++ b/gn_auth/auth/authorisation/resources/views.py @@ -534,13 +534,21 @@ def resources_authorisation(): } for resid in resource_ids }) except _HTTPException as _httpe: - err_msg = json.loads(_httpe.body) - if err_msg["error"] == "missing_authorization": - resp = jsonify({ - str(resid): { - "public-read": resid in pubres - } for resid in resource_ids - }) + # Any token-related failure here -- missing header, malformed + # token, expired token, etc. -- degrades to the same + # public-only view, not just the "no header at all" + # (missing_authorization) case. Without this, an invalid + # bearer token (error "invalid_token", not + # "missing_authorization") left `resp` unset below, raising + # UnboundLocalError instead of the graceful anonymous fallback + # this endpoint is documented to provide. + logger.debug("Token-related exception, falling back to public-only " + "view.", exc_info=True) + resp = jsonify({ + str(resid): { + "public-read": resid in pubres + } for resid in resource_ids + }) except AssertionError as _aerr: resp = jsonify({ "status": "bad-request", -- cgit 1.4.1