Skip to content

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

Improve API schemas (#80332)

Alexander Polyankinede6a0ec56e4
diff --git a/enterprise/backend/test/metabase_enterprise/sandbox/schema_test.clj b/enterprise/backend/test/metabase_enterprise/sandbox/schema_test.clj
index e7605ad39b3..76801d301ac 100644
--- a/enterprise/backend/test/metabase_enterprise/sandbox/schema_test.clj
+++ b/enterprise/backend/test/metabase_enterprise/sandbox/schema_test.clj
@@ -26,12 +26,16 @@
(deftest ^:parallel normalize-attribute-remappings-dimension-targets-test
(testing "dimension targets (bare, with stage-number, and with field-ref options) round-trip through the normalization transform"
- (doseq [target [[:dimension [:field (mt/id :venues :category_id) nil]]
- [:dimension [:field (mt/id :venues :category_id) nil] {"stage-number" 0}]
- [:dimension [:field (mt/id :venues :category_id) {:base-type :type/Integer}] {"stage-number" 0}]]]
+ (doseq [[target expected]
+ [[[:dimension [:field (mt/id :venues :category_id) nil]]
+ [:dimension [:field (mt/id :venues :category_id) nil]]]
+ [[:dimension [:field (mt/id :venues :category_id) nil] {"stage-number" 0}]
+ [:dimension [:field (mt/id :venues :category_id) nil] {:stage-number 0}]]
+ [[:dimension [:field (mt/id :venues :category_id) {:base-type :type/Integer}] {"stage-number" 0}]
+ [:dimension [:field (mt/id :venues :category_id) {:base-type :type/Integer}] {:stage-number 0}]]]]
(mt/with-temp [:model/PermissionsGroup group {}
:model/Sandbox g {:table_id (mt/id :venues)
:group_id (u/the-id group)
:attribute_remappings {"cat" target}}]
- (is (= {"cat" target}
+ (is (= {"cat" expected}
(t2/select-one-fn :attribute_remappings :model/Sandbox :id (u/the-id g))))))))
diff --git a/src/metabase/agent_api/api.clj b/src/metabase/agent_api/api.clj
index f5d570d662b..adf7a610d29 100644
--- a/src/metabase/agent_api/api.clj
+++ b/src/metabase/agent_api/api.clj
@@ -20,6 +20,7 @@
[metabase.dashboards.models.dashboard-card :as dashboard-card]
[metabase.events.core :as events]
[metabase.lib-be.core :as lib-be]
+ [metabase.lib-be.schema :as lib-be.schema]
[metabase.lib.core :as lib]
[metabase.lib.schema.common :as lib.schema.common]
[metabase.metabot.core :as metabot]
@@ -450,6 +451,17 @@
:constraints {:max-results page-size
:max-results-bare-rows page-size}))
+(defn- normalize-and-validate-query
+ "Normalize a decoded query map to a well-formed MBQL 5 query and return it, stripping undeclared keys and
+ throwing a 400 if it is not valid. Also converts legacy MBQL to MBQL 5."
+ [q]
+ (api.macros/decode-and-validate-params :body ::lib-be.schema/maybe-legacy-query q))
+
+(defn- decode-and-validate-query
+ "Decode a base64-encoded JSON query string into a validated MBQL query map."
+ [s]
+ (normalize-and-validate-query (-> s u/decode-base64 json/decode)))
+
(mr/def ::query-request
"Request body for /v2/query, one of three shapes:
- `{:continuation_token <string>}` from a prior response (pagination);
@@ -473,39 +485,23 @@
[:handle [:map {:closed true} [:query ms/NonBlankString]]]
[:fresh ::construct-query-request]])
-(defn- native-marker?
- "True if `node` is a map carrying a native-SQL marker: a `:native` query body (the universal signal
- across legacy and MBQL 5 native forms), a legacy `:type :native`, or an MBQL 5 `:mbql.stage/native`
- `:lib/type`. Membership tests cover the keyword and json-decoded string forms and never coerce, so
- junk values don't throw. A legitimate serialized MBQL query carries none of these."
- [node]
- (and (map? node)
- (or (contains? node :native)
- (contains? #{:native "native"} (:type node))
- (contains? #{:mbql.stage/native "mbql.stage/native"} (:lib/type node)))))
-
-(defn- native-query?
- "True if `query-map` (a decoded, client-reachable query) contains native SQL anywhere in its tree —
- legacy top-level `:type :native`, a legacy nested `:source-query`'s `:native`, or an MBQL 5
- `:mbql.stage/native` stage, including inside joins or nested joins.
- A whole-tree scan, because these endpoints are MBQL-only by scope: a native marker at any depth
- means the payload is smuggling raw SQL, regardless of how it's nested."
- [query-map]
- (boolean (some native-marker? (tree-seq coll? seq query-map))))
-
(defn- reject-native-query!
- "Throw a 400 if `query-map` is a native query.
+ "Throw a 400 if `query-map` is a native query anywhere — top-level, nested, or in a join, in either the
+ legacy or the MBQL 5 form. Normalizes the payload to MBQL 5 (best-effort) and checks for a native stage
+ with [[lib/any-native-stage?]], so the check reads keyword `:lib/type`s regardless of how the JSON was
+ decoded; a payload too malformed to normalize is left for the shape and validation checks that follow.
`/v2/query` and `/v1/execute` are gated by the MBQL-execution scopes (`agent:query` /
`agent:query:execute`), not `agent:sql:execute`. The opaque base64 payloads they accept (a
- query_handle, a continuation token) could carry a native query — legacy top-level `:type :native`
- or an MBQL 5 native stage; allowing either would let a token without the SQL-execution scope run
- raw SQL, defeating the scope split and bypassing the execute-sql kill switch. Force native
- execution onto `/v1/execute-sql`, which is correctly scoped."
+ query_handle, a continuation token) could carry a native query; allowing it would let a token
+ without the SQL-execution scope run raw SQL, defeating the scope split and bypassing the
+ execute-sql kill switch. Force native execution onto `/v1/execute-sql`, which is correctly scoped."
[query-map]
- (when (native-query? query-map)
+ (when (some-> (u/ignore-exceptions (lib-be/normalize-query query-map))
+ not-empty
+ lib/any-native-stage?)
(throw (ex-info "Native queries are not supported here; use execute_sql instead."
- {:status-code 400 :query-map query-map}))))
+ {:status-code 400}))))
(defn- validate-serialized-query!
"Sanity-check a decoded MBQL query map from a client-reachable base64 payload (query_handle or token).
@@ -555,14 +551,18 @@
(let [{:keys [query pagination]} (decode-continuation-token (:continuation_token body))]
(reject-native-query! query)
(validate-serialized-query! query)
- (check-token-query-permissions! query)
- {:query query :total-limit (:limit pagination) :page (:page pagination)})
+ (let [query (normalize-and-validate-query query)]
+ (check-token-query-permissions! query)
+ {:query query :total-limit (:limit pagination) :page (:page pagination)}))
(string? (:query body))
(let [query (decode-base64-json-map (:query body))]
(reject-native-query! query)
(validate-serialized-query! query)
- {:query query :total-limit (clamp-total-limit (serialized-query-limit query)) :page 1})
+ (let [query (normalize-and-validate-query query)]
+ {:query query
+ :total-limit (clamp-total-limit (serialized-query-limit query))
+ :page 1}))
:else
(let [live-query (evaluate-external-query-to-live-query body)]
@@ -669,12 +669,11 @@
[_route-params
_query-params
{encoded-query :query} :- ::execute-query-request]
- (let [query (-> encoded-query
- u/decode-base64
- json/decode+kw)]
- (reject-native-query! query)
- (qp.streaming/streaming-response [rff :api]
- (qp/process-query (prepare-combined-query query) rff))))
+ (let [decoded (-> encoded-query u/decode-base64 json/decode)]
+ (reject-native-query! decoded)
+ (let [query (normalize-and-validate-query decoded)]
+ (qp.streaming/streaming-response [rff :api]
+ (qp/process-query (prepare-combined-query query) rff)))))
;;; --------------------------------------------------- Execute SQL --------------------------------------------------
@@ -831,7 +830,7 @@
agent side unless we dedup against REST too."
[{:keys [query display description visualization_settings] card-name :name :as body}
{:keys [card-type default-display validate-query!]}]
- (let [dataset-query (-> query u/decode-base64 json/decode+kw)
+ (let [dataset-query (decode-and-validate-query query)
;; `nil` means the root collection, so only default to the personal collection when the
;; key is absent. `(or ...)` would silently turn an explicit `null` into personal.
collection_id (if (contains? body :collection_id)
@@ -870,7 +869,7 @@
;; validation, cycle detection, permission check) see the canonical MBQL shape regardless of
;; whether the LLM sent legacy or MBQL 5.
new-query (when (contains? body :query)
- (-> (:query body) u/decode-base64 json/decode+kw lib-be/normalize-query))
+ (decode-and-validate-query (:query body)))
_ (when (and new-query validate-query!)
(validate-query! new-query))
raw-updates (cond-> {}
diff --git a/src/metabase/embedding_rest/api/embed.clj b/src/metabase/embedding_rest/api/embed.clj
index 7f59d6144b1..49fc75e1967 100644
--- a/src/metabase/embedding_rest/api/embed.clj
+++ b/src/metabase/embedding_rest/api/embed.clj
@@ -28,8 +28,8 @@
[metabase.query-processor.pivot :as qp.pivot]
[metabase.query-processor.schema :as qp.schema]
[metabase.request.core :as request]
+ [metabase.tiles.api :as api.tiles]
[metabase.util :as u]
- [metabase.util.json :as json]
[metabase.util.malli :as mu]
[metabase.util.malli.schema :as ms]
[ring.util.codec :as codec]
@@ -403,20 +403,18 @@
{:keys [parameters latField lonField]}
:- [:map
[:parameters {:optional true} ::parameters.schema/api.parameter-values]
- [:latField string?]
- [:lonField string?]]]
+ [:latField ::api.tiles/legacy-ref]
+ [:lonField ::api.tiles/legacy-ref]]]
(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))
- lat-field (json/decode+kw latField)
- lon-field (json/decode+kw lonField)]
+ card (api/check-404 (t2/select-one :model/Card card-id))]
(api.embed.common/check-embedding-enabled-for-card card)
(request/as-admin
(api.embed.common/process-tiles-query-for-card
card
(api.embed.common/tile-parameters-for-card
card (embedding.jwt/get-in-unsigned-token-or-throw unsigned [:params]) parameters)
- zoom x y lat-field lon-field))))
+ zoom x y latField lonField))))
;; 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
@@ -435,19 +433,17 @@
{:keys [parameters latField lonField]}
:- [:map
[:parameters {:optional true} ::parameters.schema/api.parameter-values]
- [:latField string?]
- [:lonField string?]]]
+ [:latField ::api.tiles/legacy-ref]
+ [:lonField ::api.tiles/legacy-ref]]]
(let [unsigned (unsign-and-translate-ids token)
dashboard-id (api.embed.common/unsigned-token->dashboard-id unsigned)
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))
- lat-field (json/decode+kw latField)
- lon-field (json/decode+kw lonField)]
+ card (api/check-404 (t2/select-one :model/Card card-id))]
(api.embed.common/check-embedding-enabled-for-dashboard dashboard)
(request/as-admin
(api.embed.common/process-tiles-query-for-dashcard
dashboard dashcard card
(api.embed.common/tile-parameters-for-dashboard
dashboard (embedding.jwt/get-in-unsigned-token-or-throw unsigned [:params]) parameters)
- zoom x y lat-field lon-field))))
+ zoom x y latField lonField))))
diff --git a/src/metabase/embedding_rest/api/preview_embed.clj b/src/metabase/embedding_rest/api/preview_embed.clj
index 3b90930f218..ec8435950f6 100644
--- a/src/metabase/embedding_rest/api/preview_embed.clj
+++ b/src/metabase/embedding_rest/api/preview_embed.clj
@@ -17,7 +17,7 @@
[metabase.parameters.schema :as parameters.schema]
[metabase.query-processor.pivot :as qp.pivot]
[metabase.request.core :as request]
- [metabase.util.json :as json]
+ [metabase.tiles.api :as api.tiles]
[metabase.util.malli.schema :as ms]
[ring.util.codec :as codec]
[toucan2.core :as t2]))
@@ -242,19 +242,17 @@
{:keys [parameters latField lonField]}
:- [:map
[:parameters {:optional true} ::parameters.schema/api.parameter-values]
- [:latField string?]
- [:lonField string?]]]
+ [:latField ::api.tiles/legacy-ref]
+ [:lonField ::api.tiles/legacy-ref]]]
(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))
- lat-field (json/decode+kw latField)
- lon-field (json/decode+kw lonField)]
+ card (api/check-404 (t2/select-one :model/Card card-id))]
(request/as-admin
(api.embed.common/process-tiles-query-for-card
card
(api.embed.common/tile-parameters-for-card
card (embed/get-in-unsigned-token-or-throw unsigned-token [:params]) parameters)
- zoom x y lat-field lon-field))))
+ zoom x y latField lonField))))
;; 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
@@ -273,18 +271,16 @@
{:keys [parameters latField lonField]}
:- [:map
[:parameters {:optional true} ::parameters.schema/api.parameter-values]
- [:latField string?]
- [:lonField string?]]]
+ [:latField ::api.tiles/legacy-ref]
+ [:lonField ::api.tiles/legacy-ref]]]
(let [unsigned-token (check-and-unsign token)
dashboard-id (embed/get-in-unsigned-token-or-throw unsigned-token [:resource :dashboard])
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))
- lat-field (json/decode+kw latField)
- lon-field (json/decode+kw lonField)]
+ card (api/check-404 (t2/select-one :model/Card card-id))]
(request/as-admin
(api.embed.common/process-tiles-query-for-dashcard
dashboard dashcard card
(api.embed.common/tile-parameters-for-dashboard
dashboard (embed/get-in-unsigned-token-or-throw unsigned-token [:params]) parameters)
- zoom x y lat-field lon-field))))
+ zoom x y latField lonField))))
diff --git a/src/metabase/lib/schema/parameter.cljc b/src/metabase/lib/schema/parameter.cljc
index 94f32ee87cc..7ad09af3fc2 100644
--- a/src/metabase/lib/schema/parameter.cljc
+++ b/src/metabase/lib/schema/parameter.cljc
@@ -237,7 +237,8 @@
;;; empty? Unclear. I don't think it matters tho.
(mr/def ::dimension.options
[:map
- {:error/message "dimension options"}
+ {:error/message "dimension options"
+ :decode/normalize lib.schema.common/normalize-map}
[:stage-number {:optional true} :int]])
;;; TODO (Cam 8/8/25) -- seems really WACK to have dimension use MBQL 4 clause order even in Lib... I guess it's not a
@@ -378,6 +379,7 @@
(mr/def ::parameter.options
"Options the frontend attaches to a parameter value."
[:map
+ {:decode/normalize lib.schema.common/normalize-map}
[:case-sensitive {:optional true} :boolean]
[:include-current {:optional true} :boolean]])
diff --git a/src/metabase/mcp/session.clj b/src/metabase/mcp/session.clj
index ac4969a4cb5..224df909c7e 100644
--- a/src/metabase/mcp/session.clj
+++ b/src/metabase/mcp/session.clj
@@ -19,6 +19,7 @@
[metabase.session.core :as session]
[metabase.util.json :as json]
[metabase.util.log :as log]
+ [metabase.util.malli.registry :as mr]
[toucan2.core :as t2])
(:import
(java.nio ByteBuffer)
@@ -129,6 +130,14 @@
"Version for the unsigned JSON client-capability hint encoded in new MCP session ids."
1)
+(mr/def ::session-payload
+ "The unsigned client-capability hint carried in the second segment of an `Mcp-Session-Id`: the
+ payload version and whether the client can render MCP Apps UI. Server-minted and only echoed back
+ by clients; validated before we read its `:ui` flag, which is the only thing relayed onward."
+ [:map
+ [:v :int]
+ [:ui :boolean]])
+
(def ^:private max-session-id-length
"Maximum persisted length for `mcp_query_handle.mcp_session_id`."
254)
@@ -187,9 +196,9 @@
(cond
(and payload-map?
known-version?
- (boolean? (:ui decoded-payload)))
+ (mr/validate ::session-payload decoded-payload))
{:extended true
- :payload decoded-payload}
+ :payload (select-keys decoded-payload [:ui])}
;; During rolling deploys, a newer node may mint a capability payload version this node does not understand.
;; The payload is only a capability hint, so keep the session valid but fall back to no MCP Apps UI support.
diff --git a/src/metabase/metabot/api/conversations.clj b/src/metabase/metabot/api/conversations.clj
index 15ad31ebba1..9df11bfd0ad 100644
--- a/src/metabase/metabot/api/conversations.clj
+++ b/src/metabase/metabot/api/conversations.clj
@@ -11,6 +11,7 @@
[metabase.api.macros :as api.macros]
[metabase.api.routes.common :refer [+auth]]
[metabase.events.core :as events]
+ [metabase.lib-be.schema :as lib-be.schema]
[metabase.metabot.conversation-title :as conversation-title]
[metabase.metabot.persistence :as metabot.persistence]
[metabase.metabot.schema :as metabot.schema]
@@ -82,7 +83,7 @@
[:map
[:name ms/NonBlankString]
[:description {:optional true} [:maybe :string]]
- [:dataset_query ms/Map]
+ [:dataset_query ::lib-be.schema/maybe-legacy-query]
[:display ms/NonBlankString]
[:visualization_settings {:optional true} [:maybe ms/Map]]
[:collection_id {:optional true} [:maybe ms/PositiveInt]]
diff --git a/src/metabase/parameters/schema.cljc b/src/metabase/parameters/schema.cljc
index 27ec0176a06..7b695712b37 100644
--- a/src/metabase/parameters/schema.cljc
+++ b/src/metabase/parameters/schema.cljc
@@ -141,7 +141,7 @@
[:schema
{:decode/api (fn [x]
(cond-> x
- (string? x) json/decode+kw))}
+ (string? x) json/decode))}
schema]))
#?(:clj
@@ -155,7 +155,8 @@
the frontend's `normalizeParameters` sends. Distinct from `::lib.schema.parameter/parameter`, which requires `:type`
and normalizes the value."
[:map
- {:description "parameter must be a map with an :id key"}
+ {:description "parameter must be a map with an :id key"
+ :decode/normalize lib.schema.common/normalize-map-no-kebab-case}
[: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]]
diff --git a/src/metabase/public_sharing_rest/api.clj b/src/metabase/public_sharing_rest/api.clj
index dbdf9315de1..bd6b5ca8825 100644
--- a/src/metabase/public_sharing_rest/api.clj
+++ b/src/metabase/public_sharing_rest/api.clj
@@ -32,7 +32,6 @@
[metabase.tiles.api :as api.tiles]
[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]
[ring.util.codec :as codec]
@@ -707,14 +706,12 @@
{:keys [parameters latField lonField]}
:- [:map
[:parameters {:optional true} ::parameters.schema/api.parameter-values]
- [:latField string?]
- [:lonField string?]]]
+ [:latField ::api.tiles/legacy-ref]
+ [:lonField ::api.tiles/legacy-ref]]]
(public-sharing.validation/check-public-sharing-enabled)
- (let [card (api/check-404 (t2/select-one :model/Card :public_uuid uuid, :archived false))
- lat-field (json/decode+kw latField)
- lon-field (json/decode+kw lonField)]
+ (let [card (api/check-404 (t2/select-one :model/Card :public_uuid uuid, :archived false))]
(request/as-admin
- (api.tiles/process-tiles-query-for-card card parameters zoom x y lat-field lon-field))))
+ (api.tiles/process-tiles-query-for-card card parameters zoom x y latField lonField))))
;; 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
@@ -734,17 +731,15 @@
{:keys [parameters latField lonField]}
:- [:map
[:parameters {:optional true} ::parameters.schema/api.parameter-values]
- [:latField string?]
- [:lonField string?]]]
+ [:latField ::api.tiles/legacy-ref]
+ [:lonField ::api.tiles/legacy-ref]]]
(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))
- lat-field (json/decode+kw latField)
- lon-field (json/decode+kw lonField)]
+ (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))]
(request/as-admin
(api.tiles/process-tiles-query-for-dashcard dashboard dashcard card
- parameters zoom x y lat-field lon-field))))
+ parameters zoom x y latField lonField))))
;;; ------------------------------------------------ Public Documents -------------------------------------------------
diff --git a/src/metabase/tiles/api.clj b/src/metabase/tiles/api.clj
index 3ee7e317b89..9805e80f3ea 100644
--- a/src/metabase/tiles/api.clj
+++ b/src/metabase/tiles/api.clj
@@ -129,7 +129,7 @@
[:schema
{:decode/api (fn [field]
(when (string? field)
- (let [deserialized (json/decode+kw field)]
+ (let [deserialized (json/decode field)]
(when (sequential? deserialized)
(mbql.normalize/normalize deserialized)))))}
[:ref ::mbql.s/field]])
@@ -202,7 +202,7 @@
[:schema
{:decode/api (fn [s]
(when (string? s)
- (let [deserialized (json/decode+kw s)]
+ (let [deserialized (json/decode s)]
(when (map? deserialized)
(lib-be/normalize-query deserialized)))))}
[:ref ::lib.schema/query]])
diff --git a/src/metabase/xrays/api/automagic_dashboards.clj b/src/metabase/xrays/api/automagic_dashboards.clj
index 2bcbac765b1..537ee864cfd 100644
--- a/src/metabase/xrays/api/automagic_dashboards.clj
+++ b/src/metabase/xrays/api/automagic_dashboards.clj
@@ -5,6 +5,7 @@
[metabase.api.common :as api]
[metabase.api.macros :as api.macros]
[metabase.lib-be.core :as lib-be]
+ [metabase.lib-be.schema :as lib-be.schema]
[metabase.lib.core :as lib]
[metabase.lib.metadata :as lib.metadata]
[metabase.models.interface :as mi]
@@ -57,7 +58,19 @@
(deferred-tru "invalid value for dashboard template name")))
(def ^:private ^{:arglists '([s])} decode-base64-json
- (comp json/decode+kw codecs/bytes->str codec/base64-decode))
+ (comp json/decode codecs/bytes->str codec/base64-decode))
+
+(mr/def ::cell-query
+ "A base64-encoded JSON cell query (a filter clause) taken on the query string. The `:decode/api` step
+ base64-decodes and JSON-parses it, then it is validated against [[::ads/root.cell-query]] -- so a handler
+ receives the ready-to-use filter clause and doesn't decode it a second time. Bad base64/JSON is left as-is
+ and fails validation, surfacing as a clean 400."
+ [:schema
+ {:decode/api (fn [s]
+ (if (string? s)
+ (try (decode-base64-json s) (catch Exception _ s))
+ s))}
+ [:ref ::ads/root.cell-query]])
(mr/def ::base-64-encoded-json
"form-encoded base-64-encoded JSON"
@@ -136,7 +149,7 @@
[:dataset_query ::ads/query]]]
"Wrap query map into a Query object (mostly to facilitate type dispatch)."
[query :- :map]
- (let [query (lib-be/normalize-query query)]
+ (let [query (api.macros/decode-and-validate-params :body ::lib-be.schema/maybe-legacy-query query)]
(mi/instance :model/Query
(merge (queries/query->database-and-table-ids query)
{:dataset_query query}))))
@@ -388,12 +401,12 @@
[{:keys [entity entity-id-or-query cell-query]} :- [:map
[:entity Entity]
[:entity-id-or-query ::entity-id-or-query]
- [:cell-query ::base-64-encoded-json]]
+ [:cell-query ::cell-query]]
{:keys [show]} :- [:map
[:show {:optional true} Show]]]
(-> (->entity entity entity-id-or-query)
(automagic-dashboards.core/automagic-analysis {:show (coerce-show show)
- :cell-query (decode-base64-json cell-query)})))
+ :cell-query cell-query})))
;; 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
@@ -407,13 +420,13 @@
[:entity-id-or-query ::entity-id-or-query]
[:prefix Prefix]
[:dashboard-template DashboardTemplate]
- [:cell-query ::base-64-encoded-json]]
+ [:cell-query ::cell-query]]
{:keys [show]} :- [:map
[:show {:optional true} Show]]]
(-> (->entity entity entity-id-or-query)
(automagic-dashboards.core/automagic-analysis {:show (coerce-show show)
:dashboard-template ["table" prefix dashboard-template]
- :cell-query (decode-base64-json cell-query)})))
+ :cell-query cell-query})))
;; 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
@@ -493,7 +506,7 @@
dashboard (automagic-dashboards.core/automagic-analysis left {:show (coerce-show show)
:query-filter nil
:comparison? true})]
- (automagic-dashboards.comparison/comparison-dashboard dashboard left right {:left {:cell-query (decode-base64-json cell-query)}})))
+ (automagic-dashboards.comparison/comparison-dashboard dashboard left right {:left {:cell-query cell-query}})))
;; 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
@@ -524,4 +537,4 @@
dashboard (automagic-dashboards.core/automagic-analysis left {:show (coerce-show show)
:dashboard-template ["table" prefix dashboard-template]
:query-filter nil})]
- (automagic-dashboards.comparison/comparison-dashboard dashboard left right {:left {:cell-query (decode-base64-json cell-query)}})))
+ (automagic-dashboards.comparison/comparison-dashboard dashboard left right {:left {:cell-query cell-query}})))
diff --git a/test/metabase/agent_api/api_test.clj b/test/metabase/agent_api/api_test.clj
index 2f43323e0db..f9d820e3bca 100644
--- a/test/metabase/agent_api/api_test.clj
+++ b/test/metabase/agent_api/api_test.clj
@@ -493,14 +493,16 @@
(doseq [[label q] [["legacy top-level :type"
{:database (mt/id) :type "native" :native {:query "select 1"}}]
["MBQL 5 native stage"
- {:lib/type "mbql/query"
+ {:database (mt/id)
+ :lib/type "mbql/query"
:stages [{:lib/type "mbql.stage/native" :native "select 1"}]}]
- ["MBQL 5 native stage nested in a join"
- {:lib/type "mbql/query"
- :stages [{:lib/type "mbql.stage/mbql"
- :joins [{:lib/type "mbql/join"
- :stages [{:lib/type "mbql.stage/native"
- :native "select 1"}]}]}]}]
+ ["native source-query nested in a join"
+ {:database (mt/id) :type "query"
+ :query {:source-table (mt/id :checkins)
+ :joins [{:source-query {:native "select 1"}
+ :alias "j"
+ :condition [:= [:field (mt/id :checkins :id) nil]
+ [:field (mt/id :checkins :id) {:join-alias "j"}]]}]}}]
["legacy nested native source-query"
{:database (mt/id) :type "query"
:query {:source-query {:native "select 1"}}}]]]
@@ -1855,3 +1857,17 @@
(is (= 1 (count (:resources resp))))
(is (nil? (-> resp :resources first :content)))
(is (some? (-> resp :resources first :error))))))
+
+(deftest decode-and-validate-query-strips-extra-keys-test
+ (testing "base64 query payloads are decoded, validated, and stripped of undeclared properties"
+ (let [encoded (u/encode-base64 (json/encode {:database (mt/id)
+ :type "query"
+ :query {:source-table (mt/id :orders)
+ :a 1
+ :a/b 2}}))
+ q (#'agent-api.api/decode-and-validate-query encoded)]
+ (is (= :mbql/query (:lib/type q)))
+ (is (not (contains? q :a)))
+ (is (not (contains? q :a/b)))
+ (is (every? (fn [stage] (not (some #(contains? stage %) [:a :a/b]))) (:stages q))
+ "undeclared properties are stripped from every stage"))))
diff --git a/test/metabase/mcp/session_test.clj b/test/metabase/mcp/session_test.clj
index a9e1eff0f61..dc723e0b4eb 100644
--- a/test/metabase/mcp/session_test.clj
+++ b/test/metabase/mcp/session_test.clj
@@ -91,6 +91,10 @@
(let [session-id (extended-session-id {:v 1 :ui true})]
(is (true? (mcp.session/valid-id? session-id)))
(is (true? (mcp.session/supports-mcp-ui? session-id)))))
+ (testing "unknown keys in a known-version hint are ignored, not rejected"
+ (let [session-id (extended-session-id {:v 1 :ui true :a 1})]
+ (is (true? (mcp.session/valid-id? session-id)))
+ (is (true? (mcp.session/supports-mcp-ui? session-id)))))
(testing "unknown payload versions keep the session valid but disable UI capability"
(let [session-id (extended-session-id {:v 2 :ui true})]
(is (true? (mcp.session/valid-id? session-id)))
diff --git a/test/metabase/metabot/api/conversations_test.clj b/test/metabase/metabot/api/conversations_test.clj
index e3734e83af6..aa066a329e1 100644
--- a/test/metabase/metabot/api/conversations_test.clj
+++ b/test/metabase/metabot/api/conversations_test.clj
@@ -662,3 +662,20 @@
(mt/user-http-request :crowberto :post 404
(str "metabot/conversations/" (random-uuid) "/saved-entity")
body))))))
+
+(deftest record-saved-entity-strips-extra-query-keys-test
+ (testing "POST /api/metabot/conversations/:id/saved-entity validates and strips undeclared keys from :dataset_query"
+ (let [user-id (mt/user->id :crowberto)]
+ (mt/with-model-cleanup [:model/Card]
+ (mt/with-temp [:model/MetabotConversation {convo-id :id} {:user_id user-id}
+ :model/MetabotMessage _ {:conversation_id convo-id :user_id user-id :role "user"}]
+ (let [created (mt/user-http-request :crowberto :post 200
+ (str "metabot/conversations/" convo-id "/saved-entity")
+ {:chart_id "chart-1"
+ :card {:name "Venues"
+ :dataset_query (assoc (venues-query) :a 1 :a/b 2)
+ :display "bar"}})
+ stored (:dataset_query (t2/select-one :model/Card :id (:id created)))]
+ (is (= :mbql/query (:lib/type stored)))
+ (is (not (contains? stored :a)))
+ (is (not (contains? stored :a/b)))))))))
diff --git a/test/metabase/parameters/schema_test.cljc b/test/metabase/parameters/schema_test.cljc
index 5fa8c3f48a6..cdeaf7b0c40 100644
--- a/test/metabase/parameters/schema_test.cljc
+++ b/test/metabase/parameters/schema_test.cljc
@@ -1,5 +1,7 @@
(ns metabase.parameters.schema-test
(:require
+ #?@(:clj [[metabase.api.macros :as api.macros]
+ [metabase.util.json :as json]])
[clojure.test :refer [deftest is testing]]
[metabase.parameters.schema :as parameters.schema]))
@@ -21,3 +23,32 @@
(deftest ^:parallel normalize-parameters-without-adding-default-type-test
(is (= [{:id "x", :target [:dimension [:template-tag "y"]]}]
(parameters.schema/normalize-parameters-without-adding-default-types [{"id" "x", "target" ["dimension" ["template-tag" "y"]]}]))))
+
+#?(:clj
+ (deftest api-parameter-values-decode-strips-extra-keys-test
+ (testing "::api.parameter-values decodes a JSON string, normalizes keys, and strips undeclared properties"
+ (is (= [{:id "_STATE_" :type :text :target [:variable [:template-tag "state"]] :value "CA"}]
+ (api.macros/decode-and-validate-params
+ :query ::parameters.schema/api.parameter-values
+ (json/encode [{:id "_STATE_"
+ :type "text"
+ :target [:variable [:template-tag "state"]]
+ :value "CA"
+ :a 1
+ :a/b 2}])))))))
+
+#?(:clj
+ (deftest api-parameter-values-decode-preserves-nested-options-test
+ (testing "nested :options maps survive the JSON-string decode + strip pass with keywordized keys"
+ (is (= [{:id "_STATE_"
+ :type :text
+ :target [:dimension [:template-tag "state"] {:stage-number 1}]
+ :value "CA"
+ :options {:case-sensitive false, :include-current true}}]
+ (api.macros/decode-and-validate-params
+ :query ::parameters.schema/api.parameter-values
+ (json/encode [{:id "_STATE_"
+ :type "text"
+ :target ["dimension" ["template-tag" "state"] {"stage-number" 1}]
+ :value "CA"
+ :options {"case-sensitive" false, "include-current" true}}])))))))
diff --git a/test/metabase/tiles/api_test.clj b/test/metabase/tiles/api_test.clj
index 350a0647b58..fc663da2ae7 100644
--- a/test/metabase/tiles/api_test.clj
+++ b/test/metabase/tiles/api_test.clj
@@ -3,12 +3,14 @@
{:clj-kondo/config '{:linters {:deprecated-var {:exclude {metabase.test.data/mbql-query {:namespaces [metabase.tiles.api-test]}}}}}}
(:require
[clojure.test :refer :all]
+ [metabase.api.macros :as api.macros]
[metabase.lib.core :as lib]
[metabase.lib.test-metadata :as meta]
[metabase.test :as mt]
[metabase.tiles.api :as api.tiles]
[metabase.util :as u]
- [metabase.util.json :as json]))
+ [metabase.util.json :as json]
+ [metabase.util.malli.registry :as mr]))
;; TODO: Assert on the contents of the response, not just the format
(defn png? [s]
@@ -261,3 +263,31 @@
:latField (encoded-lat-field-ref :mbql)
:lonField (encoded-lon-field-ref :mbql)
:query (json/encode (mt/mbql-query people {:filter [:= $people.id "X"]})))))))
+
+(deftest ^:parallel legacy-ref-schema-strips-extra-keys-test
+ (testing "the tile latField/lonField schema decodes a JSON field ref, validates it, and strips undeclared properties"
+ (let [decoded (api.macros/decode-and-validate-params
+ :query ::api.tiles/legacy-ref
+ (json/encode [:field 1 {:base-type :type/Integer :a 1 :a/b 2}]))]
+ (is (mr/validate ::api.tiles/legacy-ref decoded))
+ (is (= [:field 1 {:base-type :type/Integer}] decoded)))
+ (testing "a value that isn't a field ref is rejected with a clean 400 (not a 500 later)"
+ (is (= 400 (-> (try (api.macros/decode-and-validate-params
+ :query ::api.tiles/legacy-ref (json/encode {:not "a-ref"}))
+ (catch clojure.lang.ExceptionInfo e (ex-data e)))
+ :status-code))))))
+
+(deftest ^:parallel query-schema-strips-extra-keys-test
+ (testing "the ad-hoc tile query schema decodes a JSON query, validates it, and strips undeclared properties"
+ (let [decoded (api.macros/decode-and-validate-params
+ :query ::api.tiles/query
+ (json/encode {:database (mt/id)
+ :type "query"
+ :query {:source-table (mt/id :people)
+ :a 1
+ :a/b 2}}))]
+ (is (= :mbql/query (:lib/type decoded)))
+ (is (not (contains? decoded :a)))
+ (is (not (contains? decoded :a/b)))
+ (is (every? (fn [stage] (not (some #(contains? stage %) [:a :a/b]))) (:stages decoded))
+ "undeclared properties are stripped from every stage"))))
diff --git a/test/metabase/xrays/api/automagic_dashboards_test.clj b/test/metabase/xrays/api/automagic_dashboards_test.clj
index e4cee6ed87c..52fee25b60c 100644
--- a/test/metabase/xrays/api/automagic_dashboards_test.clj
+++ b/test/metabase/xrays/api/automagic_dashboards_test.clj
@@ -4,6 +4,7 @@
[clojure.string :as str]
[clojure.test :refer :all]
[malli.core :as mc]
+ [metabase.api.macros :as api.macros]
[metabase.indexed-entities.models.model-index :as model-index]
[metabase.permissions.core :as perms]
[metabase.permissions.models.permissions-group :as perms-group]
@@ -12,9 +13,11 @@
[metabase.test :as mt]
[metabase.test.fixtures :as fixtures]
[metabase.util :as u]
+ [metabase.util.json :as json]
[metabase.util.malli :as mu]
[metabase.util.malli.registry :as mr]
[metabase.xrays.api.automagic-dashboards :as api.magic]
+ [metabase.xrays.automagic-dashboards.schema :as ads]
[metabase.xrays.automagic-dashboards.util :as magic.util]
[metabase.xrays.test-util.automagic-dashboards :refer [with-rollback-only-transaction]]
[metabase.xrays.test-util.domain-entities :as test.de]
@@ -604,3 +607,29 @@
(let [pattern (:api/regex (mc/properties (mr/resolve-schema schema)))]
(assert (instance? java.util.regex.Pattern pattern))
(is (re= pattern "0IjoieWVhciJ9XV19LCJkYXRhYmFzZSI6MX0=")))))))
+
+(deftest ^:parallel cell-query-decode-strips-extra-properties-test
+ (testing "the ::cell-query schema decodes a base64 JSON filter clause, validates it, and strips undeclared properties"
+ (let [encoded (u/encode-base64 (json/encode [">" {:a 1 :a/b 2} ["field" {} 1] 10]))
+ result (api.macros/decode-and-validate-params :query ::api.magic/cell-query encoded)]
+ (is (mr/validate ::ads/root.cell-query result)
+ "decoded cell query is a valid filter clause")
+ (is (= :> (first result)))
+ (let [opts (second result)]
+ (is (map? opts))
+ (is (not (contains? opts :a)))
+ (is (not (contains? opts :a/b)))))))
+
+(deftest adhoc-query-decode-strips-extra-keys-test
+ (testing "adhoc queries are validated and stripped of undeclared properties"
+ (mt/with-test-user :rasta
+ (let [q {:database (mt/id)
+ :type "query"
+ :query {:source-table (mt/id :venues)
+ :a 1
+ :a/b 2}}
+ dq (:dataset_query (#'api.magic/adhoc-query-instance q))]
+ (is (mr/validate ::ads/query dq)
+ "decoded adhoc query is a valid MBQL query")
+ (is (every? (fn [stage] (not (some #(contains? stage %) [:a :a/b]))) (:stages dq))
+ "undeclared properties are stripped from every stage")))))