Skip to content

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

Harden JSON-encoded parameter-values request schema (#80035)

bryan7b9b3767654d
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.edn
index e92f2d23137..67438efba20 100644
--- a/.clj-kondo/config/modules/config.edn
+++ b/.clj-kondo/config/modules/config.edn
@@ -835,6 +835,7 @@
events
lib-be
models
+ parameters
public-sharing
queries
query-permissions
diff --git a/src/metabase/dashboards_rest/api.clj b/src/metabase/dashboards_rest/api.clj
index 259afe87cea..73feae604dd 100644
--- a/src/metabase/dashboards_rest/api.clj
+++ b/src/metabase/dashboards_rest/api.clj
@@ -50,10 +50,8 @@
[metabase.util :as u]
[metabase.util.honey-sql-2 :as h2x]
[metabase.util.i18n :refer [deferred-tru tru]]
- [metabase.util.json :as json]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
- [metabase.util.malli.registry :as mr]
[metabase.util.malli.schema :as ms]
[metabase.xrays.core :as xrays]
[ring.util.codec :as codec]
@@ -693,25 +691,15 @@
[:id ms/PositiveInt]]
_query-params
{:keys [parameters paper_size]} :- [:map
- [:parameters {:optional true} [:maybe [:or
- [:sequential ::parameters.schema/parameter-with-value]
- ;; JSON-string form for <form>-driven
- ;; downloads, mirroring the dashcard
- ;; export-format endpoint
- ms/JSONString]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:paper_size {:default "a4"} [:maybe [:enum "a4" "letter"]]]]
_request
respond
raise]
(try
(let [dashboard (api/read-check :model/Dashboard id)
- params (cond-> parameters
- (string? parameters) json/decode+kw)
- ;; the array form is validated by the endpoint schema, but a JSON string only proves it's *valid JSON* --
- ;; once decoded it must still be a well-formed parameter list, or it's a 400 (not a 500)
- _ (api/check-400 (mr/validate [:maybe [:sequential ::parameters.schema/parameter-with-value]] params))
pdf-bytes (channel.render/render-dashboard-to-pdf id api/*current-user-id*
- (or params [])
+ (or parameters [])
(keyword (or paper_size "a4")))
filename (str (or (not-empty (u/slugify (:name dashboard)))
(str "dashboard-" id))
@@ -1532,11 +1520,7 @@
format-rows? :format_rows
pivot-results? :pivot_results}
:- [:map
- [:parameters {:optional true} [:maybe [:or
- [:sequential ::parameters.schema/parameter-with-value]
- ;; support <form> encoded params for backwards compatibility... see
- ;; https://metaboat.slack.com/archives/C010L1Z4F9S/p1738003606875659
- ms/JSONString]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]]]
(m/mapply qp.dashboard/process-query-for-dashcard
@@ -1544,8 +1528,7 @@
:card (api/check-404 (t2/select-one :model/Card card-id))
:dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
:export-format export-format
- :parameters (cond-> parameters
- (string? parameters) json/decode+kw)
+ :parameters parameters
:context (api.dataset/export-format->context export-format)
:constraints nil
;; TODO -- passing this `:middleware` map is a little repetitive, need to think of a way to not have to
diff --git a/src/metabase/documents/api/document.clj b/src/metabase/documents/api/document.clj
index 7d8439efca2..13d651c6a5f 100644
--- a/src/metabase/documents/api/document.clj
+++ b/src/metabase/documents/api/document.clj
@@ -11,6 +11,7 @@
[metabase.documents.schema :as documents.schema]
[metabase.events.core :as events]
[metabase.lib-be.schema :as lib-be.schema]
+ [metabase.parameters.schema :as parameters.schema]
[metabase.public-sharing.validation :as public-sharing.validation]
[metabase.queries.core :as card]
[metabase.query-permissions.core :as query-perms]
@@ -18,7 +19,6 @@
[metabase.query-processor.card :as qp.card]
[metabase.util :as u]
[metabase.util.i18n :refer [tru]]
- [metabase.util.json :as json]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[toucan2.core :as t2]))
@@ -29,7 +29,7 @@
[:name ms/NonBlankString]
[:dataset_query ::lib-be.schema/maybe-legacy-query]
[:entity_id {:optional true} [:maybe ms/NonBlankString]]
- [:parameters {:optional true} [:maybe [:sequential ms/Map]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/parameters]]
[:parameter_mappings {:optional true} [:maybe [:sequential ms/Map]]]
[:description {:optional true} [:maybe ms/NonBlankString]]
[:display ms/NonBlankString]
@@ -485,16 +485,13 @@
format-rows? :format_rows
:as _body}
:- [:map
- [:parameters {:optional true} [:maybe [:or
- [:sequential ms/Map]
- ms/JSONString]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]]]
(validate-card-in-document document-id card-id)
(qp.card/process-query-for-card
(api/check-404 (t2/select-one :model/Card card-id)) export-format
- :parameters (cond-> parameters
- (string? parameters) json/decode+kw)
+ :parameters parameters
:constraints nil
:context (api.dataset/export-format->context export-format)
:middleware {:process-viz-settings? true
diff --git a/src/metabase/embedding_rest/api/embed.clj b/src/metabase/embedding_rest/api/embed.clj
index a7c7219ee78..7f59d6144b1 100644
--- a/src/metabase/embedding_rest/api/embed.clj
+++ b/src/metabase/embedding_rest/api/embed.clj
@@ -22,6 +22,7 @@
[metabase.embedding-rest.api.common :as api.embed.common]
[metabase.embedding.jwt :as embedding.jwt]
[metabase.events.core :as events]
+ [metabase.parameters.schema :as parameters.schema]
[metabase.query-processor.card :as qp.card]
[metabase.query-processor.middleware.constraints :as qp.constraints]
[metabase.query-processor.pivot :as qp.pivot]
@@ -401,13 +402,12 @@
[:y ms/Int]]
{:keys [parameters latField lonField]}
:- [:map
- [:parameters {:optional true} ms/JSONString]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField string?]
[:lonField string?]]]
(let [unsigned (unsign-and-translate-ids token)
card-id (api.embed.common/unsigned-token->card-id unsigned)
card (api/check-404 (t2/select-one :model/Card card-id))
- parameters (when parameters (json/decode+kw parameters))
lat-field (json/decode+kw latField)
lon-field (json/decode+kw lonField)]
(api.embed.common/check-embedding-enabled-for-card card)
@@ -434,7 +434,7 @@
[:y ms/Int]]
{:keys [parameters latField lonField]}
:- [:map
- [:parameters {:optional true} ms/JSONString]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField string?]
[:lonField string?]]]
(let [unsigned (unsign-and-translate-ids token)
@@ -442,7 +442,6 @@
dashboard (api/check-404 (t2/select-one :model/Dashboard dashboard-id))
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
card (api/check-404 (t2/select-one :model/Card card-id))
- parameters (when parameters (json/decode+kw parameters))
lat-field (json/decode+kw latField)
lon-field (json/decode+kw lonField)]
(api.embed.common/check-embedding-enabled-for-dashboard dashboard)
diff --git a/src/metabase/embedding_rest/api/preview_embed.clj b/src/metabase/embedding_rest/api/preview_embed.clj
index 5f354ec06f4..3b90930f218 100644
--- a/src/metabase/embedding_rest/api/preview_embed.clj
+++ b/src/metabase/embedding_rest/api/preview_embed.clj
@@ -14,6 +14,7 @@
[metabase.embedding-rest.api.common :as api.embed.common]
[metabase.embedding.jwt :as embed]
[metabase.embedding.validation :as embedding.validation]
+ [metabase.parameters.schema :as parameters.schema]
[metabase.query-processor.pivot :as qp.pivot]
[metabase.request.core :as request]
[metabase.util.json :as json]
@@ -240,13 +241,12 @@
[:y ms/Int]]
{:keys [parameters latField lonField]}
:- [:map
- [:parameters {:optional true} ms/JSONString]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField string?]
[:lonField string?]]]
(let [unsigned-token (check-and-unsign token)
card-id (api.embed.common/unsigned-token->card-id unsigned-token)
card (api/check-404 (t2/select-one :model/Card card-id))
- parameters (json/decode+kw parameters)
lat-field (json/decode+kw latField)
lon-field (json/decode+kw lonField)]
(request/as-admin
@@ -272,7 +272,7 @@
[:y ms/Int]]
{:keys [parameters latField lonField]}
:- [:map
- [:parameters {:optional true} ms/JSONString]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField string?]
[:lonField string?]]]
(let [unsigned-token (check-and-unsign token)
@@ -280,7 +280,6 @@
dashboard (api/check-404 (t2/select-one :model/Dashboard dashboard-id))
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
card (api/check-404 (t2/select-one :model/Card card-id))
- parameters (json/decode+kw parameters)
lat-field (json/decode+kw latField)
lon-field (json/decode+kw lonField)]
(request/as-admin
diff --git a/src/metabase/parameters/schema.cljc b/src/metabase/parameters/schema.cljc
index c8ec0080659..27ec0176a06 100644
--- a/src/metabase/parameters/schema.cljc
+++ b/src/metabase/parameters/schema.cljc
@@ -1,7 +1,8 @@
(ns metabase.parameters.schema
(:require
#?@(:clj
- ([metabase.models.interface :as mi]))
+ ([metabase.models.interface :as mi]
+ [metabase.util.json :as json]))
[metabase.lib.core :as lib]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.lib.schema.id :as lib.schema.id]
@@ -132,6 +133,17 @@
[parameters]
(lib/normalize ::parameters-with-optional-types parameters))
+#?(:clj
+ (defn json-encoded
+ "Wrap a parameters `schema` so that it also accepts the JSON-encoded string form. The public sharing, embedding and
+ tile endpoints take their parameters on the query string, where that is the only form available."
+ [schema]
+ [:schema
+ {:decode/api (fn [x]
+ (cond-> x
+ (string? x) json/decode+kw))}
+ schema]))
+
#?(:clj
(def transform-parameters
"Toucan 2 transform for columns that are sequences of Card/Dashboard parameters."
@@ -147,12 +159,30 @@
[:id ::lib.schema.common/non-blank-string]
;; the name of the template tag this value is for, when it can't be inferred from `:target`
[:name {:optional true} [:maybe ::lib.schema.common/non-blank-string]]
+ [:slug {:optional true} [:maybe ::lib.schema.common/non-blank-string]]
[:type {:optional true} [:maybe [:ref ::lib.schema.parameter/type]]]
[:value {:optional true} [:ref ::lib.schema.parameter/parameter.value]]
[:default {:optional true} [:ref ::lib.schema.parameter/parameter.value]]
[:target {:optional true} [:maybe [:ref ::lib.schema.parameter/target]]]
[:options {:optional true} [:maybe [:ref ::lib.schema.parameter/parameter.options]]]])
+;; The `:parameters` a client sends when *running* something: values for parameter slots the Card or Dashboard
+;; already declares. Contrast `::parameters`, which is the declaration of the slots themselves and belongs on the
+;; endpoints that save a Card or Dashboard.
+;;
+;; Deliberately no docstring: `mr/def` turns one into a `:description`, which is what a request that fails this
+;; schema is told it should have sent. Without it the description is built from the shape instead -- "sequence of
+;; parameter must be a map with an :id key".
+(mr/def ::parameter-values
+ [:sequential [:ref ::parameter-with-value]])
+
+;; [[parameter-values]] as it arrives over HTTP: either a JSON array, or -- for the endpoints that take parameters on
+;; the query string or from a `<form>` submission -- a JSON-encoded string, which is decoded and then held to the same
+;; schema. No docstring here for the same reason as above.
+#?(:clj
+ (mr/def ::api.parameter-values
+ (json-encoded ::parameter-values)))
+
(mr/def ::parameter-mapping
"Schema for a valid Parameter Mapping"
[:map
diff --git a/src/metabase/public_sharing_rest/api.clj b/src/metabase/public_sharing_rest/api.clj
index 129e11d0ddf..dbdf9315de1 100644
--- a/src/metabase/public_sharing_rest/api.clj
+++ b/src/metabase/public_sharing_rest/api.clj
@@ -188,9 +188,9 @@
[{:keys [uuid]} :- [:map
[:uuid ms/UUIDString]]
{:keys [parameters]} :- [:map
- [:parameters {:optional true} [:maybe ms/JSONString]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:ignore_cache {:optional true} [:maybe ms/BooleanValue]]]]
- (process-query-for-card-with-public-uuid uuid :api (json/decode+kw parameters)))
+ (process-query-for-card-with-public-uuid uuid :api parameters))
;; TODO (Cam 10/28/25) -- fix this endpoint so it uses kebab-case for query parameters for consistency with the rest
;; of the REST API
@@ -209,12 +209,12 @@
{:keys [parameters format_rows pivot_results]} :- [:map
[:format_rows {:default false} :boolean]
[:pivot_results {:default false} :boolean]
- [:parameters {:optional true} [:maybe ms/JSONString]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:csv_include_bom {:optional true} [:maybe ms/BooleanValue]]]]
(process-query-for-card-with-public-uuid
uuid
export-format
- (json/decode+kw parameters)
+ parameters
:constraints nil
:middleware {:process-viz-settings? true
:js-int-to-string? false
@@ -297,7 +297,7 @@
pre-loaded Dashboard `dashboard`.
Additional options:
- * `parameters` - MBQL query parameters, either already parsed or as a serialized JSON string
+ * `parameters` - MBQL query parameters, already decoded and validated by the caller's endpoint schema
* `export-format` - `:api` (default format with metadata), `:json` (results only), `:csv`, or `:xslx`. Default: `:api`
* `qp` - QP function to run the query with. Default [[qp/process-query]] + [[qp/userland-context]]
@@ -314,8 +314,7 @@
{:dashboard dashboard
:dashcard dashcard
:card card
- :parameters (cond-> parameters
- (string? parameters) json/decode+kw)
+ :parameters parameters
:export-format export-format
:qp qp
:make-run process-query-for-card-with-id-run-fn})]
@@ -343,7 +342,7 @@
[:dashcard-id ms/PositiveInt]
[:card-id ms/PositiveInt]]
{:keys [parameters]} :- [:map
- [:parameters {:optional true} [:maybe ms/JSONString]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:ignore_cache {:optional true} [:maybe ms/BooleanValue]]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (api/check-404 (t2/select-one :model/Card :id card-id :archived false))
@@ -371,12 +370,7 @@
[:export-format ::qp.schema/export-format]]
_query-parameters
{:keys [format_rows pivot_results parameters]} :- [:map
- [:parameters {:optional true} [:maybe
- {:decode/api
- (fn [x]
- (cond-> x
- (string? x) json/decode+kw))}
- [:sequential ::parameters.schema/parameter-with-value]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]
[:csv_include_bom {:optional true} [:maybe ms/BooleanValue]]]]
@@ -612,9 +606,9 @@
[{:keys [uuid]} :- [:map
[:uuid ms/UUIDString]]
{:keys [parameters]} :- [:map
- [:parameters {:optional true} [:maybe ms/JSONString]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:ignore_cache {:optional true} [:maybe ms/BooleanValue]]]]
- (process-query-for-card-with-public-uuid uuid :api (json/decode+kw parameters)
+ (process-query-for-card-with-public-uuid uuid :api parameters
:qp qp.pivot/run-pivot-query))
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
@@ -630,7 +624,7 @@
[:card-id ms/PositiveInt]
[:dashcard-id ms/PositiveInt]]
{:keys [parameters]} :- [:map
- [:parameters {:optional true} [:maybe ms/JSONString]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:ignore_cache {:optional true} [:maybe ms/BooleanValue]]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (api/check-404 (t2/select-one :model/Card :id card-id :archived false))
@@ -712,12 +706,11 @@
[:y ms/Int]]
{:keys [parameters latField lonField]}
:- [:map
- [:parameters {:optional true} ms/JSONString]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField string?]
[:lonField string?]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (api/check-404 (t2/select-one :model/Card :public_uuid uuid, :archived false))
- parameters (when parameters (json/decode+kw parameters))
lat-field (json/decode+kw latField)
lon-field (json/decode+kw lonField)]
(request/as-admin
@@ -740,14 +733,13 @@
[:y ms/Int]]
{:keys [parameters latField lonField]}
:- [:map
- [:parameters {:optional true} ms/JSONString]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField string?]
[:lonField string?]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [dashboard (api/check-404 (t2/select-one :model/Dashboard :public_uuid uuid, :archived false))
dashcard (api/check-404 (t2/select-one :model/DashboardCard dashcard-id))
card (api/check-404 (t2/select-one :model/Card card-id))
- parameters (when parameters (json/decode+kw parameters))
lat-field (json/decode+kw latField)
lon-field (json/decode+kw lonField)]
(request/as-admin
@@ -827,14 +819,14 @@
[:uuid ms/UUIDString]
[:card-id ms/PositiveInt]]
{:keys [parameters]} :- [:map
- [:parameters {:optional true} [:maybe ms/JSONString]]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]]]
(public-sharing.validation/check-public-sharing-enabled)
(let [card (validate-card-in-public-document uuid card-id)]
;; Run the query as admin since public documents are available to everyone anyway
(u/prog1 (process-query-for-card-with-id
card
:api
- (json/decode+kw parameters)
+ parameters
:constraints (qp.constraints/default-query-constraints))
(events/publish-event! :event/card-read {:object-id card-id :user-id api/*current-user-id* :context :question}))))
@@ -851,9 +843,7 @@
[:export-format ::qp.schema/export-format]]
_query-params
{:keys [parameters format_rows pivot_results]} :- [:map
- [:parameters {:optional true} [:maybe [:or
- [:sequential ::parameters.schema/parameter-with-value]
- ms/JSONString]]]
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]
[:csv_include_bom {:optional true} [:maybe ms/BooleanValue]]]]
@@ -862,8 +852,7 @@
(process-query-for-card-with-id
card
export-format
- (cond-> parameters
- (string? parameters) json/decode+kw)
+ parameters
:constraints nil
:middleware {:process-viz-settings? true
:js-int-to-string? false
diff --git a/src/metabase/queries_rest/api/card.clj b/src/metabase/queries_rest/api/card.clj
index dba2650efcd..d299f589c40 100644
--- a/src/metabase/queries_rest/api/card.clj
+++ b/src/metabase/queries_rest/api/card.clj
@@ -34,7 +34,6 @@
[metabase.search.core :as search]
[metabase.util :as u]
[metabase.util.i18n :refer [deferred-tru trs tru]]
- [metabase.util.json :as json]
[metabase.util.log :as log]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
@@ -953,17 +952,9 @@
csv-include-bom? :csv_include_bom
:as _body}
:- [:map
- [:parameters {:optional true} [:maybe
- ;; support JSON-encoded parameters for backwards compatibility when with this
- ;; was still submitted with a `<form>`... see
- ;; https://metaboat.slack.com/archives/C010L1Z4F9S/p1738003606875659
- {:decode/api (fn [x]
- (cond-> x
- (string? x) json/decode+kw))}
- ;; TODO -- figure out what the actual schema for parameters is supposed to be
- ;; here... [[::parameters.schema/parameter]] is used for other endpoints in this namespace but
- ;; it breaks existing tests
- [:sequential [:map-of :keyword :any]]]]
+ ;; JSON-encoded string form supported for backwards compatibility with `<form>` submission... see
+ ;; https://metaboat.slack.com/archives/C010L1Z4F9S/p1738003606875659
+ [:parameters {:optional true} [:maybe ::parameters.schema/api.parameter-values]]
[:format_rows {:default false} ms/BooleanValue]
[:pivot_results {:default false} ms/BooleanValue]
[:csv_include_bom {:default false} ms/BooleanValue]]]
diff --git a/src/metabase/tiles/api.clj b/src/metabase/tiles/api.clj
index b5bf65b7296..3ee7e317b89 100644
--- a/src/metabase/tiles/api.clj
+++ b/src/metabase/tiles/api.clj
@@ -281,14 +281,6 @@
points (result->points result lat-field-ref lon-field-ref)]
(tiles-response result zoom points)))
-(mr/def ::parameters
- "Form-encoded JSON-encoded array of parameter maps."
- [:schema
- {:decode/api (fn [s]
- (when (string? s)
- (json/decode+kw s)))}
- [:sequential ::parameters.schema/parameter-with-value]])
-
;; TODO (Cam 2025-11-25) please add a response schema to this API endpoint, it makes it easier for our customers to
;; use our API + we will need it when we make auto-TypeScript-signature generation happen
;;
@@ -303,7 +295,7 @@
[:y ms/Int]]
{:keys [parameters], lat-field :latField lon-field :lonField}
:- [:map
- [:parameters {:optional true} ::parameters]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField ::legacy-ref]
[:lonField ::legacy-ref]]]
(process-tiles-query-for-card (api/check-404 (t2/select-one :model/Card card-id))
@@ -325,7 +317,7 @@
[:y ms/Int]]
{:keys [parameters] lat-field :latField, lon-field :lonField, :as _query-params}
:- [:map
- [:parameters {:optional true} ::parameters]
+ [:parameters {:optional true} ::parameters.schema/api.parameter-values]
[:latField ::legacy-ref]
[:lonField ::legacy-ref]]]
(process-tiles-query-for-dashcard (api/check-404 (t2/select-one :model/Dashboard dashboard-id))
diff --git a/test/metabase/api/macros_test.clj b/test/metabase/api/macros_test.clj
index bb293b81a10..93401762a00 100644
--- a/test/metabase/api/macros_test.clj
+++ b/test/metabase/api/macros_test.clj
@@ -2,6 +2,7 @@
(:require
[clojure.test :refer :all]
[metabase.api.macros :as api.macros]
+ [metabase.lib.schema.parameter :as lib.schema.parameter]
[metabase.util.malli.registry :as mr]))
(set! *warn-on-reflection* true)
@@ -175,3 +176,24 @@
:metadata {:deprecated "0.50.0", :multipart true}
:params {:route {:binding _route-params}, :query {:binding _query-params}}
:body [(test)]})))
+
+(deftest ^:parallel decode-strips-undeclared-keys-test
+ (testing "request decoding drops keys the schema does not name, so an open map cannot carry values downstream"
+ (are [schema value expected] (= expected
+ ((#'api.macros/decoder schema) value))
+ [:map [:a {:optional true} :int]]
+ {:a 1, :b 2}
+ {:a 1}
+
+ ;; `{:closed false}` opts out, for the values we deliberately pass through as they arrived -- a query, viz
+ ;; settings, database details, a settings bag
+ [:map {:closed false} [:a {:optional true} :int]]
+ {:a 1, :b 2}
+ {:a 1, :b 2}
+
+ ;; stripping recurses. A parameter's `:options` are spliced into the filter clause the parameter becomes, so an
+ ;; option the schema does not name must not survive decoding. The schema stays open -- an unknown option is not
+ ;; a 400 -- so this is what keeps such a key from reaching the clause.
+ ::lib.schema.parameter/parameter
+ {:type :string/contains, :value ["A"], :options {:case-sensitive false, :lib/uuid "not-yours"}}
+ {:type :string/contains, :value ["A"], :options {:case-sensitive false}})))
diff --git a/test/metabase/lib/schema/parameter_test.cljc b/test/metabase/lib/schema/parameter_test.cljc
index 9a5f8e4a378..2cf7e4fa21e 100644
--- a/test/metabase/lib/schema/parameter_test.cljc
+++ b/test/metabase/lib/schema/parameter_test.cljc
@@ -114,3 +114,26 @@
(is (= :none
(lib/normalize ::lib.schema.parameter/widget-type "broken/=")
(lib/normalize ::lib.schema.parameter/widget-type "broken")))))
+
+(deftest ^:parallel normalize-invalid-parameter-default-test
+ (testing "A parameter with no value falls back to its `:default`, so a default is normalized like a value"
+ (are [parameter normalized] (= normalized
+ (lib/normalize ::lib.schema.parameter/parameter parameter))
+ {:type :category, :default {:k "v"}} {:type :category, :default nil}
+ {:type :category, :default [{:k "v"}]} {:type :category, :default [nil]}
+ {:type :category, :default "A"} {:type :category, :default "A"}
+ {:type :category, :default ["A" "B"]} {:type :category, :default ["A" "B"]})))
+
+(deftest ^:parallel parameter-options-test
+ (testing ":options are spliced into the filter clause the parameter becomes, so a known option is typed"
+ (are [options valid?] (= valid?
+ (mr/validate ::lib.schema.parameter/parameter
+ {:type :string/contains, :value ["A"], :options options}))
+ {:case-sensitive false} true
+ {:include-current true} true
+ {} true
+ {:case-sensitive "yes"} false
+ ;; An unrecognized option is not a validation error: the map stays open so a new frontend option does not 400
+ ;; against an older backend. Such a key never reaches the clause either way, because request decoding strips
+ ;; it -- see `decode-strips-undeclared-keys-test` in [[metabase.api.macros-test]].
+ {:lib/uuid "not-yours"} true)))
diff --git a/test/metabase/public_sharing_rest/api_test.clj b/test/metabase/public_sharing_rest/api_test.clj
index 8406f39b06c..1506fd8a13b 100644
--- a/test/metabase/public_sharing_rest/api_test.clj
+++ b/test/metabase/public_sharing_rest/api_test.clj
@@ -527,6 +527,21 @@
:type "id",
:value nil}]))))))))))
+(deftest execute-public-card-with-invalid-parameters-test
+ (testing "GET /api/public/card/:uuid/query -- malformed `parameters` is rejected with a 400"
+ (mt/with-temporary-setting-values [enable-public-sharing true]
+ (with-temp-public-card [{uuid :public_uuid} {}]
+ (let [url (str "public/card/" uuid "/query")]
+ (testing "valid JSON that is not a sequence of parameter maps"
+ (client/client :get 400 url
+ :parameters (json/encode {:_PRICE_ 3})))
+ (testing "a sequence of maps missing the required :id"
+ (client/client :get 400 url
+ :parameters (json/encode [{:value 3}])))
+ (testing "a parameter with a non-scalar garbage :id"
+ (client/client :get 400 url
+ :parameters (json/encode [{:id {:not "a string"} :value 3}]))))))))
+
;; Cards with required params
(defn- do-with-required-param-card! [f]
(mt/with-temporary-setting-values [enable-public-sharing true]