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 OAuth and MCP throttling (#71884)
Chris Truter70552d31d84e
diff --git a/.clj-kondo/config/modules/config.edn b/.clj-kondo/config/modules/config.ednindex 432ac2bc9a3..fd9d5293b8b 100644--- a/.clj-kondo/config/modules/config.edn+++ b/.clj-kondo/config/modules/config.edn@@ -1375,6 +1375,7 @@:uses #{apiappearancemodels+ requestsettingssystemutil}}diff --git a/src/metabase/mcp/api.clj b/src/metabase/mcp/api.cljindex 5fc384e8854..bbe03f7aa71 100644--- a/src/metabase/mcp/api.clj+++ b/src/metabase/mcp/api.clj@@ -16,7 +16,8 @@[metabase.system.core :as system][metabase.util.json :as json][metabase.util.log :as log]- [oidc-provider.store :as oidc.store])+ [oidc-provider.store :as oidc.store]+ [throttle.core :as throttle])(:import(java.io BufferedWriter OutputStreamWriter)(java.net URI)@@ -246,6 +247,30 @@(or session-err{:status 200 :headers {"Content-Type" "application/json"} :body ""})))+;;; -------------------------------------------------- Throttling --------------------------------------------------++;; MCP is auth-gated (session cookie or bearer token), so the risk is lower than the+;; unauthenticated OAuth endpoints. The threshold is generous to accommodate users running+;; multiple concurrent agents (e.g. 5 agents × 200 req/min). throttle/check counts every+;; request (not just failures) which is correct here — we want to cap total throughput+;; regardless of success to prevent resource exhaustion from a compromised token.+(def ^:private one-minute-ms (* 60 1000))++(def ^:private mcp-throttler+ (throttle/make-throttler :user-id :attempts-threshold 1000 :attempt-ttl-ms one-minute-ms))++(defn- check-throttle+ "Returns a 429 JSON-RPC response if rate-limited, nil otherwise."+ [user-id]+ (try+ (throttle/check mcp-throttler user-id)+ nil+ (catch clojure.lang.ExceptionInfo e+ (let [message (ex-message e)+ retry-seconds (some->> message (re-find #"(\d+) seconds") second)]+ (cond-> (json-response 429 (jsonrpc-error nil -32000 message))+ retry-seconds (assoc-in [:headers "Retry-After"] retry-seconds))))))+;;; ---------------------------------------------------- Handler ---------------------------------------------------(defn- www-authenticate-discovery []@@ -261,15 +286,17 @@session-auth api/*current-user-id*](letfn [(dispatch [user-id token-scopes](request/with-current-user user-id- (try- (let [request (assoc request :token-scopes token-scopes)]- (case (:request-method request)- :post (respond (handle-post user-id request))- :get (handle-get user-id request respond raise)- :delete (respond (handle-delete user-id request))- (respond (json-response 405 (jsonrpc-error nil -32600 "Method not allowed")))))- (catch Throwable e- (raise e)))))]+ (if-let [throttle-err (check-throttle user-id)]+ (respond throttle-err)+ (try+ (let [request (assoc request :token-scopes token-scopes)]+ (case (:request-method request)+ :post (respond (handle-post user-id request))+ :get (handle-get user-id request respond raise)+ :delete (respond (handle-delete user-id request))+ (respond (json-response 405 (jsonrpc-error nil -32600 "Method not allowed")))))+ (catch Throwable e+ (raise e))))))](cond(some? origin-error)(respond origin-error)diff --git a/src/metabase/oauth_server/api/oauth.clj b/src/metabase/oauth_server/api/oauth.cljindex 60394bf2701..f097a87e45a 100644--- a/src/metabase/oauth_server/api/oauth.clj+++ b/src/metabase/oauth_server/api/oauth.clj@@ -9,13 +9,15 @@[metabase.oauth-server.consent-page :as consent-page][metabase.oauth-server.core :as oauth-server][metabase.oauth-server.settings :as oauth-settings]+ [metabase.request.core :as request][metabase.system.core :as system][metabase.util.log :as log][oidc-provider.core :as oidc][oidc-provider.protocol :as proto][oidc-provider.registration :as reg][oidc-provider.util :as oidc-util]- [ring.util.response :as response])+ [ring.util.response :as response]+ [throttle.core :as throttle])(:import(clojure.lang ExceptionInfo)(java.net URLEncoder)))@@ -108,51 +110,110 @@(str site-url "/auth/login"))]redirect))+;;; ------------------------------------------------ Throttling ---------------------------------------------------++(def ^:private one-minute-ms (* 60 1000))+(def ^:private one-hour-ms (* 60 one-minute-ms))++;; /oauth/token is the highest-risk endpoint: unauthenticated, accepts client credentials,+;; and is the primary target for brute-forcing client secrets or replaying authorization codes.+;; Per-client_id is tight (one agent per client)+(def ^:private token-client-throttler+ (throttle/make-throttler :client-id :attempts-threshold 10 :attempt-ttl-ms one-hour-ms))++;; Per-IP is wider to accommodate many agents behind NAT each doing their own token refresh cycle.+(def ^:private token-ip-throttler+ (throttle/make-throttler :ip-address :attempts-threshold 50 :attempt-ttl-ms one-hour-ms))++;; /oauth/register is unauthenticated and creates server-side state (client records).+;; Without throttling, an attacker can exhaust storage or generate unlimited client_id/secret pairs.+;; Per-IP only since there's no identity at registration time. Threshold allows burst setup of+;; several agents without enabling sustained spam.+(def ^:private registration-throttler+ (throttle/make-throttler :ip-address :attempts-threshold 10 :attempt-ttl-ms one-minute-ms))++;; /oauth/authorize/decision is lower risk (requires authentication + CSRF token),+;; but a compromised session could automate consent-granting. A per-user cap limits the blast+;; radius while allowing setup of many agents in a single session.+(def ^:private authorize-decision-throttler+ (throttle/make-throttler :user-id :attempts-threshold 20 :attempt-ttl-ms one-hour-ms))++(defn- throttle-response+ "Build a 429 response from a throttle exception, extracting Retry-After when available."+ [^ExceptionInfo e]+ (let [message (ex-message e)+ retry-seconds (some->> message (re-find #"(\d+) seconds") second)]+ (cond-> {:status 429+ :headers {"Content-Type" "application/json"}+ :body {:error "too_many_requests"+ :error_description message}}+ retry-seconds (assoc-in [:headers "Retry-After"] retry-seconds))))++(defn- throttle-exception?+ "True when `e` is a throttle-limit exception thrown by [[metabase.throttle]].+ Coupled to the library's `\"Too many attempts!\"` message prefix — update here if that changes."+ [^ExceptionInfo e]+ (str/starts-with? (ex-message e) "Too many attempts!"))++(defmacro ^:private with-throttling-429+ "Like [[throttle/with-throttling]], but returns HTTP 429 with Retry-After instead of+ throwing an unhandled exception (which the generic middleware would turn into a 500)."+ {:style/indent 1}+ [bindings & body]+ `(try+ (throttle/with-throttling ~bindings ~@body)+ (catch ExceptionInfo e#+ (if (throttle-exception? e#)+ (throttle-response e#)+ (throw e#)))))+;;; ------------------------------------------------ Endpoints ----------------------------------------------------(api.macros/defendpoint :post "/register"- :- [:map [:status [:enum 201 400 403 404]] [:body :any]]+ :- [:map [:status [:enum 201 400 403 404 429]] [:body :any]]"Handles dynamic client registration (RFC 7591)."- [_route-params _query-params body :- :any]+ [_route-params _query-params body :- :any+ request](if-not (oauth-settings/oauth-server-dynamic-registration-enabled){:status 403:headers {"Content-Type" "application/json"}:body {"error" "registration_not_supported"}}- (or (when-let [provider (oauth-server/get-provider)]- (if (nil? body)- {:status 400- :headers {"Content-Type" "application/json"}- :body {"error" "invalid_client_metadata"- "error_description" "Invalid or missing JSON body"}}- (try- ;; MCP clients frequently omit application_type, scope, and may request- ;; unsupported grant types. We are required to support poorly-configured- ;; clients, so we apply sensible defaults here:- ;; - application_type defaults to "native" (not the RFC default "web") so- ;; CLI tools and desktop apps can use HTTP loopback redirects.- ;; - scope defaults to all provider-supported scopes when not specified.- (let [body (cond-> body- (not (contains? body :application_type))- (assoc :application_type "native")- (not (contains? body :scope))- (assoc :scope (str/join " " (oauth-server/all-agent-scopes)))- ;; Remove client_credentials grant type — tokens issued without a- ;; user context are unusable for MCP (validate-bearer-token requires- ;; a valid user-id).- (contains? body :grant_types)- (update :grant_types (fn [gts] (vec (remove #{"client_credentials"} gts)))))- response (oidc/dynamic-register-client provider body)- client-id (:client_id response)]- ;; Mark as dynamically registered (the library doesn't know about registration_type)- (proto/update-client (:client-store provider) client-id {:registration-type "dynamic"})- {:status 201- :headers {"Content-Type" "application/json"}- :body response})- (catch ExceptionInfo e- (reg/registration-error-response- (ex-message e)- (:error_description (ex-data e)))))))- {:status 404 :body {:error "not_found"}})))+ (with-throttling-429 [registration-throttler (request/ip-address request)]+ (or (when-let [provider (oauth-server/get-provider)]+ (if (nil? body)+ {:status 400+ :headers {"Content-Type" "application/json"}+ :body {"error" "invalid_client_metadata"+ "error_description" "Invalid or missing JSON body"}}+ (try+ ;; MCP clients frequently omit application_type, scope, and may request+ ;; unsupported grant types. We are required to support poorly-configured+ ;; clients, so we apply sensible defaults here:+ ;; - application_type defaults to "native" (not the RFC default "web") so+ ;; CLI tools and desktop apps can use HTTP loopback redirects.+ ;; - scope defaults to all provider-supported scopes when not specified.+ (let [body (cond-> body+ (not (contains? body :application_type))+ (assoc :application_type "native")+ (not (contains? body :scope))+ (assoc :scope (str/join " " (oauth-server/all-agent-scopes)))+ ;; Remove client_credentials grant type — tokens issued without a+ ;; user context are unusable for MCP (validate-bearer-token requires+ ;; a valid user-id).+ (contains? body :grant_types)+ (update :grant_types (fn [gts] (vec (remove #{"client_credentials"} gts)))))+ response (oidc/dynamic-register-client provider body)+ client-id (:client_id response)]+ ;; Mark as dynamically registered (the library doesn't know about registration_type)+ (proto/update-client (:client-store provider) client-id {:registration-type "dynamic"})+ {:status 201+ :headers {"Content-Type" "application/json"}+ :body response})+ (catch ExceptionInfo e+ (reg/registration-error-response+ (ex-message e)+ (:error_description (ex-data e)))))))+ {:status 404 :body {:error "not_found"}}))))(api.macros/defendpoint :get "/register/:client-id":- [:map [:status [:enum 200 401 404]] [:body :map]]@@ -207,7 +268,7 @@{:status 404 :body {:error "not_found"}})))(api.macros/defendpoint :post "/authorize/decision"- :- [:map [:status [:enum 302 400 401 403 404]] [:body [:or :string :map]]]+ :- [:map [:status [:enum 302 400 401 403 404 429]] [:body [:or :string :map]]]"Handles the authorization decision (POST /oauth/authorize/decision)."[_route-params _query-params bodyrequest]@@ -215,64 +276,71 @@{:status 401:headers {"Content-Type" "application/json"}:body {:error "unauthorized"}}- (or (when-let [provider (oauth-server/get-provider)]- (let [cookie-token (get-in request [:cookies csrf-cookie-name :value])- form-token (some-> (:csrf_token body) str)- auth-params (select-keys body oauth-param-keys)- params-sig (some-> (:params_sig body) str)]- (if (or (str/blank? cookie-token)- (str/blank? form-token)- (not (oidc-util/constant-time-eq? cookie-token form-token)))- {:status 403- :headers {"Content-Type" "application/json"}- :body {:error "csrf_validation_failed"}}- (let [approved (= "true" (str (:approved body)))]- (try- (let [parsed (oidc/parse-authorization-request provider auth-params)- ;; Verify the HMAC against the *parsed* params (same normalized form as the consent page).- ;; This must happen after parsing to ensure form-encoding round-trips don't cause mismatches.- parsed-params (select-keys parsed oauth-param-keys)]- (if (or (str/blank? params-sig)- (not (re-matches #"[a-fA-F0-9]+" params-sig))- (odd? (count params-sig))- (not (verify-oauth-params-signature cookie-token parsed-params params-sig)))- {:status 403+ (with-throttling-429 [authorize-decision-throttler (:metabase-user-id request)]+ (or (when-let [provider (oauth-server/get-provider)]+ (let [cookie-token (get-in request [:cookies csrf-cookie-name :value])+ form-token (some-> (:csrf_token body) str)+ auth-params (select-keys body oauth-param-keys)+ params-sig (some-> (:params_sig body) str)]+ (if (or (str/blank? cookie-token)+ (str/blank? form-token)+ (not (oidc-util/constant-time-eq? cookie-token form-token)))+ {:status 403+ :headers {"Content-Type" "application/json"}+ :body {:error "csrf_validation_failed"}}+ (let [approved (= "true" (str (:approved body)))]+ (try+ (let [parsed (oidc/parse-authorization-request provider auth-params)+ ;; Verify the HMAC against the *parsed* params (same normalized form as the consent page).+ ;; This must happen after parsing to ensure form-encoding round-trips don't cause mismatches.+ parsed-params (select-keys parsed oauth-param-keys)]+ (if (or (str/blank? params-sig)+ (not (re-matches #"[a-fA-F0-9]+" params-sig))+ (odd? (count params-sig))+ (not (verify-oauth-params-signature cookie-token parsed-params params-sig)))+ {:status 403+ :headers {"Content-Type" "application/json"}+ :body {:error "params_tampered"}}+ (redirect-authorization-decision provider parsed approved request)))+ (catch ExceptionInfo e+ (log/warn e "OAuth authorization decision failed")+ {:status 400:headers {"Content-Type" "application/json"}- :body {:error "params_tampered"}}- (redirect-authorization-decision provider parsed approved request)))- (catch ExceptionInfo e- (log/warn e "OAuth authorization decision failed")- {:status 400- :headers {"Content-Type" "application/json"}- :body {:error "invalid_request"- :error_description "The authorization request is invalid."}}))))))- {:status 404 :body {:error "not_found"}})))+ :body {:error "invalid_request"+ :error_description "The authorization request is invalid."}}))))))+ {:status 404 :body {:error "not_found"}}))))(api.macros/defendpoint :post "/token"- :- [:map [:status [:enum 200 400 401 404]] [:body :map]]+ :- [:map [:status [:enum 200 400 401 404 429]] [:body :map]]"Handles the token endpoint (POST /oauth/token)."[_route-params _query-params bodyrequest]- (or (when-let [provider (oauth-server/get-provider)]- (let [authorization-header (get-in request [:headers "authorization"])]- (try- (let [response (oidc/token-request provider body authorization-header)]- {:status 200- :headers {"Content-Type" "application/json"- "Cache-Control" "no-store"- "Pragma" "no-cache"}- :body response})- (catch ExceptionInfo e- (log/warn e "OAuth token request failed")- (let [data (ex-data e)- error (or (:error data) "invalid_request")]- {:status (if (= error "invalid_client") 401 400)- :headers {"Content-Type" "application/json"- "Cache-Control" "no-store"- "Pragma" "no-cache"}- :body {:error error- :error_description (or (:error_description data) "The token request is invalid.")}})))))- {:status 404 :body {:error "not_found"}}))+ (let [ip-address (request/ip-address request)+ ;; Fall back to IP when client_id isn't in the body (e.g. confidential clients using+ ;; HTTP Basic auth) to avoid pooling unrelated clients into a shared throttle bucket.+ client-id (or (:client_id body) ip-address)]+ (with-throttling-429 [token-client-throttler client-id+ token-ip-throttler ip-address]+ (or (when-let [provider (oauth-server/get-provider)]+ (let [authorization-header (get-in request [:headers "authorization"])]+ (try+ (let [response (oidc/token-request provider body authorization-header)]+ {:status 200+ :headers {"Content-Type" "application/json"+ "Cache-Control" "no-store"+ "Pragma" "no-cache"}+ :body response})+ (catch ExceptionInfo e+ (log/warn e "OAuth token request failed")+ (let [data (ex-data e)+ error (or (:error data) "invalid_request")]+ {:status (if (= error "invalid_client") 401 400)+ :headers {"Content-Type" "application/json"+ "Cache-Control" "no-store"+ "Pragma" "no-cache"}+ :body {:error error+ :error_description (or (:error_description data) "The token request is invalid.")}})))))+ {:status 404 :body {:error "not_found"}}))))(api.macros/defendpoint :post "/revoke":- [:map [:status [:enum 200 404]]]diff --git a/test/metabase/mcp/api_test.clj b/test/metabase/mcp/api_test.cljindex b1183bb3cce..e57640d954d 100644--- a/test/metabase/mcp/api_test.clj+++ b/test/metabase/mcp/api_test.clj@@ -3,6 +3,7 @@[clojure.string :as str][clojure.test :refer :all][metabase.api.macros.scope :as scope]+ [metabase.mcp.api :as mcp.api][metabase.mcp.tools :as mcp.tools][metabase.oauth-server.core :as oauth-server][metabase.search.test-util :as search.tu]@@ -11,6 +12,7 @@[metabase.test.fixtures :as fixtures][metabase.test.http-client :as client][metabase.util.json :as json]+ [throttle.core :as throttle][toucan2.core :as t2]))(set! *warn-on-reflection* true)@@ -513,3 +515,22 @@(#'mcp.tools/invoke-agent-api :get (str "/v1/table/" (mt/id :orders)) #{::scope/unrestricted} nil))](is (not (:isError result))"Agent API should accept unrestricted scopes")))))++;;; ------------------------------------------------- Throttling ---------------------------------------------------++(deftest mcp-throttle-returns-429-test+ (testing "MCP endpoint returns 429 with JSON-RPC error when rate-limited"+ (let [[session-id _] (initialize!)]+ ;; Replace throttler after initialization so the handshake doesn't consume attempts+ (with-redefs [mcp.api/mcp-throttler (throttle/make-throttler :user-id :attempts-threshold 1)]+ ;; First request succeeds (consumes the single attempt)+ (is (= 200 (:status (mcp-request (jsonrpc-request "ping")+ {"mcp-session-id" session-id}))))+ ;; Second request should be throttled+ (is (=? {:status 429+ :headers {"Retry-After" string?}+ :body {:jsonrpc "2.0"+ :error {:code -32000+ :message #(str/starts-with? % "Too many attempts!")}}}+ (mcp-request (jsonrpc-request "ping")+ {"mcp-session-id" session-id})))))))