Skip to content

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

Missed spots with deprecating /mcp route (#75500)

Chris Truter1cd718fd8e42
diff --git a/src/metabase/oauth_server/api/metadata.clj b/src/metabase/oauth_server/api/metadata.clj
index 9340f4deaf3..ff6c520e988 100644
--- a/src/metabase/oauth_server/api/metadata.clj
+++ b/src/metabase/oauth_server/api/metadata.clj
@@ -73,10 +73,11 @@
[]
(protected-resource-metadata "/api/mcp"))
-;; Some clients probe the bare resource path instead of the resource-specific one; serve the same metadata here so
-;; the request doesn't fall through to the SPA's HTML catch-all and trip a `JSON.parse` error (BOT-1617).
+;; Some clients probe the bare resource path instead of the resource-specific one; serve metadata here so the
+;; request doesn't fall through to the SPA's HTML catch-all and trip a `JSON.parse` error (BOT-1617). Advertise the
+;; canonical `/api/metabase-mcp` resource, matching the URL clients are now told to use.
(api.macros/defendpoint :get "/oauth-protected-resource"
:- resource-metadata-response-schema
"Returns OAuth Protected Resource Metadata (RFC 9728) for the MCP endpoint."
[]
- (protected-resource-metadata "/api/mcp"))
+ (protected-resource-metadata "/api/metabase-mcp"))
diff --git a/src/metabase/server/middleware/misc.clj b/src/metabase/server/middleware/misc.clj
index 0848b2e5e85..d657a8cbae1 100644
--- a/src/metabase/server/middleware/misc.clj
+++ b/src/metabase/server/middleware/misc.clj
@@ -60,32 +60,39 @@
;; 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`
(defn- forwarded-scheme
- "The scheme a TLS-terminating proxy used to reach us, from `X-Forwarded-Proto`."
- [x-forwarded-proto]
- ;; first hop of a comma-separated chain (`https, http`), lower-cased: URL schemes are case-insensitive
- ;; (RFC 3986) but normalize-site-url's "http" prefix check is not, so `HTTPS` would be mangled as scheme-less.
- (some-> x-forwarded-proto (str/split #",") first str/trim not-empty u/lower-case-en))
+ "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]}]
+ ;; Proto-style headers carry the scheme directly. Take the first hop of a comma-separated chain (`https, http`)
+ ;; and lower-case it: URL schemes are case-insensitive (RFC 3986) but normalize-site-url's "http" prefix check
+ ;; is not, so `HTTPS` would be mangled as scheme-less. Normalize first and branch on the result so a blank
+ ;; proto header (e.g. `X-Forwarded-Proto: ""`) falls through to the boolean-style HTTPS indicators below.
+ (or (some-> (or x-forwarded-proto x-forwarded-protocol x-url-scheme)
+ (str/split #",") first str/trim not-empty u/lower-case-en)
+ ;; Boolean-style headers are `on` when the original request was HTTPS.
+ (when-let [ssl (or x-forwarded-ssl front-end-https)]
+ (when (= "on" (u/lower-case-en ssl)) "https"))))
-(defn- maybe-set-site-url* [{{:strs [origin x-forwarded-host x-forwarded-proto host user-agent]} :headers, uri :uri}]
- (when (and (mdb/db-is-set-up?)
- (not (system/site-url))
- (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
- ;; terminated TLS with -- otherwise `normalize-site-url` defaults to `http://` and a TLS-terminating proxy ends
- ;; up advertising `http://` auth/discovery URLs over an `https` origin, breaking MCP OAuth (BOT-1617). Only
- ;; prepend when the host is scheme-less; a scheme-bearing host passes through untouched.
- (when-let [site-url (or origin
- (when-let [host (or x-forwarded-host host)]
- (if-let [scheme (and (not (str/includes? host "://"))
- (forwarded-scheme x-forwarded-proto))]
- (str scheme "://" host)
- host)))]
- (log/infof "Setting Metabase site URL to %s" site-url)
- (try
- (system/site-url! site-url)
- (catch Throwable e
- (log/warn e "Failed to set site-url"))))))
+(defn- maybe-set-site-url* [{headers :headers, uri :uri}]
+ (let [{:strs [origin x-forwarded-host host user-agent]} headers]
+ (when (and (mdb/db-is-set-up?)
+ (not (system/site-url))
+ (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
+ ;; terminated TLS with -- otherwise `normalize-site-url` defaults to `http://` and a TLS-terminating proxy ends
+ ;; up advertising `http://` auth/discovery URLs over an `https` origin, breaking MCP OAuth (BOT-1617). Only
+ ;; prepend when the host is scheme-less; a scheme-bearing host passes through untouched.
+ (when-let [site-url (or origin
+ (when-let [host (or x-forwarded-host host)]
+ (if-let [scheme (and (not (str/includes? host "://"))
+ (forwarded-scheme headers))]
+ (str scheme "://" host)
+ host)))]
+ (log/infof "Setting Metabase site URL to %s" site-url)
+ (try
+ (system/site-url! site-url)
+ (catch Throwable e
+ (log/warn e "Failed to set site-url")))))))
(defn maybe-set-site-url
"Middleware to set the `site-url` setting on the initial setup request"
diff --git a/test/metabase/oauth_server/api_test.clj b/test/metabase/oauth_server/api_test.clj
index 6bc99962f70..df861a6e3ea 100644
--- a/test/metabase/oauth_server/api_test.clj
+++ b/test/metabase/oauth_server/api_test.clj
@@ -50,12 +50,12 @@
response))))))))
(deftest protected-resource-metadata-bare-path-test
- (testing "GET /.well-known/oauth-protected-resource (no resource suffix) serves the same metadata as JSON (BOT-1617)"
+ (testing "GET /.well-known/oauth-protected-resource (no resource suffix) serves JSON advertising the canonical resource (BOT-1617)"
(mt/with-temporary-setting-values [site-url "http://localhost:3000"]
(let [response (mt/user-http-request :crowberto :get 200
".well-known/oauth-protected-resource")]
- (is (=? {:resource "http://localhost:3000/api/mcp"
- :authorization_servers ["http://localhost:3000"]
+ (is (=? {:resource "http://localhost:3000/api/metabase-mcp"
+ :authorization_servers ["http://localhost:3000"]
:bearer_methods_supported ["header"]}
response))))))
diff --git a/test/metabase/server/middleware/misc_test.clj b/test/metabase/server/middleware/misc_test.clj
index 394398a17a1..1e1a6694213 100644
--- a/test/metabase/server/middleware/misc_test.clj
+++ b/test/metabase/server/middleware/misc_test.clj
@@ -70,6 +70,16 @@
(maybe-set-site-url (-> (mock-request "/" nil "mb.example.com" nil)
(ring.mock/header "X-Forwarded-Proto" "https, http")))
(is (= "https://mb.example.com" (system/site-url)))))
+ (testing "the alternate HTTPS-indicator headers `u/https?` recognizes also seed the scheme"
+ (doseq [[header value] [["X-Forwarded-Protocol" "https"]
+ ["X-URL-Scheme" "https"]
+ ["X-Forwarded-Ssl" "on"]
+ ["Front-End-Https" "on"]]]
+ (testing header
+ (mt/with-temporary-setting-values [site-url nil]
+ (maybe-set-site-url (-> (mock-request "/" nil "mb.example.com" nil)
+ (ring.mock/header header value)))
+ (is (= "https://mb.example.com" (system/site-url)))))))
(testing "without `X-Forwarded-Proto`, a scheme-less host falls back to http:// (unchanged behavior)"
(mt/with-temporary-setting-values [site-url nil]
(maybe-set-site-url (mock-request "/" nil "mb.example.com" nil))