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


##########
tests/unit_tests/commands/databases/tables_test.py:
##########
@@ -291,3 +309,130 @@ def test_tables_without_catalog(
         cache=database_without_catalog.table_cache_enabled,
         cache_timeout=database_without_catalog.table_cache_timeout,
     )
+
+
+def test_tables_unknown_schema(
+    mocker: MockerFixture,
+    database_without_catalog: MagicMock,
+) -> None:
+    """
+    A schema that does not exist in the database is rejected before any
+    table or view lookup runs.
+    """
+    get_datasources_accessible_by_user = mocker.patch.object(
+        security_manager,
+        "get_datasources_accessible_by_user",
+    )
+
+    with pytest.raises(DatabaseSchemaNotFoundError):
+        TablesDatabaseCommand(1, None, "not_a_schema", False).run()
+
+    database_without_catalog.get_all_table_names_in_schema.assert_not_called()
+    database_without_catalog.get_all_view_names_in_schema.assert_not_called()
+    
database_without_catalog.get_all_materialized_view_names_in_schema.assert_not_called()

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Line exceeds 88-char limit</b></div>
   <div id="fix">
   
   Line 332 
(`database_without_catalog.get_all_materialized_view_names_in_schema.assert_not_called()`)
 is 90 chars, exceeding the repo's 88-char ruff/black `line-length` 
(pyproject.toml:445). Per `AGENTS.md` the changed files must pass pre-commit 
(ruff E501), so CI will flag this line. Wrap it across lines.
   </div>
   
   
   </div>
   
   <details>
   <summary><b>Citations</b></summary>
   <ul>
   
   <li>
   Rule Violated: <a 
href="https://github.com/apache/superset/blob/e2a2ece/AGENTS.md#L7";>AGENTS.md:7</a>
   </li>
   
   </ul>
   </details>
   
   
   
   
   <small><i>Code Review Run #08df9f</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



##########
superset/commands/database/tables.py:
##########
@@ -177,3 +175,40 @@ def validate(self) -> None:
         self._model = cast(Database, DatabaseDAO.find_by_id(self._db_id))
         if not self._model:
             raise DatabaseNotFoundError()
+
+        self._catalog_name = self._catalog_name or 
self._model.get_default_catalog()
+        if not self._model.db_engine_spec.supports_schemas:
+            self._schema_name = None
+
+        if self._schema_name:
+            self._validate_schema(self._schema_name)
+
+    def _validate_schema(self, schema_name: str) -> None:
+        """
+        Accept only a schema that the schemas endpoint would list for this 
user.
+
+        The schema has to exist in the database and be accessible to the user,
+        otherwise ``DatabaseSchemaNotFoundError`` is raised before any table or
+        view lookup is run.
+        """
+        try:
+            schemas = self._model.get_all_schema_names(
+                catalog=self._catalog_name,
+                cache=self._model.schema_cache_enabled,
+                cache_timeout=self._model.schema_cache_timeout or None,
+                force=self._force,
+            )
+            accessible = schema_name in schemas and bool(

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>CWE-178: Case-Sensitive Schema Gate</b></div>
   <div id="fix">
   
   The new gate in `_validate_schema` uses case-sensitive membership 
(`schema_name in schemas`) against raw identifiers from `get_all_schema_names`, 
while `schema_name` arrives unnormalized from the `schema_name` rison param in 
`databases/api.py`. On case-preserving engines a case variant the engine would 
resolve now 404s before any lookup; the upload path in `databases/api.py` 
already lowercases schemas for exactly this reason. Consider case-insensitive 
matching. ([CWE-178](https://cwe.mitre.org/data/definitions/178.html))
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #08df9f</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



##########
superset/commands/database/tables.py:
##########
@@ -177,3 +175,40 @@ def validate(self) -> None:
         self._model = cast(Database, DatabaseDAO.find_by_id(self._db_id))
         if not self._model:
             raise DatabaseNotFoundError()
+
+        self._catalog_name = self._catalog_name or 
self._model.get_default_catalog()
+        if not self._model.db_engine_spec.supports_schemas:
+            self._schema_name = None
+
+        if self._schema_name:
+            self._validate_schema(self._schema_name)
+
+    def _validate_schema(self, schema_name: str) -> None:
+        """
+        Accept only a schema that the schemas endpoint would list for this 
user.
+
+        The schema has to exist in the database and be accessible to the user,
+        otherwise ``DatabaseSchemaNotFoundError`` is raised before any table or
+        view lookup is run.
+        """
+        try:
+            schemas = self._model.get_all_schema_names(
+                catalog=self._catalog_name,
+                cache=self._model.schema_cache_enabled,
+                cache_timeout=self._model.schema_cache_timeout or None,
+                force=self._force,
+            )

Review Comment:
   <div>
   
   
   <div id="suggestion">
   <div id="issue"><b>Extra schema-listing round trip</b></div>
   <div id="fix">
   
   `validate()` now calls `get_all_schema_names` on every request carrying a 
schema, adding one inspector round trip before the table/view/materialized-view 
lookups in `run()`. When `schema_cache_enabled` is false (core.py:463) this is 
an uncached metadata query per tables call on a hot UI path. Consider leaning 
on the schema cache or folding existence validation into the existing lookups.
   </div>
   
   
   </div>
   
   
   
   
   <small><i>Code Review Run #08df9f</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