You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by jl...@apache.org on 2026/09/22 21:31:10 UTC
(superset) branch master updated: fix: tighten object-level and destination checks across four unrelated endpoints (#44036)
This is an automated email from the ASF dual-hosted git repository.
sadpandajoe pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git
The following commit(s) were added to refs/heads/master by this push:
new 7dd70eaf2d6 fix: tighten object-level and destination checks across four unrelated endpoints (#44036)
7dd70eaf2d6 is described below
commit 7dd70eaf2d61d554c3dd9c919b89cc9ba184f1c5
Author: Evan Rusackas <ev...@preset.io>
AuthorDate: Tue Sep 22 14:31:03 2026 -0700
fix: tighten object-level and destination checks across four unrelated endpoints (#44036)
Co-authored-by: Claude Sonnet 5 <no...@anthropic.com>
---
superset/commands/database/exceptions.py | 9 +-
superset/commands/database/update.py | 20 +-
superset/commands/database/utils.py | 47 ++++
superset/commands/semantic_layer/update.py | 55 ++++-
superset/config.py | 9 +
superset/db_engine_specs/base.py | 82 ++++++-
superset/db_engine_specs/gsheets.py | 1 +
superset/db_engine_specs/snowflake.py | 1 +
superset/security/api.py | 13 +-
superset/security/manager.py | 61 +++++-
superset/utils/network.py | 115 ++++++++++
superset/views/utils.py | 12 +
tests/integration_tests/security/api_tests.py | 28 +++
tests/unit_tests/commands/databases/utils_test.py | 58 +++++
.../commands/semantic_layer/update_test.py | 96 ++++++++
tests/unit_tests/db_engine_specs/test_base.py | 243 ++++++++++++++++++++-
.../unit_tests/db_engine_specs/test_databricks.py | 28 ++-
.../db_engine_specs/test_databricks_multi_cloud.py | 4 +
tests/unit_tests/db_engine_specs/test_gsheets.py | 27 ++-
tests/unit_tests/db_engine_specs/test_snowflake.py | 7 +-
tests/unit_tests/db_engine_specs/test_trino.py | 7 +-
tests/unit_tests/security/manager_test.py | 177 ++++++++++++++-
tests/unit_tests/utils/test_network.py | 81 ++++++-
tests/unit_tests/views/test_utils.py | 69 +++++-
24 files changed, 1210 insertions(+), 40 deletions(-)
diff --git a/superset/commands/database/exceptions.py b/superset/commands/database/exceptions.py
index 0e2347a292b..7dc1564b4a1 100644
--- a/superset/commands/database/exceptions.py
+++ b/superset/commands/database/exceptions.py
@@ -55,10 +55,11 @@ class DatabaseUpdateUnsafeRebindError(ValidationError):
super().__init__(
_(
"This update would change the connection's effective "
- "destination (host/port, engine parameters, or SSH tunnel "
- "endpoint) while reusing the stored credential. Provide "
- "the real password (or SSH tunnel credential) to confirm "
- "a connection move."
+ "destination (host/port, engine parameters, SSH tunnel "
+ "endpoint, or OAuth2 endpoint URIs) while reusing the stored "
+ "credential. Provide the real password (or SSH tunnel "
+ "credential / OAuth2 client secret) to confirm a connection "
+ "move."
),
field_name=field_name,
)
diff --git a/superset/commands/database/update.py b/superset/commands/database/update.py
index 6af1375d39a..285ae71431a 100644
--- a/superset/commands/database/update.py
+++ b/superset/commands/database/update.py
@@ -37,6 +37,7 @@ from superset.commands.database.exceptions import (
from superset.commands.database.sync_permissions import SyncPermissionsCommand
from superset.commands.database.utils import (
engine_params_changed,
+ oauth2_endpoint_rebind_unsafe,
ssh_tunnel_rebind_unsafe,
uri_identity_changed,
)
@@ -255,8 +256,9 @@ class UpdateDatabaseCommand(BaseCommand):
def _check_no_unsafe_secret_rebind(self) -> None:
"""
Refuse an update that changes the connection's effective destination
- (URI host/port, `extra.engine_params`, or the SSH tunnel endpoint)
- while leaving the corresponding stored secret masked.
+ (URI host/port, `extra.engine_params`, the SSH tunnel endpoint, or the
+ OAuth2 endpoint URIs in `encrypted_extra`) while leaving the
+ corresponding stored secret masked.
Without this, an editor could silently redirect the real stored
password/encrypted_extra/SSH tunnel credential to a different
@@ -327,3 +329,17 @@ class UpdateDatabaseCommand(BaseCommand):
raise DatabaseInvalidError(
exceptions=[DatabaseUpdateUnsafeRebindError(field_name="ssh_tunnel")]
)
+
+ # The OAuth2 endpoints live inside encrypted_extra, so a change there is
+ # invisible to the URI/engine-params check above -- yet the stored client
+ # secret is what the next token exchange posts to the new endpoint.
+ if "masked_encrypted_extra" in self._properties and (
+ oauth2_endpoint_rebind_unsafe(
+ model.encrypted_extra, self._properties["masked_encrypted_extra"]
+ )
+ ):
+ raise DatabaseInvalidError(
+ exceptions=[
+ DatabaseUpdateUnsafeRebindError(field_name="masked_encrypted_extra")
+ ]
+ )
diff --git a/superset/commands/database/utils.py b/superset/commands/database/utils.py
index a4be6fce731..ef428c11065 100644
--- a/superset/commands/database/utils.py
+++ b/superset/commands/database/utils.py
@@ -135,6 +135,53 @@ def ssh_tunnel_rebind_unsafe(
return not has_fresh_credential or stale_private_key_password
+OAUTH2_ENDPOINT_FIELDS = (
+ "authorization_request_uri",
+ "token_request_uri",
+ "redirect_uri",
+)
+
+
+def oauth2_endpoint_rebind_unsafe(
+ existing_encrypted_extra: str | None, submitted_masked_encrypted_extra: str | None
+) -> bool:
+ """
+ Whether a submitted ``masked_encrypted_extra`` repoints the OAuth2 client
+ at different endpoint URIs while reusing the stored client secret (the
+ ``$.oauth2_client_info.secret`` mask is restored verbatim by
+ ``unmask_encrypted_extra`` before the update persists).
+
+ The URI/engine-params destination check does not see these fields, yet
+ the next token exchange posts the real client secret to whatever
+ ``token_request_uri`` now says -- so an endpoint change must come with a
+ freshly supplied secret, exactly like a host change must come with a
+ fresh password.
+ """
+ try:
+ existing = json.loads(existing_encrypted_extra or "{}")
+ submitted = json.loads(submitted_masked_encrypted_extra or "{}")
+ except (TypeError, ValueError):
+ return False # malformed payloads are rejected by schema validation elsewhere
+ existing_info = (
+ existing.get("oauth2_client_info") if isinstance(existing, dict) else None
+ )
+ submitted_info = (
+ submitted.get("oauth2_client_info") if isinstance(submitted, dict) else None
+ )
+ if not isinstance(existing_info, dict) or not isinstance(submitted_info, dict):
+ return False
+ if not existing_info.get("secret"):
+ return False # nothing stored to carry over
+ # Only the mask sentinel restores the stored secret; an absent key drops it,
+ # and a different value is a fresh secret the caller is entitled to attach.
+ secret_reused = submitted_info.get("secret") == PASSWORD_MASK
+ endpoint_changed = any(
+ existing_info.get(field) != submitted_info.get(field)
+ for field in OAUTH2_ENDPOINT_FIELDS
+ )
+ return secret_reused and endpoint_changed
+
+
def ping(engine: Engine) -> bool:
try:
time_delta = app.config["TEST_DATABASE_CONNECTION_TIMEOUT"]
diff --git a/superset/commands/semantic_layer/update.py b/superset/commands/semantic_layer/update.py
index 36860d9b958..6e42bc3ca52 100644
--- a/superset/commands/semantic_layer/update.py
+++ b/superset/commands/semantic_layer/update.py
@@ -35,6 +35,7 @@ from superset.commands.semantic_layer.exceptions import (
)
from superset.commands.semantic_layer.utils import validate_configuration
from superset.commands.utils import current_user_can_modify_object
+from superset.constants import PASSWORD_MASK
from superset.daos.semantic_layer import SemanticLayerDAO, SemanticViewDAO
from superset.exceptions import SupersetSecurityException
from superset.semantic_layers.masking import (
@@ -49,6 +50,10 @@ from superset.utils.decorators import on_error, transaction
logger = logging.getLogger(__name__)
+# Sentinel distinguishing "key absent" from "key present with value None"
+# when reading the stored configuration -- dict.get's own default can't.
+_MISSING = object()
+
def _unmask_configuration(
existing_raw_configuration: str | None,
@@ -64,13 +69,61 @@ def _unmask_configuration(
otherwise overwrite the real stored values with the mask string. This
delegates to :func:`superset.semantic_layers.masking.unmask_configuration`,
which restores masked values recursively so nested/union secrets survive
- the round-trip too, not just top-level ones."""
+ the round-trip too, not just top-level ones.
+
+ A masked top-level key is only ever restored, though, when every OTHER
+ submitted top-level key is unchanged from what's stored -- including a
+ stored key being dropped from the payload -- i.e. this is a pure "reveal
+ what I was shown masked" round-trip, not an edit that also changes some
+ other connector field. Without that check, an editor (entitled to edit
+ this connection, but not to see its real secret -- that's the entire
+ reason GET/list mask it) could reveal a masked value while simultaneously
+ changing a destination-relevant field in the same request, poisoning the
+ stored configuration: the very next legitimate call through this layer
+ (``POST /<uuid>/schema/runtime`` always uses the stored, now-poisoned
+ configuration) would send the real secret to wherever that field now
+ points. Semantic layer connector schemas are pluggable and defined
+ outside this repo (see ``superset/core/api/core_api_injection.py``), so
+ unlike the analogous database-connection fix there's no fixed
+ "destination fields" list to narrow this to -- any other top-level field
+ changing at all is treated as unsafe to combine with a secret reveal.
+ """
try:
existing_configuration: dict[str, Any] = (
json.loads(existing_raw_configuration) if existing_raw_configuration else {}
)
except (TypeError, ValueError):
existing_configuration = {}
+
+ masked_keys = {
+ key
+ for key, value in new_configuration.items()
+ if value == PASSWORD_MASK and key in existing_configuration
+ }
+ # `.get(key)` alone can't tell "key absent from storage" apart from "key
+ # present and stored as None" -- both return None -- so a newly
+ # introduced key with an explicit None value would be misread as
+ # unchanged and let a masked secret slip through alongside it. A
+ # sentinel default makes that distinction explicit. Iterating only the
+ # submitted keys would also miss a REMOVED key: the update replaces the
+ # stored dictionary wholesale, so dropping an optional field while
+ # reusing the masked secret changes the effective configuration just as
+ # surely as editing one.
+ removed_keys = set(existing_configuration) - set(new_configuration)
+ if masked_keys and (
+ removed_keys
+ or any(
+ key not in masked_keys
+ and existing_configuration.get(key, _MISSING) != value
+ for key, value in new_configuration.items()
+ )
+ ):
+ raise SemanticLayerInvalidError(
+ "This update changes the configuration while reusing a stored "
+ "secret value (a masked field). Provide the real value for any "
+ "masked field to confirm a configuration change."
+ )
+
masked_reference: dict[str, Any] = mask_configuration(
layer_type, existing_configuration
)
diff --git a/superset/config.py b/superset/config.py
index 648e4f9fd0a..cd874d43908 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -2777,6 +2777,15 @@ DATABASE_OAUTH2_JWT_ALGORITHM = "HS256"
# Timeout when fetching access and refresh tokens.
DATABASE_OAUTH2_TIMEOUT = timedelta(seconds=30)
+# When True, the OAuth2 authorization/token endpoint URIs configured for a
+# database (either via DATABASE_OAUTH2_CLIENTS or, per-connection, via a
+# database's own encrypted_extra.oauth2_client_info) are permitted to target
+# hosts in private/internal IP ranges (RFC-1918, loopback, link-local).
+# Intended for deployments with a legitimately internal identity provider.
+# Leave False (the default) in any deployment where untrusted users can
+# create or edit database connections.
+DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS: bool = False
+
# Enable/disable CSP warning
CONTENT_SECURITY_POLICY_WARNING = True
diff --git a/superset/db_engine_specs/base.py b/superset/db_engine_specs/base.py
index 0f41a7317f3..a32aeb9f34d 100644
--- a/superset/db_engine_specs/base.py
+++ b/superset/db_engine_specs/base.py
@@ -35,11 +35,10 @@ from typing import (
TypedDict,
Union,
)
-from urllib.parse import urlencode, urljoin
+from urllib.parse import urlencode, urljoin, urlparse
from uuid import UUID, uuid4
import pandas as pd
-import requests
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from deprecation import deprecated
@@ -95,7 +94,12 @@ from superset.utils import core as utils, json
from superset.utils.core import ColumnSpec, GenericDataType, QuerySource
from superset.utils.hashing import hash_from_str
from superset.utils.json import redact_sensitive, reveal_sensitive
-from superset.utils.network import is_hostname_valid, is_port_open
+from superset.utils.network import (
+ get_ssrf_safe_requester,
+ is_hostname_valid,
+ is_port_open,
+ is_safe_host,
+)
from superset.utils.oauth2 import (
encode_oauth2_state,
generate_code_challenge,
@@ -901,6 +905,47 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
return config
+ @staticmethod
+ def _validate_oauth2_endpoint_host(uri: str) -> None:
+ """
+ Validate an OAuth2 authorization/token endpoint URI before it's used.
+
+ ``config["authorization_request_uri"]``/``config["token_request_uri"]``
+ can come from a database's own ``encrypted_extra.oauth2_client_info``
+ (editable by anyone with ``can_write`` on Database, not just the
+ deployment operator). The authorization URI is handed to the user's
+ browser as a redirect target; the token URI is POSTed to directly by
+ this server, carrying the connection's ``client_secret`` in the
+ request body. Neither is otherwise validated, so an attacker with
+ write access to one database's config could point either at an
+ internal host, exfiltrating the client secret (token URI) or using
+ Superset as an open redirect into the internal network (authorization
+ URI) -- and since the connection is typically shared, this is
+ exercised by every user who goes through that database's OAuth2 flow,
+ not just the one who configured it.
+
+ Operators with a legitimately internal IdP can opt out via
+ ``DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS`` -- but that flag only
+ widens which *hosts* are acceptable, not which URI *schemes* are;
+ a non-http(s) scheme is refused unconditionally.
+ """
+ try:
+ parsed = urlparse(uri)
+ except ValueError as ex:
+ # e.g. an unmatched IPv6 bracket -- urlparse raises rather than
+ # returning an unusable result.
+ raise OAuth2Error("Invalid OAuth2 endpoint URI") from ex
+
+ if parsed.scheme not in ("http", "https"):
+ raise OAuth2Error("Invalid OAuth2 endpoint URI")
+
+ if app.config["DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS"]:
+ return
+
+ if not parsed.hostname or not is_safe_host(parsed.hostname):
+ logger.warning("OAuth2 endpoint refused: target host is not allowed")
+ raise OAuth2Error("Invalid OAuth2 endpoint URI")
+
@classmethod
def get_oauth2_authorization_uri(
cls,
@@ -916,6 +961,7 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
(e.g., Google's prompt=consent).
"""
uri = config["authorization_request_uri"]
+ cls._validate_oauth2_endpoint_host(uri)
params: dict[str, str] = {
"scope": config["scope"],
"response_type": "code",
@@ -947,6 +993,7 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
"""
timeout = app.config["DATABASE_OAUTH2_TIMEOUT"].total_seconds()
uri = config["token_request_uri"]
+ cls._validate_oauth2_endpoint_host(uri)
req_body: dict[str, str] = {
"code": code,
"client_id": config["id"],
@@ -959,10 +1006,21 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
if code_verifier:
req_body["code_verifier"] = code_verifier
+ # `_validate_oauth2_endpoint_host` only checked the hostname; a
+ # server at that (safe) host could still respond with a 30x
+ # redirecting the actual request to an internal target, or a
+ # low-TTL DNS record could resolve differently by the time this
+ # connects (DNS rebinding). Don't follow redirects, and re-validate
+ # the address actually connected to.
+ requester = get_ssrf_safe_requester(
+ allow_unsafe_hosts=app.config["DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS"]
+ )
response = (
- requests.post(uri, data=req_body, timeout=timeout)
+ requester.post(uri, data=req_body, timeout=timeout, allow_redirects=False)
if config["request_content_type"] == "data"
- else requests.post(uri, json=req_body, timeout=timeout)
+ else requester.post(
+ uri, json=req_body, timeout=timeout, allow_redirects=False
+ )
)
response.raise_for_status()
return response.json()
@@ -978,16 +1036,26 @@ class BaseEngineSpec: # pylint: disable=too-many-public-methods
"""
timeout = app.config["DATABASE_OAUTH2_TIMEOUT"].total_seconds()
uri = config["token_request_uri"]
+ cls._validate_oauth2_endpoint_host(uri)
req_body = {
"client_id": config["id"],
"client_secret": config["secret"],
"refresh_token": refresh_token,
"grant_type": "refresh_token",
}
+ # See the matching comment in ``get_oauth2_token``: the hostname
+ # check above doesn't protect against a 30x redirect to an internal
+ # target or DNS rebinding, so route through the peer-validating
+ # requester and refuse to follow redirects.
+ requester = get_ssrf_safe_requester(
+ allow_unsafe_hosts=app.config["DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS"]
+ )
response = (
- requests.post(uri, data=req_body, timeout=timeout)
+ requester.post(uri, data=req_body, timeout=timeout, allow_redirects=False)
if config["request_content_type"] == "data"
- else requests.post(uri, json=req_body, timeout=timeout)
+ else requester.post(
+ uri, json=req_body, timeout=timeout, allow_redirects=False
+ )
)
if response.status_code in (400, 401, 403):
try:
diff --git a/superset/db_engine_specs/gsheets.py b/superset/db_engine_specs/gsheets.py
index a75c46eac71..3cf7971f943 100644
--- a/superset/db_engine_specs/gsheets.py
+++ b/superset/db_engine_specs/gsheets.py
@@ -197,6 +197,7 @@ class GSheetsEngineSpec(ShillelaghEngineSpec):
from superset.utils.oauth2 import encode_oauth2_state, generate_code_challenge
uri = config["authorization_request_uri"]
+ cls._validate_oauth2_endpoint_host(uri)
params: dict[str, str] = {
"scope": config["scope"],
"response_type": "code",
diff --git a/superset/db_engine_specs/snowflake.py b/superset/db_engine_specs/snowflake.py
index 63ae8886228..c1bcd86dca3 100644
--- a/superset/db_engine_specs/snowflake.py
+++ b/superset/db_engine_specs/snowflake.py
@@ -373,6 +373,7 @@ class SnowflakeEngineSpec(PostgresBaseEngineSpec):
Return URI for initial OAuth2 request.
"""
uri = config["authorization_request_uri"]
+ cls._validate_oauth2_endpoint_host(uri)
# When calling the Snowflake OAuth authorization endpoint for a custom client,
# specify only the query parameters documented in the URL below.
# Adding unsupported parameters
diff --git a/superset/security/api.py b/superset/security/api.py
index ccb6371526b..085fc6f7a28 100644
--- a/superset/security/api.py
+++ b/superset/security/api.py
@@ -37,6 +37,7 @@ from sqlalchemy import asc, desc
from sqlalchemy.orm import selectinload
from superset.commands.dashboard.embedded.exceptions import (
+ EmbeddedDashboardAccessDeniedError,
EmbeddedDashboardNotFoundError,
)
from superset.commands.exceptions import ForbiddenError
@@ -223,7 +224,9 @@ class SecurityRestApi(BaseSupersetApi):
"""
try:
body = guest_token_create_schema.load(request.json)
- self.appbuilder.sm.validate_guest_token_resources(body["resources"])
+ self.appbuilder.sm.validate_guest_token_resources(
+ body["resources"], datasets=body.get("datasets")
+ )
guest_token_validator_hook = current_app.config.get(
"GUEST_TOKEN_VALIDATOR_HOOK"
)
@@ -265,6 +268,14 @@ class SecurityRestApi(BaseSupersetApi):
return self.response(200, token=token)
except EmbeddedDashboardNotFoundError as error:
return self.response_400(message=error.message)
+ except EmbeddedDashboardAccessDeniedError as error:
+ # The minting principal is not entitled to the dashboard being
+ # scoped (see validate_guest_token_resources): an authorization
+ # denial, not a server fault, so answer 403 rather than letting
+ # @safe turn it into a logged 500.
+ # FAB 5.x: response_403() takes no message argument (unlike
+ # response_400), so build the 403 explicitly.
+ return self.response(403, message=error.message)
except ValidationError as error:
return self.response_400(message=error.messages)
diff --git a/superset/security/manager.py b/superset/security/manager.py
index 70bcaa55676..5398bb07de1 100644
--- a/superset/security/manager.py
+++ b/superset/security/manager.py
@@ -5464,10 +5464,12 @@ class SupersetSecurityManager( # pylint: disable=too-many-public-methods
audience = audience()
return audience
- @staticmethod
- def validate_guest_token_resources(resources: GuestTokenResources) -> None:
+ def validate_guest_token_resources(
+ self, resources: GuestTokenResources, datasets: Optional[list[int]] = None
+ ) -> None:
# pylint: disable=import-outside-toplevel
from superset.commands.dashboard.embedded.exceptions import (
+ EmbeddedDashboardAccessDeniedError,
EmbeddedDashboardNotFoundError,
)
from superset.daos.dashboard import EmbeddedDashboardDAO
@@ -5481,11 +5483,66 @@ class SupersetSecurityManager( # pylint: disable=too-many-public-methods
embedded = EmbeddedDashboardDAO.find_by_id(str(resource["id"]))
if not embedded:
raise EmbeddedDashboardNotFoundError()
+ dashboard = embedded.dashboard
elif not dashboard.embedded:
# A raw dashboard id must still reference an embedded dashboard;
# otherwise a guest token could be scoped to a non-embedded one.
raise EmbeddedDashboardNotFoundError()
+ # The caller minting the token must themselves be entitled to
+ # the dashboard being scoped. `grant_guest_token` is a
+ # coarse, instance-wide permission -- without this check, an
+ # operator who narrows it to a non-Admin role (a realistic
+ # "embedding backend service" grant) would let that
+ # principal mint a fully valid guest token for *any*
+ # embedded dashboard, not just ones they have access to.
+ try:
+ self.raise_for_access(dashboard=dashboard)
+ self._raise_for_guest_token_datasource_access(dashboard, datasets)
+ except SupersetSecurityException as ex:
+ raise EmbeddedDashboardAccessDeniedError() from ex
+
+ def _raise_for_guest_token_datasource_access(
+ self, dashboard: "Dashboard", datasets: Optional[list[int]]
+ ) -> None:
+ """
+ Require the minting principal to be entitled to every datasource the
+ guest token will grant, not merely to the dashboard.
+
+ A dashboard-scoped guest token reads every member datasource (or the
+ ``datasets`` allowlist, when the token carries one), whereas
+ ``raise_for_access(dashboard=...)`` is satisfied, for a dashboard
+ without explicit viewers, by access to any ONE member datasource. A
+ service role with ``grant_guest_token`` plus access to a single chart
+ could otherwise mint a token exposing charts it cannot read itself.
+ Callers whose dashboard entitlement already covers every member chart
+ (admin, editor, or a viewer of a published RBAC dashboard) need no
+ per-datasource check.
+ """
+ if self.is_admin() or self.is_editor(dashboard):
+ return
+ if dashboard.viewers and dashboard.published and self.is_viewer(dashboard):
+ return
+ seen: set[tuple[str | None, int | None]] = set()
+ for slc in dashboard.slices:
+ key = (slc.datasource_type, slc.datasource_id)
+ if key in seen:
+ continue
+ seen.add(key)
+ resolved = slc.resolved_datasource
+ if resolved is None:
+ # Unresolvable datasource: inaccessible, never absent (same
+ # stance as raise_for_access).
+ raise SupersetSecurityException(
+ self.get_dashboard_access_error_object(dashboard)
+ )
+ if datasets is not None and resolved.id not in datasets:
+ continue # the token will not grant this datasource
+ if not self.can_access_datasource(resolved):
+ raise SupersetSecurityException(
+ self.get_datasource_access_error_object(resolved)
+ )
+
def create_guest_access_token(
self,
user: GuestTokenUser,
diff --git a/superset/utils/network.py b/superset/utils/network.py
index 819a5ffbf25..86c8ce8030c 100644
--- a/superset/utils/network.py
+++ b/superset/utils/network.py
@@ -18,6 +18,12 @@ import ipaddress
import platform
import socket
import subprocess
+from typing import Any
+
+import requests
+from requests.adapters import HTTPAdapter
+from urllib3.connection import HTTPConnection, HTTPSConnection
+from urllib3.connectionpool import HTTPConnectionPool, HTTPSConnectionPool
# Networks that must never be reached via user-supplied hostnames.
# Includes loopback, RFC-1918 private ranges, link-local (covers cloud
@@ -44,6 +50,115 @@ PORT_TIMEOUT = 5
PING_TIMEOUT = 5
+class SSRFProtectionError(ConnectionError):
+ """
+ Raised when an outbound request's connected peer is not a public,
+ globally-routable address. Subclasses ``ConnectionError`` so it's
+ already covered by any caller that catches connection failures broadly
+ (and, when raised from within an active ``requests``/``urllib3``
+ connection attempt, surfaces to callers as a
+ ``requests.exceptions.ConnectionError``, since ``requests`` wraps
+ whatever a connection class raises during ``connect()``).
+ """
+
+
+def _raise_for_unsafe_peer(conn: HTTPConnection) -> None:
+ """
+ Validate that a connection's actual peer is publicly routable.
+
+ An upfront ``is_safe_host`` check resolves and validates the hostname
+ once, ahead of time; the connection opened here is resolved
+ independently and may reach a different address (DNS rebinding via a
+ low-TTL record), so the check has to be repeated against the address
+ actually connected to.
+ """
+ sock = conn.sock
+ if sock is None:
+ return
+ peer = sock.getpeername()[0]
+ if not is_safe_ip(ipaddress.ip_address(peer)):
+ raise SSRFProtectionError("Request target host is not allowed.")
+
+
+class _PeerValidatingHTTPConnection(HTTPConnection):
+ """HTTP connection that validates the peer address on connect."""
+
+ def connect(self) -> None:
+ super().connect()
+ _raise_for_unsafe_peer(self)
+
+
+class _PeerValidatingHTTPSConnection(HTTPSConnection):
+ """HTTPS connection that validates the peer address after the handshake."""
+
+ def connect(self) -> None:
+ super().connect()
+ _raise_for_unsafe_peer(self)
+
+
+class _PeerValidatingHTTPConnectionPool(HTTPConnectionPool):
+ ConnectionCls = _PeerValidatingHTTPConnection
+
+
+class _PeerValidatingHTTPSConnectionPool(HTTPSConnectionPool):
+ ConnectionCls = _PeerValidatingHTTPSConnection
+
+
+class PeerValidatingHTTPAdapter(HTTPAdapter):
+ """
+ Transport adapter that routes requests through connection classes which
+ validate the connected peer address, closing the TOCTOU window between
+ an upfront ``is_safe_host`` check and the connection ``send()`` actually
+ opens.
+
+ Mirrors the peer-validation approach already used for webhook alert/
+ report dispatch (``superset.reports.notifications.webhook``) and
+ dataset-import data URIs
+ (``superset.commands.dataset.importers.v1.utils``); factored out here so
+ other outbound-request call sites (e.g. OAuth2 token/authorization
+ endpoints) can reuse it instead of re-implementing it.
+ """
+
+ def init_poolmanager(self, *args: Any, **kwargs: Any) -> None:
+ super().init_poolmanager(*args, **kwargs)
+ # Assign a new dict rather than mutating the manager's dict in
+ # place -- the attribute otherwise aliases urllib3's module-global
+ # default scheme-to-pool-class mapping.
+ self.poolmanager.pool_classes_by_scheme = {
+ "http": _PeerValidatingHTTPConnectionPool,
+ "https": _PeerValidatingHTTPSConnectionPool,
+ }
+
+
+def get_ssrf_safe_requester(allow_unsafe_hosts: bool = False) -> Any:
+ """
+ Return a ``requests``-compatible object (the ``requests`` module, or a
+ ``Session``) for making an outbound request to a host that isn't fully
+ operator-controlled.
+
+ The returned session's transport re-validates the actually-connected
+ peer address on every request, closing the TOCTOU window an upfront
+ ``is_safe_host`` check alone leaves open. Pass ``allow_unsafe_hosts=True``
+ for deployments that intentionally target internal hosts (the caller is
+ still expected to have made that an explicit, documented opt-in).
+ """
+ if allow_unsafe_hosts:
+ return requests
+ session = requests.Session()
+ # Ignore HTTP(S)_PROXY / NO_PROXY from the environment. A proxied request
+ # goes through Requests' separately built ProxyManager, whose pools never
+ # use the peer-validating connection classes installed below -- and the
+ # peer we'd see there is the proxy itself, not the target -- so the
+ # rebinding check would be silently inactive. Deployments that must route
+ # these calls through a proxy opt in via ``allow_unsafe_hosts`` (which
+ # returns the plain ``requests`` module and honours the environment).
+ session.trust_env = False
+ adapter = PeerValidatingHTTPAdapter()
+ session.mount("http://", adapter)
+ session.mount("https://", adapter)
+ return session
+
+
def is_safe_ip(ip: ipaddress.IPv4Address | ipaddress.IPv6Address) -> bool:
"""
Return True if a single IP address is public and globally routable.
diff --git a/superset/views/utils.py b/superset/views/utils.py
index af982778fc7..63bb7037586 100644
--- a/superset/views/utils.py
+++ b/superset/views/utils.py
@@ -44,6 +44,7 @@ from superset.common.db_query_status import QueryStatus
from superset.exceptions import (
SerializationError,
SupersetException,
+ SupersetSecurityException,
)
from superset.extensions import security_manager
from superset.legacy import update_time_range
@@ -504,6 +505,17 @@ def get_dashboard_extra_filters(
):
return []
+ # Does the caller actually have access to this dashboard? A chart can
+ # legitimately be reused across multiple dashboards, so passing chart
+ # membership above isn't an entitlement check -- without this, a
+ # principal who owns/can access some chart also embedded on a
+ # dashboard they have no access to could pull that dashboard's default
+ # filter configuration into their own request.
+ try:
+ security_manager.raise_for_access(dashboard=dashboard)
+ except SupersetSecurityException:
+ return []
+
with contextlib.suppress(json.JSONDecodeError):
json_metadata = json.loads(dashboard.json_metadata)
native_filters = [
diff --git a/tests/integration_tests/security/api_tests.py b/tests/integration_tests/security/api_tests.py
index 4f7e67f7ffd..0dcdc13102e 100644
--- a/tests/integration_tests/security/api_tests.py
+++ b/tests/integration_tests/security/api_tests.py
@@ -17,12 +17,17 @@
# isort:skip_file
"""Tests for security api methods"""
+from unittest.mock import patch
+
import jwt
import pytest
from flask.ctx import AppContext
from flask_wtf.csrf import generate_csrf
from superset import db, security_manager
+from superset.commands.dashboard.embedded.exceptions import (
+ EmbeddedDashboardAccessDeniedError,
+)
from superset.daos.dashboard import EmbeddedDashboardDAO
from superset.models.dashboard import Dashboard
from superset.utils.urls import get_url_host
@@ -377,6 +382,29 @@ class TestSecurityGuestTokenApi(SupersetTestCase):
self.assert400(response)
+ def test_post_guest_token_dashboard_access_denied(self):
+ """
+ A minting principal that is not entitled to the scoped dashboard gets
+ a 403, not a 500: validate_guest_token_resources raises
+ EmbeddedDashboardAccessDeniedError and the endpoint must translate
+ it, since @safe would otherwise report it as a server error.
+ """
+ self.login(ADMIN_USERNAME)
+ user = {"username": "bob", "first_name": "Bob", "last_name": "Also Bob"}
+ resource = {"type": "dashboard", "id": "any-id"}
+ params = {"user": user, "resources": [resource], "rls": []}
+
+ with patch.object(
+ security_manager,
+ "validate_guest_token_resources",
+ side_effect=EmbeddedDashboardAccessDeniedError(),
+ ):
+ response = self.client.post(
+ self.uri, data=json.dumps(params), content_type="application/json"
+ )
+
+ self.assert403(response)
+
@pytest.mark.usefixtures("load_birth_names_dashboard_with_slices", scope="class")
class TestSecurityGuestTokenApiTokenValidator(SupersetTestCase):
diff --git a/tests/unit_tests/commands/databases/utils_test.py b/tests/unit_tests/commands/databases/utils_test.py
index a8fb3a36e54..31810305359 100644
--- a/tests/unit_tests/commands/databases/utils_test.py
+++ b/tests/unit_tests/commands/databases/utils_test.py
@@ -33,8 +33,11 @@ from superset.commands.database.utils import (
add_perm,
add_pvm,
add_vm,
+ oauth2_endpoint_rebind_unsafe,
ping,
)
+from superset.constants import PASSWORD_MASK
+from superset.utils import json
from tests.conftest import with_config
@@ -210,3 +213,58 @@ def test_add_pvm_existing(db_session: Session, mocker: MockerFixture):
assert result == mock_pvm
sm.find_permission_view_menu.assert_called_once_with("existinf_perm", "existing_vm")
db_session.add.assert_not_called()
+
+
+_OAUTH2_STORED = json.dumps(
+ {
+ "oauth2_client_info": {
+ "id": "client",
+ "secret": "real-secret",
+ "scope": "read",
+ "authorization_request_uri": "https://idp.example.com/authorize",
+ "token_request_uri": "https://idp.example.com/token",
+ }
+ }
+)
+
+
+def _oauth2_submitted(overrides: dict[str, str] | None = None) -> str:
+ info = json.loads(_OAUTH2_STORED)["oauth2_client_info"]
+ info["secret"] = PASSWORD_MASK
+ info.update(overrides or {})
+ return json.dumps({"oauth2_client_info": info})
+
+
+def test_oauth2_endpoint_rebind_unsafe_masked_secret_new_token_endpoint() -> None:
+ """Repointing token_request_uri while reusing the masked secret is unsafe:
+ the next exchange would post the real secret to the new host."""
+ assert oauth2_endpoint_rebind_unsafe(
+ _OAUTH2_STORED,
+ _oauth2_submitted({"token_request_uri": "https://other.example.org/token"}),
+ )
+
+
+def test_oauth2_endpoint_rebind_unsafe_allows_fresh_secret() -> None:
+ """A freshly supplied secret makes the endpoint change a deliberate one."""
+ assert not oauth2_endpoint_rebind_unsafe(
+ _OAUTH2_STORED,
+ _oauth2_submitted(
+ {
+ "token_request_uri": "https://other.example.org/token",
+ "secret": "new-secret",
+ }
+ ),
+ )
+
+
+def test_oauth2_endpoint_rebind_unsafe_masked_secret_unchanged_endpoints() -> None:
+ """The plain masked round-trip (nothing repointed) stays allowed."""
+ assert not oauth2_endpoint_rebind_unsafe(_OAUTH2_STORED, _oauth2_submitted())
+
+
+def test_oauth2_endpoint_rebind_unsafe_without_stored_client() -> None:
+ """Nothing stored means nothing can be carried over."""
+ assert not oauth2_endpoint_rebind_unsafe(
+ "{}",
+ _oauth2_submitted({"token_request_uri": "https://other.example.org/token"}),
+ )
diff --git a/tests/unit_tests/commands/semantic_layer/update_test.py b/tests/unit_tests/commands/semantic_layer/update_test.py
index 86cb4f92636..d6f13ac90bf 100644
--- a/tests/unit_tests/commands/semantic_layer/update_test.py
+++ b/tests/unit_tests/commands/semantic_layer/update_test.py
@@ -547,6 +547,102 @@ def test_unmask_configuration_missing_existing_key() -> None:
assert result == {"account": "test", "password": PASSWORD_MASK}
+def test_unmask_configuration_rejects_secret_reveal_with_changed_field() -> None:
+ """
+ A masked field must not be revealed in the same update that also
+ changes some other configuration field. An editor is entitled to edit
+ this connection, but not to see its real secret (that's the entire
+ reason the read path masks it) -- revealing it while also changing a
+ potentially destination-relevant field would poison the stored
+ configuration with the real secret attached to attacker-controlled
+ config, silently leaking it on the next legitimate use of this layer.
+ """
+ with pytest.raises(SemanticLayerInvalidError):
+ _unmask_configuration(
+ '{"account": "prod-account", "password": "hunter2"}',
+ {"account": "attacker-account", "password": PASSWORD_MASK},
+ "test_provider",
+ )
+
+
+def test_unmask_configuration_rejects_secret_reveal_with_new_none_valued_key() -> None:
+ """
+ A newly introduced key with an explicit ``None`` value must be treated
+ as a configuration change, the same as any other new/changed key --
+ ``dict.get(key)`` alone can't distinguish "key absent from storage" from
+ "key present and stored as None" (both return None), which would let
+ this slip through as "unchanged" and reveal the masked secret alongside
+ it.
+ """
+ with pytest.raises(SemanticLayerInvalidError):
+ _unmask_configuration(
+ '{"account": "prod-account", "password": "hunter2"}',
+ {
+ "account": "prod-account",
+ "password": PASSWORD_MASK,
+ "proxy_host": None,
+ },
+ "test_provider",
+ )
+
+
+def test_unmask_configuration_rejects_secret_reveal_with_removed_key() -> None:
+ """
+ Dropping a stored key while reusing the masked secret is a configuration
+ change too: the update replaces the stored dictionary wholesale, so the
+ key really is gone afterwards while the real secret is carried over.
+ Comparing only the submitted keys would let that through.
+ """
+ with pytest.raises(SemanticLayerInvalidError):
+ _unmask_configuration(
+ '{"account": "prod-account", "region": "eu-west-1", "password": "hunter2"}',
+ {"account": "prod-account", "password": PASSWORD_MASK},
+ "test_provider",
+ )
+
+
+def test_unmask_configuration_allows_removed_key_with_fresh_secret() -> None:
+ """
+ Dropping a stored key is still allowed when a genuinely fresh secret is
+ supplied alongside it -- only the masked round-trip is restricted.
+ """
+ result = _unmask_configuration(
+ '{"account": "prod-account", "region": "eu-west-1", "password": "hunter2"}',
+ {"account": "prod-account", "password": "fresh-secret"},
+ "test_provider",
+ )
+
+ assert result == {"account": "prod-account", "password": "fresh-secret"}
+
+
+def test_unmask_configuration_allows_fresh_secret_with_changed_field() -> None:
+ """
+ A deliberate configuration change is still possible when a genuinely
+ fresh (non-masked) secret is supplied alongside it.
+ """
+ result = _unmask_configuration(
+ '{"account": "prod-account", "password": "hunter2"}',
+ {"account": "new-account", "password": "fresh-secret"},
+ "test_provider",
+ )
+
+ assert result == {"account": "new-account", "password": "fresh-secret"}
+
+
+def test_unmask_configuration_allows_unrelated_field_addition_with_no_mask() -> None:
+ """
+ Adding/changing fields with no masked value present at all is
+ unaffected -- there's no secret being reused, so nothing to protect.
+ """
+ result = _unmask_configuration(
+ '{"account": "prod-account"}',
+ {"account": "new-account", "extra_option": "value"},
+ "test_provider",
+ )
+
+ assert result == {"account": "new-account", "extra_option": "value"}
+
+
def test_update_semantic_layer_preserves_masked_secret_end_to_end(
mocker: MockerFixture,
) -> None:
diff --git a/tests/unit_tests/db_engine_specs/test_base.py b/tests/unit_tests/db_engine_specs/test_base.py
index 7a649b8dbd4..3bb258fb199 100644
--- a/tests/unit_tests/db_engine_specs/test_base.py
+++ b/tests/unit_tests/db_engine_specs/test_base.py
@@ -39,7 +39,7 @@ from superset.db_engine_specs.base import (
convert_inspector_columns,
)
from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
-from superset.exceptions import OAuth2RedirectError
+from superset.exceptions import OAuth2Error, OAuth2RedirectError
from superset.sql.parse import Table
from superset.superset_typing import (
OAuth2ClientConfig,
@@ -971,6 +971,18 @@ def test_extract_errors_no_match_falls_back(mocker: MockerFixture) -> None:
assert result == [expected]
+@pytest.fixture(autouse=True)
+def _mock_safe_oauth2_host(mocker: MockerFixture) -> None:
+ """
+ OAuth2 endpoint URIs are now validated via ``is_safe_host`` (real DNS
+ resolution) before use. The test fixtures below use non-resolving
+ example hostnames, so mock it the same way test_impala.py mocks it for
+ its own SSRF check; SSRF-rejection behavior itself is covered by
+ dedicated tests further down that override this per-test.
+ """
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
+
+
def test_get_oauth2_authorization_uri_standard_params(mocker: MockerFixture) -> None:
"""
Test that BaseEngineSpec.get_oauth2_authorization_uri uses standard OAuth 2.0
@@ -1067,7 +1079,7 @@ def test_get_oauth2_token_without_pkce(mocker: MockerFixture) -> None:
"""
from superset.db_engine_specs.base import BaseEngineSpec
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.json.return_value = {
"access_token": "test-access-token", # noqa: S105
"expires_in": 3600,
@@ -1100,7 +1112,7 @@ def test_get_oauth2_token_with_pkce(mocker: MockerFixture) -> None:
from superset.db_engine_specs.base import BaseEngineSpec
from superset.utils.oauth2 import generate_code_verifier
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.json.return_value = {
"access_token": "test-access-token", # noqa: S105
"expires_in": 3600,
@@ -1184,7 +1196,7 @@ def test_get_oauth2_token_additional_params(mocker: MockerFixture) -> None:
"audience": "https://api.example.com",
}
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.json.return_value = {
"access_token": "test-access-token", # noqa: S105
"expires_in": 3600,
@@ -1221,7 +1233,7 @@ def test_get_oauth2_fresh_token_success(mocker: MockerFixture) -> None:
"""
from superset.db_engine_specs.base import BaseEngineSpec
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.status_code = 200
mock_post.return_value.json.return_value = {
"access_token": "new-access-token",
@@ -1252,7 +1264,7 @@ def test_get_oauth2_fresh_token_raises_on_invalid_grant(
from superset.db_engine_specs.base import BaseEngineSpec
from superset.exceptions import OAuth2TokenRefreshError
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.status_code = 400
mock_post.return_value.json.return_value = {"error": "invalid_grant"}
@@ -1286,7 +1298,7 @@ def test_get_oauth2_fresh_token_preserves_token_on_ambiguous_error(
from superset.db_engine_specs.base import BaseEngineSpec
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.status_code = status_code
mock_post.return_value.json.return_value = {"error": error}
mock_post.return_value.raise_for_status.side_effect = HTTPError()
@@ -1315,7 +1327,7 @@ def test_get_oauth2_fresh_token_raises_on_server_error(mocker: MockerFixture) ->
from superset.db_engine_specs.base import BaseEngineSpec
- mock_post = mocker.patch("superset.db_engine_specs.base.requests.post")
+ mock_post = _mock_requester(mocker).return_value.post
mock_post.return_value.status_code = 500
mock_post.return_value.raise_for_status.side_effect = HTTPError("500 Server Error")
@@ -1333,6 +1345,220 @@ def test_get_oauth2_fresh_token_raises_on_server_error(mocker: MockerFixture) ->
BaseEngineSpec.get_oauth2_fresh_token(config, "refresh-token")
+def _mock_requester(mocker: MockerFixture) -> Any:
+ """
+ Patch ``get_ssrf_safe_requester`` where ``base.py`` looks it up and
+ return the mock, so callers can configure ``.return_value.post`` and/or
+ assert whether (and how) a requester was obtained at all.
+ """
+ return mocker.patch("superset.db_engine_specs.base.get_ssrf_safe_requester")
+
+
+def _oauth2_config_targeting(uri: str) -> OAuth2ClientConfig:
+ return {
+ "id": "client-id",
+ "secret": "client-secret",
+ "scope": "read write",
+ "redirect_uri": "http://localhost:8088/api/v1/database/oauth2/",
+ "authorization_request_uri": uri,
+ "token_request_uri": uri,
+ "request_content_type": "json",
+ }
+
+
+def test_get_oauth2_token_rejects_unsafe_host(mocker: MockerFixture) -> None:
+ """
+ ``token_request_uri`` can come from a database's own
+ ``encrypted_extra.oauth2_client_info`` (editable by anyone with
+ ``can_write`` on Database), and is POSTed to directly by this server
+ carrying the connection's client_secret. An internal/private target
+ must be refused rather than silently reaching it.
+ """
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=False)
+ mock_get_requester = _mock_requester(mocker)
+
+ config = _oauth2_config_targeting("http://169.254.169.254/latest/meta-data/")
+
+ with pytest.raises(OAuth2Error):
+ BaseEngineSpec.get_oauth2_token(config, "code")
+
+ mock_get_requester.assert_not_called()
+
+
+def test_get_oauth2_fresh_token_rejects_unsafe_host(mocker: MockerFixture) -> None:
+ """
+ Same protection as ``get_oauth2_token``, for the refresh-token exchange.
+ """
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=False)
+ mock_get_requester = _mock_requester(mocker)
+
+ config = _oauth2_config_targeting("http://10.0.0.5/token")
+
+ with pytest.raises(OAuth2Error):
+ BaseEngineSpec.get_oauth2_fresh_token(config, "refresh-token")
+
+ mock_get_requester.assert_not_called()
+
+
+def test_get_oauth2_authorization_uri_rejects_unsafe_host(
+ mocker: MockerFixture,
+) -> None:
+ """
+ ``authorization_request_uri`` is handed to the user's browser as a
+ redirect target; an internal host would turn Superset into an open
+ redirect into the internal network.
+ """
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=False)
+
+ config = _oauth2_config_targeting("http://192.168.1.1/authorize")
+ state: OAuth2State = {
+ "database_id": 1,
+ "user_id": 1,
+ "default_redirect_uri": "http://localhost:8088/api/v1/oauth2/",
+ "tab_id": "1234",
+ }
+
+ with pytest.raises(OAuth2Error):
+ BaseEngineSpec.get_oauth2_authorization_uri(config, state)
+
+
+def test_oauth2_endpoint_rejects_non_http_scheme(mocker: MockerFixture) -> None:
+ """
+ A non-http(s) scheme is refused outright, before any host resolution.
+ """
+ is_safe_host = mocker.patch("superset.db_engine_specs.base.is_safe_host")
+ mock_get_requester = _mock_requester(mocker)
+
+ config = _oauth2_config_targeting("file:///etc/passwd")
+
+ with pytest.raises(OAuth2Error):
+ BaseEngineSpec.get_oauth2_token(config, "code")
+
+ is_safe_host.assert_not_called()
+ mock_get_requester.assert_not_called()
+
+
+def test_oauth2_endpoint_allows_internal_host_when_configured(
+ mocker: MockerFixture,
+) -> None:
+ """
+ Operators with a legitimately internal IdP can opt out via
+ DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS -- the host is not even checked
+ once that's set.
+ """
+ mocker.patch.dict(
+ "superset.db_engine_specs.base.app.config",
+ {"DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS": True},
+ )
+ is_safe_host = mocker.patch("superset.db_engine_specs.base.is_safe_host")
+ mock_get_requester = _mock_requester(mocker)
+ mock_post = mock_get_requester.return_value.post
+ mock_post.return_value.json.return_value = {
+ "access_token": "access-token",
+ "expires_in": 3600,
+ }
+
+ config = _oauth2_config_targeting("http://10.0.0.5/token")
+
+ BaseEngineSpec.get_oauth2_token(config, "code")
+
+ is_safe_host.assert_not_called()
+ mock_get_requester.assert_called_once_with(allow_unsafe_hosts=True)
+ mock_post.assert_called_once()
+
+
+def test_oauth2_endpoint_scheme_check_applies_even_with_internal_hosts_allowed(
+ mocker: MockerFixture,
+) -> None:
+ """
+ DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS widens which *hosts* are acceptable,
+ not which URI *schemes* are -- a non-http(s) scheme (e.g. ``file://``)
+ must still be refused even when that flag is set.
+ """
+ mocker.patch.dict(
+ "superset.db_engine_specs.base.app.config",
+ {"DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS": True},
+ )
+ is_safe_host = mocker.patch("superset.db_engine_specs.base.is_safe_host")
+ mock_get_requester = _mock_requester(mocker)
+
+ config = _oauth2_config_targeting("file:///etc/passwd")
+
+ with pytest.raises(OAuth2Error):
+ BaseEngineSpec.get_oauth2_token(config, "code")
+
+ is_safe_host.assert_not_called()
+ mock_get_requester.assert_not_called()
+
+
+def test_oauth2_endpoint_malformed_uri_raises_oauth2_error(
+ mocker: MockerFixture,
+) -> None:
+ """
+ ``urlparse`` raises a bare ``ValueError`` (not caught anywhere upstream
+ of ``get_oauth2_authorization_uri``) for malformed IPv6 bracket syntax.
+ That must surface as ``OAuth2Error`` rather than an uncaught ValueError.
+ """
+ is_safe_host = mocker.patch("superset.db_engine_specs.base.is_safe_host")
+
+ config = _oauth2_config_targeting("http://[::1/authorize")
+ state: OAuth2State = {
+ "database_id": 1,
+ "user_id": 1,
+ "default_redirect_uri": "http://localhost:8088/api/v1/oauth2/",
+ "tab_id": "1234",
+ }
+
+ with pytest.raises(OAuth2Error):
+ BaseEngineSpec.get_oauth2_authorization_uri(config, state)
+
+ is_safe_host.assert_not_called()
+
+
+def test_get_oauth2_token_does_not_follow_redirects(mocker: MockerFixture) -> None:
+ """
+ A hostname check alone doesn't stop a server at that (safe) host from
+ responding with a 30x that redirects the actual request -- carrying
+ ``client_secret`` -- to an internal target. The request must be made
+ with ``allow_redirects=False``.
+ """
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
+ mock_get_requester = _mock_requester(mocker)
+ mock_post = mock_get_requester.return_value.post
+ mock_post.return_value.json.return_value = {
+ "access_token": "access-token",
+ "expires_in": 3600,
+ }
+
+ config = _oauth2_config_targeting("https://oauth.example.com/token")
+
+ BaseEngineSpec.get_oauth2_token(config, "code")
+
+ mock_get_requester.assert_called_once_with(allow_unsafe_hosts=False)
+ assert mock_post.call_args.kwargs["allow_redirects"] is False
+
+
+def test_get_oauth2_fresh_token_does_not_follow_redirects(
+ mocker: MockerFixture,
+) -> None:
+ """Same protection as ``get_oauth2_token``, for the refresh-token exchange."""
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
+ mock_get_requester = _mock_requester(mocker)
+ mock_post = mock_get_requester.return_value.post
+ mock_post.return_value.status_code = 200
+ mock_post.return_value.json.return_value = {
+ "access_token": "access-token",
+ "expires_in": 3600,
+ }
+
+ config = _oauth2_config_targeting("https://oauth.example.com/token")
+
+ BaseEngineSpec.get_oauth2_fresh_token(config, "refresh-token")
+
+ mock_get_requester.assert_called_once_with(allow_unsafe_hosts=False)
+ assert mock_post.call_args.kwargs["allow_redirects"] is False
+
+
def test_start_oauth2_dance_uses_config_redirect_uri(mocker: MockerFixture) -> None:
"""
Test that start_oauth2_dance uses DATABASE_OAUTH2_REDIRECT_URI config if set.
@@ -1345,6 +1571,7 @@ def test_start_oauth2_dance_uses_config_redirect_uri(mocker: MockerFixture) -> N
"DATABASE_OAUTH2_REDIRECT_URI": custom_redirect_uri,
"SECRET_KEY": "test-secret-key",
"DATABASE_OAUTH2_JWT_ALGORITHM": "HS256",
+ "DATABASE_OAUTH2_ALLOW_INTERNAL_HOSTS": True,
},
)
mocker.patch("superset.daos.key_value.KeyValueDAO")
diff --git a/tests/unit_tests/db_engine_specs/test_databricks.py b/tests/unit_tests/db_engine_specs/test_databricks.py
index c08d65fba2a..fe147df0928 100644
--- a/tests/unit_tests/db_engine_specs/test_databricks.py
+++ b/tests/unit_tests/db_engine_specs/test_databricks.py
@@ -867,7 +867,10 @@ def test_get_oauth2_token_native(
"""
Test `get_oauth2_token` for Native engine.
"""
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "access-token",
"expires_in": 3600,
@@ -895,6 +898,7 @@ def test_get_oauth2_token_native(
"grant_type": "authorization_code",
},
timeout=30.0,
+ allow_redirects=False,
)
@@ -905,7 +909,10 @@ def test_get_oauth2_token_python(
"""
Test `get_oauth2_token` for Python Connector engine.
"""
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "access-token",
"expires_in": 3600,
@@ -933,6 +940,7 @@ def test_get_oauth2_token_python(
"grant_type": "authorization_code",
},
timeout=30.0,
+ allow_redirects=False,
)
@@ -943,7 +951,10 @@ def test_get_oauth2_fresh_token_native(
"""
Test `get_oauth2_fresh_token` for Native engine.
"""
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "new-access-token",
"expires_in": 3600,
@@ -970,6 +981,7 @@ def test_get_oauth2_fresh_token_native(
"grant_type": "refresh_token",
},
timeout=30.0,
+ allow_redirects=False,
)
@@ -1027,6 +1039,10 @@ def test_get_oauth2_authorization_uri_derives_from_workspace_host(
database = mocker.MagicMock()
database.url_object.host = host
mocker.patch("superset.db.session.get", return_value=database)
+ # is_safe_host does live DNS resolution; whether these fixture hosts
+ # happen to resolve depends on real-world DNS state outside test
+ # control, so pin it rather than relying on that.
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
url = spec.get_oauth2_authorization_uri(
_unresolved_oauth2_config(), _oauth2_state()
@@ -1102,7 +1118,10 @@ def test_get_oauth2_fresh_token_python(
"""
Test `get_oauth2_fresh_token` for Python Connector engine.
"""
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "new-access-token",
"expires_in": 3600,
@@ -1129,6 +1148,7 @@ def test_get_oauth2_fresh_token_python(
"grant_type": "refresh_token",
},
timeout=30.0,
+ allow_redirects=False,
)
diff --git a/tests/unit_tests/db_engine_specs/test_databricks_multi_cloud.py b/tests/unit_tests/db_engine_specs/test_databricks_multi_cloud.py
index 65f8218e3a1..948d97655b7 100644
--- a/tests/unit_tests/db_engine_specs/test_databricks_multi_cloud.py
+++ b/tests/unit_tests/db_engine_specs/test_databricks_multi_cloud.py
@@ -91,6 +91,10 @@ def test_get_oauth2_authorization_uri_uses_workspace_host(
"superset.db.session.get",
return_value=_mock_database(mocker, host),
)
+ # is_safe_host does live DNS resolution; whether these fixture hosts
+ # happen to resolve depends on real-world DNS state outside test
+ # control, so pin it rather than relying on that.
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
state: OAuth2State = {
"database_id": 1,
diff --git a/tests/unit_tests/db_engine_specs/test_gsheets.py b/tests/unit_tests/db_engine_specs/test_gsheets.py
index 5981caa85c6..d43d210ea64 100644
--- a/tests/unit_tests/db_engine_specs/test_gsheets.py
+++ b/tests/unit_tests/db_engine_specs/test_gsheets.py
@@ -707,7 +707,10 @@ def test_get_oauth2_token(
"""
from superset.db_engine_specs.gsheets import GSheetsEngineSpec
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "access-token",
"expires_in": 3600,
@@ -733,6 +736,7 @@ def test_get_oauth2_token(
"grant_type": "authorization_code",
},
timeout=30.0,
+ allow_redirects=False,
)
@@ -745,7 +749,10 @@ def test_get_oauth2_fresh_token(
"""
from superset.db_engine_specs.gsheets import GSheetsEngineSpec
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "access-token",
"expires_in": 3600,
@@ -770,6 +777,7 @@ def test_get_oauth2_fresh_token(
"grant_type": "refresh_token",
},
timeout=30.0,
+ allow_redirects=False,
)
@@ -904,7 +912,10 @@ def test_get_oauth2_fresh_token_success(
"""
from superset.db_engine_specs.gsheets import GSheetsEngineSpec
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "new-access-token",
"expires_in": 3600,
@@ -928,7 +939,10 @@ def test_get_oauth2_fresh_token_invalid_grant(
"""
from superset.db_engine_specs.gsheets import GSheetsEngineSpec
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().status_code = 400
requests.post().text = (
'{"error": "invalid_grant",'
@@ -955,7 +969,10 @@ def test_get_oauth2_fresh_token_other_http_error(
http_error = HTTPError()
http_error.response = mock_response
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().raise_for_status.side_effect = http_error
with pytest.raises(HTTPError):
diff --git a/tests/unit_tests/db_engine_specs/test_snowflake.py b/tests/unit_tests/db_engine_specs/test_snowflake.py
index c7f14e9e337..b7a33b7110d 100644
--- a/tests/unit_tests/db_engine_specs/test_snowflake.py
+++ b/tests/unit_tests/db_engine_specs/test_snowflake.py
@@ -542,7 +542,11 @@ def test_get_oauth2_token(
"""
from superset.db_engine_specs.snowflake import SnowflakeEngineSpec
- requests: mock.MagicMock = mocker.patch("superset.db_engine_specs.base.requests")
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
+ mock_get_requester: mock.MagicMock = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "access-token",
"expires_in": 3600,
@@ -568,6 +572,7 @@ def test_get_oauth2_token(
"grant_type": "authorization_code",
},
timeout=30.0,
+ allow_redirects=False,
)
diff --git a/tests/unit_tests/db_engine_specs/test_trino.py b/tests/unit_tests/db_engine_specs/test_trino.py
index 0d744d16ae1..343efef4e11 100644
--- a/tests/unit_tests/db_engine_specs/test_trino.py
+++ b/tests/unit_tests/db_engine_specs/test_trino.py
@@ -1109,7 +1109,11 @@ def test_get_oauth2_token(
"""
from superset.db_engine_specs.trino import TrinoEngineSpec
- requests = mocker.patch("superset.db_engine_specs.base.requests")
+ mocker.patch("superset.db_engine_specs.base.is_safe_host", return_value=True)
+ mock_get_requester = mocker.patch(
+ "superset.db_engine_specs.base.get_ssrf_safe_requester"
+ )
+ requests = mock_get_requester.return_value
requests.post().json.return_value = {
"access_token": "access-token",
"expires_in": 3600,
@@ -1135,6 +1139,7 @@ def test_get_oauth2_token(
"grant_type": "authorization_code",
},
timeout=30.0,
+ allow_redirects=False,
)
diff --git a/tests/unit_tests/security/manager_test.py b/tests/unit_tests/security/manager_test.py
index e25e5a251db..71fe1611d39 100644
--- a/tests/unit_tests/security/manager_test.py
+++ b/tests/unit_tests/security/manager_test.py
@@ -4695,18 +4695,193 @@ def test_validate_guest_token_resources_rejects_non_embedded_int_id(
def test_validate_guest_token_resources_accepts_embedded_int_id(
app_context: None, mocker: MockerFixture
) -> None:
- """A raw int id for an embedded dashboard is accepted."""
+ """A raw int id for an embedded dashboard is accepted when the caller
+ minting the token is entitled to it."""
from superset.security.guest_token import GuestTokenResourceType
sm = SupersetSecurityManager(appbuilder)
embedded_dash = MagicMock()
embedded_dash.embedded = [MagicMock()] # embedded
mocker.patch("superset.models.dashboard.Dashboard.get", return_value=embedded_dash)
+ raise_for_access = mocker.patch.object(sm, "raise_for_access")
+ # An admin's dashboard entitlement covers every member datasource, so the
+ # per-datasource minting check is skipped (see the dedicated tests below).
+ mocker.patch.object(sm, "is_admin", return_value=True)
sm.validate_guest_token_resources(
[{"type": GuestTokenResourceType.DASHBOARD, "id": 5}]
)
+ raise_for_access.assert_called_once_with(dashboard=embedded_dash)
+
+
+def test_validate_guest_token_resources_rejects_unauthorized_dashboard(
+ app_context: None, mocker: MockerFixture
+) -> None:
+ """
+ Minting a guest token for a dashboard the calling principal is not
+ themselves entitled to must be refused. Without this, a non-Admin role
+ granted only the coarse `can_grant_guest_token` permission (a realistic
+ "embedding backend service" grant, since SECURITY.md treats an
+ operator-narrowed permission as shifting the boundary, not redefining
+ the model) could mint a valid guest token scoped to *any* embedded
+ dashboard in the instance, not just ones it can see.
+ """
+ from superset.commands.dashboard.embedded.exceptions import (
+ EmbeddedDashboardAccessDeniedError,
+ )
+ from superset.exceptions import SupersetSecurityException
+ from superset.security.guest_token import GuestTokenResourceType
+
+ sm = SupersetSecurityManager(appbuilder)
+ embedded_dash = MagicMock()
+ embedded_dash.embedded = [MagicMock()] # embedded
+ mocker.patch("superset.models.dashboard.Dashboard.get", return_value=embedded_dash)
+ mocker.patch.object(
+ sm,
+ "raise_for_access",
+ side_effect=SupersetSecurityException(mocker.MagicMock()),
+ )
+
+ with pytest.raises(EmbeddedDashboardAccessDeniedError):
+ sm.validate_guest_token_resources(
+ [{"type": GuestTokenResourceType.DASHBOARD, "id": 5}]
+ )
+
+
+def _guest_token_dashboard_with_datasources(
+ mocker: MockerFixture, ids: list[int]
+) -> MagicMock:
+ """A published, non-RBAC dashboard whose slices resolve to datasources ``ids``."""
+ dashboard = MagicMock()
+ dashboard.embedded = [MagicMock()]
+ dashboard.viewers = []
+ dashboard.published = True
+ slices = []
+ for ds_id in ids:
+ slc = MagicMock()
+ slc.datasource_type = "table"
+ slc.datasource_id = ds_id
+ slc.resolved_datasource = MagicMock(id=ds_id)
+ slices.append(slc)
+ dashboard.slices = slices
+ mocker.patch("superset.models.dashboard.Dashboard.get", return_value=dashboard)
+ return dashboard
+
+
+def test_validate_guest_token_resources_requires_every_granted_datasource(
+ app_context: None, mocker: MockerFixture
+) -> None:
+ """
+ A dashboard-scoped guest token grants every member datasource, but
+ ``raise_for_access(dashboard=...)`` passes on a non-RBAC dashboard as soon
+ as the caller can read any ONE of them. Minting must therefore require
+ access to each datasource the token will grant.
+ """
+ from superset.commands.dashboard.embedded.exceptions import (
+ EmbeddedDashboardAccessDeniedError,
+ )
+ from superset.security.guest_token import GuestTokenResourceType
+
+ sm = SupersetSecurityManager(appbuilder)
+ _guest_token_dashboard_with_datasources(mocker, [1, 2])
+ mocker.patch.object(sm, "raise_for_access", return_value=None)
+ mocker.patch.object(sm, "is_admin", return_value=False)
+ mocker.patch.object(sm, "is_editor", return_value=False)
+ mocker.patch.object(sm, "is_viewer", return_value=False)
+ # Can read datasource 1 only.
+ mocker.patch.object(sm, "can_access_datasource", side_effect=lambda ds: ds.id == 1)
+
+ with pytest.raises(EmbeddedDashboardAccessDeniedError):
+ sm.validate_guest_token_resources(
+ [{"type": GuestTokenResourceType.DASHBOARD, "id": 5}]
+ )
+
+
+def test_validate_guest_token_resources_datasets_claim_limits_the_check(
+ app_context: None, mocker: MockerFixture
+) -> None:
+ """
+ When the token carries a ``datasets`` allowlist, only those datasources
+ are granted, so only those need to be accessible to the minting caller.
+ """
+ from superset.security.guest_token import GuestTokenResourceType
+
+ sm = SupersetSecurityManager(appbuilder)
+ _guest_token_dashboard_with_datasources(mocker, [1, 2])
+ mocker.patch.object(sm, "raise_for_access", return_value=None)
+ mocker.patch.object(sm, "is_admin", return_value=False)
+ mocker.patch.object(sm, "is_editor", return_value=False)
+ mocker.patch.object(sm, "is_viewer", return_value=False)
+ mocker.patch.object(sm, "can_access_datasource", side_effect=lambda ds: ds.id == 1)
+
+ # Restricted to datasource 1, which the caller can read: allowed.
+ sm.validate_guest_token_resources(
+ [{"type": GuestTokenResourceType.DASHBOARD, "id": 5}], datasets=[1]
+ )
+
+
+def test_validate_guest_token_resources_rbac_viewer_skips_datasource_check(
+ app_context: None, mocker: MockerFixture
+) -> None:
+ """
+ A viewer of a published RBAC dashboard is entitled to every member chart
+ through the dashboard itself, so no per-datasource check applies.
+ """
+ from superset.security.guest_token import GuestTokenResourceType
+
+ sm = SupersetSecurityManager(appbuilder)
+ dashboard = _guest_token_dashboard_with_datasources(mocker, [1, 2])
+ dashboard.viewers = [MagicMock()]
+ mocker.patch.object(sm, "raise_for_access", return_value=None)
+ mocker.patch.object(sm, "is_admin", return_value=False)
+ mocker.patch.object(sm, "is_editor", return_value=False)
+ mocker.patch.object(sm, "is_viewer", return_value=True)
+ can_access = mocker.patch.object(sm, "can_access_datasource", return_value=False)
+
+ sm.validate_guest_token_resources(
+ [{"type": GuestTokenResourceType.DASHBOARD, "id": 5}]
+ )
+ can_access.assert_not_called()
+
+
+def test_validate_guest_token_resources_checks_access_via_embedded_dao_fallback(
+ app_context: None, mocker: MockerFixture
+) -> None:
+ """
+ The same access check applies on the EmbeddedDashboardDAO lookup path
+ (a resource id that isn't a plain dashboard id -- e.g. the embedded
+ config's own uuid), not just the `Dashboard.get` path.
+ """
+ from superset.exceptions import SupersetSecurityException
+ from superset.security.guest_token import GuestTokenResourceType
+
+ sm = SupersetSecurityManager(appbuilder)
+ mocker.patch("superset.models.dashboard.Dashboard.get", return_value=None)
+ target_dashboard = MagicMock()
+ embedded = MagicMock()
+ embedded.dashboard = target_dashboard
+ mocker.patch(
+ "superset.daos.dashboard.EmbeddedDashboardDAO.find_by_id",
+ return_value=embedded,
+ )
+ raise_for_access = mocker.patch.object(
+ sm,
+ "raise_for_access",
+ side_effect=SupersetSecurityException(mocker.MagicMock()),
+ )
+
+ from superset.commands.dashboard.embedded.exceptions import (
+ EmbeddedDashboardAccessDeniedError,
+ )
+
+ with pytest.raises(EmbeddedDashboardAccessDeniedError):
+ sm.validate_guest_token_resources(
+ [{"type": GuestTokenResourceType.DASHBOARD, "id": "some-uuid"}]
+ )
+
+ raise_for_access.assert_called_once_with(dashboard=target_dashboard)
+
def test_is_editor_query_owner(mocker: MockerFixture, app_context: None) -> None:
"""
diff --git a/tests/unit_tests/utils/test_network.py b/tests/unit_tests/utils/test_network.py
index 2db44f8ba48..d81eb4ccdc0 100644
--- a/tests/unit_tests/utils/test_network.py
+++ b/tests/unit_tests/utils/test_network.py
@@ -15,11 +15,17 @@
# specific language governing permissions and limitations
# under the License.
import ipaddress
-from unittest.mock import patch
+from unittest.mock import MagicMock, patch
import pytest
-from superset.utils.network import is_safe_host, is_safe_ip
+from superset.utils.network import (
+ get_ssrf_safe_requester,
+ is_safe_host,
+ is_safe_ip,
+ PeerValidatingHTTPAdapter,
+ SSRFProtectionError,
+)
@pytest.mark.parametrize(
@@ -164,3 +170,74 @@ def test_is_safe_host_rejects_cgnat_range() -> None:
return_value=[(None, None, None, None, ("100.100.100.200", 0))],
):
assert is_safe_host("cgnat-host") is False
+
+
+def test_peer_validating_connection_blocks_rebound_peer() -> None:
+ """
+ A hostname that passes ``is_safe_host`` at validation time and then
+ re-resolves to an internal address by the time the connection is opened
+ (DNS rebinding) must be rejected before any request bytes are sent --
+ mirrors the equivalent test for webhook dispatch and dataset-import
+ data-URI fetches, which use the same pattern.
+ """
+ from urllib3.connection import HTTPConnection
+
+ from superset.utils.network import _PeerValidatingHTTPConnection
+
+ sock = MagicMock()
+ sock.getpeername.return_value = ("169.254.169.254", 80)
+
+ with patch.object(
+ HTTPConnection, "connect", lambda self: setattr(self, "sock", sock)
+ ):
+ conn = _PeerValidatingHTTPConnection("rebinder.example.com")
+ with pytest.raises(SSRFProtectionError):
+ conn.connect()
+
+
+def test_peer_validating_connection_allows_public_peer() -> None:
+ """A connection whose actual peer resolves to a public address is
+ allowed through unmodified."""
+ from urllib3.connection import HTTPConnection
+
+ from superset.utils.network import _PeerValidatingHTTPConnection
+
+ sock = MagicMock()
+ sock.getpeername.return_value = ("93.184.216.34", 80) # example.com, public
+
+ with patch.object(
+ HTTPConnection, "connect", lambda self: setattr(self, "sock", sock)
+ ):
+ conn = _PeerValidatingHTTPConnection("example.com")
+ conn.connect() # should not raise
+
+
+def test_get_ssrf_safe_requester_returns_plain_requests_when_allowed() -> None:
+ """
+ With ``allow_unsafe_hosts=True`` (an explicit, documented operator
+ opt-in), the plain ``requests`` module is returned -- no peer pinning.
+ """
+ import requests
+
+ assert get_ssrf_safe_requester(allow_unsafe_hosts=True) is requests
+
+
+def test_get_ssrf_safe_requester_ignores_environment_proxies() -> None:
+ """
+ An environment proxy would route the request through a ProxyManager whose
+ pools bypass the peer-validating connection classes (and whose peer is the
+ proxy, not the target), leaving the rebinding check inactive -- so the
+ protected session must not honour HTTP(S)_PROXY.
+ """
+ assert get_ssrf_safe_requester().trust_env is False
+
+
+def test_get_ssrf_safe_requester_pins_peer_by_default() -> None:
+ """
+ By default, ``get_ssrf_safe_requester`` returns a session whose adapters
+ validate the connected peer address rather than the plain ``requests``
+ module.
+ """
+ requester = get_ssrf_safe_requester()
+ assert isinstance(requester.get_adapter("http://x/"), PeerValidatingHTTPAdapter)
+ assert isinstance(requester.get_adapter("https://x/"), PeerValidatingHTTPAdapter)
diff --git a/tests/unit_tests/views/test_utils.py b/tests/unit_tests/views/test_utils.py
index 805b760c66a..b1f5d65ebc8 100644
--- a/tests/unit_tests/views/test_utils.py
+++ b/tests/unit_tests/views/test_utils.py
@@ -110,7 +110,10 @@ def test_get_dashboard_extra_filters_includes_native_filter_defaults(
db.session.add_all([chart, dashboard])
db.session.flush()
- with patch("superset.charts.data.dashboard_filter_context._check_dashboard_access"):
+ with (
+ patch("superset.charts.data.dashboard_filter_context._check_dashboard_access"),
+ patch("superset.views.utils.security_manager.raise_for_access"),
+ ):
extra_filters = get_dashboard_extra_filters(chart.id, dashboard.id)
assert extra_filters == [{"col": "region", "op": "IN", "val": ["APAC"]}]
@@ -123,6 +126,7 @@ def test_get_dashboard_extra_filters_includes_native_filter_defaults(
with (
patch("superset.charts.data.dashboard_filter_context._check_dashboard_access"),
+ patch("superset.views.utils.security_manager.raise_for_access"),
patch(
"superset.views.utils.build_extra_filters",
return_value=[legacy_filter],
@@ -134,3 +138,66 @@ def test_get_dashboard_extra_filters_includes_native_filter_defaults(
legacy_filter,
{"col": "region", "op": "IN", "val": ["APAC"]},
]
+
+
+def test_get_dashboard_extra_filters_denies_unauthorized_dashboard(
+ session: Session,
+) -> None:
+ """
+ A chart can legitimately be reused across multiple dashboards, so
+ chart-membership on a dashboard is not an entitlement check for that
+ dashboard. A caller who can access the chart but not the dashboard it's
+ also placed on must not have that dashboard's filter configuration
+ pulled into their request.
+ """
+ Dashboard.metadata.create_all(session.get_bind())
+
+ dataset = SqlaTable(
+ table_name="unauthorized_dash_table",
+ database=Database(
+ database_name="unauthorized_dash_db", sqlalchemy_uri="sqlite://"
+ ),
+ )
+ db.session.add(dataset)
+ db.session.flush()
+
+ chart = Slice(
+ slice_name="shared_chart",
+ datasource_id=dataset.id,
+ datasource_type="table",
+ )
+ dashboard = Dashboard(
+ dashboard_title="dashboard_caller_cant_access",
+ slices=[chart],
+ published=True,
+ json_metadata=json.dumps(
+ {
+ "default_filters": json.dumps(
+ {"legacy-filter": {"country": ["Brazil"]}}
+ ),
+ "filter_scopes": {},
+ }
+ ),
+ position_json="{}",
+ )
+ db.session.add_all([chart, dashboard])
+ db.session.flush()
+
+ from unittest.mock import MagicMock
+
+ from superset.exceptions import SupersetSecurityException
+
+ with (
+ patch("superset.charts.data.dashboard_filter_context._check_dashboard_access"),
+ patch(
+ "superset.views.utils.security_manager.raise_for_access",
+ side_effect=SupersetSecurityException(MagicMock()),
+ ),
+ patch(
+ "superset.views.utils.build_extra_filters",
+ return_value=[{"col": "country", "op": "in", "val": ["Brazil"]}],
+ ),
+ ):
+ extra_filters = get_dashboard_extra_filters(chart.id, dashboard.id)
+
+ assert extra_filters == []