A platform for community discussion. Free, open, simple.
FIX: Enforce max limit for data explorer queries (#38320)
Alan Guo Xiang Tanb99859cfc5d5
diff --git a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rbindex c1e35159a47..118c1227638 100644--- a/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb+++ b/plugins/discourse-data-explorer/app/controllers/discourse_data_explorer/query_controller.rb@@ -168,15 +168,14 @@ module DiscourseDataExploreropts[:explain] = true if params[:explain] == "true"opts[:limit] = if params[:format] == "csv"- if params[:limit].present?- limit = params[:limit].to_i- limit = QUERY_RESULT_MAX_LIMIT if limit > QUERY_RESULT_MAX_LIMIT- limit- else- QUERY_RESULT_MAX_LIMIT- end- elsif params[:limit].present?- params[:limit] == "ALL" ? "ALL" : params[:limit].to_i+ limit = params.fetch(:limit, QUERY_RESULT_MAX_LIMIT).to_i+ limit = QUERY_RESULT_MAX_LIMIT if limit > QUERY_RESULT_MAX_LIMIT+ limit+ else+ fetch_limit_from_params(+ default: SiteSetting.data_explorer_query_result_limit,+ max: QUERY_RESULT_MAX_LIMIT,+ )endresult = DataExplorer.run_query(query, query_params, opts)diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjsindex ee315b7edfa..48370a2ca23 100644--- a/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/components/query-result.gjs@@ -10,6 +10,7 @@ import getURL from "discourse/lib/get-url";import Badge from "discourse/models/badge";import Category from "discourse/models/category";import I18n, { i18n } from "discourse-i18n";+import { QUERY_RESULT_MAX_LIMIT } from "discourse/plugins/discourse-data-explorer/discourse/lib/constants";import DataExplorerBarChart from "./data-explorer-bar-chart";import QueryRowContent from "./query-row-content";import BadgeViewComponent from "./result-types/badge";@@ -291,7 +292,7 @@ export default class QueryResult extends Component {addInput("params", JSON.stringify(this.params));addInput("explain", this.explainText);- addInput("limit", "1000000");+ addInput("limit", String(QUERY_RESULT_MAX_LIMIT));ajax("/session/csrf.json").then((csrf) => {addInput("authenticity_token", csrf.csrf);diff --git a/plugins/discourse-data-explorer/assets/javascripts/discourse/lib/constants.js b/plugins/discourse-data-explorer/assets/javascripts/discourse/lib/constants.jsnew file mode 100644index 00000000000..9d445d30f4e--- /dev/null+++ b/plugins/discourse-data-explorer/assets/javascripts/discourse/lib/constants.js@@ -0,0 +1,4 @@+// DO NOT EDIT THIS FILE!!!+// Update it by running `rake javascript:update_constants`++export const QUERY_RESULT_MAX_LIMIT = 10000;diff --git a/plugins/discourse-data-explorer/lib/tasks/javascript.rake b/plugins/discourse-data-explorer/lib/tasks/javascript.rakenew file mode 100644index 00000000000..3f4f5800374--- /dev/null+++ b/plugins/discourse-data-explorer/lib/tasks/javascript.rake@@ -0,0 +1,11 @@+# frozen_string_literal: true++task "javascript:update_constants" => :environment do+ write_template(+ "../plugins/discourse-data-explorer/assets/javascripts/discourse/lib/constants.js",+ "update_constants",+ <<~JS,+ export const QUERY_RESULT_MAX_LIMIT = #{DiscourseDataExplorer::QUERY_RESULT_MAX_LIMIT};+ JS+ )+enddiff --git a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rbindex 9f977b60337..81fe8f92233 100644--- a/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb+++ b/plugins/discourse-data-explorer/spec/requests/query_controller_spec.rb@@ -499,21 +499,17 @@ describe DiscourseDataExplorer::QueryController dopost "/admin/plugins/discourse-data-explorer/queries/#{query.id}/run.json",params: {- limit: "ALL",+ limit: DiscourseDataExplorer::QUERY_RESULT_MAX_LIMIT + 1,}- expect(response_json["rows"].count).to eq(3)+ expect(response.status).to eq(400)endit "should limit the results in CSV download" do- begin- original_const = DiscourseDataExplorer::QUERY_RESULT_MAX_LIMIT- DiscourseDataExplorer.send(:remove_const, "QUERY_RESULT_MAX_LIMIT")- DiscourseDataExplorer.const_set("QUERY_RESULT_MAX_LIMIT", 2)-- query = make_query <<~SQL+ query = make_query <<~SQLSELECT id FROM posts- SQL+ SQL+ stub_const(DiscourseDataExplorer, "QUERY_RESULT_MAX_LIMIT", 2) dopost "/admin/plugins/discourse-data-explorer/queries/#{query.id}/run.csv",params: {download: 1,@@ -527,16 +523,12 @@ describe DiscourseDataExplorer::QueryController do}expect(response.body.split("\n").count).to eq(2)- # The value `ALL` is not supported in csv exports.post "/admin/plugins/discourse-data-explorer/queries/#{query.id}/run.csv",params: {download: 1,- limit: "ALL",+ limit: DiscourseDataExplorer::QUERY_RESULT_MAX_LIMIT + 1,}- expect(response.body.split("\n").count).to eq(1)- ensure- DiscourseDataExplorer.send(:remove_const, "QUERY_RESULT_MAX_LIMIT")- DiscourseDataExplorer.const_set("QUERY_RESULT_MAX_LIMIT", original_const)+ expect(response.body.split("\n").count).to eq(3)endendend