Visitar URL original
fix(embedded): fail closed when the guest-user check can't resolve a principal by eschutho · Pull Request #43896 · apache/superset · GitHub
Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(embedded): fail closed when the guest-token form read raises
request.form parses the body lazily on first access, so the guest-token
fallback in is_sanitization_required() could itself raise -- werkzeug's
RequestEntityTooLarge/ClientDisconnected for an oversized or truncated
body, or an OSError from the WSGI input stream or the multipart temp-file
spool -- escaping the error handler it exists to protect. Guard the form
read and fail closed when the body can't be inspected.

Also make test_send_chart_response_does_not_half_redact_guest_query_error
exercise the raising path it describes: the decision is resolved once, so
use a single raising side_effect and assert one lookup.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Signed-off-by: Elizabeth Thompson <eschutho@gmail.com>
  • Loading branch information
eschutho and claude committed Oct 8, 2026
commit db7177e821aa4ab98732ce51074c46fe884011b7
25 changes: 16 additions & 9 deletions superset/utils/error_sanitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

from flask import current_app, has_request_context, request
from flask_babel import lazy_gettext as _
from werkzeug.exceptions import HTTPException

from superset.errors import ErrorLevel, SupersetError, SupersetErrorType

Expand Down Expand Up @@ -98,8 +99,8 @@ def is_sanitization_required() -> bool:
When it does raise the principal is unknown, so this makes a deliberate
availability-over-confidentiality trade-off rather than guessing "not a
guest". Inside a request context it falls back to whether the request even
carries a guest token: reading headers and form fields cannot raise, anyone
presenting a token is redacted (failing closed for the only principal whose
carries a guest token: anyone presenting a token, or whose body can't be read
to check for one, is redacted (failing closed for the only principal whose
errors are redacted), and a genuinely anonymous request keeps its error so
ordinary failures are not over-sanitized. Outside a request context (e.g. a
Celery worker, where a guest principal may be active via ``override_user`` and
Expand All @@ -124,14 +125,20 @@ def is_sanitization_required() -> bool:
# ``override_user``). Fail closed: a guest principal may be active
# and the redacted payload is delivered to the embedded viewer.
return True
# Reading the token header and form field cannot raise, so this fallback
# is itself incapable of breaking the error handler. ``.get`` on the
# config keeps that guarantee even if the key is somehow absent.
# ``.get`` on the config keeps the header read from raising even if the
# key is somehow absent.
header_name = current_app.config.get("GUEST_TOKEN_HEADER_NAME")
return bool(
(header_name and request.headers.get(header_name))
or request.form.get("guest_token")
)
if header_name and request.headers.get(header_name):
return True
try:
# ``request.form`` parses the body lazily on first access, which
# raises ``RequestEntityTooLarge``/``ClientDisconnected`` for an
# oversized or truncated body, or an ``OSError`` from the WSGI
# server's input stream or the multipart temp-file spool.
return bool(request.form.get("guest_token"))
Comment on lines +130 to +138

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Duplicated guest-token detection

The new fallback re-implements the guest-token detection already in security/manager.py (request.headers.get(get_conf()["GUEST_TOKEN_HEADER_NAME"]) or request.form.get("guest_token")). It also diverges: current_app.config.get(...) here vs get_conf()["..."] there. A future change to the header/field name or config access in one place silently desyncs the other. Consider a shared helper.

Code Review Run #635ee4


Should Bito avoid suggestions like this for future reviews? (Manage Rules)

  • Yes, avoid them

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm leaving this one as is on purpose. The two reads look alike but have different contracts:

  • request_loader (and the copy in get_guest_user_from_request) is the authentication path. It may raise. For example, an oversized body raises a 413 there, which is the right response.
  • This fallback runs inside the error handler after resolving the principal has already failed, so it must never raise, and it fails closed (redacts) when the body can't be read. That's why it uses current_app.config.get(...) rather than get_conf()[...], which can raise KeyError, and why the form read has its own broad except.

A shared helper would have to pick one of those two behaviours. That would either make this fallback able to raise again, which is the bug this PR closes, or make authentication swallow body errors, which changes auth behaviour and is out of scope here. The things that could drift are the GUEST_TOKEN_HEADER_NAME config key and the guest_token form field. Both are part of the embedded SDK's request format, and the fallback tests build their requests from the same config key and field name.

except (HTTPException, OSError):
# The body can't be inspected, so fail closed.
return True


def sanitize_error_message(
Expand Down
14 changes: 5 additions & 9 deletions tests/unit_tests/charts/test_chart_data_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -623,13 +623,9 @@ def test_send_chart_response_does_not_half_redact_guest_query_error(
) -> None:
"""
The redaction decision is resolved once and threaded down, so the block can
never pop ``stacktrace`` while leaking the raw ``error``. Previously the
outer guest check and the inner ``sanitize_error_message`` resolved the
principal independently: an ``is_guest_user`` that succeeded for the outer
check but raised for the inner one (e.g. a DB session that broke mid-request)
dropped the stacktrace yet kept the raw driver error, failing open. The
request carries a guest token so, even if the check raises, the shared
fallback fails closed and both are redacted.
never pop ``stacktrace`` while leaking the raw ``error``. When the guest check
raises (e.g. a broken DB session), the request's guest token makes the
fallback fail closed, and both the stacktrace and the error are redacted.
"""
result = _json_execution_result(
{
Expand All @@ -648,7 +644,7 @@ def test_send_chart_response_does_not_half_redact_guest_query_error(
),
patch(
"superset.charts.data.api.security_manager.is_guest_user",
side_effect=[True, RuntimeError("session broke mid-request")],
side_effect=RuntimeError("session broke mid-request"),
) as mock_is_guest_user,
patch(
"superset.charts.data.api.security_manager.can_access",
Expand All @@ -660,7 +656,7 @@ def test_send_chart_response_does_not_half_redact_guest_query_error(
query = json.loads(response.get_data(as_text=True))["result"][0]
assert query["error"] == str(GENERIC_ERROR_MESSAGE)
assert "stacktrace" not in query
assert mock_is_guest_user.called
mock_is_guest_user.assert_called_once()


def test_get_data_response_redacts_guest_query_failure(app: SupersetApp) -> None:
Expand Down
25 changes: 25 additions & 0 deletions tests/unit_tests/utils/test_error_sanitization.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from unittest.mock import patch

import pytest
from flask import request

from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
from superset.utils.error_sanitization import (
Expand Down Expand Up @@ -243,6 +244,30 @@ def test_unresolvable_principal_with_form_guest_token_is_redacted(
assert mock_is_guest_user.called


def test_unresolvable_principal_with_unreadable_body_fails_closed(
app: SupersetApp,
) -> None:
"""
``request.form`` parses the body lazily, so the fallback's form read raises
``RequestEntityTooLarge`` for an oversized body. That must not escape the
error handler; the token can't be ruled out, so it fails closed.
"""
is_guest_user = patch(
"superset.security.SupersetSecurityManager.is_guest_user",
side_effect=RuntimeError("find_role on a broken session"),
)
with (
app.test_request_context(
"/", method="POST", data={"guest_token": "a.guest.token"}
),
is_guest_user as mock_is_guest_user,
):
request.max_content_length = 1
assert is_sanitization_required() is True
assert sanitize_error_message(DB_ERROR) == str(GENERIC_ERROR_MESSAGE)
assert mock_is_guest_user.called


def test_unresolvable_principal_without_request_context_fails_closed(
app: SupersetApp,
) -> None:
Expand Down