This is an automated email from the ASF dual-hosted git repository.

EnxDev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/superset.git


The following commit(s) were added to refs/heads/master by this push:
     new c4e145b071f fix(sql_lab): raise 400 not 500 on malformed Jinja during 
CSV export access check (#43866)
c4e145b071f is described below

commit c4e145b071f8ec708ca16485852de3dd8bec9117
Author: Enzo Martellucci <[email protected]>
AuthorDate: Tue Sep 8 12:27:29 2026 +0200

    fix(sql_lab): raise 400 not 500 on malformed Jinja during CSV export access 
check (#43866)
---
 superset/commands/sql_lab/export.py               | 10 ++++++++++
 tests/integration_tests/sql_lab/commands_tests.py | 16 ++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/superset/commands/sql_lab/export.py 
b/superset/commands/sql_lab/export.py
index e488bf0a3ae..93aea9d58c6 100644
--- a/superset/commands/sql_lab/export.py
+++ b/superset/commands/sql_lab/export.py
@@ -22,6 +22,7 @@ from typing import Any, cast, TypedDict
 import pandas as pd
 from flask import current_app as app
 from flask_babel import gettext as __
+from jinja2.exceptions import TemplateError
 
 from superset import db, results_backend, results_backend_use_msgpack
 from superset.commands.base import BaseCommand
@@ -80,6 +81,15 @@ class SqlResultExportCommand(BaseCommand):
                 ),
                 status=403,
             ) from ex
+        except TemplateError as ex:
+            raise SupersetErrorException(
+                SupersetError(
+                    message=str(ex),
+                    error_type=SupersetErrorType.GENERIC_COMMAND_ERROR,
+                    level=ErrorLevel.ERROR,
+                ),
+                status=400,
+            ) from ex
 
     def run(
         self,
diff --git a/tests/integration_tests/sql_lab/commands_tests.py 
b/tests/integration_tests/sql_lab/commands_tests.py
index 11db302ac56..893d68d41ae 100644
--- a/tests/integration_tests/sql_lab/commands_tests.py
+++ b/tests/integration_tests/sql_lab/commands_tests.py
@@ -211,6 +211,22 @@ class TestSqlResultExportCommand(SupersetTestCase):
                 == SupersetErrorType.QUERY_SECURITY_ACCESS_ERROR
             )
 
+    @pytest.mark.usefixtures("create_database_and_query")
+    def test_validation_malformed_jinja(self) -> None:
+        command = export.SqlResultExportCommand("test")
+
+        with mock.patch(
+            "superset.models.sql_lab.Query.raise_for_access",
+            side_effect=TemplateSyntaxError("unexpected end of template", 
lineno=1),
+        ):
+            with pytest.raises(SupersetErrorException) as ex_info:
+                command.run()
+            assert (
+                ex_info.value.error.error_type
+                == SupersetErrorType.GENERIC_COMMAND_ERROR
+            )
+            assert ex_info.value.status == 400
+
     @pytest.mark.usefixtures("create_database_and_query")
     @patch("superset.models.sql_lab.Query.raise_for_access", lambda _: None)
     @patch("superset.models.core.Database.get_df")

Reply via email to