mirror of
https://github.com/apache/superset.git
synced 2026-07-26 00:22:31 +00:00
Compare commits
1 Commits
feat/logou
...
feat/post-
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ace0fdeb55 |
@@ -22,7 +22,15 @@ from typing import Any, TYPE_CHECKING
|
||||
|
||||
from flask import current_app
|
||||
from flask_babel import gettext as _
|
||||
from marshmallow import EXCLUDE, fields, post_load, Schema, validate
|
||||
from marshmallow import (
|
||||
EXCLUDE,
|
||||
fields,
|
||||
post_load,
|
||||
Schema,
|
||||
validate,
|
||||
validates_schema,
|
||||
ValidationError,
|
||||
)
|
||||
from marshmallow.validate import Length, Range
|
||||
from marshmallow_union import Union
|
||||
|
||||
@@ -937,6 +945,42 @@ class ChartDataPostProcessingOperationSchema(Schema):
|
||||
},
|
||||
)
|
||||
|
||||
# Map post-processing operation -> its options schema, for operations that
|
||||
# declare one. Operations without a dedicated schema are not structurally
|
||||
# validated here.
|
||||
_OPTIONS_SCHEMAS: dict[str, type[Schema]] = {
|
||||
"aggregate": ChartDataAggregateOptionsSchema,
|
||||
"rolling": ChartDataRollingOptionsSchema,
|
||||
"select": ChartDataSelectOptionsSchema,
|
||||
"sort": ChartDataSortOptionsSchema,
|
||||
"contribution": ChartDataContributionOptionsSchema,
|
||||
"prophet": ChartDataProphetOptionsSchema,
|
||||
"boxplot": ChartDataBoxplotOptionsSchema,
|
||||
"pivot": ChartDataPivotOptionsSchema,
|
||||
"geohash_decode": ChartDataGeohashDecodeOptionsSchema,
|
||||
"geohash_encode": ChartDataGeohashEncodeOptionsSchema,
|
||||
"geodetic_parse": ChartDataGeodeticParseOptionsSchema,
|
||||
}
|
||||
|
||||
@validates_schema
|
||||
def validate_options(self, data: dict[str, Any], **kwargs: Any) -> None:
|
||||
"""Validate ``options`` against the operation's option schema.
|
||||
|
||||
Validation is lenient (unknown keys are ignored) so it surfaces wrong
|
||||
types / out-of-range values on declared fields without rejecting
|
||||
payloads that carry extra keys.
|
||||
"""
|
||||
operation = data.get("operation")
|
||||
options = data.get("options")
|
||||
if not isinstance(operation, str) or not isinstance(options, dict):
|
||||
return
|
||||
schema_cls = self._OPTIONS_SCHEMAS.get(operation)
|
||||
if schema_cls is None:
|
||||
return
|
||||
errors = schema_cls(unknown=EXCLUDE).validate(options)
|
||||
if errors:
|
||||
raise ValidationError({"options": errors})
|
||||
|
||||
|
||||
class ChartDataFilterSchema(Schema):
|
||||
col = fields.Raw(
|
||||
|
||||
@@ -671,13 +671,6 @@ class SupersetAppInitializer: # pylint: disable=too-many-public-methods
|
||||
"""Register app-level request handlers"""
|
||||
from flask import request, Response
|
||||
|
||||
from superset.security.session_validation import (
|
||||
register_inactive_user_logout,
|
||||
)
|
||||
|
||||
# End the session of any user who is deactivated mid-session.
|
||||
register_inactive_user_logout(self.superset_app)
|
||||
|
||||
@self.superset_app.after_request
|
||||
def apply_http_headers(response: Response) -> Response:
|
||||
"""Applies the configuration's http headers to all responses"""
|
||||
|
||||
@@ -1,74 +0,0 @@
|
||||
# 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.
|
||||
"""Terminate the session of a user who has been deactivated mid-session.
|
||||
|
||||
Flask-Login only consults ``is_active`` when establishing a login; for an
|
||||
already-authenticated user it does not re-check it on subsequent requests. So a
|
||||
user disabled by an administrator keeps their session until it expires. This
|
||||
hook re-checks ``is_active`` on each request and logs the user out as soon as
|
||||
their account is deactivated. (Deleted users are already handled: the user
|
||||
loader returns ``None`` and the request is anonymous.)
|
||||
"""
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
import logging
|
||||
from typing import Any
|
||||
|
||||
from flask import request
|
||||
from flask_login import current_user, logout_user
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Endpoints that must stay reachable for an anonymous/logging-out user.
|
||||
_EXEMPT_ENDPOINT_TOKENS = (
|
||||
"static",
|
||||
"appbuilder",
|
||||
"login",
|
||||
"logout",
|
||||
"auth",
|
||||
"health",
|
||||
)
|
||||
|
||||
|
||||
def _is_exempt_endpoint(endpoint: str | None) -> bool:
|
||||
if not endpoint:
|
||||
return True
|
||||
lowered = endpoint.lower()
|
||||
return any(token in lowered for token in _EXEMPT_ENDPOINT_TOKENS)
|
||||
|
||||
|
||||
def register_inactive_user_logout(app: Any) -> None:
|
||||
"""Register the before-request hook that logs out deactivated users."""
|
||||
|
||||
@app.before_request
|
||||
def _logout_inactive_user() -> None: # pylint: disable=unused-variable
|
||||
if _is_exempt_endpoint(request.endpoint):
|
||||
return
|
||||
|
||||
if not getattr(current_user, "is_authenticated", False):
|
||||
return
|
||||
|
||||
# ``is_active`` is False once an admin deactivates the account. End the
|
||||
# session now; the request then proceeds as anonymous and the normal
|
||||
# access controls deny protected views.
|
||||
if not current_user.is_active:
|
||||
logger.info(
|
||||
"Logging out deactivated user (id=%s)",
|
||||
getattr(current_user, "id", None),
|
||||
)
|
||||
logout_user()
|
||||
@@ -152,3 +152,53 @@ def test_time_grain_validation_with_config_addons(app_context: None) -> None:
|
||||
}
|
||||
result = schema.load(custom_data)
|
||||
assert result["time_grain"] == "PT10M"
|
||||
|
||||
|
||||
def test_post_processing_operation_validates_options(app_context: None) -> None:
|
||||
"""options are validated against the operation's option schema (leniently)."""
|
||||
from superset.charts.schemas import ChartDataPostProcessingOperationSchema
|
||||
|
||||
schema = ChartDataPostProcessingOperationSchema()
|
||||
|
||||
# Valid prophet options load.
|
||||
schema.load(
|
||||
{
|
||||
"operation": "prophet",
|
||||
"options": {
|
||||
"time_grain": "P1D",
|
||||
"periods": 7,
|
||||
"confidence_interval": 0.8,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
# Out-of-range confidence_interval (must be 0-1) on a declared field is
|
||||
# rejected.
|
||||
with pytest.raises(ValidationError) as exc_info:
|
||||
schema.load(
|
||||
{
|
||||
"operation": "prophet",
|
||||
"options": {
|
||||
"time_grain": "P1D",
|
||||
"periods": 7,
|
||||
"confidence_interval": 2.0,
|
||||
},
|
||||
}
|
||||
)
|
||||
assert "options" in exc_info.value.messages
|
||||
|
||||
# Extra/unknown keys are tolerated (lenient validation).
|
||||
schema.load(
|
||||
{
|
||||
"operation": "prophet",
|
||||
"options": {
|
||||
"time_grain": "P1D",
|
||||
"periods": 7,
|
||||
"confidence_interval": 0.8,
|
||||
"some_future_option": True,
|
||||
},
|
||||
}
|
||||
)
|
||||
|
||||
# An operation without a dedicated schema accepts arbitrary options.
|
||||
schema.load({"operation": "flatten", "options": {"anything": [1, 2, 3]}})
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
# 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 superset.security.session_validation import _is_exempt_endpoint
|
||||
|
||||
|
||||
@pytest.mark.parametrize(
|
||||
"endpoint,expected",
|
||||
[
|
||||
(None, True),
|
||||
("AuthDBView.login", True),
|
||||
("AuthDBView.logout", True),
|
||||
("appbuilder.static", True),
|
||||
("SupersetIndexView.index", False),
|
||||
("Superset.dashboard", False),
|
||||
("ChartRestApi.get_list", False),
|
||||
],
|
||||
)
|
||||
def test_is_exempt_endpoint(endpoint, expected) -> None:
|
||||
assert _is_exempt_endpoint(endpoint) is expected
|
||||
Reference in New Issue
Block a user