The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:
TypeScript analyticsbibusiness-intelligencebusinessintelligenceclojuredashboarddatadata-analysisdata-visualizationdatabasemetabasemysqlpostgrespostgresqlreportingslacksql-editorvisualization
only derive site-url from authenticated superuser requests (#81130)
dpsutton9bf3b136af58
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 59f6dec1c0d..35ddb08cfe0 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -2200,7 +2200,6 @@requestsessionsettings- setupssosystemtracing@@ -2421,6 +2420,7 @@metabase.task.core}:uses #{app-dbclassloader+ configtracingutil}}diff --git a/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj b/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.cljindex ab421969703..95fef27caaa 100644--- a/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj+++ b/enterprise/backend/src/metabase_enterprise/sso/integrations/sso_utils.clj@@ -68,7 +68,7 @@our-host (some-> (system/site-url) (URI.) (.getHost))](api/check-400 (or (nil? redirect-url)(relative-uri? redirect)- (= (.getHost redirect) our-host)))+ (and our-host (= (.getHost redirect) our-host))))redirect-url)(catch Exception e(log/errorf "Invalid redirect URL: %s" (ex-message e))diff --git a/src/metabase/driver/h2.clj b/src/metabase/driver/h2.cljindex 9385a1a0a0c..d18d81385c5 100644--- a/src/metabase/driver/h2.clj+++ b/src/metabase/driver/h2.clj@@ -29,11 +29,12 @@[metabase.util.malli :as mu][metabase.util.performance :as perf :refer [every? some]])(:import- (java.sql Clob ResultSet ResultSetMetaData SQLException Types)+ (java.sql Clob ResultSet ResultSetMetaData SQLException)(java.time OffsetTime)+ (org.h2.api JavaObjectSerializer)(org.h2.command CommandInterface Parser)(org.h2.engine SessionLocal)- (org.h2.util StringUtils)))+ (org.h2.util JdbcUtils StringUtils)))(set! *warn-on-reflection* true)@@ -42,6 +43,22 @@(driver/register! :h2, :parent #{:sql-jdbc ::like-escape-char-built-in/like-escape-char-built-in})+;; H2 materializes a JAVA_OBJECT column value by deserializing its bytes with an ObjectInputStream. That+;; can happen anywhere a JAVA_OBJECT appears in a result — a top-level column, an ARRAY element, a nested+;; row — not just where a column's declared type is JAVA_OBJECT. Metabase never reads Java objects out of+;; query results, so we register the JavaObjectSerializer H2 routes all such deserialization through and+;; refuse it at that single point, covering every position uniformly. (`read-column-thunk :h2` below also+;; rejects a top-level JAVA_OBJECT column, for a clearer error on the common case.)+(def ^:private java-object-serializer+ (reify JavaObjectSerializer+ (serialize [_ _obj]+ (throw (ex-info "Reading and writing Java objects is not supported for H2." {})))+ (deserialize [_ _bytes]+ (throw (ex-info "Reading Java objects is not supported for H2." {})))))++(when-not *compile-files*+ (set! (. JdbcUtils serializer) java-object-serializer))+(defmethod driver/connection-hosts :h2[_driver {:keys [db]}](if (and (string? db)@@ -620,22 +637,17 @@(.setReadOnly conn (not write?)))(f conn))))-;; de-CLOB any CLOB values that come back+;; de-CLOB any CLOB values that come back. (A JAVA_OBJECT value is refused during reads by the+;; JavaObjectSerializer registered above, so it needs no handling here.)(defmethod sql-jdbc.execute/read-column-thunk :h2[_ ^ResultSet rs ^ResultSetMetaData rsmeta ^Integer i]- (let [col-type (.getColumnType rsmeta i)]- (when (= col-type Types/JAVA_OBJECT)- (throw (ex-info "Unable to parse jdbc type"- {:column-index i- :column-name (.getColumnName rsmeta i)- :column-type col-type})))- (let [classname (some-> (.getColumnClassName rsmeta i)- (Class/forName true (driver-api/the-classloader)))]- (if (isa? classname Clob)- (fn []- (driver-api/clob->str (.getObject rs i)))- (fn []- (.getObject rs i))))))+ (let [classname (some-> (.getColumnClassName rsmeta i)+ (Class/forName true (driver-api/the-classloader)))]+ (if (isa? classname Clob)+ (fn []+ (driver-api/clob->str (.getObject rs i)))+ (fn []+ (.getObject rs i)))))(defmethod sql-jdbc.execute/set-parameter [:h2 OffsetTime][driver prepared-statement i t]diff --git a/src/metabase/embedding_rest/api/common.clj b/src/metabase/embedding_rest/api/common.cljindex 7745b1b703b..315abd3a24b 100644--- a/src/metabase/embedding_rest/api/common.clj+++ b/src/metabase/embedding_rest/api/common.clj@@ -13,6 +13,7 @@[metabase.models.resolution :as models.resolution][metabase.notification.payload.core :as notification.payload][metabase.parameters.dashboard :as parameters.dashboard]+ [metabase.parameters.params :as params][metabase.public-sharing-rest.api :as api.public][metabase.queries.core :as queries][metabase.query-processor.card :as qp.card]@@ -142,7 +143,7 @@(vec (for [[slug value] slug->value:let [slug (u/qualified-name slug)param-type (get slug->type slug)- default-options (parameters.dashboard/param-type->default-options param-type)]]+ default-options (params/param-type->default-options param-type)]](cond-> {:slug slug:id (or (get slug->id slug)(throw (ex-info (tru "No matching parameter with slug {0}. Found: {1}" (pr-str slug) (pr-str (keys slug->id)))@@ -216,10 +217,20 @@merged-slug->value (validate-and-merge-params embedding-params token-params slug-query-params)](into {} (for [[slug value] merged-slug->value:when value]- [(get slug->id (name slug)) value]))))+ [(or (get slug->id (name slug))+ (throw (ex-info (tru "The parameter {0} does not exist on this dashboard." (name slug))+ {:status-code 400})))+ value]))));;; ---------------------------------------------- Other Param Util Fns ----------------------------------------------+(defn- locked-slug->value+ "The `\"locked\"` parameter values carried by the signed JWT. These are supplied by the embedding server rather+ than the client, and are passed to the parameter value lookups as constraints so the values offered for the+ enabled parameters match the rows the locked values select."+ [embedding-params token-params]+ (into {} (filter (fn [[slug _value]] (= (get embedding-params (keyword slug)) "locked"))) token-params))+(defn- enabled-param-slugs"The set of param slugs (as keywords) from `dashboard-or-card-params` that may be exposed to embed viewers: onlythose explicitly whitelisted as \"enabled\" in `embedding-params`. Anything else — absent from the whitelist, or@@ -493,7 +504,10 @@;; guest embeds always use the router (primary) database, never a routed destination(database-routing/with-database-routing-off(request/as-admin- (queries/card-param-values card param-key search-prefix)))+ (queries/card-param-values card param-key search-prefix+ (queries/card-param-constraints+ card+ (locked-slug->value embedding-params slug-token-params)))))(catch Throwable e(throw (ex-info (.getMessage e){:card-id (u/the-id card)@@ -537,7 +551,10 @@(try(database-routing/with-database-routing-off(request/as-admin- (queries/card-param-remapped-value card param-key value)))+ (queries/card-param-remapped-value card param-key value+ (queries/card-param-constraints+ card+ (locked-slug->value embedding-params slug-token-params)))))(catch Throwable e(throw (ex-info (.getMessage e){:card-id (u/the-id card)diff --git a/src/metabase/embedding_rest/api/preview_embed.clj b/src/metabase/embedding_rest/api/preview_embed.cljindex b2ce9fcb69b..602daad8c20 100644--- a/src/metabase/embedding_rest/api/preview_embed.clj+++ b/src/metabase/embedding_rest/api/preview_embed.clj@@ -15,7 +15,6 @@[metabase.embedding.jwt :as embed][metabase.embedding.validation :as embedding.validation][metabase.parameters.schema :as parameters.schema]- [metabase.query-processor.middleware.permissions :as qp.perms][metabase.query-processor.pivot :as qp.pivot][metabase.request.core :as request][metabase.tiles.api :as api.tiles]@@ -64,18 +63,23 @@:constraints {:max-results max-results}:query-params (api.embed.common/parse-query-params query-params))))+(defn- preview-card-for-param-values+ "The Card the parameter value lookups run against. Unlike the metadata endpoints this is the stored Card rather than+ the sanitized copy: the lookups need its real query and its full `:parameters`, since the locked parameters the+ preview must apply as constraints are exactly the ones the sanitized copy drops. Only `:embedding_params` comes+ from the token, so a preview reflects the settings being edited rather than the published ones."+ [unsigned-token]+ (let [card-id (api.embed.common/unsigned-token->card-id unsigned-token)]+ (-> (api/check-404 (t2/select-one :model/Card card-id))+ (assoc :embedding_params (embed/get-in-unsigned-token-or-throw unsigned-token [:_embedding_params])))))+(api.macros/defendpoint :get "/card/:token/params/:param-key/values" :- ms/FieldValuesResult"Embedded version of api.card filter values endpoint."[{:keys [token param-key]} :- [:map[:token string?][:param-key string?]]](let [unsigned-token (check-and-unsign token)- ;; resolving param values needs the Card's real query, so skip the query stripping- ;; in [[metabase.public-sharing-rest.api/remove-card-non-public-columns]]- card (binding [qp.perms/*param-values-query* true]- (api.embed.common/card-for-unsigned-token- unsigned-token- :embedding-params (embed/get-in-unsigned-token-or-throw unsigned-token [:_embedding_params])))]+ card (preview-card-for-param-values unsigned-token)](api.embed.common/card-param-values {:unsigned-token unsigned-token:card card:param-key param-key})))@@ -91,12 +95,7 @@[:param-key ms/NonBlankString]]{:keys [value]} :- [:map [:value :string]]](let [unsigned-token (check-and-unsign token)- ;; resolving param values needs the Card's real query, so skip the query stripping- ;; in [[metabase.public-sharing-rest.api/remove-card-non-public-columns]]- card (binding [qp.perms/*param-values-query* true]- (api.embed.common/card-for-unsigned-token- unsigned-token- :embedding-params (embed/get-in-unsigned-token-or-throw unsigned-token [:_embedding_params])))]+ card (preview-card-for-param-values unsigned-token)](api.embed.common/card-param-remapped-value {:unsigned-token unsigned-token:card card:param-key param-keydiff --git a/src/metabase/lib/schema.cljc b/src/metabase/lib/schema.cljcindex 5dd20c0e0a1..9b984e0ef15 100644--- a/src/metabase/lib/schema.cljc+++ b/src/metabase/lib/schema.cljc@@ -260,7 +260,9 @@{:page 1, :items 10} = items 1-10{:page 2, :items 10} = items 11-20"[:map- {:decode/normalize common/normalize-map}+ {:decode/normalize common/normalize-map+ :decode/api common/remove-internal-keys+ :encode/serialize common/remove-internal-keys}[:page pos-int?][:items pos-int?]])diff --git a/src/metabase/lib/schema/binning.cljc b/src/metabase/lib/schema/binning.cljcindex 831eeb20346..c54fa4ab2bb 100644--- a/src/metabase/lib/schema/binning.cljc+++ b/src/metabase/lib/schema/binning.cljc@@ -32,7 +32,9 @@{:decode/normalize (fn [binning](when-some [binning (lib.schema.common/normalize-map binning)](cond-> binning- (:strategy binning) (update :strategy lib.schema.common/normalize-keyword))))}+ (:strategy binning) (update :strategy lib.schema.common/normalize-keyword))))+ :decode/api lib.schema.common/remove-internal-keys+ :encode/serialize lib.schema.common/remove-internal-keys}[:multi {:dispatch (fn [x](keyword (some #(get x %) [:strategy "strategy"]))):error/fn (fn [{:keys [value]} _]diff --git a/src/metabase/lib/schema/constraints.cljc b/src/metabase/lib/schema/constraints.cljcindex 3800c50e8c5..f1a4b30c5e5 100644--- a/src/metabase/lib/schema/constraints.cljc+++ b/src/metabase/lib/schema/constraints.cljc@@ -9,7 +9,9 @@override these values."[:and[:map- {:decode/normalize lib.schema.common/normalize-map}+ {:decode/normalize lib.schema.common/normalize-map+ :decode/api lib.schema.common/remove-internal-keys+ :encode/serialize lib.schema.common/remove-internal-keys}[:max-results{:optional true:descriptiondiff --git a/src/metabase/lib/schema/middleware_options.cljc b/src/metabase/lib/schema/middleware_options.cljcindex 892d5938b86..c51cfae3fc2 100644--- a/src/metabase/lib/schema/middleware_options.cljc+++ b/src/metabase/lib/schema/middleware_options.cljc@@ -6,7 +6,9 @@(mr/def ::middleware-options"Additional options that can be used to toggle middleware on or off."[:map- {:decode/normalize lib.schema.common/normalize-map}+ {:decode/normalize lib.schema.common/normalize-map+ :decode/api lib.schema.common/remove-internal-keys+ :encode/serialize lib.schema.common/remove-internal-keys}[:skip-results-metadata?{:optional true:descriptiondiff --git a/src/metabase/lib/schema/parameter.cljc b/src/metabase/lib/schema/parameter.cljcindex 7ad09af3fc2..3a51fed2af0 100644--- a/src/metabase/lib/schema/parameter.cljc+++ b/src/metabase/lib/schema/parameter.cljc@@ -392,7 +392,9 @@[:and{:description "parameter must be a map with a :type key"}[:map- {:decode/normalize #'normalize-parameter}+ {:decode/normalize #'normalize-parameter+ :decode/api #'lib.schema.common/remove-internal-keys+ :encode/serialize #'lib.schema.common/remove-internal-keys}[:type [:ref ::type]];; TODO -- these definitely SHOULD NOT be optional but a ton of tests aren't passing them in like they should be.;; At some point we need to go fix those tests and then make these keys requireddiff --git a/src/metabase/lib/schema/template_tag.cljc b/src/metabase/lib/schema/template_tag.cljcindex 0fb63b47eaf..dfb0dca2fc6 100644--- a/src/metabase/lib/schema/template_tag.cljc+++ b/src/metabase/lib/schema/template_tag.cljc@@ -196,7 +196,9 @@{:decode/normalize (fn [tag](when-some [tag (common/normalize-map tag)](cond-> tag- (:type tag) (update :type common/normalize-keyword))))}+ (:type tag) (update :type common/normalize-keyword))))+ :decode/api common/remove-internal-keys+ :encode/serialize common/remove-internal-keys}[:multi {:dispatch #(keyword (:type %))}[:temporal-unit [:ref ::temporal-unit]][:dimension [:ref ::field-filter]]diff --git a/src/metabase/parameters/dashboard.clj b/src/metabase/parameters/dashboard.cljindex a6ab42f9ae2..5076df4ed52 100644--- a/src/metabase/parameters/dashboard.clj+++ b/src/metabase/parameters/dashboard.clj@@ -3,7 +3,6 @@[medley.core :as m][metabase.api.common :as api][metabase.lib.core :as lib]- [metabase.lib.schema.parameter :as lib.schema.parameter][metabase.parameters.chain-filter :as chain-filter][metabase.parameters.custom-values :as custom-values][metabase.parameters.params :as params]@@ -19,21 +18,10 @@"How many results to return when chain filtering"1000)-(defn- param-type->op [type]- (if (get-in lib.schema.parameter/types [type :operator])- (keyword (name type))- :=))--(defn param-type->default-options- "Default chain-filter constraint options based on parameter type."- [type]- (when (#{:string/contains :string/does-not-contain :string/starts-with :string/ends-with} type)- {:case-sensitive false}))-(mu/defn- param->fields[param :- ::parameters.schema/parameter & {:keys [op-override]}]- (let [op (or op-override (param-type->op (:type param)))- options (or (:options param) (param-type->default-options (:type param)))]+ (let [op (or op-override (params/param-type->op (:type param)))+ options (or (:options param) (params/param-type->default-options (:type param)))](for [field-id (params/dashboard-param->field-ids param)]{:field-id field-id:op opdiff --git a/src/metabase/parameters/params.clj b/src/metabase/parameters/params.cljindex 539e68a439e..c3a23890c16 100644--- a/src/metabase/parameters/params.clj+++ b/src/metabase/parameters/params.clj@@ -34,6 +34,19 @@;;; | SHARED |;;; +----------------------------------------------------------------------------------------------------------------++(defn param-type->op+ "The MBQL filter operator a chain-filter constraint should use for a parameter of `param-type`."+ [param-type]+ (if (get-in lib.schema.parameter/types [param-type :operator])+ (keyword (name param-type))+ :=))++(defn param-type->default-options+ "Default chain-filter constraint options based on parameter type."+ [param-type]+ (when (#{:string/contains :string/does-not-contain :string/starts-with :string/ends-with} param-type)+ {:case-sensitive false}))+(defn assert-valid-parameters"Receive a Parameterized Object and check if its parameters is valid."[{:keys [parameters]}]diff --git a/src/metabase/queries/card.clj b/src/metabase/queries/card.cljindex e04c98f2a99..120b2e97718 100644--- a/src/metabase/queries/card.clj+++ b/src/metabase/queries/card.clj@@ -31,14 +31,52 @@param :- ::parameters.schema/parameter](params/param-target->field-id (:target param) card))+(mu/defn card-param-constraints :- [:maybe ::chain-filter/constraints]+ "Chain-filter constraints for the parameters of `card` named in `slug->value`. Callers pass the parameters whose+ values they have already fixed and the caller may not change, so the values returned for the rest can be limited to+ the rows those values match. Throws if a slug names no parameter of the card: a constraint that silently went+ missing would widen the values returned rather than narrow them.++ Note this only reaches parameters whose values come from a Field -- one drawing values from a static list or from+ another card is returned in full, as it is on a dashboard."+ [card :- ::queries.schema/card+ slug->value :- [:maybe [:map-of :any :any]]]+ (let [;; the same source [[get-param-or-throw]] uses: a native card may carry no `:parameters` at all and be+ ;; described entirely by its template tags+ slug->param (into {}+ (map (juxt (comp keyword :slug) identity))+ (or (seq (:parameters card))+ (card/template-tag-parameters card)))]+ (vec (for [[slug value] slug->value+ ;; an empty collection means "no value", as it does in `validate-and-merge-params`, and a parameter+ ;; with no value constrains nothing whether or not it still exists+ :let [value (if (and (not (string? value)) (seqable? value)) (not-empty value) value)]+ :when (some? value)+ :let [param (or (get slug->param (keyword slug))+ (throw (ex-info (tru "The parameter {0} does not exist on this card." (name slug))+ {:status-code 400})))]+ ;; a parameter targeting a raw `{{variable}}` resolves to no Field and so cannot constrain, the same as+ ;; on a dashboard+ :let [field-id (param->field-id card param)]+ :when field-id]+ {:field-id field-id+ :op (params/param-type->op (:type param))+ :options (or (:options param) (params/param-type->default-options (:type param)))+ :value value}))))+(mu/defn- mapping->field-values :- ms/FieldValuesResult"Get param values for the \"old style\" parameters. This mimic's the api/dashboard version except we don't have- chain-filter issues or dashcards to worry about."+ dashcards to worry about. With `constraints`, values are chain-filtered to the rows those constraints match, the+ same way the dashboard version filters on its other parameter values."[card :- ::queries.schema/cardparam :- ::parameters.schema/parameter- query-string :- [:maybe :string]]+ query-string :- [:maybe :string]+ constraints :- [:maybe ::chain-filter/constraints]](when-let [field-id (param->field-id card param)]- (parameters.field/search-values-from-field-id field-id query-string)))+ (cond+ (empty? constraints) (parameters.field/search-values-from-field-id field-id query-string)+ (seq query-string) (chain-filter/chain-filter-search field-id constraints query-string)+ :else (chain-filter/chain-filter field-id constraints))))(mu/defn card-param-values"Fetch values for a parameter that contain `query`. If `query` is nil or not provided, return all values.@@ -47,26 +85,37 @@- static-list: user defined values list- card: values is result of running a card"([card param-key]- (card-param-values card param-key nil))+ (card-param-values card param-key nil nil))++ ([card param-key query-string]+ (card-param-values card param-key query-string nil))([card :- ms/Mapparam-key :- ::lib.schema.parameter/id- query-string :- [:maybe ms/NonBlankString]]+ query-string :- [:maybe ms/NonBlankString]+ constraints :- [:maybe ::chain-filter/constraints]](let [param (get-param-or-throw card param-key)](custom-values/parameter->values param query-string (mu/fn :- ms/FieldValuesResult []- (mapping->field-values card param query-string))))))+ (mapping->field-values card param query-string constraints))))))(mu/defn card-param-remapped-value- "Fetch the remapped value for the given `value` of parameter with ID `:param-key` of `card`."- [card :- ::queries.schema/card- param-key :- ::lib.schema.parameter/id- value]- (or (let [param (get-param-or-throw card param-key)]- (custom-values/parameter-remapped-value- param- value- #(when-let [field-id (param->field-id card param)]- (-> (chain-filter/chain-filter field-id [{:field-id field-id, :op :=, :value value}] :limit 1)- :values- first))))- [value]))+ "Fetch the remapped value for the given `value` of parameter with ID `:param-key` of `card`. `constraints` limits+ the rows the remapping is read from."+ ([card param-key value]+ (card-param-remapped-value card param-key value nil))++ ([card :- ::queries.schema/card+ param-key :- ::lib.schema.parameter/id+ value+ constraints :- [:maybe ::chain-filter/constraints]]+ (or (let [param (get-param-or-throw card param-key)]+ (custom-values/parameter-remapped-value+ param+ value+ #(when-let [field-id (param->field-id card param)]+ (-> (chain-filter/chain-filter field-id+ (into [{:field-id field-id, :op :=, :value value}] constraints)+ :limit 1)+ :values+ first))))+ [value])))diff --git a/src/metabase/queries/core.clj b/src/metabase/queries/core.cljindex 8f63cc27caf..5bcbb3110ae 100644--- a/src/metabase/queries/core.clj+++ b/src/metabase/queries/core.clj@@ -23,6 +23,7 @@(p/import-vars[metabase.queries.card+ card-param-constraintscard-param-valuescard-param-remapped-value][metabase.queries.models.carddiff --git a/src/metabase/server/middleware/misc.clj b/src/metabase/server/middleware/misc.cljindex 0e422fe340f..44cbbf7bfc2 100644--- a/src/metabase/server/middleware/misc.clj+++ b/src/metabase/server/middleware/misc.clj@@ -6,7 +6,6 @@[metabase.config.core :as config][metabase.request.core :as request][metabase.server.streaming-response]- [metabase.setup.core :as setup][metabase.system.core :as system][metabase.util :as u][metabase.util.log :as log])@@ -54,12 +53,12 @@;;; ------------------------------------------------ SETTING SITE-URL -------------------------------------------------;; It's important for us to know what the site URL is for things like returning links, etc. this is stored in the-;; `site-url` Setting; we can set it automatically by looking at the `Origin`, `X-Forwarded-Host`, or `Host` headers-;; sent with a request.+;; It's important for us to know what the site URL is for things like returning links, etc. This is stored in the+;; `site-url` Setting; when it isn't set explicitly (`MB_SITE_URL`) we can derive it by looking at the `Origin`,+;; `X-Forwarded-Host`, or `Host` headers of a request.;;-;; Effectively the very first API request that gets sent to us (usually some sort of setup request) ends up setting-;; the (initial) value of `site-url`+;; Derivation only happens on a request made by an authenticated superuser (`:is-superuser?`), so it's the admin's+;; own first post-setup request that seeds `site-url`(defn- forwarded-scheme"The scheme a TLS-terminating proxy used to reach us, inferred from the same forwarded headers as [[u/https?]]."[{:strs [x-forwarded-proto x-forwarded-protocol x-url-scheme x-forwarded-ssl front-end-https]}]@@ -77,8 +76,7 @@(let [{:strs [origin x-forwarded-host host user-agent]} headers](when (and (mdb/db-is-set-up?)(not (system/site-url))- (or (not (setup/has-user-setup))- superuser?)+ superuser?(not (#{"/api/health" "/livez" "/readyz"} uri))(or (nil? user-agent) ((complement str/includes?) user-agent "HealthChecker")));; `origin` already carries a scheme; the `*-host` headers normally don't, so prepend the scheme the proxy@@ -98,7 +96,8 @@(log/warnf "Failed to set site-url: %s" (ex-message e))))))))(defn maybe-set-site-url- "Middleware to set the `site-url` setting on the initial setup request"+ "Middleware that derives the `site-url` setting from a request's headers, but only when the request is made by an+ authenticated superuser and `site-url` isn't already set."[handler](fn [request respond raise](maybe-set-site-url* request)diff --git a/src/metabase/system/settings.clj b/src/metabase/system/settings.cljindex 0edc86e27e9..e81ed6e721f 100644--- a/src/metabase/system/settings.clj+++ b/src/metabase/system/settings.clj@@ -66,7 +66,11 @@(setting/set-value-of-type! :string :site-url new-value))):doc "This URL is critical for things like SSO authentication, email links, embedding and more.Even difference with `http://` vs `https://` can cause problems.- Make sure that the address defined is how Metabase is being accessed.")+ Make sure that the address defined is how Metabase is being accessed.+ If left unset, Metabase learns this value from the request headers of the first authenticated+ admin, so an operator who completes setup in a browser doesn't have to configure it. Deployments+ that provision headlessly, run multi-tenant, or otherwise never sign in as an admin should set+ `MB_SITE_URL` explicitly.");;; TODO -- we might want to move this into a separate `metabase.i18n` module(defsetting site-localediff --git a/src/metabase/task/bootstrap.clj b/src/metabase/task/bootstrap.cljindex 545e4d06ed3..1cdfa48f00d 100644--- a/src/metabase/task/bootstrap.clj+++ b/src/metabase/task/bootstrap.clj@@ -1,6 +1,7 @@(ns metabase.task.bootstrap(:require[metabase.classloader.core :as classloader]+ [metabase.task.secure-delegate :as secure-delegate][metabase.util.log :as log]))(set! *warn-on-reflection* true)@@ -78,14 +79,13 @@connection properties ahead of time, we'll need to set these at runtime rather than Setting them in the`quartz.properties` file.)- Sets the default per-DB `DriverDelegate` (`PostgreSQLDelegate` on Postgres for BLOB handling,- otherwise the `StdJDBCDelegate` from `quartz.properties`), then runs any setters registered via- [[register-jdbc-property-setter!]] — e.g. the `mq` module's queue node-affinity delegate, which- overrides the delegate with a filtering subclass. A registered setter that throws is logged and+ Installs Metabase's per-DB `DriverDelegate` (see [[metabase.task.secure-delegate]]): a+ `StdJDBCDelegate`/`PostgreSQLDelegate` subclass that reads BLOB columns through a class allow-list, so+ Quartz reconstructs only the plain-data classes Metabase's job data is made of. Then runs any setters+ registered via [[register-jdbc-property-setter!]]. A registered setter that throws is logged andskipped so the scheduler still gets a working delegate."[db-type]- (when (= db-type :postgres)- (System/setProperty "org.quartz.jobStore.driverDelegateClass" "org.quartz.impl.jdbcjobstore.PostgreSQLDelegate"))+ (secure-delegate/install! db-type)(doseq [setter @jdbc-property-setters](try(setter db-type)diff --git a/src/metabase/task/secure_delegate.clj b/src/metabase/task/secure_delegate.cljnew file mode 100644index 00000000000..3bc783135f1--- /dev/null+++ b/src/metabase/task/secure_delegate.clj@@ -0,0 +1,99 @@+(ns metabase.task.secure-delegate+ "Metabase's Quartz `DriverDelegate`s and their installation.++ Quartz's stock delegate reads BLOB columns with a bare `ObjectInputStream`, which reconstructs any+ serializable class the bytes name. Job data is only ever EDN-shaped data in Quartz's own map/trigger+ classes, so we override `getObjectFromBlob` to read through a class allow-list+ ([[metabase.util.deserialization-allowlist]]) — everything outside that set is treated as unreadable.++ The override on the delegate is the only place to do this: `getObjectFromBlob` is `protected` and its+ `ObjectInputStream` is a local, so the filter can't be attached from outside. Scoping it here means+ nothing else in the JVM is touched (drivers, query cache).++ The delegate classes are `gen-class`. [[install!]] loads the one matching the app DB and sets it as+ Quartz's `driverDelegateClass`; it must run before the scheduler initializes (see+ `metabase.task.bootstrap`)."+ (:require+ [clojure.java.io :as io]+ [metabase.classloader.core :as classloader]+ [metabase.config.core :as config]+ [metabase.util.deserialization-allowlist :as deserialization-allowlist]+ [metabase.util.log :as log])+ (:import+ (java.net URL)+ (java.sql Blob ResultSet)))++(set! *warn-on-reflection* true)++;;; ------------------------------------------------ the blob reads ------------------------------------------------++(def blob-allowed-prefixes+ "Classes job data serializes to: the EDN value space plus Quartz's own wrappers (`org.quartz.*`,+ e.g. `JobDataMap`)."+ (conj deserialization-allowlist/clojure-data-prefixes "org.quartz."))++(defn object-from-blob-std+ "`getObjectFromBlob` for `StdJDBCDelegate` (H2/MySQL) — reads via `getBlob` like the base class, but+ through the allow-list."+ [^ResultSet rs ^String col]+ (let [^Blob blob (.getBlob rs col)]+ (when (and blob (pos? (.length blob)))+ (deserialization-allowlist/read-object (.getBinaryStream blob) blob-allowed-prefixes))))++(defn object-from-blob-postgres+ "`getObjectFromBlob` for `PostgreSQLDelegate` — reads via `getBytes` like the base class, but through+ the allow-list."+ [^ResultSet rs ^String col]+ (deserialization-allowlist/read-object-bytes (.getBytes rs col) blob-allowed-prefixes))++;;; ------------------------------------------------ installation ------------------------------------------------+;;;+;;; The invariant: a jar contains everything. From a jar the gen-class is AOT-compiled and present, so we+;;; never compile at runtime — a missing class there is a build failure we throw on. From source (dev,+;;; test) there's no AOT, so we compile the gen-class on demand ([[dev-compile-delegate!]]) — the only+;;; runtime bytecode/classpath work here, fenced to source only. Jar-vs-source is [[config/jar?]], not+;;; `run-mode` (e2e runs a jar but isn't prod).++(def ^:private driver-delegate-property "org.quartz.jobStore.driverDelegateClass")++;; Postgres uses PostgreSQLDelegate (byte-based BLOB handling); everything else uses StdJDBCDelegate.+(def ^:private delegate+ {:postgres {:class "metabase.task.SecurePostgresDelegate" :ns 'metabase.task.secure-delegate-postgres}+ :default {:class "metabase.task.SecureStdDelegate" :ns 'metabase.task.secure-delegate-std}})++(defn- class-loadable? [^String class-name]+ (try+ (boolean (Class/forName class-name false (classloader/the-classloader)))+ (catch ClassNotFoundException _ false)))++(defn- dev-compile-delegate!+ "SOURCE-ONLY. Compile the gen-class `ns-sym` to a temp dir and add that dir to the classpath so+ `the-classloader` can find the class by name. A plain `require` won't do — it defines the class into a+ *child* of `the-classloader` that Quartz can't see. Asserts we're not in a jar, so this never compiles+ bytecode or mutates the classpath in a shipped uberjar (there the class is AOT-compiled)."+ [ns-sym]+ (assert (not (config/jar?)) "dev-compile-delegate! must not run from a jar")+ (let [dir (io/file (System/getProperty "java.io.tmpdir") "mb-quartz-secure-delegate")]+ (.mkdirs dir)+ (classloader/add-url-to-classpath! ^URL (-> dir .toURI .toURL))+ (binding [*compile-path* (str dir)]+ (compile ns-sym))))++(defn install!+ "Set Quartz's `driverDelegateClass` to the secure delegate for `db-type`. Must run before the+ scheduler initializes. Returns the installed class name.++ From a jar (prod, e2e) the class is AOT-compiled and already present. From source (dev, test) it is+ compiled on demand ([[dev-compile-delegate!]]). If it's missing from a jar, that's an AOT build+ problem, so we throw rather than compile at runtime."+ [db-type]+ (let [{:keys [class ns]} (get delegate db-type (:default delegate))]+ (when-not (class-loadable? class)+ (when (config/jar?)+ (throw (ex-info (str "Quartz secure delegate " class " is missing from the jar; it should be "+ "AOT-compiled at build time. This is a build problem.")+ {:class class})))+ (log/infof "Compiling Quartz secure delegate %s (running from source, no AOT)" class)+ (dev-compile-delegate! ns))+ (System/setProperty driver-delegate-property class)+ class))diff --git a/src/metabase/task/secure_delegate_postgres.clj b/src/metabase/task/secure_delegate_postgres.cljnew file mode 100644index 00000000000..a669d9b2269--- /dev/null+++ b/src/metabase/task/secure_delegate_postgres.clj@@ -0,0 +1,15 @@+(ns metabase.task.secure-delegate-postgres+ "Quartz `DriverDelegate` for Postgres: `PostgreSQLDelegate` (byte-based BLOB reads) with+ `getObjectFromBlob` overridden to read through the allow-list. The postgres counterpart of+ `metabase.task.secure-delegate-std`; keep them in sync. See [[metabase.task.secure-delegate]]."+ (:gen-class :extends org.quartz.impl.jdbcjobstore.PostgreSQLDelegate+ :name metabase.task.SecurePostgresDelegate)+ (:require+ [metabase.task.secure-delegate :as secure-delegate]))++(set! *warn-on-reflection* true)++(defn -getObjectFromBlob+ "gen-class override — see [[metabase.task.secure-delegate/object-from-blob-postgres]]."+ [_this rs col-name]+ (secure-delegate/object-from-blob-postgres rs col-name))diff --git a/src/metabase/task/secure_delegate_std.clj b/src/metabase/task/secure_delegate_std.cljnew file mode 100644index 00000000000..ee0726e0c7c--- /dev/null+++ b/src/metabase/task/secure_delegate_std.clj@@ -0,0 +1,15 @@+(ns metabase.task.secure-delegate-std+ "Quartz `DriverDelegate` for H2/MySQL: `StdJDBCDelegate` with `getObjectFromBlob` overridden to read+ through the allow-list. Logic lives in [[metabase.task.secure-delegate]]; keep this in sync with+ `metabase.task.secure-delegate-postgres` (differs only in base class)."+ (:gen-class :extends org.quartz.impl.jdbcjobstore.StdJDBCDelegate+ :name metabase.task.SecureStdDelegate)+ (:require+ [metabase.task.secure-delegate :as secure-delegate]))++(set! *warn-on-reflection* true)++(defn -getObjectFromBlob+ "gen-class override — see [[metabase.task.secure-delegate/object-from-blob-std]]."+ [_this rs col-name]+ (secure-delegate/object-from-blob-std rs col-name))diff --git a/src/metabase/util/deserialization_allowlist.clj b/src/metabase/util/deserialization_allowlist.cljnew file mode 100644index 00000000000..41a0add4985--- /dev/null+++ b/src/metabase/util/deserialization_allowlist.clj@@ -0,0 +1,69 @@+(ns metabase.util.deserialization-allowlist+ "A composable allow-list for Java deserialization.++ `ObjectInputStream.readObject` will reconstruct any serializable class on the classpath, not just the+ ones a caller stores. JEP 290's `ObjectInputFilter` lets you declare which classes a stream is+ expected to contain and reject the rest before they're constructed. This namespace provides that for+ EDN-shaped data (keywords, strings, numbers, maps/sets/vectors), which serializes to a small fixed+ set of Clojure/JDK classes — [[clojure-data-prefixes]]. Callers that wrap their data (Quartz in a+ `JobDataMap`, say) add their own prefixes on top via [[allow-list-filter]].++ Apply the filter to a single `ObjectInputStream` you control — not process-wide — so it constrains+ only your reads. Leaf namespace (JDK only), usable from any module."+ (:import+ (java.io ByteArrayInputStream InputStream ObjectInputFilter ObjectInputFilter$Status ObjectInputStream)))++(set! *warn-on-reflection* true)++(def clojure-data-prefixes+ "Class-name prefixes the EDN value space serializes to. By package, not exact class, because Clojure's+ collections use private inner classes (`PersistentHashMap$BitmapIndexedNode` and friends) not worth+ enumerating. Add your own wrapper prefixes on top when calling [[allow-list-filter]]."+ ["clojure.lang." ; Keyword, Persistent{Array,Hash}Map/Set/Vector, BigInt, Ratio, … and inner nodes+ "java.lang." ; String, Long, Integer, Double, Boolean, Character, …+ "java.util." ; HashMap/ArrayList/… when a caller passes raw JDK collections+ "java.math." ; BigDecimal, BigInteger+ "java.time."]) ; the java.time value types++(defn- element-class+ "Peel array dimensions off `c`, returning the ultimate element `Class`."+ ^Class [^Class c]+ (loop [^Class k c]+ (if (.isArray k) (recur (.componentType k)) k)))++(defn allow-list-filter+ "An `ObjectInputFilter` allowing only classes under one of `prefixes` (plus primitive array elements),+ rejecting everything else. `prefixes` is the full allow-list — e.g.+ `(conj clojure-data-prefixes \"my.wrapper.\")`. Attach via `.setObjectInputFilter` to one stream you+ control; not for process-wide use."+ ^ObjectInputFilter [prefixes]+ (let [prefixes (vec prefixes)]+ (reify ObjectInputFilter+ (checkInput [_ info]+ (let [c (.serialClass info)]+ (if (nil? c)+ ObjectInputFilter$Status/ALLOWED ; no class to check (array lengths, back-refs, …)+ (let [element (element-class c)]+ (if (or (.isPrimitive element)+ (let [n (.getName element)]+ (some #(.startsWith n ^String %) prefixes)))+ ObjectInputFilter$Status/ALLOWED+ ObjectInputFilter$Status/REJECTED))))))))++(defn read-object+ "Read one object from `in` through an [[allow-list-filter]] of `allowed-prefixes`, closing the stream.+ A class outside the allow-list throws `InvalidClassException` rather than being constructed. Returns+ nil for a nil or empty stream."+ [^InputStream in allowed-prefixes]+ (when (and in+ (not (and (instance? ByteArrayInputStream in)+ (zero? (.available ^ByteArrayInputStream in)))))+ (with-open [ois (doto (ObjectInputStream. in)+ (.setObjectInputFilter (allow-list-filter allowed-prefixes)))]+ (.readObject ois))))++(defn read-object-bytes+ "Like [[read-object]] but from a byte array. Returns nil for nil or empty `ba`."+ [^bytes ba allowed-prefixes]+ (when (and ba (pos? (alength ba)))+ (read-object (ByteArrayInputStream. ba) allowed-prefixes)))diff --git a/src/metabase/util/files.clj b/src/metabase/util/files.cljindex d43a3633769..7fe037ff085 100644--- a/src/metabase/util/files.clj+++ b/src/metabase/util/files.clj@@ -230,20 +230,23 @@to find the plugin manifests in the jar."[pattern]- (let [jar-url (URL. (str "jar:file:" (get-jar-path) "!/"))]- (with-open [fs (jar-file-system-from-url jar-url)]- (let [matcher (.getPathMatcher fs pattern)- root (.getPath fs "/" (into-array String []))- files (atom [])]- (Files/walkFileTree root- (proxy [java.nio.file.SimpleFileVisitor] []- (preVisitDirectory [dir _attrs]- (if (or (str/starts-with? (str dir) "/metabase")- (= (str "/") (str dir)))- java.nio.file.FileVisitResult/CONTINUE- java.nio.file.FileVisitResult/SKIP_SUBTREE))- (visitFile [file _attrs]- (when (.matches matcher file)- (swap! files conj file))- java.nio.file.FileVisitResult/CONTINUE)))- (into [] (map #(.toUri ^Path %) @files))))))+ ;; Open the jar via a Path, not a jar:file: URL — [[get-jar-path]] returns the decoded filesystem path, and+ ;; stuffing that back into a URL string breaks URL->URI conversion when the path contains a space. The Path-based+ ;; overload also bypasses NIO's global URI-keyed filesystem cache, so closing it here can't clobber a filesystem+ ;; some other consumer obtained via FileSystems/getFileSystem.+ (with-open [fs (nio-fs (get-jar-path))]+ (let [matcher (.getPathMatcher fs pattern)+ root (.getPath fs "/" (into-array String []))+ files (atom [])]+ (Files/walkFileTree root+ (proxy [java.nio.file.SimpleFileVisitor] []+ (preVisitDirectory [dir _attrs]+ (if (or (str/starts-with? (str dir) "/metabase")+ (= (str "/") (str dir)))+ java.nio.file.FileVisitResult/CONTINUE+ java.nio.file.FileVisitResult/SKIP_SUBTREE))+ (visitFile [file _attrs]+ (when (.matches matcher file)+ (swap! files conj file))+ java.nio.file.FileVisitResult/CONTINUE)))+ (into [] (map #(.toUri ^Path %) @files)))))diff --git a/test/metabase/driver/h2_test.clj b/test/metabase/driver/h2_test.cljindex 04a34314ceb..01a6184331f 100644--- a/test/metabase/driver/h2_test.clj+++ b/test/metabase/driver/h2_test.clj@@ -485,3 +485,16 @@(sql-jdbc.actions/maybe-parse-sql-error:h2 actions.error/violate-check-constraint nil :model.row/create"Check constraint violation: \"users_email_check\"")))))++;;; Metabase never reads Java objects out of H2 query results. Requiring the h2 driver registers a+;;; JavaObjectSerializer that refuses to reconstruct them, so reading a JAVA_OBJECT value fails rather+;;; than deserializing arbitrary classes. This verifies that refusal at the serializer itself.++(deftest ^:parallel java-object-deserialization-is-refused-test+ (testing "the registered H2 serializer refuses to reconstruct a Java object"+ (let [^org.h2.api.JavaObjectSerializer serializer @#'h2/java-object-serializer]+ (is (thrown-with-msg? Exception #"(?i)not supported"+ (.deserialize serializer (byte-array 0))))))+ (testing "ordinary values still read normally"+ (let [spec (mdb/spec :h2 {:db "mem:h2_read_test"})]+ (is (= [{:x 1}] (jdbc/query spec ["SELECT 1 AS x"]))))))diff --git a/test/metabase/embedding_rest/api/embed_test.clj b/test/metabase/embedding_rest/api/embed_test.cljindex 6b1e058bbee..2ba2d2787cf 100644--- a/test/metabase/embedding_rest/api/embed_test.clj+++ b/test/metabase/embedding_rest/api/embed_test.clj@@ -1281,6 +1281,117 @@:values [["Fred 62"] ["Red Medicine"]]}response))))))))))+(deftest card-param-values-respect-locked-parameters-test+ (testing "a locked parameter constrains the values offered for the card's enabled parameters"+ (with-embedding-enabled-and-new-secret-key!+ (let [mp (mt/metadata-provider)]+ (with-temp-card [card {:enable_embedding true+ :embedding_params {:price "locked" :cat "enabled" :name "enabled"}+ :dataset_query+ (-> (lib/native-query mp (str "SELECT ID, NAME, CATEGORY_ID, PRICE FROM VENUES "+ "WHERE {{price}} AND {{cat}} AND {{name}}"))+ (lib/with-template-tags+ {"price" {:id "p1"+ :name "price"+ :display-name "Price"+ :type :dimension+ :widget-type :number/=+ :dimension (lib/ref (lib.metadata/field mp (mt/id :venues :price)))}+ "cat" {:id "c1"+ :name "cat"+ :display-name "Cat"+ :type :dimension+ :widget-type :number/=+ :dimension (lib/ref (lib.metadata/field mp (mt/id :venues :category_id)))}+ "name" {:id "n1"+ :name "name"+ :display-name "Name"+ :type :dimension+ :widget-type :string/=+ :dimension (lib/ref (lib.metadata/field mp (mt/id :venues :name)))}}))+ :parameters [{:id "p1" :type :number/= :slug "price" :name "Price"+ :target [:dimension [:template-tag "price"]]}+ {:id "c1" :type :number/= :slug "cat" :name "Cat"+ :target [:dimension [:template-tag "cat"]]}+ {:id "n1" :type :string/= :slug "name" :name "Name"+ :target [:dimension [:template-tag "name"]]}]}]+ ;; price 4 occurs only in categories 40 and 67+ (let [token (card-token card {:params {:price 4}})+ url #(format "embed/card/%s/params/%s/%s" token %1 %2)]+ (testing "the locked parameter is not itself queryable"+ (is (= "Cannot search for values: \"price\" is not an enabled parameter."+ (client/client :get 400 (url "p1" "values")))))+ (testing "values for an enabled parameter are limited to the rows the locked value allows"+ (is (= [[40 "Japanese"] [67 "Steakhouse"]]+ (:values (client/client :get 200 (url "c1" "values"))))))+ (testing "search is limited the same way"+ (is (= [] (:values (client/client :get 200 (url "n1" "search/red"))))))+ (testing "a value outside those rows has no remapping"+ (is (= ["2"] (client/client :get 200 (str (url "c1" "remapping") "?value=2")))))+ (testing "a value within them still remaps"+ (is (= [40 "Japanese"] (client/client :get 200 (str (url "c1" "remapping") "?value=40")))))))))))++(deftest card-param-values-locked-slug-must-exist-test+ (testing "a locked entry in embedding_params whose slug names no parameter is refused rather than dropped"+ (with-embedding-enabled-and-new-secret-key!+ (let [mp (mt/metadata-provider)]+ (with-temp-card [card {:enable_embedding true+ ;; the policy locks "price"; the parameter entry is slugged "price_v2"+ :embedding_params {:price "locked" :cat "enabled"}+ :dataset_query+ (-> (lib/native-query mp (str "SELECT ID, CATEGORY_ID, PRICE FROM VENUES "+ "WHERE {{price}} AND {{cat}}"))+ (lib/with-template-tags+ {"price" {:id "p1"+ :name "price"+ :display-name "Price"+ :type :dimension+ :widget-type :number/=+ :dimension (lib/ref (lib.metadata/field mp (mt/id :venues :price)))}+ "cat" {:id "c1"+ :name "cat"+ :display-name "Cat"+ :type :dimension+ :widget-type :number/=+ :dimension (lib/ref (lib.metadata/field mp (mt/id :venues :category_id)))}}))+ :parameters [{:id "p1" :type :number/= :slug "price_v2" :name "Price"+ :target [:dimension [:template-tag "price"]]}+ {:id "c1" :type :number/= :slug "cat" :name "Cat"+ :target [:dimension [:template-tag "cat"]]}]}]+ (let [token (card-token card {:params {:price 4}})]+ (testing "values"+ (is (= "The parameter price does not exist on this card."+ (client/client :get 400 (format "embed/card/%s/params/c1/values" token)))))+ (testing "remapping"+ (is (= "The parameter price does not exist on this card."+ (client/client :get 400 (format "embed/card/%s/params/c1/remapping?value=2" token)))))))))))++(deftest card-param-values-native-card-without-parameters-test+ (testing "a native card described only by its template tags, with an empty locked value, still serves values"+ (mt/with-temporary-setting-values [enable-embedding-static true]+ (with-new-secret-key!+ (mt/with-temp+ [:model/Card card {:enable_embedding true+ :embedding_params {:total "locked" :state "enabled"}+ :dataset_query+ {:database (mt/id)+ :type :native+ :native {:query "SELECT * FROM ORDERS WHERE {{total}} AND {{state}}"+ :template-tags {"total" {:id "t1"+ :name "total"+ :display-name "Total"+ :type :dimension+ :widget-type :number/>=+ :dimension [:field (mt/id :orders :total) nil]}+ "state" {:id "s1"+ :name "state"+ :display-name "State"+ :type :dimension+ :widget-type :string/=+ :dimension [:field (mt/id :people :state) nil]}}}}}]+ (let [token (card-token card {:params {:total []}})]+ (is (seq (:values (client/client :get 200 (format "embed/card/%s/params/s1/values" token)))))))))))+;;; ------------------------------------------------ Chain filtering -------------------------------------------------(defn- do-with-chain-filter-fixtures! [f]diff --git a/test/metabase/lib/serialize_test.cljc b/test/metabase/lib/serialize_test.cljcindex 043ec306888..de2aa1e8708 100644--- a/test/metabase/lib/serialize_test.cljc+++ b/test/metabase/lib/serialize_test.cljc@@ -1,11 +1,30 @@(ns metabase.lib.serialize-test(:require- [clojure.test :refer [deftest is]]+ [clojure.test :refer [deftest is testing]][metabase.lib.core]+ [metabase.lib.schema :as-alias lib.schema]+ [metabase.lib.schema.binning :as-alias lib.schema.binning]+ [metabase.lib.schema.constraints :as-alias lib.schema.constraints]+ [metabase.lib.schema.middleware-options :as-alias lib.schema.middleware-options]+ [metabase.lib.schema.parameter :as-alias lib.schema.parameter]+ [metabase.lib.schema.template-tag :as-alias lib.schema.template-tag][metabase.lib.serialize :as lib.serialize]))(comment metabase.lib.core/keep-me)+(deftest ^:parallel strip-internal-keys-test+ (doseq [[schema m]+ [[::lib.schema.binning/binning {:strategy :num-bins, :num-bins 8}]+ [::lib.schema.parameter/parameter {:type :category, :id "p1"}]+ [::lib.schema.template-tag/template-tag {:type :text, :name "t", :display-name "T", :id "id1"}]+ [::lib.schema.constraints/constraints {:max-results 10}]+ [::lib.schema.middleware-options/middleware-options {:userland-query? true}]+ [::lib.schema/page {:page 1, :items 10}]]+ f [lib.serialize/prepare-after-deserialization+ lib.serialize/prepare-for-serialization]]+ (testing (str schema " via " f)+ (is (= m (f schema (assoc m :a/b 1)))))))+(deftest ^:parallel remove-info-test(is (= {:lib/type :mbql/query:stages [{:lib/type :mbql.stage/mbqldiff --git a/test/metabase/server/middleware/misc_test.clj b/test/metabase/server/middleware/misc_test.cljindex 9bc17e4ec2a..8fd3db1225a 100644--- a/test/metabase/server/middleware/misc_test.clj+++ b/test/metabase/server/middleware/misc_test.clj@@ -10,15 +10,16 @@(defn- maybe-set-site-url[request]+ ;; `site-url` is only ever derived from a request now made by an authenticated superuser; mark the request as such+ ;; the way the upstream auth middleware would, so these tests exercise the derivation path.((mw.misc/maybe-set-site-url (fn [request respond _] (respond request)))- request+ (assoc request :is-superuser? true)identity(fn [e] (throw e))))(defmacro ^:private with-fresh-instance![& body]- `(mt/with-temporary-setting-values [~'has-user-setup false]- ~@body))+ `(do ~@body))(defn- mock-request[uri origin-header x-forwarded-host-header host-header]@@ -57,6 +58,27 @@(maybe-set-site-url request)(is (= "https://mb1.example.com" (system/site-url)))))))))+(deftest maybe-set-site-url-requires-superuser-test+ ;; `maybe-set-site-url` is Ring middleware: it sets `site-url` as a side effect, then calls the handler it wraps. We+ ;; pass `req` in and wrap a throwaway handler whose own args we ignore -- the test asserts the side effect via+ ;; `(system/site-url)`, not the response. (Unlike the shared `maybe-set-site-url` helper above, this doesn't inject+ ;; `:is-superuser?`, so each case sets that flag itself.)+ (letfn [(run-middleware [req]+ ((mw.misc/maybe-set-site-url (fn [_req respond _raise] (respond nil)))+ req identity (fn [e] (throw e))))]+ (testing "site-url is NOT derived from a non-superuser request, even before the first user exists"+ (mt/with-temporary-setting-values [site-url nil, has-user-setup false]+ (run-middleware (assoc (mock-request "/api/setup" nil nil "attacker.example")+ :is-superuser? false))+ (is (nil? (system/site-url))+ "a pre-setup request does not set site-url")))+ (testing "site-url IS derived from an authenticated superuser request"+ (mt/with-temporary-setting-values [site-url nil]+ (run-middleware (assoc (mock-request "/" nil nil "mb.example.com")+ :is-superuser? true))+ (is (= "http://mb.example.com" (system/site-url))+ "a superuser request still auto-derives site-url")))))+(deftest maybe-set-site-url-forwarded-proto-test(with-fresh-instance!(testing "scheme-less `*-host` headers get the scheme from `X-Forwarded-Proto` (BOT-1617)"diff --git a/test/metabase/task/secure_delegate_test.clj b/test/metabase/task/secure_delegate_test.cljnew file mode 100644index 00000000000..6fa9d0082fe--- /dev/null+++ b/test/metabase/task/secure_delegate_test.clj@@ -0,0 +1,80 @@+(ns metabase.task.secure-delegate-test+ (:require+ [clojure.test :refer :all]+ [metabase.classloader.core :as classloader]+ [metabase.task.secure-delegate :as secure-delegate]+ [metabase.util.deserialization-allowlist :as dal])+ (:import+ (java.io ByteArrayInputStream ByteArrayOutputStream ObjectOutputStream)+ (java.sql Blob ResultSet)+ (org.quartz JobDataMap)))++(set! *warn-on-reflection* true)++(defn- serialize-bytes ^bytes [obj]+ (let [baos (ByteArrayOutputStream.)]+ (with-open [oos (ObjectOutputStream. baos)]+ (.writeObject oos obj))+ (.toByteArray baos)))++;;; A JobDataMap of the plain data a queue job carries, and a serializable class Metabase never stores+;;; (c3p0's connection-pool datasource) standing in for anything outside the EDN + Quartz value space.+(defn- job-data ^JobDataMap []+ (JobDataMap. {"database-id" (int 99)+ "payload" {:foo 1 :bar #{:x :y} :nested {:a [1 2 3]}}+ "opts" (doto (java.util.HashMap.) (.put "k" "v"))}))++(defn- unlisted-object [] (com.mchange.v2.c3p0.PoolBackedDataSource.))++;;; The delegates read a blob differently — StdJDBCDelegate via getBlob, PostgreSQLDelegate via getBytes+;;; — so we hand each a minimal ResultSet returning our bytes the way it expects, and call the real+;;; getObjectFromBlob override. No database needed: we're testing the delegate's read, not JDBC.++(defn- getblob-result-set ^ResultSet [^bytes bs]+ (reify ResultSet+ (^Blob getBlob [_ ^String _col]+ (reify Blob+ (length [_] (alength bs))+ (getBinaryStream [_] (ByteArrayInputStream. bs))))))++(defn- getbytes-result-set ^ResultSet [^bytes bs]+ (reify ResultSet+ (^bytes getBytes [_ ^String _col] bs)))++;; The gen-class delegate classes aren't AOT-compiled at this file's compile time (from source they're+;; compiled by install! at runtime), so we can't name them literally — a `require` doesn't make them+;; resolvable to the compiler either. We construct one by name and invoke its getObjectFromBlob override+;; reflectively, unwrapping the InvocationTargetException so the filter's own exception surfaces.+(defn- read-through-delegate! [db-type result-set-fn obj]+ (let [k (Class/forName (secure-delegate/install! db-type) true (classloader/the-classloader))+ delegate (.newInstance k)+ m (.getMethod k "getObjectFromBlob" (into-array Class [ResultSet String]))]+ (try+ (.invoke m delegate (object-array [(result-set-fn (serialize-bytes obj)) "data"]))+ (catch java.lang.reflect.InvocationTargetException e+ (throw (.getCause e))))))++(deftest getObjectFromBlob-reads-through-allow-list-test+ (testing "each secure delegate's getObjectFromBlob reads allowed job data and refuses an unlisted class"+ (doseq [[label db-type result-set-fn] [["StdJDBCDelegate (getBlob)" :h2 getblob-result-set]+ ["PostgreSQLDelegate (getBytes)" :postgres getbytes-result-set]]]+ (testing label+ (testing "reconstructs a JobDataMap of allowed data"+ (is (= JobDataMap (class (read-through-delegate! db-type result-set-fn (job-data))))))+ (testing "refuses an unlisted class before constructing it"+ (is (thrown? java.io.InvalidClassException+ (read-through-delegate! db-type result-set-fn (unlisted-object)))))))))++(deftest blob-allowed-prefixes-are-edn-plus-quartz-test+ (testing "the Quartz blob allow-set is the EDN value space plus Quartz's own wrapper classes"+ (is (= (conj dal/clojure-data-prefixes "org.quartz.")+ secure-delegate/blob-allowed-prefixes))))++(deftest getObjectFromBlob-override-canary-test+ (testing "each secure delegate declares its OWN getObjectFromBlob(ResultSet, String) — if a Quartz+ bump renames or re-signs that method our override silently stops overriding, and this fails"+ (doseq [class-name ["metabase.task.SecureStdDelegate" "metabase.task.SecurePostgresDelegate"]]+ (let [k (Class/forName class-name true (classloader/the-classloader))+ m (.getMethod k "getObjectFromBlob" (into-array Class [ResultSet String]))]+ (is (= k (.getDeclaringClass m))+ (str class-name " declares its own getObjectFromBlob override"))))))diff --git a/test/metabase/util/deserialization_allowlist_test.clj b/test/metabase/util/deserialization_allowlist_test.cljnew file mode 100644index 00000000000..4ff21834c63--- /dev/null+++ b/test/metabase/util/deserialization_allowlist_test.clj@@ -0,0 +1,68 @@+(ns metabase.util.deserialization-allowlist-test+ (:require+ [clojure.test :refer :all]+ [metabase.util.deserialization-allowlist :as dal])+ (:import+ (java.io ByteArrayInputStream ByteArrayOutputStream ObjectInputFilter ObjectInputFilter$Status+ ObjectInputStream ObjectOutputStream)))++(set! *warn-on-reflection* true)++(defn- serialize-bytes ^bytes [obj]+ (let [baos (ByteArrayOutputStream.)]+ (with-open [oos (ObjectOutputStream. baos)]+ (.writeObject oos obj))+ (.toByteArray baos)))++(defn- read-through [filter ^bytes bytes]+ (with-open [ois (doto (ObjectInputStream. (ByteArrayInputStream. bytes))+ (.setObjectInputFilter filter))]+ (.readObject ois)))++(defn- decision-for-class+ "The filter's own decision for `klass` alone — asked directly, independent of any ambient/process-wide+ filter, so these tests are robust in a JVM that already has a serial filter installed."+ [^ObjectInputFilter filter ^Class klass]+ (.checkInput filter (reify java.io.ObjectInputFilter$FilterInfo+ (serialClass [_] klass)+ (arrayLength [_] -1)+ (depth [_] 1)+ (references [_] 0)+ (streamBytes [_] 0))))++(deftest clojure-data-round-trips-test+ (testing "the Clojure-data allow-list permits the EDN value space and nothing else"+ (let [f (dal/allow-list-filter dal/clojure-data-prefixes)]+ (is (= {:foo 1 :bar #{:x :y} :nested {:a [1 2 3]} :s "hi" :n 4.5M}+ (read-through f (serialize-bytes {:foo 1 :bar #{:x :y} :nested {:a [1 2 3]} :s "hi" :n 4.5M})))+ "keywords, maps, sets, vectors, strings, and numbers all round-trip")+ (is (= "bare-string" (read-through f (serialize-bytes "bare-string")))))))++(deftest extra-prefixes-compose-test+ (testing "a caller splashes its own allowed prefixes on top of the Clojure-data base"+ (let [data-only (dal/allow-list-filter dal/clojure-data-prefixes)+ with-awt (dal/allow-list-filter (conj dal/clojure-data-prefixes "java.awt."))+ pt-bytes (serialize-bytes (java.awt.Point. 1 2))]+ (is (thrown? java.io.InvalidClassException (read-through data-only pt-bytes))+ "java.awt.Point is not in the base vocabulary")+ (is (= (java.awt.Point. 1 2) (read-through with-awt pt-bytes))+ "adding the java.awt. prefix admits it — without touching the base"))))++(deftest rejects-unlisted-class-test+ (testing "the filter allows a listed data class and refuses an unlisted one, decided per-class"+ ;; Ask the filter directly (via checkInput) rather than round-tripping through a stream, so the+ ;; assertions hold even in a JVM that already has a process-wide serial filter installed. c3p0's+ ;; PoolBackedDataSource is a serializable class on the classpath that stands in for "anything not in+ ;; the EDN value space".+ (let [f (dal/allow-list-filter dal/clojure-data-prefixes)]+ (is (= ObjectInputFilter$Status/ALLOWED+ (decision-for-class f clojure.lang.PersistentHashMap))+ "a Clojure data class is allowed")+ (is (= ObjectInputFilter$Status/REJECTED+ (decision-for-class f com.mchange.v2.c3p0.PoolBackedDataSource))+ "an unlisted class is rejected")))+ (testing "and end-to-end, an unlisted class throws from the filter during readObject"+ (let [f (dal/allow-list-filter dal/clojure-data-prefixes)+ unlisted-bytes (serialize-bytes (com.mchange.v2.c3p0.PoolBackedDataSource.))]+ (is (thrown-with-msg? java.io.InvalidClassException #"(?i)filter"+ (read-through f unlisted-bytes))))))