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


##########
superset/extensions/storage/api.py:
##########
@@ -0,0 +1,740 @@
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements.  See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership.  The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License.  You may obtain a copy of the License at
+#
+#   http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied.  See the License for the
+# specific language governing permissions and limitations
+# under the License.
+
+"""
+REST API for extension storage.
+
+Provides HTTP endpoints for frontend extensions to access server-side
+ephemeral storage without direct backend code.
+
+All operations are user-scoped by default. Use `?shared=true` query param
+to access shared state visible to all users.
+"""
+
+from __future__ import annotations
+
+import base64
+from typing import Any
+
+from flask import g, request
+from flask.wrappers import Response
+from flask_appbuilder.api import BaseApi, expose, protect, safe
+
+from superset.extensions.storage.codecs import DEFAULT_CODEC, get_codec, 
SAFE_CODECS
+from superset.extensions.storage.ephemeral_dao import (
+    ExtensionEphemeralDAO,
+    ExtensionEphemeralTTLInvalid,
+    ExtensionEphemeralValueTooLarge,
+)
+from superset.extensions.storage.persistent_dao import (
+    ExtensionStorageDAO,
+    ExtensionStorageKeyTooLong,
+    ExtensionStorageListPayloadTooLarge,
+    ExtensionStorageQuotaExceeded,
+    ExtensionStorageValueTooLarge,
+)
+from superset.extensions.storage.utils import get_extension_or_404, parse_ttl
+from superset.key_value.exceptions import KeyValueCodecEncodeException
+from superset.utils.decorators import transaction
+
+
+def _decoded_result_for_wire(decoded: Any) -> tuple[Any, bool]:
+    """Convert a codec-decoded value into its JSON wire representation.
+
+    JSON has no byte type, so a raw `bytes` value is base64-encoded to a
+    string for the response and flagged as such; every other value is
+    already JSON-representable as-is. Checked on the decoded value's
+    actual type, not the codec's name, so this keeps working for a codec
+    this module didn't define.
+
+    :returns: (wire_value, is_binary)
+    """
+    if isinstance(decoded, bytes):
+        return base64.b64encode(decoded).decode("ascii"), True
+    return decoded, False
+
+
+def _wire_value_for_request(value: Any, is_binary: bool) -> Any:
+    """Convert a request body's JSON `value` into a codec's input type.
+
+    `is_binary` is an explicit flag from the caller: JSON has no byte
+    type, so there is no way to tell, from the JSON `value` alone,
+    whether it is a base64 string that must be decoded to bytes before
+    being handed to the codec's `encode`, or a literal value to pass
+    through as-is. Only the caller knows which it sent.
+
+    :raises ValueError: if `is_binary` is set and `value` is not a valid
+        base64 string.
+    """
+    if is_binary:
+        return base64.b64decode(value, validate=True)
+    return value

Review Comment:
   **Suggestion:** Invalid base64 payloads can raise `binascii.Error`, but the 
callers only catch `ValueError`/`TypeError`; this can leak a 500 instead of 
returning the intended 400 error. Catch the base64-specific exception in the 
request parsing path. [possible bug]
   
   <details>
   <summary><b>Severity Level:</b> Major ⚠️</summary>
   
   ```mdx
   - ❌ Ephemeral storage PUT with bad base64 returns server error.
   - ⚠️ Persistent storage PUT binary writes less robust to misuse.
   ```
   </details>
   <details>
   <summary><b>Steps of Reproduction ✅ </b></summary>
   
   ```mdx
   1. Start a Superset instance that includes `ExtensionStorageRestApi` from
   `superset/extensions/storage/api.py`.
   
   2. Send an HTTP PUT request to the ephemeral storage endpoint
   `/api/v1/extensions/test_publisher/test_ext/storage/ephemeral/test_key` 
implemented by
   `set_ephemeral` at `superset/extensions/storage/api.py:194-299`, with JSON 
body `{"value":
   "not-base64!", "isBinary": true, "ttl": 60}`.
   
   3. In `set_ephemeral` (`api.py:266-283`), the body is parsed, `is_binary` is 
set to True
   (line 276), and `_wire_value_for_request` (`api.py:71-85`) is called with 
the string
   `"not-base64!"` and `True`.
   
   4. Inside `_wire_value_for_request` (line 83), `base64.b64decode(value, 
validate=True)`
   raises `binascii.Error` for the invalid payload; this exception is not 
caught by the
   surrounding `except (ValueError, TypeError)` in `set_ephemeral` (lines 
279-282), so the
   view raises and `@safe` returns an HTTP 500 instead of the intended 400 
error.
   ```
   </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=e5189e1f4e3d4b3db4c18c4d3500dc75&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=e5189e1f4e3d4b3db4c18c4d3500dc75&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/extensions/storage/api.py
   **Line:** 71:85
   **Comment:**
        *Possible Bug: Invalid base64 payloads can raise `binascii.Error`, but 
the callers only catch `ValueError`/`TypeError`; this can leak a 500 instead of 
returning the intended 400 error. Catch the base64-specific exception in the 
request parsing path.
   
   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%2F39171&comment_hash=bc07bb5e8fed17e54371a7d2479d0df19532b347c6eb79d26ea949307e6b975b&reaction=like'>👍</a>
 | <a 
href='https://app.codeant.ai/feedback?pr_url=https%3A%2F%2Fgithub.com%2Fapache%2Fsuperset%2Fpull%2F39171&comment_hash=bc07bb5e8fed17e54371a7d2479d0df19532b347c6eb79d26ea949307e6b975b&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