You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@superset.apache.org by ru...@apache.org on 2026/06/02 00:05:46 UTC

(superset) branch fix/schema-output-and-input-hardening created (now f2be49e3ddc)

This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a change to branch fix/schema-output-and-input-hardening
in repository https://gitbox.apache.org/repos/asf/superset.git


      at f2be49e3ddc fix(schemas): tighten guest dataset fields, external_url protocols, ssh creds, prophet bounds

This branch includes the following new commits:

     new f2be49e3ddc fix(schemas): tighten guest dataset fields, external_url protocols, ssh creds, prophet bounds

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



(superset) 01/01: fix(schemas): tighten guest dataset fields, external_url protocols, ssh creds, prophet bounds

Posted by ru...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

rusackas pushed a commit to branch fix/schema-output-and-input-hardening
in repository https://gitbox.apache.org/repos/asf/superset.git

commit f2be49e3ddc35f7c10070bb7a8e79a0b45d6b627
Author: Claude Code <no...@anthropic.com>
AuthorDate: Mon Jun 1 17:05:26 2026 -0700

    fix(schemas): tighten guest dataset fields, external_url protocols, ssh creds, prophet bounds
    
    Several schema-hardening improvements:
    
    - DashboardDatasetSchema: guest users no longer receive sql, select_star,
      perm, edit_url, fetch_values_predicate or template_params in addition to
      the previously removed owners/database.
    - external_url fields on chart and dashboard schemas now reject any scheme
      other than http/https via a reusable validate_external_url validator.
    - DatabaseSSHTunnel credential fields (password, private_key,
      private_key_password) are marked load_only so they are accepted on input
      but never serialized in responses; masked response paths are unaffected.
    - ChartDataProphetOptionsSchema.periods now has an upper bound read from the
      new MAX_PROPHET_PERIODS config (default 10000).
    
    Adds unit tests covering each behavior.
    
    Co-Authored-By: Claude Opus 4.8 <no...@anthropic.com>
---
 superset/charts/schemas.py                  |  37 ++++++++--
 superset/config.py                          |   4 ++
 superset/dashboards/schemas.py              |  18 ++++-
 superset/databases/schemas.py               |   9 ++-
 superset/utils/schema.py                    |  28 +++++++-
 tests/unit_tests/charts/test_schemas.py     |  80 ++++++++++++++++++++++
 tests/unit_tests/dashboards/schema_tests.py | 100 ++++++++++++++++++++++++++++
 tests/unit_tests/databases/schema_tests.py  |  31 +++++++++
 8 files changed, 296 insertions(+), 11 deletions(-)

diff --git a/superset/charts/schemas.py b/superset/charts/schemas.py
index 0a603cbb6ec..6a39f295463 100644
--- a/superset/charts/schemas.py
+++ b/superset/charts/schemas.py
@@ -62,6 +62,34 @@ def get_time_grain_choices() -> Any:
     ]
 
 
+# Fallback upper bound for the number of Prophet forecast periods when the
+# application config cannot be read (for example, outside of an app context).
+DEFAULT_MAX_PROPHET_PERIODS = 10000
+
+
+def get_max_prophet_periods() -> int:
+    """Get the configured upper bound for Prophet forecast periods."""
+    try:
+        return current_app.config.get(
+            "MAX_PROPHET_PERIODS", DEFAULT_MAX_PROPHET_PERIODS
+        )
+    except RuntimeError:
+        # Outside app context, fall back to the default bound
+        return DEFAULT_MAX_PROPHET_PERIODS
+
+
+def validate_prophet_periods(value: int) -> None:
+    """Ensure the number of Prophet forecast periods stays within bounds."""
+    Range(
+        min=1,
+        max=get_max_prophet_periods(),
+        error=_(
+            "`periods` must be between 1 and %(max)s",
+            max=get_max_prophet_periods(),
+        ),
+    )(value)
+
+
 #
 # RISON/JSON schemas for query parameters
 #
@@ -242,7 +270,7 @@ class ChartPostSchema(Schema):
         metadata={"description": certification_details_description}, allow_none=True
     )
     is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
-    external_url = fields.String(allow_none=True)
+    external_url = fields.String(allow_none=True, validate=utils.validate_external_url)
     uuid = fields.UUID(allow_none=True)
 
 
@@ -300,7 +328,7 @@ class ChartPutSchema(Schema):
         metadata={"description": certification_details_description}, allow_none=True
     )
     is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
-    external_url = fields.String(allow_none=True)
+    external_url = fields.String(allow_none=True, validate=utils.validate_external_url)
     tags = fields.List(fields.Integer(metadata={"description": tags_description}))
     uuid = fields.UUID(allow_none=True)
 
