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


##########
superset/views/utils.py:
##########
@@ -214,6 +215,24 @@ def loads_request_json(request_json_data: str) -> 
dict[Any, Any]:
     return parsed if isinstance(parsed, dict) else {}
 
 
+def get_request_json_body() -> dict[Any, Any]:
+    """Parse the request body as JSON, coercing failures to ``{}``.
+
+    ``request.is_json`` only inspects the Content-Type header, not whether the
+    body is actually parseable JSON. Callers reaching ``get_form_data`` from a
+    non-HTTP-chart-data context (e.g. an MCP tool call rendering
+    ``filter_values()``) can have a request context whose Content-Type claims
+    JSON but whose body isn't a JSON chart-data payload, which makes Werkzeug
+    raise ``BadRequest`` from ``request.get_json()``.
+    """
+    if not request.is_json:
+        return {}
+    try:
+        return request.get_json(cache=True)
+    except BadRequest:
+        return {}

Review Comment:
   **Suggestion:** `get_request_json_body()` is annotated and used as if it 
always returns a dict, but `request.get_json(cache=True)` can return valid 
non-dict JSON values (for example `null`, arrays, or scalars). In those cases 
`get_form_data()` will later treat the result like a mapping and can raise 
runtime errors (e.g., membership/get access on `None` or list). Normalize 
non-dict JSON bodies to `{}` before returning. [logic error]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ get_form_data crashes on scalar JSON request bodies.
   - ⚠️ Filter_values()/get_filters Jinja macros error under affected requests.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Start the Superset application with the PR code, and note that 
`get_form_data()` in
   `superset/views/utils.py:376-391` now obtains `json_data` via 
`get_request_json_body()` at
   line 383.
   
   2. In a Flask request context (e.g. via `app.test_request_context`), send a 
request with
   `content_type="application/json"` and a valid non-object JSON body such as 
`"null"` or
   `"42"` (scalar JSON). This makes `request.is_json` true and 
`request.get_json(cache=True)`
   return `None` or an int rather than a dict, executed in 
`get_request_json_body()` at lines
   228-232.
   
   3. Within that same request context, call `get_form_data(slice_id=None,
   use_slice_data=False, initial_form_data=None)` from 
`superset/views/utils.py:376-381`.
   Execution enters the `has_request_context()` branch and uses the non-dict 
value returned
   from `get_request_json_body()` as `json_data`.
   
   4. When `get_form_data()` evaluates `first_query = (json_data["queries"][0] 
if "queries"
   in json_data and json_data["queries"] else None)` at lines 387-390, the 
expression
   `"queries" in json_data` raises `TypeError` for `None` or scalar values, 
causing a runtime
   crash. This demonstrates that callers treating `json_data` as a mapping will 
fail when
   `get_request_json_body()` returns non-dict JSON.
   ```
   </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=f0d82ca5fbb849b3b198d0050d1983de&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=f0d82ca5fbb849b3b198d0050d1983de&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/views/utils.py
   **Line:** 218:233
   **Comment:**
        *Logic Error: `get_request_json_body()` is annotated and used as if it 
always returns a dict, but `request.get_json(cache=True)` can return valid 
non-dict JSON values (for example `null`, arrays, or scalars). In those cases 
`get_form_data()` will later treat the result like a mapping and can raise 
runtime errors (e.g., membership/get access on `None` or list). Normalize 
non-dict JSON bodies to `{}` before returning.
   
   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%2F42196&comment_hash=c2f0401104fe45bc23d50f70fcfc731d9094774f3a92a8fcc724685ac4b99dac&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F42196&comment_hash=c2f0401104fe45bc23d50f70fcfc731d9094774f3a92a8fcc724685ac4b99dac&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