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


##########
tests/unit_tests/db_engine_specs/test_presto.py:
##########
@@ -410,3 +410,70 @@ def test_extract_errors_maps_401_to_access_denied() -> 
None:
     result = PrestoEngineSpec.extract_errors(Exception(msg))
     assert len(result) == 1
     assert result[0].error_type == 
SupersetErrorType.CONNECTION_ACCESS_DENIED_ERROR
+
+
+def test_latest_sub_partition_rejects_unknown_field(
+    mocker: MockerFixture,
+) -> None:
+    """Regression test for #41869.
+
+    ``PrestoBaseEngineSpec.latest_sub_partition`` previously used a chained
+    comparison (``k not in k in part_fields``) that Python evaluates as
+    ``(k not in k) and (k in part_fields)``. Since ``k not in k`` is always
+    ``False`` for strings, the guard was unreachable and unknown kwarg names
+    were silently accepted, flowing into ``_partition_query`` and enabling
+    SQL injection via the ``latest_sub_partition`` Jinja macro. This test
+    locks in that unknown fields are now rejected before reaching the query
+    builder.
+    """
+    from superset.db_engine_specs.presto import PrestoBaseEngineSpec
+    from superset.exceptions import SupersetTemplateException
+
+    database = mocker.MagicMock()
+    database.get_indexes.return_value = [{"column_names": ["ds", 
"event_type"]}]
+    table = mocker.MagicMock()
+

Review Comment:
   **Suggestion:** Add an explicit type annotation for this local test variable 
to comply with the type-hint requirement for annotatable variables. 
[custom_rule]
   
   **Severity Level:** Minor ⚠️
   <details>
   <summary><b>Why it matters? 🤔 </b></summary>
   
   This new local variable is a Python value that can be explicitly annotated, 
but it is left untyped in the added code. That matches the type-hint rule for 
annotatable variables.
   </details>
   <details>
   <summary><b>Rule source 📖 </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=55353fac455e4e3f96172350013b73ba&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=55353fac455e4e3f96172350013b73ba&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:** tests/unit_tests/db_engine_specs/test_presto.py
   **Line:** 435:435
   **Comment:**
        *Custom Rule: Add an explicit type annotation for this local test 
variable to comply with the type-hint requirement for annotatable variables.
   
   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%2F41877&comment_hash=d95f90559c2fe266ebd71ab8c1f764cb036189e6ecf7c94279d7c62bc8ed3c6b&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41877&comment_hash=d95f90559c2fe266ebd71ab8c1f764cb036189e6ecf7c94279d7c62bc8ed3c6b&reaction=dislike'>👎</a>



##########
tests/unit_tests/db_engine_specs/test_presto.py:
##########
@@ -410,3 +410,70 @@ def test_extract_errors_maps_401_to_access_denied() -> 
None:
     result = PrestoEngineSpec.extract_errors(Exception(msg))
     assert len(result) == 1
     assert result[0].error_type == 
SupersetErrorType.CONNECTION_ACCESS_DENIED_ERROR
+
+
+def test_latest_sub_partition_rejects_unknown_field(
+    mocker: MockerFixture,
+) -> None:
+    """Regression test for #41869.
+
+    ``PrestoBaseEngineSpec.latest_sub_partition`` previously used a chained
+    comparison (``k not in k in part_fields``) that Python evaluates as
+    ``(k not in k) and (k in part_fields)``. Since ``k not in k`` is always
+    ``False`` for strings, the guard was unreachable and unknown kwarg names
+    were silently accepted, flowing into ``_partition_query`` and enabling
+    SQL injection via the ``latest_sub_partition`` Jinja macro. This test
+    locks in that unknown fields are now rejected before reaching the query
+    builder.
+    """
+    from superset.db_engine_specs.presto import PrestoBaseEngineSpec
+    from superset.exceptions import SupersetTemplateException
+
+    database = mocker.MagicMock()
+    database.get_indexes.return_value = [{"column_names": ["ds", 
"event_type"]}]
+    table = mocker.MagicMock()
+
+    with pytest.raises(SupersetTemplateException) as exc_info:
+        PrestoBaseEngineSpec.latest_sub_partition(
+            database,
+            table,
+            unknown_field="anything",
+        )
+
+    assert "unknown_field" in str(exc_info.value)
+    assert "not part of the partitioning key" in str(exc_info.value)
+
+
+def test_partition_query_escapes_single_quote_in_filter_value(
+    mocker: MockerFixture,
+) -> None:
+    """Regression test for #41869.
+
+    ``_partition_query`` previously interpolated filter values directly into
+    the SQL ``WHERE`` clause with an f-string, allowing SQL injection via any
+    caller that let user input reach ``filters``. Values must be escaped
+    (single-quote doubling per SQL standard) so a ``'`` in the value cannot
+    break out of the string literal.
+    """
+    from superset.db_engine_specs.presto import PrestoBaseEngineSpec
+
+    database = mocker.MagicMock()
+    database.get_extra.return_value = {}
+    table = Table("my_table", "my_schema")
+
+    injected = "2024-01-01' UNION SELECT secret FROM other_table--"

Review Comment:
   **Suggestion:** Annotate this local string variable with its explicit type 
