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
Improve header validation (#82509)
Nathan Voxlanda9ca5c4289cc
diff --git a/src/metabase/analytics/sdk.clj b/src/metabase/analytics/sdk.cljindex 428124e55c8..9c951ca2853 100644--- a/src/metabase/analytics/sdk.clj+++ b/src/metabase/analytics/sdk.clj@@ -15,6 +15,7 @@[metabase.config.core :as config][metabase.request.current :as request.current][metabase.request.user-agent :as request.user-agent]+ [metabase.util :as u][metabase.util.log :as log][metabase.util.malli :as mu][metabase.util.malli.registry :as mr]@@ -228,21 +229,24 @@(when (str/starts-with? (or uri "") prefix) surface))embedding-route-mapping)))+(defn- truncate-header+ "The embedding columns are varchar(254); truncate rather than fail the row insert on an oversized header."+ [s]+ (some-> s (u/truncate 254)))+(defn embedding-mw"Reads Metabase Client and Version headers and binds them to *metabase-client{-version}*."[handler](fn embedding-mw-fn[request respond raise](let [metabase-client-header (get-in request [:headers "x-metabase-client"])- version (get-in request [:headers "x-metabase-client-version"])- ;; column is varchar(254); truncate rather than fail the row insert on an oversized header- identifier (some-> (get-in request [:headers "x-metabase-client-identifier"])- (as-> s (subs s 0 (min (count s) 254))))+ version (truncate-header (get-in request [:headers "x-metabase-client-version"]))+ identifier (truncate-header (get-in request [:headers "x-metabase-client-identifier"]))preview? (= (get-in request [:headers "x-metabase-embedded-preview"]) "true")route (embedding-route (:uri request));; *client* is the SDK/client identity from the header, with -preview suffix if applicable- client (cond-> metabase-client-header- preview? (some-> (str "-preview")))]+ client (truncate-header (cond-> metabase-client-header+ preview? (some-> (str "-preview"))))](binding [*client* client*client-identifier* identifier*route* routediff --git a/src/metabase/app_db/sql_errors.clj b/src/metabase/app_db/sql_errors.cljindex 7ecb78025be..e432f44c819 100644--- a/src/metabase/app_db/sql_errors.clj+++ b/src/metabase/app_db/sql_errors.clj@@ -1,6 +1,9 @@(ns metabase.app-db.sql-errors"Classification of SQL errors raised by the supported application databases.- [[error-kind]] names the class of error an exception represents; [[table-not-found?]] is its common special case."+ [[error-kind]] names the class of error an exception represents; [[table-not-found?]] is its common special case.+ [[row-rejected?]] tells a bad row apart from a failure of the database itself."+ (:require+ [clojure.string :as str])(:import(java.sql SQLException)))@@ -82,3 +85,15 @@"Whether `e` was caused by querying a table that does not exist."[e](= :table-not-found (error-kind e)))++(defn row-rejected?+ "Whether `e` was caused by the app DB rejecting a row's data: a data exception (SQLSTATE class 22, e.g. a value too+ long for its column) or an integrity-constraint violation (class 23). Other failures, such as a lost connection or+ a pool checkout timeout, are not about any one row. Walks the full cause and JDBC next-exception chains."+ [e]+ (boolean+ (some #(when (instance? SQLException %)+ (when-let [state (.getSQLState ^SQLException %)]+ (or (str/starts-with? state "22")+ (str/starts-with? state "23"))))+ (exception-chain e))))diff --git a/src/metabase/query_processor/dashboard.clj b/src/metabase/query_processor/dashboard.cljindex c24a564827d..0973167c28d 100644--- a/src/metabase/query_processor/dashboard.clj+++ b/src/metabase/query_processor/dashboard.clj@@ -195,8 +195,10 @@merged-parameters (vals (merge (dashboard-param-defaults dashboard-param-id->param card-id)request-param-id->param))](when-let [user-id api/*current-user-id*]- (when (seq request-params)- (user-parameter-value/store! user-id dashboard-id request-params)))+ ;; only ids the dashboard has: the rest are rejected below, and their ids are unbounded while the+ ;; `user_parameter_value.parameter_id` column is not+ (when-let [known-params (not-empty (filter #(contains? dashboard-param-id->param (:id %)) request-params))]+ (user-parameter-value/store! user-id dashboard-id known-params)))(log/tracef "Merged %d dashboard parameter(s) and %d request parameter(s) into %d parameter(s)"(count dashboard-param-id->param)(count request-param-id->param)diff --git a/src/metabase/query_processor/db.clj b/src/metabase/query_processor/db.cljindex e2db6da1554..352f28b7c92 100644--- a/src/metabase/query_processor/db.clj+++ b/src/metabase/query_processor/db.clj@@ -56,10 +56,12 @@(t2/delete! (t2/table-name :model/QueryCache) :updated_at [:<= updated-before]))(mu/defn insert-query-executions!- "Insert the QueryExecution `rows`."+ "Insert the QueryExecution `rows`. Runs in its own (possibly nested) transaction so that if the app DB rejects a+ row, the failure is rolled back to a savepoint instead of aborting a caller's enclosing transaction."[rows :- [:sequential::queries.schema/query-execution.update]]- (t2/insert! :model/QueryExecution rows))+ (t2/with-transaction [_conn]+ (t2/insert! :model/QueryExecution rows)))(mu/defn set-card-result-metadata!"Set the result metadata of the Card with `card-id` without touching `updated_at`."diff --git a/src/metabase/query_processor/middleware/process_userland_query.clj b/src/metabase/query_processor/middleware/process_userland_query.cljindex 5d6cdf0068b..db07059596b 100644--- a/src/metabase/query_processor/middleware/process_userland_query.clj+++ b/src/metabase/query_processor/middleware/process_userland_query.clj@@ -13,6 +13,7 @@[metabase.analytics.core :as analytics.core][metabase.analytics.settings :as analytics.settings][metabase.api.common :as api]+ [metabase.app-db.sql-errors :as sql-errors][metabase.batch-processing.core :as grouper][metabase.events.core :as events][metabase.lib.computed :as lib.computed]@@ -46,6 +47,26 @@(def ^:private save-execution-metadata-queue-capacity 500)+(defn- insert-query-executions-resiliently!+ "Insert a batch of QueryExecutions in one statement. The batch is one transaction, so a single row the app DB+ rejects (e.g. an overlong value in a narrow column) would discard every row in it: on failure, retry the rows one+ at a time so only the bad row is lost. Any other failure (e.g. the app DB is down) would fail every retry too, each+ possibly waiting for a connection, so then the batch is dropped."+ [query-executions]+ (try+ (query-processor.db/insert-query-executions! query-executions)+ (catch Throwable e+ (if (sql-errors/row-rejected? e)+ (do+ (log/warnf "Error saving %d QueryExecution(s) as a batch, retrying individually: %s"+ (count query-executions) (ex-message e))+ (doseq [query-execution query-executions]+ (try+ (query-processor.db/insert-query-executions! [query-execution])+ (catch Throwable e+ (log/errorf "Error saving query execution info: %s" (ex-message e))))))+ (log/errorf "Error saving %d QueryExecution(s): %s" (count query-executions) (ex-message e))))))+(defn- save-execution-metadata!*"Save a batch of `QueryExecution`s and update the average execution times for the corresponding `Query`s."[query-executions]@@ -59,14 +80,11 @@(query/save-queries-and-update-average-execution-times! entries)(catch Throwable e(log/errorf "Error updating query average execution times: %s" (ex-message e)))))- (try- (let [{with-context true, no-context false} (group-by (comp some? :context) query-executions)]- (when (seq no-context)- (log/warnf "Cannot save %d QueryExecution(s), missing :context" (count no-context)))- (when (seq with-context)- (query-processor.db/insert-query-executions! (map #(dissoc % :json_query) with-context))))- (catch Throwable e- (log/errorf "Error saving query execution info: %s" (ex-message e))))))+ (let [{with-context true, no-context false} (group-by (comp some? :context) query-executions)]+ (when (seq no-context)+ (log/warnf "Cannot save %d QueryExecution(s), missing :context" (count no-context)))+ (when (seq with-context)+ (insert-query-executions-resiliently! (map #(dissoc % :json_query) with-context))))))(defonce ^:private save-execution-metadata-queue(delay (grouper/start!diff --git a/src/metabase/request/current.clj b/src/metabase/request/current.cljindex d70b46e73a4..abfd0a423d2 100644--- a/src/metabase/request/current.clj+++ b/src/metabase/request/current.clj@@ -5,7 +5,8 @@TODO -- move stuff like `*current-user-id*` into this namespace."(:require[clojure.string :as str]- [metabase.request.settings :as request.settings]))+ [metabase.request.settings :as request.settings]+ [metabase.util :as u]))#_{:clj-kondo/ignore [:metabase/discourage-dynamic-vars]}(def ^:private ^:dynamic *request*@@ -84,4 +85,7 @@(str/split #"\s*,\s*")last;; strip out non-ip-address characters like square brackets which we get sometimes- (str/replace #"[^0-9a-fA-F.:]" ""))))+ (str/replace #"[^0-9a-fA-F.:]" "")+ ;; no IP address is longer than 45 characters, and the header is attacker-controlled: bound it so an+ ;; oversized value cannot fail the insert of the varchar(45) columns it lands in+ (u/truncate 45))))diff --git a/src/metabase/server/middleware/browser_cookie.clj b/src/metabase/server/middleware/browser_cookie.cljindex 4bb34ed8345..6c72d47d250 100644--- a/src/metabase/server/middleware/browser_cookie.clj+++ b/src/metabase/server/middleware/browser_cookie.clj@@ -8,6 +8,7 @@[metabase.api.macros :as api.macros][metabase.request.core :as request][metabase.request.schema :as request.schema]+ [metabase.util :as u][metabase.util.malli :as mu][metabase.util.malli.schema :as ms][ring.util.response :as response]))@@ -38,11 +39,18 @@browser-id :- ms/NonBlankString](response/set-cookie response browser-id-cookie-name browser-id (cookie-options request)))+(defn- valid-browser-id+ "The cookie value when it is the UUID we issued, else nil. The value ends up in the char(36) `login_history.device_id`+ column, so anything else (a tampered cookie) is replaced rather than sent to the app DB."+ [value]+ (when (some->> value str (re-matches u/uuid-regex))+ (str value)))+(defn ensure-browser-id-cookie"Set a permanent browser identifier cookie if one is not already set."[handler](fn [request respond raise]- (if-let [browser-id (get-in request [:cookies browser-id-cookie-name :value])]+ (if-let [browser-id (valid-browser-id (get-in request [:cookies browser-id-cookie-name :value]))](handler (assoc request :browser-id browser-id) respond raise)(let [browser-id (str (random-uuid))](handlerdiff --git a/src/metabase/view_log/db.clj b/src/metabase/view_log/db.cljindex 711e13b1528..c29d00138be 100644--- a/src/metabase/view_log/db.clj+++ b/src/metabase/view_log/db.clj@@ -46,7 +46,8 @@(mu/defn insert-view-logs!"Insert the ViewLog rows `views`, returning the number inserted."[views :- [:sequential ::view-log.schema/view-log.update]]- (t2/insert! :model/ViewLog views))+ (t2/with-transaction [_conn]+ (t2/insert! :model/ViewLog views)))(mu/defn card-type"The `:type` of the Card with `card-id`, or nil."diff --git a/src/metabase/view_log/events/view_log.clj b/src/metabase/view_log/events/view_log.cljindex 1da2c783e4f..239683238bc 100644--- a/src/metabase/view_log/events/view_log.clj+++ b/src/metabase/view_log/events/view_log.clj@@ -5,6 +5,7 @@[metabase.analytics.core :as analytics][metabase.api.common :as api][metabase.app-db.cluster-lock :as cluster-lock]+ [metabase.app-db.sql-errors :as sql-errors][metabase.audit-app.core :as audit][metabase.batch-processing.core :as grouper][metabase.events.core :as events]@@ -84,12 +85,25 @@(def ^:private record-view-queue-capacity 500)-(defn- record-views!* [views]+(defn- record-views!*+ "Insert a batch of views in one statement. The batch is one transaction, so a single row the app DB rejects (e.g.+ an overlong value in a narrow column) would discard every row in it: on failure, retry the rows one at a time so+ only the bad row is lost. Any other failure (e.g. the app DB is down) would fail every retry too, each possibly+ waiting for a connection, so then the batch is dropped."+ [views](log/debugf "Recording %d views" (count views))(try(view-log.db/insert-view-logs! views)(catch Exception e- (log/errorf "Failed to record views: %s" (ex-message e)))))+ (if (sql-errors/row-rejected? e)+ (do+ (log/warnf "Failed to record %d views as a batch, retrying individually: %s" (count views) (ex-message e))+ (doseq [view views]+ (try+ (view-log.db/insert-view-logs! [view])+ (catch Exception e+ (log/errorf "Failed to record view: %s" (ex-message e))))))+ (log/errorf "Failed to record %d views: %s" (count views) (ex-message e))))))(defonce ^:private record-view-queue(delay (grouper/start!diff --git a/test/metabase/app_db/sql_errors_test.clj b/test/metabase/app_db/sql_errors_test.cljindex 3399c0c6a9c..520f345a10a 100644--- a/test/metabase/app_db/sql_errors_test.clj+++ b/test/metabase/app_db/sql_errors_test.clj@@ -44,3 +44,19 @@inner (SQLException. "inner" "42000" outer)](.setNextException outer inner)(is (nil? (sql-errors/error-kind outer))))))++(deftest ^:parallel row-rejected?-test+ (testing "data exceptions and integrity-constraint violations mean the app DB rejected a row"+ (are [state] (true? (sql-errors/row-rejected? (SQLException. "rejected" state)))+ "22001" "22003" "22007" "23000" "23502" "23505"))+ (testing "a rejected row is found through causes and JDBC next-exception links"+ (is (true? (sql-errors/row-rejected? (ex-info "outer" {} (SQLException. "too long" "22001")))))+ (is (true? (sql-errors/row-rejected? (doto (SQLException. "batch failed" "HY000")+ (.setNextException (SQLException. "too long" "22001")))))))+ (testing "connection failures, timeouts, and other errors do not mean a row was rejected"+ (are [e] (false? (sql-errors/row-rejected? e))+ (SQLException. "connection refused" "08001")+ (java.sql.SQLTransientConnectionException. "pool checkout timed out")+ (SQLException. "syntax" "42000")+ (SQLException. "no state")+ (ex-info "plain" {}))))diff --git a/test/metabase/query_processor/dashboard_test.clj b/test/metabase/query_processor/dashboard_test.cljindex ada566e7369..e3700b233ad 100644--- a/test/metabase/query_processor/dashboard_test.clj+++ b/test/metabase/query_processor/dashboard_test.clj@@ -17,6 +17,7 @@[metabase.query-processor.test :as qp][metabase.query-processor.test-util :as qp.test-util][metabase.test :as mt]+ [metabase.users.models.user-parameter-value :as user-parameter-value][toucan2.core :as t2]))(defn- run-query-for-dashcard [dashboard-id card-id dashcard-id & options]@@ -88,6 +89,29 @@#"Invalid parameter value type :number/!= for parameter \"_PRICE_\".*"(resolve-params [{:id "_PRICE_", :value 4, :type :number/!=}])))))))+(deftest unknown-parameters-are-not-stored-test+ (testing "request parameters the dashboard does not have never reach the last-used-parameter store (their ids are+ unbounded, and `user_parameter_value.parameter_id` is varchar(36); one rejected row would discard the whole+ batch of every user's saved parameters)"+ (api.dashboard-test/with-chain-filter-fixtures [{{dashboard-id :id} :dashboard+ {card-id :id} :card+ {dashcard-id :id} :dashcard}]+ (let [stored (atom [])]+ (mt/with-dynamic-fn-redefs [user-parameter-value/store! (fn [_user-id _dashboard-id params] (swap! stored into params))]+ (mt/with-current-user (mt/user->id :rasta)+ (testing "an unknown id is rejected without being stored"+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"Dashboard does not have a parameter with ID"+ (resolve-params-for-query dashboard-id card-id dashcard-id+ [{:id "_PRICE_", :value 4}+ {:id (apply str (repeat 40 "z")), :value 3}])))+ (is (empty? (filter #(= (apply str (repeat 40 "z")) (:id %)) @stored))))+ (testing "known ids are still stored"+ (reset! stored [])+ (resolve-params-for-query dashboard-id card-id dashcard-id [{:id "_PRICE_", :value 4}])+ (is (= ["_PRICE_"] (map :id @stored))))))))))+(deftest ^:parallel resolve-parameters-validation-test-2(testing "Resolves new operator type arguments without error (#25031)"(mt/dataset test-datadiff --git a/test/metabase/query_processor/middleware/process_userland_query_test.clj b/test/metabase/query_processor/middleware/process_userland_query_test.cljindex dd883bb132f..78354681c27 100644--- a/test/metabase/query_processor/middleware/process_userland_query_test.clj+++ b/test/metabase/query_processor/middleware/process_userland_query_test.clj@@ -4,11 +4,13 @@metabase.test.data/run-mbql-query {:namespaces [metabase.query-processor.middleware.process-userland-query-test]}}}}}}(:require[buddy.core.codecs :as codecs]+ [buddy.core.hash :as buddy-hash][clojure.core.async :as a][clojure.test :refer :all][java-time.api :as t][metabase.driver :as driver][metabase.events.core :as events]+ [metabase.query-processor.db :as query-processor.db][metabase.query-processor.error-type :as qp.error-type][metabase.query-processor.middleware.process-userland-query :as process-userland-query][metabase.query-processor.pipeline :as qp.pipeline]@@ -18,7 +20,8 @@[metabase.query-processor.test :as qp][metabase.query-processor.util :as qp.util][metabase.test :as mt]- [methodical.core :as methodical]))+ [methodical.core :as methodical]+ [toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -117,6 +120,55 @@(qe))"QueryExecution should be saved"))))+(deftest oversized-row-does-not-destroy-batch-test+ (testing "a row the app DB rejects (here: an oversized embedding_client) does not take the rest of the batch with it"+ (mt/with-model-cleanup [:model/QueryExecution]+ (let [marker (str "oversized-row-batch-test-" (random-uuid))+ ;; hash must be a real 32-byte digest: the column is BINARY(32) on H2/MySQL (only Postgres' bytea is unbounded)+ row (fn [i client]+ {:hash (buddy-hash/sha256 (str marker "-" i))+ :started_at (t/offset-date-time)+ :running_time 1+ :result_rows 0+ :native false+ :context :ad-hoc+ :cache_hit true+ :error (str marker "-" i)+ :embedding_client client})+ rows [(row 1 "embedding-sdk-react")+ (row 2 (apply str (repeat 255 "c")))+ (row 3 "embedding-sdk-react")]]+ (#'process-userland-query/save-execution-metadata!* rows)+ (is (= [(str marker "-1") (str marker "-3")]+ (t2/select-fn-vec :error :model/QueryExecution+ :error [:like (str marker "%")]+ {:order-by [[:id :asc]]})))))+ (testing "inside a caller's transaction (the synchronous path of grouper/submit!) the good rows are still saved,+ and the rejected statement does not abort that transaction"+ (let [marker (str "oversized-row-batch-test-" (random-uuid))]+ (t2/with-transaction [_conn nil {:rollback-only true}]+ (#'process-userland-query/save-execution-metadata!*+ [{:hash (buddy-hash/sha256 (str marker "-1")) :started_at (t/offset-date-time) :running_time 1+ :result_rows 0 :native false :context :ad-hoc :cache_hit true :error (str marker "-1")+ :embedding_client (apply str (repeat 255 "c"))}+ {:hash (buddy-hash/sha256 (str marker "-2")) :started_at (t/offset-date-time) :running_time 1+ :result_rows 0 :native false :context :ad-hoc :cache_hit true :error (str marker "-2")+ :embedding_client "embedding-sdk-react"}])+ (is (= [(str marker "-2")]+ (t2/select-fn-vec :error :model/QueryExecution :error [:like (str marker "%")])))+ (is (pos? (t2/count :model/User))))))))++(deftest unavailable-app-db-does-not-retry-rows-test+ (testing "when the batch fails for a reason other than a rejected row (e.g. the app DB is down), do not retry each+ row: every retry would fail too, and could wait for a connection"+ (let [calls (atom 0)]+ (mt/with-dynamic-fn-redefs [query-processor.db/insert-query-executions!+ (fn [_rows]+ (swap! calls inc)+ (throw (java.sql.SQLTransientConnectionException. "pool checkout timed out")))]+ (#'process-userland-query/insert-query-executions-resiliently! [{:error "1"} {:error "2"} {:error "3"}])+ (is (= 1 @calls))))))+(deftest failure-test(let [query (mt/mbql-query venues)](with-query-execution! [qe query]diff --git a/test/metabase/request/util_test.clj b/test/metabase/request/util_test.cljindex d1fad4cb1b0..fdf819e1c9f 100644--- a/test/metabase/request/util_test.clj+++ b/test/metabase/request/util_test.clj@@ -146,7 +146,11 @@(let [mock-request (-> (ring.mock/request :get "api/session")(ring.mock/header "X-Forwarded-For" "5.6.7.8"))](is (= "127.0.0.1"- (request.current/ip-address mock-request))))))))+ (request.current/ip-address mock-request))))))+ (testing "an oversized header value is bounded to the 45 characters an IP address can occupy"+ (let [mock-request (-> (ring.mock/request :get "api/session")+ (ring.mock/header "X-Forwarded-For" (apply str (repeat 100 "a"))))]+ (is (= 45 (count (request.current/ip-address mock-request))))))))(def ^:private mock-geojs-responses"Canned GeoJS responses for test IPs. These mock what GeoJS would return."diff --git a/test/metabase/server/middleware/browser_cookie_test.clj b/test/metabase/server/middleware/browser_cookie_test.cljindex 7caec376688..33a27b94d4f 100644--- a/test/metabase/server/middleware/browser_cookie_test.clj+++ b/test/metabase/server/middleware/browser_cookie_test.clj@@ -29,6 +29,17 @@(is (= (str test-uuid) (:body response)))(is (nil? (:cookies response))))))+(deftest malformed-cookie-test+ (testing "a DEVICE cookie that is not a UUID is replaced, since it lands in the char(36) login_history.device_id"+ (doseq [value [(apply str (repeat 40 "z")) "not-a-uuid" "" (str "x" test-uuid)]]+ (let [request (-> (ring.mock/request :get "http://localhost/foo")+ (assoc :cookies {browser-id-cookie-name {:value value}}))+ response (handler request)+ browser-id (:body response)]+ (is (not= value browser-id))+ (is (some? (UUID/fromString browser-id)))+ (is (= browser-id (get-in response [:cookies browser-id-cookie-name :value])))))))+(deftest no-existing-cookie(testing "set DEVICE cookie with SameSite=Lax if served over HTTP"(let [request (ring.mock/request :get "http://localhost/foo")diff --git a/test/metabase/server/middleware/sdk_test.clj b/test/metabase/server/middleware/sdk_test.cljindex 2d6c5ccd8b2..e9826eed189 100644--- a/test/metabase/server/middleware/sdk_test.clj+++ b/test/metabase/server/middleware/sdk_test.clj@@ -42,7 +42,31 @@(is (= version(:body response "no-body"))))nil- "1.1.1"))+ "1.1.1")+ (testing "oversized versions are truncated to fit the varchar(254) column"+ (let [request (mock-request {:version (apply str (repeat 300 "1"))})+ handler (analytics.core/embedding-mw+ (fn [_ respond _] (respond {:status 200 :body (sdk/get-version)})))+ response (handler request identity identity)]+ (is (= 254 (count (:body response)))))))++(deftest oversized-client-header-test+ (let [bound-client (fn [headers]+ (let [request (reduce-kv ring.mock/header (ring.mock/request :get "api/health") headers)+ handler (analytics.core/embedding-mw+ (fn [_ respond _] (respond {:status 200 :body (sdk/get-client)})))+ response (handler request identity identity)]+ (:body response)))]+ (testing "a 254-byte client is stored as-is"+ (is (= (apply str (repeat 254 "c"))+ (bound-client {"x-metabase-client" (apply str (repeat 254 "c"))}))))+ (testing "oversized clients are truncated to fit the varchar(254) column"+ (is (= 254 (count (bound-client {"x-metabase-client" (apply str (repeat 255 "c"))})))))+ (testing "the -preview suffix cannot push a client that fit over the limit"+ (let [client (bound-client {"x-metabase-client" (apply str (repeat 250 "c"))+ "x-metabase-embedded-preview" "true"})]+ (is (= 254 (count client)))+ (is (str/starts-with? client (apply str (repeat 250 "c"))))))))(deftest bind-client-identifier-test(are [identifier]diff --git a/test/metabase/view_log/events/view_log_test.clj b/test/metabase/view_log/events/view_log_test.cljindex 12c0cae9c1c..cc6e7a07a6a 100644--- a/test/metabase/view_log/events/view_log_test.clj+++ b/test/metabase/view_log/events/view_log_test.clj@@ -17,6 +17,7 @@[metabase.test.http-client :as client][metabase.util :as u][metabase.util.json :as json]+ [metabase.view-log.db :as view-log.db][metabase.view-log.events.view-log :as events.view-log][toucan2.core :as t2]))@@ -230,6 +231,42 @@(is (= 2 (t2/select-one-fn :view_count :model/Table (:id table)))"view_count should be incremented"))))))+(deftest oversized-row-does-not-destroy-batch-test+ (testing "a row the app DB rejects (here: an oversized embedding_client) does not take the rest of the batch with it"+ ;; no `with-temp`: its rollback-only transaction would be aborted by the rejected statement on Postgres, and+ ;; `model_id` is not a foreign key, so a Card is not needed+ (mt/with-model-cleanup [:model/ViewLog]+ (let [model-id (+ 1000000 (rand-int 1000000))+ view (fn [client]+ {:model "card"+ :model_id model-id+ :user_id (mt/user->id :rasta)+ :has_access true+ :timestamp (t/offset-date-time)+ :embedding_client client})]+ (#'events.view-log/record-views!* [(view "embedding-sdk-react")+ (view (apply str (repeat 255 "c")))+ (view "embedding-sdk-react")])+ (is (= 2 (t2/count :model/ViewLog :model "card" :model_id model-id)))+ (testing "inside a caller's transaction (the synchronous path of grouper/submit!) the good rows are still saved,+ and the rejected statement does not abort that transaction"+ (t2/with-transaction [_conn nil {:rollback-only true}]+ (#'events.view-log/record-views!* [(view (apply str (repeat 255 "c")))+ (view "embedding-sdk-react")])+ (is (= 3 (t2/count :model/ViewLog :model "card" :model_id model-id)))+ (is (pos? (t2/count :model/User)))))))))++(deftest unavailable-app-db-does-not-retry-rows-test+ (testing "when the batch fails for a reason other than a rejected row (e.g. the app DB is down), do not retry each+ row: every retry would fail too, and could wait for a connection"+ (let [calls (atom 0)]+ (mt/with-dynamic-fn-redefs [view-log.db/insert-view-logs!+ (fn [_views]+ (swap! calls inc)+ (throw (java.sql.SQLTransientConnectionException. "pool checkout timed out")))]+ (#'events.view-log/record-views!* [{:model "card"} {:model "card"} {:model "card"}])+ (is (= 1 @calls))))))+(deftest increment-view-counts!*-test(mt/with-temp [:model/Card {card-1-id :id} {}:model/Card {card-2-id :id} {:view_count 2}