sha174n commented on code in PR #44080:
URL: https://github.com/apache/superset/pull/44080#discussion_r4131387004


##########
superset/views/datasource/views.py:
##########
@@ -121,25 +176,52 @@ def save(self) -> FlaskResponse:
         except SupersetSecurityException as ex:
             raise DatasetForbiddenError() from ex
 
-        if database_id != orm_datasource.database_id:
-            new_database = DatasetDAO.get_database_by_id(database_id)
-            if new_database is None:
+        database_changed = database_id != orm_datasource.database_id
+        requested_sql = datasource_dict.get("sql")
+        # The table this request lands on: an omitted key clears the field.
+        requested_table = Table(
+            datasource_dict.get("table_name"),
+            datasource_dict.get("schema") or None,
+            datasource_dict.get("catalog") or None,
+        )
+
+        # Editorship of the dataset alone is not sufficient to repoint it.
+        # Ports ``UpdateDatasetCommand``'s table and SQL checks, not the other
+        # source validation the command also runs. As there, the two are
+        # independent: one save can do both. They need separate
+        # ``raise_for_access`` calls because passing ``sql`` builds an
+        # ephemeral query that supersedes ``table``.
+        repoints_table = _repoints_table(
+            orm_datasource, database_changed, requested_sql, requested_table
+        )
+        repoints_sql = _repoints_sql(
+            orm_datasource, database_changed, requested_sql, requested_table
+        )
+        if repoints_table or repoints_sql:
+            target_database = (
+                DatasetDAO.get_database_by_id(database_id)
+                if database_changed
+                else orm_datasource.database
+            )
+            if target_database is None:
                 return json_error_response(_("Database not found."), 
status=422)
             try:
-                security_manager.raise_for_access(
-                    database=new_database,
-                    # Check access against the table/schema/catalog the
-                    # request is repointing to, not the dataset's current
-                    # values -- update_from_object (below) applies whatever
-                    # table_name/schema/catalog the request supplies.
-                    table=Table(
-                        datasource_dict.get("table_name", 
orm_datasource.table_name),
-                        datasource_dict.get("schema", orm_datasource.schema),
-                        datasource_dict.get("catalog", orm_datasource.catalog),
-                    ),
-                )
+                if repoints_table:
+                    security_manager.raise_for_access(
+                        database=target_database,
+                        table=requested_table,
+                    )
+                if repoints_sql:
+                    security_manager.raise_for_access(
+                        database=target_database,
+                        sql=requested_sql,
+                        catalog=requested_table.catalog,
+                        schema=requested_table.schema,
+                    )

Review Comment:
   A `SupersetParseError` here is already a 422: it subclasses 
`SupersetErrorException` with `status = 422`, and `save` is wrapped in 
`@handle_api_exception`, which returns `json_error_response([ex.error], 
status=ex.status)` with the parse message, line and column intact. The command 
wraps it in a `field_name="sql"` envelope because the REST API aggregates 
marshmallow `ValidationError`s; this view has no such aggregator and reports 
every other error through `json_error_response`, so matching that envelope here 
would only make this one error inconsistent with its neighbours.
   
   Your additional suggestion was the real gap, and it is fixed in 6c803a4: the 
body is unvalidated JSON, so a non-string value never reached the parser as a 
parse error at all, it failed on the type and surfaced as a 500. 
`_requested_target` now type-checks `sql`, `table_name`, `schema` and `catalog` 
up front and returns a 422, covering the render path for the last three as 
well. Parametrized test added over all four fields.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to