instead of relying only on inference. [custom_rule]
   
   **Severity Level:** Minor ⚠️
   <details>
   <summary><b>Why it matters? 🤔 </b></summary>
   
   This is a newly added Python local variable with an obvious concrete type 
(`str`) that could be annotated, but no type hint is present, so it violates 
the type-hint requirement.
   </details>
   <details>
   <summary><b>Rule source 📖 </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=1100c35306384055a1bf023f49726f5b&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=1100c35306384055a1bf023f49726f5b&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:** tests/unit_tests/db_engine_specs/test_presto.py
   **Line:** 464:464
   **Comment:**
        *Custom Rule: Annotate this local string variable with its explicit 
type instead of relying only on inference.
   
   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%2F41877&comment_hash=0b60097711eac454a3fcf0891a147d7bce7925fc718f372053d4828ec8b2dcdd&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41877&comment_hash=0b60097711eac454a3fcf0891a147d7bce7925fc718f372053d4828ec8b2dcdd&reaction=dislike'>👎</a>



##########
tests/unit_tests/db_engine_specs/test_presto.py:
##########
@@ -410,3 +410,70 @@ def test_extract_errors_maps_401_to_access_denied() -> 
None:
     result = PrestoEngineSpec.extract_errors(Exception(msg))
     assert len(result) == 1
     assert result[0].error_type == 
SupersetErrorType.CONNECTION_ACCESS_DENIED_ERROR
+
+
+def test_latest_sub_partition_rejects_unknown_field(
+    mocker: MockerFixture,
+) -> None:
+    """Regression test for #41869.
+
+    ``PrestoBaseEngineSpec.latest_sub_partition`` previously used a chained
+    comparison (``k not in k in part_fields``) that Python evaluates as
+    ``(k not in k) and (k in part_fields)``. Since ``k not in k`` is always
+    ``False`` for strings, the guard was unreachable and unknown kwarg names
+    were silently accepted, flowing into ``_partition_query`` and enabling
+    SQL injection via the ``latest_sub_partition`` Jinja macro. This test
+    locks in that unknown fields are now rejected before reaching the query
+    builder.
+    """
+    from superset.db_engine_specs.presto import PrestoBaseEngineSpec
+    from superset.exceptions import SupersetTemplateException
+
+    database = mocker.MagicMock()
+    database.get_indexes.return_value = [{"column_names": ["ds", 
"event_type"]}]
+    table = mocker.MagicMock()
+
+    with pytest.raises(SupersetTemplateException) as exc_info:
+        PrestoBaseEngineSpec.latest_sub_partition(
+            database,
+            table,
+            unknown_field="anything",
+        )
+
+    assert "unknown_field" in str(exc_info.value)
+    assert "not part of the partitioning key" in str(exc_info.value)
+
+
+def test_partition_query_escapes_single_quote_in_filter_value(
+    mocker: MockerFixture,
+) -> None:
+    """Regression test for #41869.
+
+    ``_partition_query`` previously interpolated filter values directly into
+    the SQL ``WHERE`` clause with an f-string, allowing SQL injection via any
+    caller that let user input reach ``filters``. Values must be escaped
+    (single-quote doubling per SQL standard) so a ``'`` in the value cannot
+    break out of the string literal.
+    """
+    from superset.db_engine_specs.presto import PrestoBaseEngineSpec
+
+    database = mocker.MagicMock()
+    database.get_extra.return_value = {}
+    table = Table("my_table", "my_schema")
+
+    injected = "2024-01-01' UNION SELECT secret FROM other_table--"
+    sql = PrestoBaseEngineSpec._partition_query(
+        table,
+        indexes=[{"column_names": ["ds", "event_type"]}],
+        database=database,
+        filters={"ds": injected},
+    )

Review Comment:
   **Suggestion:** Add a concrete type annotation for the query result variable 
returned by the helper call. [custom_rule]
   
   **Severity Level:** Minor ⚠️
   <details>
   <summary><b>Why it matters? 🤔 </b></summary>
   
   The added local variable stores the query string returned by the helper call 
and can be annotated, but the code omits a type hint. This fits the type-hint 
rule for annotatable variables.
   </details>
   <details>
   <summary><b>Rule source 📖 </b></summary>
   
   .cursor/rules/dev-standard.mdc (line 28)
   </details>
   
   [![Fix in 
Cursor](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-cursor-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=cursor&prompt_id=6fef147bfa534e1585d79bc47de86b54&service=github&base_url=https%3A%2F%2Fgithub.com&org=apache&repo=apache%2Fsuperset)
 [![Fix in VSCode 
Claude](https://new-codeant-butcket.s3.us-west-1.amazonaws.com/badges/fix-in-vscode-claude-flat.svg)](https://app.codeant.ai/fix-in-ide?tool=vscode-claude&prompt_id=6fef147bfa534e1585d79bc47de86b54&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:** tests/unit_tests/db_engine_specs/test_presto.py
   **Line:** 465:470
   **Comment:**
        *Custom Rule: Add a concrete type annotation for the query result 
variable returned by the helper call.
   
   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%2F41877&comment_hash=e0c828a089a5bafbe3ee358122b508ac84295067af4d72f78a27a13b60163d09&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F41877&comment_hash=e0c828a089a5bafbe3ee358122b508ac84295067af4d72f78a27a13b60163d09&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