Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Serve no values for a filter whose widget is an Input box (#82535)

Nathan Voxlandda7629bba6d3
diff --git a/src/metabase/parameters/custom_values.clj b/src/metabase/parameters/custom_values.clj
index 8426c0aafd8..85319dc5931 100644
--- a/src/metabase/parameters/custom_values.clj
+++ b/src/metabase/parameters/custom_values.clj
@@ -201,8 +201,24 @@
;;; --------------------------------------------- Putting it together ----------------------------------------------
+(def ^:private input-box-by-default-types
+ "Parameter types whose widget is an Input box unless `values_query_type` says otherwise.
+ Mirrors `getDefaultQueryType` in `parameter-source.ts`."
+ #{:string/contains :string/does-not-contain :string/starts-with :string/ends-with
+ :number/<= :number/>= :number/between})
+
+(defn- input-box?
+ "Is `parameter`'s widget an Input box (`values_query_type` `none`), the setting that says to offer no list of values?
+ The frontend never asks for values for such a widget, so the values routes should not hand them out either."
+ [parameter]
+ (let [query-type (some-> (:values_query_type parameter) keyword)]
+ (or (= query-type :none)
+ (and (nil? query-type)
+ (contains? input-box-by-default-types (some-> (:type parameter) keyword))))))
+
(mu/defn parameter->values :- ms/FieldValuesResult
- "Given a parameter with a custom-values source, return the values.
+ "Given a parameter with a custom-values source, return the values. A parameter whose widget is an Input box offers
+ no values, whatever its source.
`default-case-thunk` is a 0-arity function that returns values list when:
- :values_source_type = card but the card is archived or the card no longer contains the value-field.
@@ -212,21 +228,23 @@
default-case-thunk :- [:=> [:cat :any] [:map {:closed true}
[:has_more_values :boolean]
[:values ms/FieldValuesList]]]]
- (case (:values_source_type parameter)
- :static-list (static-list-values parameter query-string)
- :card (let [config (:values_source_config parameter)
- card (parameters.db/card (:card_id config))]
- (when-not (mi/can-read? card)
- (throw (ex-info "You don't have permissions to do that." {:status-code 403})))
- (or (when-not (:archived card)
- (when-let [query (card-query (:id card) (some-> (:dataset_query card) not-empty lib-be/normalize-query))]
- (when (can-get-card-values? query (:value_field config))
- (card-values query config query-string))))
- (default-case-thunk)))
- nil (default-case-thunk)
- (throw (ex-info (tru "Invalid parameter source {0}" (:values_source_type parameter))
- {:status-code 400
- :parameter parameter}))))
+ (if (input-box? parameter)
+ {:values [], :has_more_values false}
+ (case (:values_source_type parameter)
+ :static-list (static-list-values parameter query-string)
+ :card (let [config (:values_source_config parameter)
+ card (parameters.db/card (:card_id config))]
+ (when-not (mi/can-read? card)
+ (throw (ex-info "You don't have permissions to do that." {:status-code 403})))
+ (or (when-not (:archived card)
+ (when-let [query (card-query (:id card) (some-> (:dataset_query card) not-empty lib-be/normalize-query))]
+ (when (can-get-card-values? query (:value_field config))
+ (card-values query config query-string))))
+ (default-case-thunk)))
+ nil (default-case-thunk)
+ (throw (ex-info (tru "Invalid parameter source {0}" (:values_source_type parameter))
+ {:status-code 400
+ :parameter parameter})))))
(defn pk-of-fk-pk-field-ids
"Check if the collection `field-ids` contains the IDs of FK fields pointing to the same PK and
diff --git a/test/metabase/dashboards_rest/api_test.clj b/test/metabase/dashboards_rest/api_test.clj
index 85c368c8b41..4d007a0442c 100644
--- a/test/metabase/dashboards_rest/api_test.clj
+++ b/test/metabase/dashboards_rest/api_test.clj
@@ -5860,6 +5860,7 @@
(mt/with-temp [:model/Card {source-id :id} {:dataset_query (lib/query mp (lib.metadata/table mp (mt/id :orders)))}
:model/Dashboard {id :id}
{:parameters [{:name "Number" :slug "number" :id "_NUM_" :type :number/<=
+ :values_query_type "list"
:values_source_type "card"
:values_source_config
{:card_id source-id
@@ -5988,10 +5989,13 @@
"backend does not clean up dashcard parameter_mappings that reference a removed parameter_id")))))
(deftest values-endpoint-does-not-gate-by-operator-type-test
- (testing "GET /api/dashboard/:id/params/:param-key/values has no operator-type gating"
+ (testing "GET /api/dashboard/:id/params/:param-key/values has no operator-type gating once the widget is a dropdown"
(with-chain-filter-fixtures [{:keys [dashboard param-keys]}]
(mt/user-http-request :rasta :put 200 (str "dashboard/" (:id dashboard))
- {:parameters (mapv (fn [p] (cond-> p (= (:id p) (:category-name param-keys)) (assoc :type :string/starts-with)))
+ {:parameters (mapv (fn [p]
+ (cond-> p
+ (= (:id p) (:category-name param-keys))
+ (assoc :type :string/starts-with, :values_query_type "list")))
(:parameters dashboard))})
(is (seq (:values (mt/user-http-request :rasta :get 200
(chain-filter-values-url (:id dashboard) (:category-name param-keys)))))))))
diff --git a/test/metabase/parameters/custom_values_test.clj b/test/metabase/parameters/custom_values_test.clj
index ce78c83bed2..00b2fbafcdf 100644
--- a/test/metabase/parameters/custom_values_test.clj
+++ b/test/metabase/parameters/custom_values_test.clj
@@ -373,6 +373,53 @@
nil
(constantly mock-default-result))))))))))
+(deftest ^:parallel parameter->values-input-box-test
+ (testing "a parameter whose widget is an Input box offers no values, whatever its source"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (mt/with-temp [:model/Card {card-id :id} (mt/card-with-source-metadata-for-query (mt/mbql-query venues))]
+ (doseq [[widget-desc widget] [["values_query_type = none" {:type :string/=, :values_query_type :none}]
+ ;; the frontend defaults these to an Input box and never saves the default
+ ["string/contains, no values_query_type" {:type :string/contains}]
+ ["string/starts-with, no values_query_type" {:type :string/starts-with}]
+ ["number/between, no values_query_type" {:type :number/between}]
+ ["number/<=, no values_query_type" {:type :number/<=}]]
+ [source-desc source] [["a connected field" {}]
+ ["a static list" {:values_source_type :static-list
+ :values_source_config {:values ["African" "American"]}}]
+ ["a card" {:values_source_type :card
+ :values_source_config {:card_id card-id
+ :value_field (mt/$ids $venues.name)}}]]
+ query-string [nil "af"]]
+ (testing (format "widget: %s, source: %s, query: %s" widget-desc source-desc (pr-str query-string))
+ (is (= {:values []
+ :has_more_values false}
+ (custom-values/parameter->values
+ (merge {:name "Name", :slug "name", :id "_NAME_"}
+ widget
+ source)
+ query-string
+ (fn [] (throw (ex-info "Shouldn't ask the connected field for values" {}))))))))))))
+
+(deftest ^:parallel parameter->values-not-input-box-test
+ (testing "a parameter whose widget is not an Input box still offers values"
+ (mt/with-current-user (mt/user->id :crowberto)
+ (doseq [[widget-desc widget] [["string/=, no values_query_type" {:type :string/=}]
+ ["string/contains, values_query_type = list" {:type :string/contains, :values_query_type :list}]
+ ["string/contains, values_query_type = search" {:type :string/contains, :values_query_type :search}]
+ ["number/between, values_query_type = list" {:type :number/between, :values_query_type :list}]]]
+ (testing widget-desc
+ (is (= {:values [["African"] ["American"]]
+ :has_more_values false}
+ (custom-values/parameter->values
+ (merge {:name "Name"
+ :slug "name"
+ :id "_NAME_"
+ :values_source_type :static-list
+ :values_source_config {:values ["African" "American"]}}
+ widget)
+ nil
+ (fn [] (throw (ex-info "Shouldn't ask the connected field for values" {})))))))))))
+
(deftest ^:parallel parameter->values-join-aliased-value-field-test
(let [mp (mt/metadata-provider)
venue-table (lib.metadata/table mp (mt/id :venues))
diff --git a/test/metabase/public_sharing_rest/api_test.clj b/test/metabase/public_sharing_rest/api_test.clj
index 11730114f82..03ef250cc45 100644
--- a/test/metabase/public_sharing_rest/api_test.clj
+++ b/test/metabase/public_sharing_rest/api_test.clj
@@ -1522,6 +1522,54 @@
(param-values-url :card field-filter-uuid
(:field-values param-keys) "bar"))))))))))))
+(deftest param-values-input-box-test
+ (testing "A filter set to Input box (values_query_type = none) offers no values, even anonymously (SEC-1211)"
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (let [name-param-id "_NAME_"
+ contains-param-id "_NAME_CONTAINS_"
+ category-param-id "_CATEGORY_"
+ parameters [{:id name-param-id
+ :name "Name"
+ :slug "name"
+ :type :string/=
+ :values_query_type "none"
+ :target [:dimension [:field (mt/id :venues :name) nil]]}
+ ;; the frontend defaults a `contains` filter to an Input box without saving
+ ;; `values_query_type`
+ {:id contains-param-id
+ :name "Name contains"
+ :slug "name_contains"
+ :type :string/contains
+ :target [:dimension [:field (mt/id :venues :name) nil]]}
+ {:id category-param-id
+ :name "Category"
+ :slug "category"
+ :type :id
+ :values_query_type "none"
+ :target [:dimension [:field (mt/id :venues :category_id) nil]]}]]
+ (mt/with-temp [:model/Card {card-id :id, card-uuid :public_uuid} {:public_uuid (str (random-uuid))
+ :dataset_query (mt/mbql-query venues {:filter [:= $price 1]})
+ :parameters parameters}
+ :model/Dashboard {dash-uuid :public_uuid, dashboard-id :id} {:public_uuid (str (random-uuid))
+ :parameters (mapv #(dissoc % :target) parameters)}
+ :model/DashboardCard _ {:dashboard_id dashboard-id
+ :card_id card-id
+ :parameter_mappings (for [{:keys [id target]} parameters]
+ {:parameter_id id, :card_id card-id, :target target})}]
+ (doseq [[model uuid] [[:card card-uuid] [:dashboard dash-uuid]]
+ param-id [name-param-id contains-param-id]]
+ (testing (format "GET /api/public/%s/:uuid/params/%s/values" (name model) param-id)
+ (is (= {:values [], :has_more_values false}
+ (client/client :get 200 (param-values-url model uuid param-id)))))
+ (testing (format "GET /api/public/%s/:uuid/params/%s/search/:query" (name model) param-id)
+ (is (= {:values [], :has_more_values false}
+ (client/client :get 200 (param-values-url model uuid param-id "red"))))))
+ (doseq [[model uuid] [[:card card-uuid] [:dashboard dash-uuid]]]
+ (testing (format "GET /api/public/%s/:uuid/params/:param-key/remapping still labels a chosen value" (name model))
+ (is (= [2 "American"]
+ (client/client :get 200 (format "public/%s/%s/params/%s/remapping?value=2"
+ (name model) uuid category-param-id)))))))))))
+
(deftest card-param-fields-public-columns-test
(testing "GET /api/public/card/:uuid :param_fields only carry the public Field columns"
(mt/with-temporary-setting-values [enable-public-sharing true]