Skip to content

fix(sql_lab): raise 400 not 500 on malformed Jinja during CSV export access check - #43866

Open
EnxDev wants to merge 3 commits into
masterfrom
enxdev/fix/sqllab-export-jinja-400
Open

fix(sql_lab): raise 400 not 500 on malformed Jinja during CSV export access check#43866
EnxDev wants to merge 3 commits into
masterfrom
enxdev/fix/sqllab-export-jinja-400

Conversation

@EnxDev

@EnxDev EnxDev commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

SUMMARY

SqlResultExportCommand.validate(), used by the SQL Lab CSV export endpoint, calls self._query.raise_for_access().
This call is currently guarded only against SupersetSecurityException.

raise_for_access() may re-parse the query’s unrendered Jinja through process_jinja_sql(). If the template is invalid for example, because of an unclosed {% if %} this can raise a raw jinja2.exceptions.TemplateError. Because the error isn’t caught, it results in an opaque HTTP 500 response instead of a structured client error.

Other call sites in the same subsystem already handle this case. superset/sqllab/api.py, superset/commands/sql_lab/estimate.py, and superset/commands/sql_lab/results.py catch TemplateError around the same raise_for_access() call.

This behavior was introduced in #43145, where export.py was listed as a follow-up but was missed.

This change adds an except TemplateError clause alongside the existing SupersetSecurityException handler in validate().
It raises a SupersetErrorException with status=400 and error_type=GENERIC_COMMAND_ERROR, matching the existing implementation in the other SQL Lab paths.

This is an additive error-handling change only. Existing success and failure paths remain unchanged.

BEFORE/AFTER SCREENSHOTS OR ANIMATED GIF

  • No B/A

TESTING INSTRUCTIONS

  • Added TestSqlResultExportCommand.test_validation_malformed_jinja to tests/integration_tests/sql_lab/commands_tests.py. The test follows the existing test_validation_malformed_jinja in TestSqlExecutionResultsCommand.

  • Run the integration tests:

    pytest tests/integration_tests/sql_lab/commands_tests.py -q
  • To test manually:

  1. Enable ENABLE_TEMPLATE_PROCESSING.
  2. In SQL Lab, run a query containing malformed Jinja, such as an unclosed {% if %}.
  3. Export the results as CSV.
  4. Confirm that the request returns a 400 response with a clear error message instead of a 500.

ADDITIONAL INFORMATION

  • Has associated issue:
  • Required feature flags:
  • Changes UI
  • Includes DB Migration (follow approval process in SIP-59)
    • Migration is atomic, supports rollback & is backwards-compatible
    • Confirm DB migration upgrade and downgrade tested
    • Runtime estimates and downtime expectations provided
  • Introduces new feature or API
  • Removes existing feature or API

…access check

SqlResultExportCommand.validate() only caught SupersetSecurityException
around raise_for_access(), so a raw jinja2 TemplateError from re-parsing
unrendered Jinja surfaced as an opaque 500. Sibling call sites already
guard this with except TemplateError; this brings export.py in line.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
@bito-code-review

bito-code-review Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #e6be6f

Actionable Suggestions - 0
Review Details
  • Files reviewed - 2 · Commit Range: 92090e0..92090e0
    • superset/commands/sql_lab/export.py
    • tests/integration_tests/sql_lab/commands_tests.py
  • Files skipped - 0
  • Tools
    • MyPy (Static Code Analysis) - ✔︎ Successful
    • Astral Ruff (Static Code Analysis) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

@netlify

netlify Bot commented Sep 4, 2026

Copy link
Copy Markdown

Deploy Preview for superset-docs-preview ready!

Name Link
🔨 Latest commit b046fb8
🔍 Latest deploy log https://app.netlify.com/projects/superset-docs-preview/deploys/6a9adeee66b1850008209a60
😎 Deploy Preview https://deploy-preview-43866--superset-docs-preview.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@codecov

codecov Bot commented Sep 4, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 79.42%. Comparing base (a068517) to head (b046fb8).
⚠️ Report is 8 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master   #43866   +/-   ##
=======================================
  Coverage   79.42%   79.42%           
=======================================
  Files        2895     2895           
  Lines      167997   168000    +3     
  Branches    38903    38903           
=======================================
+ Hits       133434   133440    +6     
+ Misses      32064    32061    -3     
  Partials     2499     2499           
Flag Coverage Δ
hive 37.77% <33.33%> (-0.01%) ⬇️
javascript 74.94% <ø> (+<0.01%) ⬆️
mysql 57.48% <100.00%> (+<0.01%) ⬆️
postgres 57.52% <100.00%> (+<0.01%) ⬆️
presto 39.66% <33.33%> (-0.01%) ⬇️
python 83.87% <100.00%> (+<0.01%) ⬆️
sqlite 57.21% <100.00%> (+<0.01%) ⬆️
unit 74.40% <33.33%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@EnxDev
EnxDev requested a review from msyavuz September 4, 2026 14:53
@bito-code-review

bito-code-review Bot commented Sep 4, 2026

Copy link
Copy Markdown
Contributor

Code Review Agent Run #1e0fc3

Actionable Suggestions - 0
Review Details
  • Files reviewed - 1 · Commit Range: 92090e0..b046fb8
    • superset-frontend/plugins/plugin-chart-country-map/test/countries.test.ts
  • Files skipped - 0
  • Tools
    • Eslint (Linter) - ✔︎ Successful
    • Whispers (Secret Scanner) - ✔︎ Successful
    • Detect-secrets (Secret Scanner) - ✔︎ Successful

Bito Usage Guide

Commands

Type the following command in the pull request comment and save the comment.

  • /review - Manually triggers an incremental AI Review.

  • /review full - Manually triggers a full AI Review.

  • /pause - Pauses automatic reviews on this pull request.

  • /resume - Resumes automatic reviews.

  • /resolve - Marks all Bito-posted review comments as resolved.

  • /abort - Cancels all in-progress reviews.

Refer to the documentation for additional commands.

Configuration

This repository uses Superset You can customize the agent settings here or contact your Bito workspace admin at evan@preset.io.

Documentation & Help

AI Code Review powered by Bito Logo

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant