codeant-ai-for-open-source[bot] commented on code in PR #40128:
URL: https://github.com/apache/superset/pull/40128#discussion_r3350007167


##########
superset/commands/dashboard/restore.py:
##########
@@ -0,0 +1,89 @@
+# 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.
+"""Command to restore a soft-deleted dashboard."""
+
+from superset.commands.dashboard.exceptions import (
+    DashboardForbiddenError,
+    DashboardNotFoundError,
+    DashboardRestoreFailedError,
+    DashboardSlugConflictError,
+)
+from superset.commands.restore import BaseRestoreCommand
+from superset.daos.dashboard import DashboardDAO
+from superset.extensions import db
+from superset.models.dashboard import Dashboard
+
+
+class RestoreDashboardCommand(BaseRestoreCommand[Dashboard]):
+    """Restore a soft-deleted dashboard by clearing its ``deleted_at`` field.
+
+    Most behaviour is inherited from ``BaseRestoreCommand``. The override
+    here adds the slug-conflict check: with the partial unique index on
+    ``slug WHERE deleted_at IS NULL``, slug reuse during the soft-deleted
+    window is allowed, so a restore may now collide with an active row
+    that claimed the slug while this one was deleted. Raise a clean
+    domain error in that case instead of letting the unique-index
+    violation surface as an opaque ``IntegrityError`` at flush time.
+    """
+
+    dao = DashboardDAO
+    not_found_exc = DashboardNotFoundError
+    forbidden_exc = DashboardForbiddenError
+    restore_failed_exc = DashboardRestoreFailedError
+
+    def validate(self) -> Dashboard:  # type: ignore[override]
+        """Extend ``BaseRestoreCommand.validate`` with a slug-conflict 
pre-check.
+
+        Raises ``DashboardSlugConflictError`` when the dashboard has a
+        ``slug`` that has been claimed by another active dashboard while
+        this one was soft-deleted. Surfacing the conflict as a domain
+        error here keeps callers from seeing an opaque ``IntegrityError``
+        at flush time on dialects with the partial index, and a
+        constraint-violation 500 on dialects without it.
+        """
+        model = super().validate()
+        if model.slug and self._has_active_slug_twin(model):
+            raise DashboardSlugConflictError()

Review Comment:
   **Suggestion:** The slug-conflict guard is skipped when the slug is an empty 
string because the condition relies on truthiness. Empty-string slugs are still 
subject to database uniqueness, so restore can bypass this pre-check and fail 
later with a generic integrity error instead of the intended domain conflict. 
Check for `None` explicitly (not truthiness) before running the conflict query. 
[incorrect condition logic]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Restore endpoint returns generic error for empty-slug conflicts.
   - ⚠️ Operators lose specific slug-conflict error messaging.
   - ⚠️ Logs show lower-level integrity failure instead of domain error.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Note the restore endpoint `DashboardRestApi.restore` at
   `superset/dashboards/api.py:8-41`, which calls 
`RestoreDashboardCommand(uuid).run()` to
   restore a soft-deleted dashboard by UUID.
   
   2. In `BaseRestoreCommand.run` (`superset/commands/restore.py:65-74`), 
`model =
   self.validate()` is called first, and then `model.restore()` (from 
`SoftDeleteMixin`) is
   invoked inside a transaction wrapper that rethrows SQLAlchemy errors as
   `DashboardRestoreFailedError`.
   
   3. `RestoreDashboardCommand.validate` 
(`superset/commands/dashboard/restore.py:48-61`)
   calls `super().validate()` to load the soft-deleted `Dashboard` row, then 
performs the
   slug-conflict pre-check:
   
      `if model.slug and self._has_active_slug_twin(model): raise
      DashboardSlugConflictError()`.
   
      When `model.slug == ""` (empty string), this condition is false, so
      `_has_active_slug_twin` is never executed and no 
`DashboardSlugConflictError` is
      raised.
   
   4. `_has_active_slug_twin` (`superset/commands/dashboard/restore.py:64-88`) 
queries
   `Dashboard` for another active row with the same slug and a different id. 
Combined with
   the model comment in `superset/models/dashboard.py:148-153` describing the 
partial unique
   index `ix_dashboards_active_slug` on `slug WHERE deleted_at IS NULL`, a 
soft-deleted
   dashboard with `slug == ""` that is restored while another active dashboard 
already has
   `slug == ""` will violate this unique index at flush time. Because the 
truthiness guard
   skipped `_has_active_slug_twin`, the conflict is only detected by the 
database, which
   raises an `IntegrityError` that is wrapped as `DashboardRestoreFailedError` 
by
   `BaseRestoreCommand.run` instead of the intended 
`DashboardSlugConflictError`.
   ```
   </details>
   
   [Fix in 
Cursor](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1b31c347cf7941fe8b14ae21650a22fd&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 | [Fix in VSCode 
Claude](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=1b31c347cf7941fe8b14ae21650a22fd&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
   
   *(Use Cmd/Ctrl + Click for best experience)*
   <details>
   <summary><b>Prompt for AI Agent 🤖 </b></summary>
   
   ```mdx
   This is a comment left during a code review.
   
   **Path:** superset/commands/dashboard/restore.py
   **Line:** 59:60
   **Comment:**
        *Incorrect Condition Logic: The slug-conflict guard is skipped when the 
slug is an empty string because the condition relies on truthiness. 
Empty-string slugs are still subject to database uniqueness, so restore can 
bypass this pre-check and fail later with a generic integrity error instead of 
the intended domain conflict. Check for `None` explicitly (not truthiness) 
before running the conflict query.
   
   Validate the correctness of the flagged issue. If correct, How can I resolve 
this? If you propose a fix, implement it and please make it concise.
   Once fix is implemented, also check other comments on the same PR, and ask 
user if the user wants to fix the rest of the comments as well. if said yes, 
then fetch all the comments validate the correctness and implement a minimal fix
   ```
   </details>
   <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40128&comment_hash=f7007ac7b955cdce3d0678297f57d83132524341570c003da061e536dd538770&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F40128&comment_hash=f7007ac7b955cdce3d0678297f57d83132524341570c003da061e536dd538770&reaction=dislike'>👎</a>



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