seunggabi commented on code in PR #44499:
URL: https://github.com/apache/superset/pull/44499#discussion_r4068076899


##########
superset/utils/download_reason.py:
##########
@@ -0,0 +1,80 @@
+# 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.
+"""Optional "download reason" capture for data exports.
+
+When the REQUIRE_DOWNLOAD_REASON feature flag is on, the frontend asks the
+user for a reason before exporting data (CSV/XLSX from SQL Lab, Explore and
+dashboards) and sends it as the download_reason request parameter. Export
+endpoints reject requests without one and attach the reason to their event log
+entry (logs.json), so downloads can be audited from Security → Action Log
+without any new table.
+"""
+
+from __future__ import annotations
+
+from typing import Callable
+
+from flask import request
+from flask_babel import gettext as _
+
+from superset import is_feature_enabled
+from superset.errors import ErrorLevel, SupersetError, SupersetErrorType
+from superset.exceptions import SupersetErrorException
+
+DOWNLOAD_REASON_PARAM = "download_reason"
+DOWNLOAD_REASON_FEATURE_FLAG = "REQUIRE_DOWNLOAD_REASON"
+
+
+class DownloadReasonRequiredError(SupersetErrorException):
+    """Raised when the flag is on and an export request carries no reason."""
+
+    status = 400
+
+    def __init__(self) -> None:
+        super().__init__(
+            SupersetError(
+                message=_("A reason is required to download data."),
+                error_type=SupersetErrorType.INVALID_PAYLOAD_SCHEMA_ERROR,
+                level=ErrorLevel.ERROR,
+            ),
+            status=400,
+        )
+
+
+def get_download_reason() -> str | None:
+    """Return the trimmed download_reason from the query string or form."""
+    value = request.args.get(DOWNLOAD_REASON_PARAM) or request.form.get(
+        DOWNLOAD_REASON_PARAM
+    )

Review Comment:
   Handled: `get_download_reason` strips each source and falls through to the 
form when the query value is blank, so `?download_reason=%20` plus a valid form 
reason is accepted (see the loop over `request.args` / `request.form`). Covered 
by `tests/unit_tests/utils/download_reason_test.py`.



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