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
Add request info to 'Request canceled before finishing' log line (#77023)
Nathan Voxland471df8492b7e
diff --git a/src/metabase/server/middleware/exceptions.clj b/src/metabase/server/middleware/exceptions.cljindex 32368142945..8c755555af1 100644--- a/src/metabase/server/middleware/exceptions.clj+++ b/src/metabase/server/middleware/exceptions.clj@@ -4,8 +4,10 @@[clojure.java.jdbc :as jdbc][clojure.string :as str][metabase.analytics-interface.core :as analytics]+ [metabase.request.core :as request][metabase.server.middleware.security :as mw.security][metabase.server.settings :as server.settings]+ [metabase.util :as u][metabase.util.i18n :refer [tru]][metabase.util.log :as log])(:import@@ -18,12 +20,14 @@(defmulti api-exception-response"Convert an uncaught exception from an API endpoint into an appropriate format to be returned by the REST API (e.g. a- map, which eventually gets serialized to JSON, or a plain string message)."- {:arglists '([e])}- class)+ map, which eventually gets serialized to JSON, or a plain string message). `request` is the Ring request that+ triggered the exception, for methods that want to include request info in their logging."+ {:arglists '([e request])}+ (fn [e _request]+ (class e)))(defmethod api-exception-response Throwable- [^Throwable e]+ [^Throwable e _request](let [{:keys [status-code], :as info} (ex-data e)other-info (dissoc info :status-code :schema :type :toucan2/context-trace ::log/context)body (cond@@ -58,14 +62,17 @@:body body}))(defmethod api-exception-response SQLException- [e]- (-> ((get-method api-exception-response (.getSuperclass SQLException)) e)+ [e request]+ (-> ((get-method api-exception-response (.getSuperclass SQLException)) e request)(assoc-in [:body :sql-exception-chain] (str/split (with-out-str (jdbc/print-sql-exception-chain e))#"\s*\n\s*"))))(defmethod api-exception-response EofException- [_e]- (log/info "Request canceled before finishing.")+ [_e {:keys [request-method uri], :as request}]+ (log/infof "Request canceled before finishing: %s %s from client %s"+ (or (some-> request-method name u/upper-case-en) "?")+ uri+ (or (request/ip-address request) "unknown")){:status-code 204, :body nil, :headers (mw.security/security-headers)})(defn catch-api-exceptions@@ -76,7 +83,7 @@(handlerrequestrespond- (comp respond api-exception-response))))+ #(respond (api-exception-response % request)))))(defn catch-uncaught-exceptions"Middleware (with `[request respond raise]`) that catches any unexpected Exceptions and reroutes them through `raise`diff --git a/test/metabase/server/middleware/exceptions_test.clj b/test/metabase/server/middleware/exceptions_test.cljindex 01b343430fa..9fd51b262e4 100644--- a/test/metabase/server/middleware/exceptions_test.clj+++ b/test/metabase/server/middleware/exceptions_test.clj@@ -4,7 +4,11 @@[clojure.test :refer :all][metabase.server.middleware.exceptions :as mw.exceptions][metabase.server.settings :as server.settings]- [metabase.test :as mt]))+ [metabase.test :as mt])+ (:import+ (org.eclipse.jetty.io EofException)))++(set! *warn-on-reflection* true)(defn- create-test-exception"Create a test exception with a stacktrace."@@ -18,7 +22,7 @@(testing "When hide-stacktraces is false, exception response includes the error message"(mt/with-temporary-setting-values [server.settings/hide-stacktraces false](let [exception (create-test-exception "Test error message")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= 500 (:status response)))(is (= "Test error message" (get-in response [:body :message])))))))@@ -26,7 +30,7 @@(testing "When hide-stacktraces is false, exception response includes stacktrace information"(mt/with-temporary-setting-values [server.settings/hide-stacktraces false](let [exception (create-test-exception "Test error message")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (contains? (:body response) :trace)"Response should contain :trace key with stacktrace")(is (vector? (get-in response [:body :trace]))@@ -36,7 +40,7 @@(testing "When hide-stacktraces is false, exception response includes ex-data"(mt/with-temporary-setting-values [server.settings/hide-stacktraces false](let [exception (create-test-exception "Test error message")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= "test-value" (get-in response [:body :custom-data]))"Response should include custom data from ex-info")))))@@ -44,7 +48,7 @@(testing "When hide-stacktraces is false, exception response includes exception chain information"(mt/with-temporary-setting-values [server.settings/hide-stacktraces false](let [exception (create-test-exception "Test error message")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (contains? (:body response) :via)"Response should contain :via key with exception chain")))))@@ -52,7 +56,7 @@(testing "When hide-stacktraces is true, exception response contains only a generic message"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (create-test-exception "Test error message with sensitive info")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= 500 (:status response)))(is (= "Something went wrong" (get-in response [:body :message]))"Should return generic message instead of actual exception message")))))@@ -61,7 +65,7 @@(testing "When hide-stacktraces is true, exception response does not include stacktrace"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (create-test-exception "Test error message with sensitive info")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (not (contains? (:body response) :trace))"Response should not contain :trace key")))))@@ -69,7 +73,7 @@(testing "When hide-stacktraces is true, exception response does not include ex-data"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (create-test-exception "Test error message with sensitive info")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (not (contains? (:body response) :custom-data))"Response should not include custom data from ex-info")))))@@ -77,7 +81,7 @@(testing "When hide-stacktraces is true, exception response does not include exception chain"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (create-test-exception "Test error message with sensitive info")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (not (contains? (:body response) :via))"Response should not contain :via key")))))@@ -85,7 +89,7 @@(testing "When hide-stacktraces is true, exception response does not reveal the original message"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (create-test-exception "Test error message with sensitive info")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (not (str/includes? (str (:body response)) "sensitive info"))"Response should not contain any part of the original error message")))))@@ -93,7 +97,7 @@(testing "When hide-stacktraces is true, exception response body is minimal"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (create-test-exception "Test error message with sensitive info")- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= {:message "Something went wrong"} (:body response))"Response body should only contain the generic message")))))@@ -101,7 +105,7 @@(testing "404 errors with status-code and no extra data return plain message"(mt/with-temporary-setting-values [server.settings/hide-stacktraces false](let [exception (ex-info "Resource not found" {:status-code 404})- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= 404 (:status response)))(is (= "Resource not found" (:body response))"Should return plain message for 404s")))))@@ -110,7 +114,7 @@(testing "404 errors with extra data hide details when hide-stacktraces is enabled"(mt/with-temporary-setting-values [server.settings/hide-stacktraces true](let [exception (ex-info "Resource not found with details" {:status-code 404 :resource-id 123})- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= 404 (:status response))"Status should remain 404 from exception")(is (= "Something went wrong" (get-in response [:body :message]))@@ -125,7 +129,7 @@{:status-code 400:errors {:email "Invalid email format":password "Password too short"}})- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= 400 (:status response)))(is (= {:email "Invalid email format":password "Password too short"}@@ -139,7 +143,7 @@{:status-code 400:errors {:email "Invalid email format":password "Password too short"}})- response (mw.exceptions/api-exception-response exception)]+ response (mw.exceptions/api-exception-response exception nil)](is (= 400 (:status response)))(is (= {:email "Invalid email format":password "Password too short"}@@ -180,6 +184,46 @@(is (not (contains? (:body @captured-response) :trace))"Response should not include stacktrace"))))))+(deftest catch-api-exceptions-middleware-eof-exception-logs-request-info-test+ (testing "EofException (request canceled) log line includes the HTTP method, URI, and client IP"+ (let [handler (mw.exceptions/catch-api-exceptions+ (fn [_request _respond raise]+ (raise (EofException. "canceled"))))+ captured-response (atom nil)]+ (mt/with-log-messages-for-level [messages [metabase.server.middleware.exceptions :info]]+ (handler+ {:request-method :post+ :uri "/api/dataset"+ :remote-addr "10.1.2.3"+ :headers {}}+ (fn [response] (reset! captured-response response))+ (fn [_e] (is false "Should not call raise")))+ (let [log-message (->> (messages) (map :message) (str/join "\n"))]+ (is (str/includes? log-message "Request canceled before finishing"))+ (is (str/includes? log-message "POST /api/dataset")+ "Log message should include the HTTP method and URI")+ (is (str/includes? log-message "10.1.2.3")+ "Log message should include the client IP address")))+ (is (= 204 (:status-code @captured-response))))))++(deftest catch-api-exceptions-middleware-eof-exception-logs-forwarded-ip-test+ (testing "EofException log line uses X-Forwarded-For to identify the client behind a proxy"+ (let [handler (mw.exceptions/catch-api-exceptions+ (fn [_request _respond raise]+ (raise (EofException. "canceled"))))]+ (mt/with-log-messages-for-level [messages [metabase.server.middleware.exceptions :info]]+ (handler+ {:request-method :get+ :uri "/api/card/1/query"+ :remote-addr "127.0.0.1"+ :headers {"x-forwarded-for" "203.0.113.7"}}+ (fn [_response])+ (fn [_e] (is false "Should not call raise")))+ (let [log-message (->> (messages) (map :message) (str/join "\n"))]+ (is (str/includes? log-message "GET /api/card/1/query"))+ (is (str/includes? log-message "203.0.113.7")+ "Log message should report the forwarded client IP, not the proxy's"))))))+(deftest catch-uncaught-exceptions-middleware-test(testing "catch-uncaught-exceptions middleware routes exceptions to raise handler"(let [test-exception (create-test-exception "Uncaught exception test")@@ -198,10 +242,10 @@(testing "An unhandled exception (no status-code in ex-data) increments :metabase-api/unhandled-errors"(mt/with-prometheus-system! [_ system](let [initial (mt/metric-value system :metabase-api/unhandled-errors)]- (mw.exceptions/api-exception-response (Exception. "boom"))+ (mw.exceptions/api-exception-response (Exception. "boom") nil)(is (< initial (mt/metric-value system :metabase-api/unhandled-errors))))))(testing "An exception with an explicit status-code does NOT increment the counter"(mt/with-prometheus-system! [_ system](let [initial (mt/metric-value system :metabase-api/unhandled-errors)]- (mw.exceptions/api-exception-response (ex-info "not found" {:status-code 404}))+ (mw.exceptions/api-exception-response (ex-info "not found" {:status-code 404}) nil)(is (= initial (mt/metric-value system :metabase-api/unhandled-errors)))))))