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
Ignore components of cors urls (#76192)
lbrdnkd99249106342
diff --git a/src/metabase/mcp/settings.clj b/src/metabase/mcp/settings.cljindex e82381687f6..4b42c6a3709 100644--- a/src/metabase/mcp/settings.clj+++ b/src/metabase/mcp/settings.clj@@ -65,6 +65,40 @@:encryption :no:audit :getter)+(defn- strip-scheme+ [s]+ (if-let [idx (str/index-of s "://")]+ (subs s (+ idx 3))+ s))++(defn- origin-has-real-path?+ "True if `entry` (a single CORS origin, optionally `scheme://`-prefixed) has a path component beyond+ an optional bare trailing slash, e.g. `http://localhost:6274/sse` (but not `http://localhost:6274/`)."+ [entry]+ (let [without-scheme (strip-scheme entry)+ slash-idx (str/index-of without-scheme "/")]+ (boolean (and slash-idx+ (< (inc slash-idx) (count without-scheme))))))++(defn- validate-no-origin-paths!+ "Throws if any space-separated entry in `origins-raw` has a path component. CORS origins are+ scheme + host + port only, so a path pasted into this setting (e.g. `http://localhost:6274/sse`)+ wouldn't do what an admin might expect (only the origin is honored, not the path) — better to+ reject it outright than silently drop it and leave the setting looking saved."+ [origins-raw]+ (when-let [bad-entries (->> (str/split (or origins-raw "") #"\s+")+ (filter not-empty)+ (filter origin-has-real-path?)+ not-empty)]+ (throw (ex-info (str "CORS origins must not include a path: " (str/join ", " bad-entries))+ {:status-code 400}))))++(defn- -mcp-apps-cors-custom-origins!+ "The setter for [[mcp-apps-cors-custom-origins]]."+ [new-value]+ (validate-no-origin-paths! new-value)+ (setting/set-value-of-type! :string :mcp-apps-cors-custom-origins new-value))+(defsetting mcp-apps-cors-custom-origins(deferred-tru "Custom CORS origins for self-hosted MCP clients, space-separated."):type :string@@ -72,7 +106,8 @@:visibility :admin:export? false:encryption :no- :audit :getter)+ :audit :getter+ :setter #'-mcp-apps-cors-custom-origins!);;; ------------------------------------------------ Helpers ------------------------------------------------diff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.cljindex 2d6287cf1d6..555b7731a8f 100644--- a/src/metabase/server/middleware/security.clj+++ b/src/metabase/server/middleware/security.clj@@ -60,6 +60,26 @@the original request was HTTPS; if sent in response to an HTTP request, this is simply ignored)"{"Strict-Transport-Security" "max-age=31536000"})+(def ^:private url-scheme-pattern+ "RFC 3986 generic URI scheme syntax: a letter followed by any number of letters, digits, `+`, `-`,+ or `.`. We don't restrict this to a closed set of known schemes (`http`, `app`, ...) since MCP+ clients are free to register arbitrary custom schemes (e.g. `vscode-webview`, `chrome-extension`,+ `electron`). Schemes are case-insensitive; callers that need case-insensitive matching normalize+ case themselves."+ "[a-zA-Z][a-zA-Z0-9+.\\-]*")++(def ^:private url-domain-pattern+ "Valid characters for a CORS origin's domain: standard hostname/IPv4 labels (letters, digits,+ hyphens, separated by dots), plus `*` so wildcard entries like `*.example.com` are still accepted."+ "[a-zA-Z0-9*](?:[a-zA-Z0-9*-]*[a-zA-Z0-9*])?(?:\\.[a-zA-Z0-9*](?:[a-zA-Z0-9*-]*[a-zA-Z0-9*])?)*")++(def ^:private url-host-pattern+ "Bracketed IPv6 literal (e.g. `[::1]`) or a hostname/IPv4/wildcard domain."+ (str "\\[[^\\]]+\\]|" url-domain-pattern))++(def ^:private url-pattern+ (re-pattern (str "^(?:(" url-scheme-pattern ")://)?(" url-host-pattern ")(?::(\\d+|\\*))?$")))+(defn try-parse-url"Like `parse-url` but returns nil silently on unparsable input. Use this when the caller is parsingclient-controlled data (e.g. `Origin`/`Host` headers) where bad input is expected and shouldn't@@ -67,9 +87,7 @@[url](if (= url "*"){:protocol nil :domain "*" :port "*"}- ;; Pattern supports both regular hostnames/IPv4 and bracketed IPv6 addresses like [::1]- (let [pattern #"^(?:(https?|app|capacitor)://)?(\[[^\]]+\]|[^:/]+)(?::(\d+|\*))?$"- [_ protocol domain port] (re-matches pattern url)]+ (let [[_ protocol domain port] (re-matches url-pattern url)](when domain{:protocol protocol :domain domain :port port}))))@@ -284,11 +302,25 @@(= reference-port "*")(= port reference-port)))+(def ^:private url-authority-pattern+ ;; _________________________________$1_____________________________________+ (re-pattern (str "^((?:" url-scheme-pattern "://)?(?:" url-host-pattern ")(?::(?:\\d+|\\*))?)(?:/.*)?$")))++(defn- strip-origin-path+ "Strips a trailing `/path` from an admin-entered CORS origin entry. Origins have no path component by+ definition (just scheme + host + port), so a bare trailing slash pasted into an allowlist setting+ (e.g. `http://localhost:6274/`) shouldn't silently make that entry fail to parse and drop out of the+ allowlist. Entries with a real path are rejected at save time (see the setting's `:setter`); this+ only needs to normalize bare trailing slashes and any non-trivial paths saved before that validation+ existed."+ [url]+ (str/replace url url-authority-pattern "$1"))+(defn parse-approved-origins"Parses the space separated string of approved origins"[approved-origins-raw](let [urls (str/split approved-origins-raw #" +")]- (keep parse-url urls)))+ (keep (comp parse-url strip-origin-path) urls)))(def ^:private loopback-hosts"Set of hostnames/IPs that represent loopback addresses.diff --git a/test/metabase/mcp/api_test.clj b/test/metabase/mcp/api_test.cljindex f1f6947d80a..2ab14571033 100644--- a/test/metabase/mcp/api_test.clj+++ b/test/metabase/mcp/api_test.clj@@ -209,6 +209,13 @@{"host" "mbtest.poom.dev""origin" "HTTPS://example.com"})](is (= 200 (:status response)))+ (is (some? (get-in response [:headers "Mcp-Session-Id"]))))))+ (testing "a trailing slash on a configured MCP CORS origin has no effect (#75839)"+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-custom-origins "http://127.0.0.1:6274/"]+ (let [response (mcp-request (jsonrpc-request "initialize")+ {"host" "mbtest.poom.dev"+ "origin" "http://127.0.0.1:6274"})]+ (is (= 200 (:status response)))(is (some? (get-in response [:headers "Mcp-Session-Id"])))))))(deftest mcp-enabled-setting-testdiff --git a/test/metabase/mcp/settings_test.clj b/test/metabase/mcp/settings_test.cljnew file mode 100644index 00000000000..569a4475277--- /dev/null+++ b/test/metabase/mcp/settings_test.clj@@ -0,0 +1,27 @@+(ns metabase.mcp.settings-test+ (:require+ [clojure.test :refer :all]+ [metabase.mcp.settings :as mcp.settings]+ [metabase.test :as mt]))++(deftest mcp-apps-cors-custom-origins-path-validation-test+ (testing "Should reject an origin with a real path (#75839)"+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-custom-origins ""]+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"CORS origins must not include a path"+ (mcp.settings/mcp-apps-cors-custom-origins! "http://localhost:6274/sse")))+ (is (thrown-with-msg?+ clojure.lang.ExceptionInfo+ #"CORS origins must not include a path"+ (mcp.settings/mcp-apps-cors-custom-origins! "http://localhost:6274 https://example.com/path")))+ (testing "the setting is left unchanged after a rejected save"+ (is (= "" (mcp.settings/mcp-apps-cors-custom-origins))))))+ (testing "Should accept a bare trailing slash"+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-custom-origins ""]+ (mcp.settings/mcp-apps-cors-custom-origins! "http://localhost:6274/")+ (is (= "http://localhost:6274/" (mcp.settings/mcp-apps-cors-custom-origins)))))+ (testing "Should accept an origin with no path at all"+ (mt/with-temporary-setting-values [mcp.settings/mcp-apps-cors-custom-origins ""]+ (mcp.settings/mcp-apps-cors-custom-origins! "http://localhost:6274 electron://example.com")+ (is (= "http://localhost:6274 electron://example.com" (mcp.settings/mcp-apps-cors-custom-origins))))))diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.cljindex 2d24f5d963c..29915fbe47e 100644--- a/test/metabase/server/middleware/security_test.clj+++ b/test/metabase/server/middleware/security_test.clj@@ -116,11 +116,22 @@"example.com:*" {:protocol nil :domain "example.com" :port "*"}"app://localhost" {:protocol "app" :domain "localhost" :port nil}"capacitor://localhost" {:protocol "capacitor" :domain "localhost" :port nil}))+ (testing "Should parse arbitrary schemes, not just a closed allowlist (MCP clients use e.g. vscode-webview, electron, chrome-extension)"+ (are [url expected] (= expected+ (mw.security/parse-url url))+ "electron://example.com" {:protocol "electron" :domain "example.com" :port nil}+ "vscode-webview://abc123" {:protocol "vscode-webview" :domain "abc123" :port nil}+ "chrome-extension://abc123" {:protocol "chrome-extension" :domain "abc123" :port nil}+ "ftp://example.com" {:protocol "ftp" :domain "example.com" :port nil}))(testing "Should return nil for invalid urls"(are [url] (nil? (mw.security/parse-url url))- "ftp://example.com""://example.com"- "example:com")))+ "example:com"+ ;; scheme must start with a letter (RFC 3986)+ "1abc://example.com"+ ;; domain may not contain characters outside hostname/IPv4/wildcard syntax+ "http://exa mple.com"+ "http://[email protected]")))(deftest ^:parallel test-parse-approved-origins(testing "Should not break on multiple spaces in a row"@@ -128,7 +139,13 @@(is (= 2 (count (mw.security/parse-approved-origins " example.com example.org ")))))(testing "Should filter out invalid origins without throwing"(is (= 1 (count (mw.security/parse-approved-origins "example.org ://example.com"))))- (is (= 1 (count (mw.security/parse-approved-origins "example.org http:/example.com"))))))+ (is (= 1 (count (mw.security/parse-approved-origins "example.org http:/example.com")))))+ (testing "A trailing slash or path should have no effect (#75839)"+ (is (= [{:protocol "http" :domain "localhost" :port "6274"}]+ (mw.security/parse-approved-origins "http://localhost:6274/")))+ (is (= (mw.security/parse-approved-origins "http://localhost:6274")+ (mw.security/parse-approved-origins "http://localhost:6274/")+ (mw.security/parse-approved-origins "http://localhost:6274/some/path")))))(deftest ^:parallel test-approved-domain?(testing "Exact match"@@ -191,6 +208,12 @@(is (mw.security/approved-origin? "https://example3.com" approved))))(testing "Different protocol should fail"(is (not (mw.security/approved-origin? "https://example1.com" "http://example1.com"))))+ (testing "Should allow custom schemes used by MCP clients, not just a closed allowlist"+ (is (mw.security/approved-origin? "electron://example.com" "electron://example.com"))+ (is (mw.security/approved-origin? "chrome-extension://abc123" "chrome-extension://abc123"))+ (is (not (mw.security/approved-origin? "electron://example.com" "app://example.com"))))+ (testing "A trailing slash on an approved origin should have no effect (#75839)"+ (is (mw.security/approved-origin? "http://localhost:6274" "http://localhost:6274/")))(testing "Origins without protocol should accept only http and https"(let [approved "example.com"](is (mw.security/approved-origin? "http://example.com" approved))