codeant-ai-for-open-source[bot] commented on code in PR #44499: URL: https://github.com/apache/superset/pull/44499#discussion_r4061958954
########## 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: ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved** as of `03d31f3`. The query and form values are trimmed before checking, and the function returns the first non-blank value while continuing to the form source when the query value is whitespace-only. <sub>If that's not right, unresolve this thread and CodeAnt will leave it open.</sub> <!-- codeant-auto-resolve-reply --> ########## superset-frontend/src/explore/exploreUtils/index.ts: ########## @@ -340,7 +344,11 @@ export const exportChart = async ({ ownState = {}, onStartStreamingExport = null, }: ExportChartParams): Promise<void> => { - const url = '/api/v1/chart/data'; + const downloadReason = await requestDownloadReason(); + if (downloadReason === null) { + return; // the user cancelled the download reason dialog + } + const url = withDownloadReason('/api/v1/chart/data', downloadReason); Review Comment: ✅ **CodeAnt verified this suggestion was addressed in subsequent commits and marked this thread resolved** as of `03d31f3`. The download-reason prompt now runs only when `resultFormat` is included in `DOWNLOAD_REASON_FORMATS`, preventing prompts for JSON exports. <sub>If that's not right, unresolve this thread and CodeAnt will leave it open.</sub> <!-- codeant-auto-resolve-reply --> -- 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]
