You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ma...@apache.org on 2026/10/06 19:45:15 UTC
(superset) 09/23: fix(security): reject a non-mapping resolver result and add @safe on consume
This is an automated email from the ASF dual-hosted git repository.
madhushreeag pushed a commit to branch madhu-one-time-login-tokens
in repository https://gitbox.apache.org/repos/asf/superset.git
commit 2f9852f02d1f8473ac7bb627aace840f3213b999
Author: madhushree agarwal <ma...@apple.com>
AuthorDate: Thu Sep 17 10:50:37 2026 -0700
fix(security): reject a non-mapping resolver result and add @safe on consume
Three review findings on the login-token flow:
resolve_identity only guarded the resolver *call*. A resolver returning a truthy
non-mapping — a string, a list, an arbitrary object — reached `.get` outside the
try and raised AttributeError, surfacing as a 500 and contradicting the
documented contract that a misbehaving resolver is a rejection, not a server
error. Guard with isinstance and log the offending type. Three cases added to
the rejection matrix.
The GET consume endpoint lacked @safe, which its sibling POST and every other
endpoint in the class carry, so an escaping exception would render Flask's HTML
500 rather than the JSON shape the rest of the API returns.
mint's purge comment claimed a unique-constraint rationale that does not apply
to a fresh uuid4. The call is still worth keeping — with a seconds-long TTL these
rows turn over far faster than the scheduled prune runs, and minting is not a hot
path — so the comment now states the real reason rather than a wrong one.
---
superset/security/api.py | 1 +
superset/security/login_token.py | 31 +++++++++++++++++++++------
tests/unit_tests/security/login_token_test.py | 5 +++++
3 files changed, 31 insertions(+), 6 deletions(-)
diff --git a/superset/security/api.py b/superset/security/api.py
index 0482e82277a..5a553bff569 100644
--- a/superset/security/api.py
+++ b/superset/security/api.py
@@ -348,6 +348,7 @@ class SecurityRestApi(BaseSupersetApi):
@expose("/login-token/", methods=("GET",))
@event_logger.log_this
@statsd_metrics
+ @safe
@transaction()
def login_with_token(self) -> Response:
"""Consume a one-time login token and establish a session.
diff --git a/superset/security/login_token.py b/superset/security/login_token.py
index 641b479eb91..436e1c4e24e 100644
--- a/superset/security/login_token.py
+++ b/superset/security/login_token.py
@@ -108,14 +108,21 @@ def get_ttl_seconds() -> int:
return ttl if ttl > 0 else DEFAULT_LOGIN_TOKEN_TTL_SECONDS
-def resolve_identity(request: Request, **kwargs: Any) -> LoginTokenUserInfo | None:
+# Each rejection below is a distinct failure with its own diagnostic — an
+# unconfigured resolver is normal and silent, a misconfigured or misbehaving one
+# is logged. Merging the branches to satisfy the return-count limit would lose
+# that distinction, which is the only signal an operator gets.
+def resolve_identity( # pylint: disable=too-many-return-statements
+ request: Request, **kwargs: Any
+) -> LoginTokenUserInfo | None:
"""Run the operator-supplied resolver against an inbound mint request.
The resolver is the whole authentication boundary for minting: it decides
what counts as proof of identity (an OIDC id token, an existing session, a
- credential checked against an internal service). A resolver that raises is
- treated as a rejection rather than a server error, so a failing validation
- can never be mistaken for a successful one.
+ credential checked against an internal service). A resolver that raises, or
+ returns something other than a mapping, is treated as a rejection rather
+ than a server error, so a failing validation can never be mistaken for a
+ successful one.
"""
resolver = current_app.config.get("LOGIN_TOKEN_IDENTITY_RESOLVER")
if resolver is None:
@@ -134,6 +141,15 @@ def resolve_identity(request: Request, **kwargs: Any) -> LoginTokenUserInfo | No
if not userinfo:
return None
+ if not isinstance(userinfo, dict):
+ # A truthy non-mapping would otherwise raise on the ``.get`` below and
+ # surface as a 500, contradicting the rejection contract above.
+ logger.error(
+ "LOGIN_TOKEN_IDENTITY_RESOLVER returned %s, expected a mapping",
+ type(userinfo).__name__,
+ )
+ return None
+
if not (userinfo.get("username") or userinfo.get("email")):
# auth_user_oauth derives the username from one of these two; without
# either there is no identity to provision.
@@ -152,8 +168,11 @@ def mint(userinfo: LoginTokenUserInfo) -> tuple[str, datetime]:
token = uuid4()
expires_on = datetime.now() + timedelta(seconds=get_ttl_seconds())
- # An explicit key plus an expiry requires purging expired entries first,
- # otherwise a stale row occupying the same key fails the unique constraint.
+ # Opportunistic GC rather than collision avoidance: a fresh uuid4 cannot
+ # collide, but with a TTL measured in seconds these rows turn over quickly,
+ # and the scheduled prune job runs far less often than tokens are minted.
+ # Minting is not a hot path, so one indexed DELETE here is cheap insurance
+ # against the table filling with dead entries between prunes.
KeyValueDAO.delete_expired_entries(LOGIN_TOKEN_RESOURCE)
KeyValueDAO.create_entry(
resource=LOGIN_TOKEN_RESOURCE,
diff --git a/tests/unit_tests/security/login_token_test.py b/tests/unit_tests/security/login_token_test.py
index ef21769e4cf..109dd967220 100644
--- a/tests/unit_tests/security/login_token_test.py
+++ b/tests/unit_tests/security/login_token_test.py
@@ -107,6 +107,11 @@ def test_get_ttl_seconds_falls_back(app_context: None) -> None:
(lambda request, **kwargs: None, "resolver declined"),
(lambda request, **kwargs: {}, "resolver returned an empty dict"),
(lambda request, **kwargs: {"first_name": "Jane"}, "no username or email"),
+ # A truthy non-mapping must reject rather than raise on `.get` and
+ # surface as a 500 — the contract is rejection, never a server error.
+ (lambda request, **kwargs: "jdoe", "resolver returned a string"),
+ (lambda request, **kwargs: ["jdoe"], "resolver returned a list"),
+ (lambda request, **kwargs: object(), "resolver returned an opaque object"),
],
)
def test_resolve_identity_rejections(