bito-code-review[bot] commented on code in PR #44080:
URL: https://github.com/apache/superset/pull/44080#discussion_r4127893390


##########
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:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>ParseError contract divergence</b></div>
   <div id="fix">
   
   The new `raise_for_access(sql=...)` call can raise `SupersetParseError` from 
`process_jinja_sql` (superset/sql/parse.py:2874), which the `except 
SupersetSecurityException` here does not catch. The ported 
`UpdateDatasetCommand._validate_sql_access` 
(superset/commands/dataset/update.py:272-278) maps it to a sql-field validation 
error; this path surfaces a generic 422 instead. Consider mapping it the same 
way.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #1236f1</i></small>
   </div>
   
   ---
   Should Bito avoid suggestions like this for future reviews? (<a 
href=https://alpha.bito.ai/home/ai-agents/review-rules>Manage Rules</a>)
   - [ ] Yes, avoid them



-- 
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