eschutho opened a new pull request, #43470:
URL: https://github.com/apache/superset/pull/43470

   ### SUMMARY
   `ExploreFormDataRestApi` (the cache backing chart Explore / Drill-by state) 
lets a client reference a SQL Lab query as the chart's datasource 
(`datasource_type=query`). Access to that datasource is validated via 
`superset.explore.utils.check_query_access`, which calls 
`security_manager.raise_for_access(query=query)`. When the query hasn't been 
executed yet (or was edited after last execution), that call falls back to 
Jinja-rendering the query's raw SQL (`process_jinja_sql`) to resolve the tables 
it references — the exact same rendering step already known to raise 
`jinja2.exceptions.TemplateError` for malformed templates (see #42366, #42401, 
and the more recent #43423/#43433 in the tag-access-check family).
   
   Unlike those command-layer call sites, this one had no `except 
TemplateError` at all, so the raw jinja2 exception propagated straight through 
`CreateFormDataCommand`/`GetFormDataCommand`/`UpdateFormDataCommand`/`DeleteFormDataCommand`
 (none of which catch anything but `SQLAlchemyError`) up to 
`ExploreFormDataRestApi`, landing as an opaque, unclassified 500 from all four 
`POST`/`PUT`/`GET`/`DELETE /api/v1/explore/form_data` handlers.
   
   ### PROBLEM
   A user-controlled input (a saved SQL Lab query with malformed Jinja 
templating, referenced as a chart's datasource) can trigger a raw 
`jinja2.exceptions.TemplateError` that surfaces as an unclassified 500, instead 
of a proper 4xx `SupersetException`.
   
   ### FIX
   - `superset/explore/utils.py::check_query_access` now catches 
`TemplateError` around the `raise_for_access(query=query)` call and re-raises 
it as `SupersetTemplateException` (existing, 422 — no new exception class 
needed), preserving the original message.
   - `superset/explore/form_data/api.py` maps `SupersetTemplateException` to a 
proper `ex.status` response in all four `ExploreFormDataRestApi` handlers, 
mirroring the existing `except SupersetTemplateException as ex: return 
self.response(ex.status, message=str(ex))` idiom already used in 
`superset/datasets/api.py`.
   
   No behavior change for any other exception type or datasource type.
   
   ### TESTING INSTRUCTIONS
   - New unit test 
`tests/unit_tests/explore/utils_test.py::test_query_malformed_jinja_template`: 
mocks `raise_for_access` to raise `jinja2.exceptions.TemplateSyntaxError` and 
asserts `check_datasource_access` now raises `SupersetTemplateException` 
instead. Verified this test fails on pre-fix code (raw `TemplateSyntaxError` 
propagates) and passes post-fix.
   - `ruff check` / `ruff format --check` clean on all changed files.
   - Full `tests/unit_tests/explore/` and `tests/unit_tests/commands/explore/` 
suites pass (24 tests).
   
   ### ADDITIONAL INFORMATION
   - [ ] Has associated issue:
   - [ ] Required feature flags:
   - [ ] Changes UI
   - [ ] Includes DB Migration
   - [ ] Introduces new feature or API
   - [ ] Removes existing feature or API


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