@@ -656,8 +684,9 @@ class ChartDataProphetOptionsSchema(ChartDataPostProcessingOperationOptionsSchem
             "description": "Time periods (in units of `time_grain`) to predict into "
             "the future",
             "example": 7,
-            "min": 0,
+            "min": 1,
         },
+        validate=validate_prophet_periods,
         required=True,
     )
     confidence_interval = fields.Float(
@@ -1671,7 +1700,7 @@ class ImportV1ChartSchema(Schema):
     version = fields.String(required=True)
     dataset_uuid = fields.UUID(required=True)
     is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
-    external_url = fields.String(allow_none=True)
+    external_url = fields.String(allow_none=True, validate=utils.validate_external_url)
     tags = fields.List(fields.String(), allow_none=True)
 
 
diff --git a/superset/config.py b/superset/config.py
index cd64e54bcc8..85ec5035cca 100644
--- a/superset/config.py
+++ b/superset/config.py
@@ -1317,6 +1317,10 @@ MAPBOX_API_KEY = os.environ.get("MAPBOX_API_KEY", "")
 # Maximum number of rows returned for any analytical database query
 SQL_MAX_ROW = 100000
 
+# Maximum number of forecast periods accepted by the Prophet post-processing
+# operation. Bounds resource usage when predicting into the future.
+MAX_PROPHET_PERIODS = 10000
+
 # Maximum number of rows for any query with Server Pagination in Table Viz type
 TABLE_VIZ_MAX_ROW_SERVER = 500000
 
diff --git a/superset/dashboards/schemas.py b/superset/dashboards/schemas.py
index a71b90a1afd..f438e327e8f 100644
--- a/superset/dashboards/schemas.py
+++ b/superset/dashboards/schemas.py
@@ -23,6 +23,7 @@ from marshmallow.validate import Length, ValidationError
 from superset import security_manager
 from superset.tags.models import TagType
 from superset.utils import json
+from superset.utils.schema import validate_external_url
 
 get_delete_ids_schema = {"type": "array", "items": {"type": "integer"}}
 get_export_ids_schema = {"type": "array", "items": {"type": "integer"}}
@@ -317,6 +318,17 @@ class DashboardDatasetSchema(Schema):
         if security_manager.is_guest_user():
             del serialized["owners"]
             del serialized["database"]
+            # Guest users should never receive fields that expose internal
+            # connection or query details.
+            for key in (
+                "sql",
+                "select_star",
+                "perm",
+                "edit_url",
+                "fetch_values_predicate",
+                "template_params",
+            ):
+                serialized.pop(key, None)
         return serialized
 
 
@@ -376,7 +388,7 @@ class DashboardPostSchema(BaseDashboardSchema):
         metadata={"description": certification_details_description}, allow_none=True
     )
     is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
-    external_url = fields.String(allow_none=True)
+    external_url = fields.String(allow_none=True, validate=validate_external_url)
     uuid = fields.UUID(allow_none=True)
 
 
@@ -440,7 +452,7 @@ class DashboardPutSchema(BaseDashboardSchema):
         metadata={"description": certification_details_description}, allow_none=True
     )
     is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
-    external_url = fields.String(allow_none=True)
+    external_url = fields.String(allow_none=True, validate=validate_external_url)
     tags = fields.List(
         fields.Integer(metadata={"description": tags_description}, allow_none=True)
     )
@@ -512,7 +524,7 @@ class ImportV1DashboardSchema(Schema):
     metadata = fields.Dict()
     version = fields.String(required=True)
     is_managed_externally = fields.Boolean(allow_none=True, dump_default=False)
-    external_url = fields.String(allow_none=True)
+    external_url = fields.String(allow_none=True, validate=validate_external_url)
     certified_by = fields.String(allow_none=True)
     certification_details = fields.String(allow_none=True)
     published = fields.Boolean(allow_none=True)
diff --git a/superset/databases/schemas.py b/superset/databases/schemas.py
index b399a25ad7c..af848436b8d 100644
--- a/superset/databases/schemas.py
+++ b/superset/databases/schemas.py
@@ -454,11 +454,14 @@ class DatabaseSSHTunnel(Schema):
     username = fields.String()
 
     # Basic Authentication
-    password = fields.String(required=False)
+    # Credential fields are load-only: accepted on input but never serialized
+    # back in responses. Response paths that surface a masked placeholder do so
+    # explicitly (see SSHTunnel.data and mask_password_info).
+    password = fields.String(required=False, load_only=True)
 
     # password protected private key authentication
-    private_key = fields.String(required=False)
-    private_key_password = fields.String(required=False)
+    private_key = fields.String(required=False, load_only=True)
+    private_key_password = fields.String(required=False, load_only=True)
 
     @validates_schema
     def validate_authentication(self, data: dict[str, Any], **kwargs: Any) -> None:
diff --git a/superset/utils/schema.py b/superset/utils/schema.py
index 8994b87c1d9..05e561a77f3 100644
--- a/superset/utils/schema.py
+++ b/superset/utils/schema.py
@@ -14,12 +14,15 @@
 # KIND, either express or implied.  See the License for the
 # specific language governing permissions and limitations
 # under the License.
-from typing import Any, Union
+from typing import Any, Optional, Union
+from urllib.parse import urlparse
 
 from marshmallow import validate, ValidationError
 
 from superset.utils import json
 
+ALLOWED_URL_SCHEMES = {"http", "https"}
+
 
 class OneOfCaseInsensitive(validate.OneOf):
     """
@@ -51,3 +54,26 @@ def validate_json(value: Union[bytes, bytearray, str]) -> None:
         json.validate_json(value)
     except json.JSONDecodeError as ex:
         raise ValidationError("JSON not valid") from ex
+
+
+def validate_external_url(value: Optional[str]) -> None:
+    """
+    Validator for externally managed object URLs.
+
+    Restricts the accepted URL schemes to ``http`` and ``https`` so that
+    other schemes (for example ``javascript:``, ``data:`` or ``vbscript:``)
+    cannot be stored and later rendered by clients. Empty values are allowed
+    since the field is optional.
+
+    :param value: the URL to validate
+    :raises ValidationError: if the value uses a disallowed scheme
+    """
+    if not value:
+        return
+
+    scheme = urlparse(value).scheme.lower()
+    if scheme not in ALLOWED_URL_SCHEMES:
+        raise ValidationError(
+            "URL must use one of the following schemes: "
+            f"{', '.join(sorted(ALLOWED_URL_SCHEMES))}."
+        )
diff --git a/tests/unit_tests/charts/test_schemas.py b/tests/unit_tests/charts/test_schemas.py
index 5466a0deadd..20e41141b74 100644
--- a/tests/unit_tests/charts/test_schemas.py
+++ b/tests/unit_tests/charts/test_schemas.py
@@ -22,6 +22,7 @@ from marshmallow import ValidationError
 from superset.charts.schemas import (
     ChartDataProphetOptionsSchema,
     ChartDataQueryObjectSchema,
+    ChartPostSchema,
     get_time_grain_choices,
 )
 
@@ -152,3 +153,82 @@ def test_time_grain_validation_with_config_addons(app_context: None) -> None:
     }
     result = schema.load(custom_data)
     assert result["time_grain"] == "PT10M"
+
+
+def test_prophet_periods_within_bound(app_context: None) -> None:
+    """Prophet periods within the configured bound are accepted"""
+    schema = ChartDataProphetOptionsSchema()
+    result = schema.load(
+        {
+            "time_grain": "P1D",
+            "periods": 7,
+            "confidence_interval": 0.8,
+        }
+    )
+    assert result["periods"] == 7
+
+
+def test_prophet_periods_over_max_rejected(app_context: None) -> None:
+    """Prophet periods over the configured maximum raise a ValidationError"""
+    schema = ChartDataProphetOptionsSchema()
+    over_max = current_app.config.get("MAX_PROPHET_PERIODS", 10000) + 1
+    with pytest.raises(ValidationError) as exc_info:
+        schema.load(
+            {
+                "time_grain": "P1D",
+                "periods": over_max,
+                "confidence_interval": 0.8,
+            }
+        )
+    assert "periods" in exc_info.value.messages
+
+
+def test_prophet_periods_below_min_rejected(app_context: None) -> None:
+    """Prophet periods below 1 raise a ValidationError"""
+    schema = ChartDataProphetOptionsSchema()
+    with pytest.raises(ValidationError) as exc_info:
+        schema.load(
+            {
+                "time_grain": "P1D",
+                "periods": 0,
+                "confidence_interval": 0.8,
+            }
+        )
+    assert "periods" in exc_info.value.messages
+
+
+def test_chart_external_url_accepts_https(app_context: None) -> None:
+    """A valid https external_url is accepted"""
+    schema = ChartPostSchema()
+    result = schema.load(
+        {
+            "slice_name": "test",
+            "datasource_id": 1,
+            "datasource_type": "table",
+            "external_url": "https://example.com/managed",
+        }
+    )
+    assert result["external_url"] == "https://example.com/managed"
+
+
+@pytest.mark.parametrize(
+    "url",
+    [
+        "javascript:alert(1)",
+        "data:text/html,<script>alert(1)</script>",
+        "vbscript:msgbox(1)",
+    ],
+)
+def test_chart_external_url_rejects_non_http(app_context: None, url: str) -> None:
+    """external_url rejects non-http(s) schemes"""
+    schema = ChartPostSchema()
+    with pytest.raises(ValidationError) as exc_info:
+        schema.load(
+            {
+                "slice_name": "test",
+                "datasource_id": 1,
+                "datasource_type": "table",
+                "external_url": url,
+            }
+        )
+    assert "external_url" in exc_info.value.messages
diff --git a/tests/unit_tests/dashboards/schema_tests.py b/tests/unit_tests/dashboards/schema_tests.py
new file mode 100644
index 00000000000..4084f7e65ff
--- /dev/null
+++ b/tests/unit_tests/dashboards/schema_tests.py
@@ -0,0 +1,100 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+import pytest
+from marshmallow import ValidationError
+from pytest_mock import MockerFixture
+
+from superset.dashboards.schemas import DashboardDatasetSchema, DashboardPostSchema
+
+GUEST_RESTRICTED_FIELDS = [
+    "owners",
+    "database",
+    "sql",
+    "select_star",
+    "perm",
+    "edit_url",
+    "fetch_values_predicate",
+    "template_params",
+]
+
+
+def _dataset_payload() -> dict:
+    return {
+        "id": 1,
+        "database": {"id": 1, "name": "test_db"},
+        "owners": [{"id": 1}],
+        "sql": "SELECT 1",
+        "select_star": "SELECT * FROM t",
+        "perm": "[db].[table]",
+        "edit_url": "/edit/1",
+        "fetch_values_predicate": "1 = 1",
+        "template_params": "{}",
+        "table_name": "t",
+    }
+
+
+def test_dashboard_dataset_guest_filtering(mocker: MockerFixture) -> None:
+    """Guest users should not receive sensitive dataset fields."""
+    mocker.patch(
+        "superset.dashboards.schemas.security_manager.is_guest_user",
+        return_value=True,
+    )
+    result = DashboardDatasetSchema().dump(_dataset_payload())
+    for field in GUEST_RESTRICTED_FIELDS:
+        assert field not in result, f"{field} should be removed for guest users"
+    assert result["table_name"] == "t"
+
+
+def test_dashboard_dataset_non_guest_keeps_fields(mocker: MockerFixture) -> None:
+    """Non-guest users keep the sensitive dataset fields."""
+    mocker.patch(
+        "superset.dashboards.schemas.security_manager.is_guest_user",
+        return_value=False,
+    )
+    result = DashboardDatasetSchema().dump(_dataset_payload())
+    assert result["sql"] == "SELECT 1"
+    assert result["perm"] == "[db].[table]"
+    assert "database" in result
+
+
+def test_dashboard_external_url_accepts_https() -> None:
+    """A valid https external_url is accepted."""
+    schema = DashboardPostSchema()
+    result = schema.load(
+        {
+            "dashboard_title": "test",
+            "external_url": "https://example.com/managed",
+        }
+    )
+    assert result["external_url"] == "https://example.com/managed"
+
+
+@pytest.mark.parametrize(
+    "url",
+    [
+        "javascript:alert(1)",
+        "data:text/html,<script>alert(1)</script>",
+        "vbscript:msgbox(1)",
+    ],
+)
+def test_dashboard_external_url_rejects_non_http(url: str) -> None:
+    """external_url rejects non-http(s) schemes."""
+    schema = DashboardPostSchema()
+    with pytest.raises(ValidationError) as exc_info:
+        schema.load({"dashboard_title": "test", "external_url": url})
+    assert "external_url" in exc_info.value.messages
diff --git a/tests/unit_tests/databases/schema_tests.py b/tests/unit_tests/databases/schema_tests.py
index 1fd14343a8a..cd36d96bd00 100644
--- a/tests/unit_tests/databases/schema_tests.py
+++ b/tests/unit_tests/databases/schema_tests.py
@@ -378,3 +378,34 @@ def test_import_schema_allows_masked_fields_for_existing_db(
     }
     # Should not raise - masked values are allowed for existing DBs
     schema.load(config)
+
+
+def test_ssh_tunnel_credentials_load_only() -> None:
+    """
+    Credential fields on DatabaseSSHTunnel are accepted on input (load) but
+    never serialized in output (dump).
+    """
+    from superset.databases.schemas import DatabaseSSHTunnel
+
+    schema = DatabaseSSHTunnel()
+    payload = {
+        "server_address": "localhost",
+        "server_port": 22,
+        "username": "user",
+        "password": "secret",  # noqa: S106
+        "private_key": "PRIVATE",
+        "private_key_password": "keysecret",  # noqa: S106
+    }
+
+    # Load accepts the credential fields
+    loaded = schema.load(payload)
+    assert loaded["password"] == "secret"  # noqa: S105
+    assert loaded["private_key"] == "PRIVATE"
+    assert loaded["private_key_password"] == "keysecret"  # noqa: S105
+
+    # Dump never emits the credential fields
+    dumped = schema.dump(payload)
+    assert "password" not in dumped
+    assert "private_key" not in dumped
+    assert "private_key_password" not in dumped
+    assert dumped["server_address"] == "localhost"