|
16 | 16 | # under the License. |
17 | 17 | from __future__ import annotations |
18 | 18 |
|
| 19 | +import inspect |
19 | 20 | from typing import Any, TYPE_CHECKING |
20 | 21 |
|
21 | 22 | from flask import request |
|
31 | 32 | TimeRangeParseFailError, |
32 | 33 | ) |
33 | 34 | from superset.daos.chart import ChartDAO |
| 35 | +from superset.daos.datasource import DatasourceDAO |
34 | 36 | from superset.exceptions import QueryObjectValidationError |
35 | 37 | from superset.legacy import update_time_range |
36 | 38 | from superset.superset_typing import FlaskResponse |
@@ -78,7 +80,9 @@ def query(self) -> FlaskResponse: |
78 | 80 | ) from ex |
79 | 81 | if not isinstance(query_context_params, dict): |
80 | 82 | raise QueryObjectValidationError(_("query_context must be a JSON object")) |
81 | | - query_context = self.get_query_context_factory().create(**query_context_params) |
| 83 | + factory = self.get_query_context_factory() |
| 84 | + self._validate_query_context_shape(factory, query_context_params) |
| 85 | + query_context = factory.create(**query_context_params) |
82 | 86 | query_context.raise_for_access() |
83 | 87 | result = query_context.get_payload() |
84 | 88 | payload_json = result["queries"] |
@@ -145,6 +149,68 @@ def time_range(self, **kwargs: Any) -> FlaskResponse: |
145 | 149 | error_msg = {"message": _("Unexpected time range: %(error)s", error=error)} |
146 | 150 | return self.json_response(error_msg, 400) |
147 | 151 |
|
| 152 | + @staticmethod |
| 153 | + def _validate_query_context_shape( |
| 154 | + factory: QueryContextFactory, params: dict[str, Any] |
| 155 | + ) -> None: |
| 156 | + """ |
| 157 | + Reject a ``query_context`` that cannot be passed to ``factory.create``. |
| 158 | +
|
| 159 | + Binding the keys against the factory signature and checking the types of |
| 160 | + the required members turns user-fixable input errors into a 400 without |
| 161 | + guarding the ``create`` call itself, so internal errors still surface. |
| 162 | + """ |
| 163 | + signature = inspect.signature(factory.create) |
| 164 | + try: |
| 165 | + signature.bind(**params) |
| 166 | + except TypeError as ex: |
| 167 | + unexpected = sorted(set(params) - set(signature.parameters)) |
| 168 | + missing = sorted( |
| 169 | + name |
| 170 | + for name, parameter in signature.parameters.items() |
| 171 | + if parameter.default is inspect.Parameter.empty and name not in params |
| 172 | + ) |
| 173 | + details = [] |
| 174 | + if unexpected: |
| 175 | + details.append(f"unexpected keys: {', '.join(unexpected)}") |
| 176 | + if missing: |
| 177 | + details.append(f"missing keys: {', '.join(missing)}") |
| 178 | + raise QueryObjectValidationError( |
| 179 | + _("query_context is invalid: %(details)s", details="; ".join(details)) |
| 180 | + ) from ex |
| 181 | + |
| 182 | + datasource = params["datasource"] |
| 183 | + if ( |
| 184 | + not isinstance(datasource, dict) |
| 185 | + or "id" not in datasource |
| 186 | + or not isinstance(datasource.get("type"), str) |
| 187 | + or datasource["type"] not in DatasourceDAO.sources |
| 188 | + ): |
| 189 | + raise QueryObjectValidationError( |
| 190 | + _( |
| 191 | + "query_context is invalid: %(details)s", |
| 192 | + details="datasource must be an object with an id and a valid type", |
| 193 | + ) |
| 194 | + ) |
| 195 | + form_data = params.get("form_data") |
| 196 | + if form_data is not None and not isinstance(form_data, dict): |
| 197 | + raise QueryObjectValidationError( |
| 198 | + _( |
| 199 | + "query_context is invalid: %(details)s", |
| 200 | + details="form_data must be an object", |
| 201 | + ) |
| 202 | + ) |
| 203 | + queries = params["queries"] |
| 204 | + if not isinstance(queries, list) or not all( |
| 205 | + isinstance(query, dict) for query in queries |
| 206 | + ): |
| 207 | + raise QueryObjectValidationError( |
| 208 | + _( |
| 209 | + "query_context is invalid: %(details)s", |
| 210 | + details="queries must be a list of objects", |
| 211 | + ) |
| 212 | + ) |
| 213 | + |
148 | 214 | def get_query_context_factory(self) -> QueryContextFactory: |
149 | 215 | if self.query_context_factory is None: |
150 | 216 | # pylint: disable=import-outside-toplevel |
|
0 commit comments