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


##########
superset/charts/data/api.py:
##########
@@ -404,13 +437,15 @@ def data_from_cache(self, cache_key: str) -> Response:
               $ref: '#/components/responses/500'
         """
         try:
-            cached_data = self._load_query_context_form_from_cache(cache_key)
-            # Set form_data in Flask Global as it is used as a fallback
-            # for async queries with jinja context
-            g.form_data = cached_data
-            query_context = self._create_query_context_from_form(cached_data)
-            command = ChartDataCommand(query_context)
-            command.validate()
+            with chart_timing_phase("context"):
+                cached_data = 
self._load_query_context_form_from_cache(cache_key)
+                # Set form_data in Flask Global as it is used as a fallback
+                # for async queries with jinja context
+                g.form_data = cached_data
+                query_context = 
self._create_query_context_from_form(cached_data)
+                command = ChartDataCommand(query_context)
+            with chart_timing_phase("authorize"):
+                command.validate()

Review Comment:
   **Suggestion:** The new authorization step in the cache endpoint calls 
`validate()` but does not handle `QueryObjectValidationError`, so 
malformed/invalid cached query contexts will now bubble up as 500s instead of a 
client error response. Add a `QueryObjectValidationError` handler (consistent 
with the other chart data endpoints) around this validation path and return a 
400 response. [api mismatch]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Async chart results endpoint errors on invalid cached queries.
   - ⚠️ Clients see 500 instead of actionable validation message.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Enable global async queries so that chart async jobs use the Celery task
   `load_chart_data_into_cache` at `superset/tasks/async_queries.py:8-40`, 
which on success
   stores a query context in the cache and returns a URL 
`/api/v1/chart/data/<cache_key>`
   (lines 20-36).
   
   2. In a controlled test or development environment, construct a cached query 
context entry
   whose `form_data` produces a `QueryObject` with invalid configuration, for 
example
   duplicate metric/column labels that cause `QueryObject.validate()` to raise
   `QueryObjectValidationError` as implemented in 
`superset/common/query_object.py:85-110`
   (duplicate labels path at lines 100-110), and store it under a cache key that
   `QueryContextCacheLoader.load()` will return (loader defined in
   `superset/charts/data/query_context_cache_loader.py:23-30`).
   
   3. Issue an HTTP GET request to `/api/v1/chart/data/<cache_key>`, which is 
handled by
   `ChartDataRestApi.data_from_cache` in `superset/charts/data/api.py:135-199`; 
inside this
   method, the try-block at lines 181-187 loads the cached form data via
   `_load_query_context_form_from_cache`, builds a `QueryContext` with
   `_create_query_context_from_form`, and constructs `command =
   ChartDataCommand(query_context)`.
   
   4. Still inside `data_from_cache`, the new authorization phase at lines 
188-189 calls
   `command.validate()` within `with chart_timing_phase("authorize")`, which 
routes to
   `QueryContext.raise_for_access()` (`superset/common/query_context.py:52-53`) 
and then
   `QueryContextProcessor.raise_for_access()`
   (`superset/common/query_context_processor.py:25-40`) that iterates queries 
and calls
   `query.validate()`, causing `QueryObject.validate()` to raise 
`QueryObjectValidationError`
   for the invalid query; this exception is not caught by the surrounding 
`except` clauses in
   `data_from_cache` (which only handle `ChartDataCacheLoadError`,
   `SupersetSecurityException`, and `ValidationError` at
   `superset/charts/data/api.py:190-197), so it propagates and results in a 500 
Internal
   Server Error instead of the 400 response used by the other chart data 
endpoints
   (`get_data` and `data` both catch `QueryObjectValidationError` at
   `superset/charts/data/api.py:241-252` and 90-101 respectively).
   ```
   </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=b6a1df383c774f539335489c5798006a&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=b6a1df383c774f539335489c5798006a&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:** superset/charts/data/api.py
   **Line:** 447:448
   **Comment:**
        *Api Mismatch: The new authorization step in the cache endpoint calls 
`validate()` but does not handle `QueryObjectValidationError`, so 
malformed/invalid cached query contexts will now bubble up as 500s instead of a 
client error response. Add a `QueryObjectValidationError` handler (consistent 
with the other chart data endpoints) around this validation path and return a 
400 response.
   
   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%2F37516&comment_hash=fd496f5b7d36401e2a65de063898c9ca7cb23362125a5406d7f53fd740097abb&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F37516&comment_hash=fd496f5b7d36401e2a65de063898c9ca7cb23362125a5406d7f53fd740097abb&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