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
Fix OIDC SSRF: validate all discovery-document URLs before HTTP requests (#72534)
Edward Paget618d6f93dc84
diff --git a/src/metabase/sso/oidc/check.clj b/src/metabase/sso/oidc/check.cljindex c24b9b5987a..9e6fd8f1ec1 100644--- a/src/metabase/sso/oidc/check.clj+++ b/src/metabase/sso/oidc/check.clj@@ -4,8 +4,8 @@Validates OIDC provider configuration by probing the discovery documentand testing client credentials against the token endpoint."(:require- [clj-http.client :as http][metabase.sso.oidc.discovery :as discovery]+ [metabase.sso.oidc.http :as oidc.http][metabase.util.log :as log]))(set! *warn-on-reflection* true)@@ -46,15 +46,11 @@When `:verified` is false, the IdP did not confirm the credentials but didn't reject them either."[token-endpoint client-id client-secret](try- (let [response (http/post token-endpoint- {:form-params {:grant_type "client_credentials"- :client_id client-id- :client_secret client-secret}- :as :json- :coerce :always- :throw-exceptions false- :conn-timeout 5000- :socket-timeout 5000})+ (let [response (oidc.http/oidc-post token-endpoint+ {:form-params {:grant_type "client_credentials"+ :client_id client-id+ :client_secret client-secret}+ :coerce :always})status (:status response)body (:body response)error-code (or (:error body)diff --git a/src/metabase/sso/oidc/discovery.clj b/src/metabase/sso/oidc/discovery.cljindex a4452e5c239..9e14313f0cc 100644--- a/src/metabase/sso/oidc/discovery.clj+++ b/src/metabase/sso/oidc/discovery.clj@@ -4,9 +4,9 @@Fetches and caches OpenID Connect discovery documents from provider issuers.Falls back to manual configuration when discovery is unavailable."(:require- [clj-http.client :as http][clojure.string :as str][java-time.api :as t]+ [metabase.sso.oidc.http :as oidc.http][metabase.sso.settings :as sso.settings][metabase.util.http :as u.http][metabase.util.log :as log]))@@ -45,16 +45,9 @@Returns the parsed JSON document or nil on error."[issuer](let [url (discovery-url issuer)]- (when-not (u.http/valid-host? (sso.settings/oidc-allowed-networks) url)- (throw (ex-info "Invalid issuer URL: address not allowed by network restrictions"- {:url url})))(try(log/infof "Fetching OIDC discovery document from %s" url)- (let [response (http/get url {:as :json- :accept :json- :throw-exceptions false- :conn-timeout 5000- :socket-timeout 5000})]+ (let [response (oidc.http/oidc-get url {:accept :json})](if (= 200 (:status response))(:body response)(do@@ -105,16 +98,21 @@(defn- get-endpoint"Extract an endpoint from the discovery document or manual configuration.+ Validates the endpoint URL against `oidc-allowed-networks`.Parameters:- config: Map containing either :discovery-document or manual endpoint configurations- discovery-key: Key in the discovery document (e.g., :authorization_endpoint)- manual-key: Key in manual configuration (e.g., :authorization-endpoint)- Returns the endpoint URL string or nil."+ Returns the endpoint URL string or nil. Throws if the URL is blocked by network restrictions."[config discovery-key manual-key]- (or (get-in config [:discovery-document discovery-key])- (get config manual-key)))+ (when-let [url (or (get-in config [:discovery-document discovery-key])+ (get config manual-key))]+ (when-not (u.http/valid-host? (sso.settings/oidc-allowed-networks) url)+ (throw (ex-info "OIDC endpoint blocked: address not allowed by network restrictions"+ {:url url :endpoint-key discovery-key})))+ url))(defn get-authorization-endpoint"Get the authorization endpoint from discovery document or manual config.diff --git a/src/metabase/sso/oidc/http.clj b/src/metabase/sso/oidc/http.cljnew file mode 100644index 00000000000..d5be7d851aa--- /dev/null+++ b/src/metabase/sso/oidc/http.clj@@ -0,0 +1,46 @@+(ns metabase.sso.oidc.http+ "Centralized HTTP client for OIDC operations.++ All server-side OIDC HTTP requests should go through this namespace+ to ensure SSRF validation via `oidc-allowed-networks`."+ (:require+ [clj-http.client :as http]+ [metabase.sso.settings :as sso.settings]+ [metabase.util.http :as u.http]+ [metabase.util.log :as log]))++(set! *warn-on-reflection* true)++(def ^:private default-opts+ {:as :json+ :throw-exceptions false+ :conn-timeout 5000+ :socket-timeout 5000})++(defn- validate-url!+ "Validate that a URL is allowed by the current `oidc-allowed-networks` setting.+ Throws `ex-info` if the URL is blocked."+ [url]+ (when-not (u.http/valid-host? (sso.settings/oidc-allowed-networks) url)+ (log/warnf "OIDC request to %s blocked by network restrictions (oidc-allowed-networks = %s)"+ url (sso.settings/oidc-allowed-networks))+ (throw (ex-info "OIDC request blocked: address not allowed by network restrictions"+ {:url url}))))++(defn oidc-get+ "Perform a validated GET request for OIDC operations.+ Validates the URL against `oidc-allowed-networks` before making the request."+ ([url]+ (oidc-get url {}))+ ([url opts]+ (validate-url! url)+ (http/get url (merge default-opts opts))))++(defn oidc-post+ "Perform a validated POST request for OIDC operations.+ Validates the URL against `oidc-allowed-networks` before making the request."+ ([url]+ (oidc-post url {}))+ ([url opts]+ (validate-url! url)+ (http/post url (merge default-opts opts))))diff --git a/src/metabase/sso/oidc/tokens.clj b/src/metabase/sso/oidc/tokens.cljindex bcde59bb262..5dd137229f9 100644--- a/src/metabase/sso/oidc/tokens.clj+++ b/src/metabase/sso/oidc/tokens.clj@@ -3,11 +3,9 @@(:require[buddy.core.keys :as keys][buddy.sign.jwt :as jwt]- [clj-http.client :as http][java-time.api :as t]- [metabase.sso.settings :as sso.settings]+ [metabase.sso.oidc.http :as oidc.http][metabase.util :as u]- [metabase.util.http :as u.http][metabase.util.log :as log]))(set! *warn-on-reflection* true)@@ -44,16 +42,9 @@(defn- fetch-jwks"Fetch JWKS from the given URI. Returns the parsed JWKS map or nil on error."[jwks-uri]- (when-not (u.http/valid-host? (sso.settings/oidc-allowed-networks) jwks-uri)- (throw (ex-info "Invalid JWKS URI: address not allowed by network restrictions"- {:url jwks-uri})))(try(log/infof "Fetching JWKS from %s" jwks-uri)- (-> (http/get jwks-uri {:as :json- :accept :json- :throw-exceptions false- :conn-timeout 5000- :socket-timeout 5000})+ (-> (oidc.http/oidc-get jwks-uri {:accept :json}):body)(catch Exception e(log/warnf e "Failed to fetch JWKS from %s" jwks-uri)diff --git a/src/metabase/sso/providers/oidc.clj b/src/metabase/sso/providers/oidc.cljindex 5b5645a4ec0..7eaadca5e7b 100644--- a/src/metabase/sso/providers/oidc.clj+++ b/src/metabase/sso/providers/oidc.clj@@ -2,10 +2,10 @@"Base OIDC authentication provider. Provides generic OIDC support that concreteimplementations (Auth0, Okta, etc.) can derive from."(:require- [clj-http.client :as http][metabase.auth-identity.core :as auth-identity][metabase.sso.oidc.common :as oidc.common][metabase.sso.oidc.discovery :as oidc.discovery]+ [metabase.sso.oidc.http :as oidc.http][metabase.sso.oidc.schema :as oidc.schema][metabase.sso.oidc.state :as oidc.state][metabase.sso.oidc.tokens :as oidc.tokens]@@ -51,16 +51,12 @@[code config](let [token-endpoint (oidc.discovery/get-token-endpoint config)](try- (let [response (http/post token-endpoint- {:form-params {:grant_type "authorization_code"- :code code- :redirect_uri (:redirect-uri config)- :client_id (:client-id config)- :client_secret (:client-secret config)}- :as :json- :throw-exceptions false- :conn-timeout 5000- :socket-timeout 5000})]+ (let [response (oidc.http/oidc-post token-endpoint+ {:form-params {:grant_type "authorization_code"+ :code code+ :redirect_uri (:redirect-uri config)+ :client_id (:client-id config)+ :client_secret (:client-secret config)}})](if (= 200 (:status response))(oidc.common/parse-token-response (:body response))(dodiff --git a/test/metabase/sso/oidc/discovery_test.clj b/test/metabase/sso/oidc/discovery_test.cljindex 3de055f9552..3e54ac0c23f 100644--- a/test/metabase/sso/oidc/discovery_test.clj+++ b/test/metabase/sso/oidc/discovery_test.clj@@ -206,35 +206,57 @@;;; ================================================== SSRF Protection Tests ==================================================(deftest discover-oidc-configuration-ssrf-protection-test- (testing "Respects oidc-allowed-networks if set"+ (testing "Respects oidc-allowed-networks if set — blocked requests return nil (no HTTP request made)"(oidc.discovery/clear-cache!)(mt/with-temporary-setting-values [oidc-allowed-networks :external-only](testing "Rejects internal addresses (localhost)"(oidc.discovery/clear-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid issuer URL: address not allowed by network restrictions"- (oidc.discovery/discover-oidc-configuration "http://localhost/oidc"))))+ (is (nil? (oidc.discovery/discover-oidc-configuration "http://localhost/oidc"))))(testing "Rejects internal addresses (127.0.0.1)"(oidc.discovery/clear-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid issuer URL: address not allowed by network restrictions"- (oidc.discovery/discover-oidc-configuration "http://127.0.0.1/oidc"))))+ (is (nil? (oidc.discovery/discover-oidc-configuration "http://127.0.0.1/oidc"))))(testing "Rejects cloud metadata endpoint"(oidc.discovery/clear-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid issuer URL: address not allowed by network restrictions"- (oidc.discovery/discover-oidc-configuration "http://169.254.169.254/metadata"))))+ (is (nil? (oidc.discovery/discover-oidc-configuration "http://169.254.169.254/metadata"))))(testing "Rejects private network addresses (192.168.x.x)"(oidc.discovery/clear-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid issuer URL: address not allowed by network restrictions"- (oidc.discovery/discover-oidc-configuration "http://192.168.1.1/oidc"))))+ (is (nil? (oidc.discovery/discover-oidc-configuration "http://192.168.1.1/oidc"))))(testing "Rejects private network addresses (10.x.x.x)"(oidc.discovery/clear-cache!)+ (is (nil? (oidc.discovery/discover-oidc-configuration "http://10.0.0.1/oidc")))))))++;;; ================================================== Endpoint Extraction Validation Tests ==================================================++(deftest get-token-endpoint-blocks-internal-hosts-test+ (testing "get-token-endpoint rejects internal hosts when oidc-allowed-networks is :external-only"+ (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (let [config {:discovery-document {:token_endpoint "http://169.254.169.254/latest/meta-data/"}}]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.discovery/get-token-endpoint config))))))++ (testing "get-token-endpoint allows all hosts when oidc-allowed-networks is :allow-all"+ (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (let [config {:discovery-document {:token_endpoint "https://provider.example.com/token"}}]+ (is (= "https://provider.example.com/token"+ (oidc.discovery/get-token-endpoint config)))))))++(deftest get-authorization-endpoint-blocks-internal-hosts-test+ (testing "get-authorization-endpoint rejects internal hosts"+ (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (let [config {:discovery-document {:authorization_endpoint "http://192.168.1.1/authorize"}}]+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.discovery/get-authorization-endpoint config)))))))++(deftest get-jwks-uri-blocks-internal-hosts-test+ (testing "get-jwks-uri rejects internal hosts"+ (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (let [config {:discovery-document {:jwks_uri "http://10.0.0.1/jwks"}}](is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid issuer URL: address not allowed by network restrictions"- (oidc.discovery/discover-oidc-configuration "http://10.0.0.1/oidc")))))))+ #"address not allowed by network restrictions"+ (oidc.discovery/get-jwks-uri config)))))))diff --git a/test/metabase/sso/oidc/http_test.clj b/test/metabase/sso/oidc/http_test.cljnew file mode 100644index 00000000000..673fd7ff342--- /dev/null+++ b/test/metabase/sso/oidc/http_test.clj@@ -0,0 +1,70 @@+(ns metabase.sso.oidc.http-test+ (:require+ [clj-http.client :as http]+ [clojure.test :refer :all]+ [metabase.sso.oidc.http :as oidc.http]+ [metabase.test :as mt]))++(deftest oidc-get-ssrf-protection-test+ (testing "oidc-get blocks internal hosts when oidc-allowed-networks is :external-only"+ (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (testing "Rejects localhost"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.http/oidc-get "http://localhost/path"))))++ (testing "Rejects cloud metadata endpoint"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.http/oidc-get "http://169.254.169.254/latest/meta-data/"))))++ (testing "Rejects private network addresses"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.http/oidc-get "http://192.168.1.1/path"))))))++ (testing "oidc-get allows requests when oidc-allowed-networks is :allow-all"+ (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (with-redefs [http/get (fn [_url _opts] {:status 200 :body {:ok true}})]+ (let [response (oidc.http/oidc-get "https://provider.example.com/discovery")]+ (is (= 200 (:status response))))))))++(deftest oidc-post-ssrf-protection-test+ (testing "oidc-post blocks internal hosts when oidc-allowed-networks is :external-only"+ (mt/with-temporary-setting-values [oidc-allowed-networks :external-only]+ (testing "Rejects cloud metadata endpoint"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.http/oidc-post "http://169.254.169.254/latest/meta-data/"+ {:form-params {:grant_type "client_credentials"}}))))++ (testing "Rejects loopback"+ (is (thrown-with-msg? clojure.lang.ExceptionInfo+ #"address not allowed by network restrictions"+ (oidc.http/oidc-post "http://127.0.0.1/token"+ {:form-params {:grant_type "client_credentials"}}))))))++ (testing "oidc-post allows requests when oidc-allowed-networks is :allow-all"+ (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (with-redefs [http/post (fn [_url _opts] {:status 200 :body {:access_token "tok"}})]+ (let [response (oidc.http/oidc-post "https://provider.example.com/token"+ {:form-params {:grant_type "client_credentials"}})]+ (is (= 200 (:status response))))))))++(deftest oidc-get-allow-all-test+ (testing "oidc-get allows all hosts when oidc-allowed-networks is :allow-all"+ (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (with-redefs [http/get (fn [_url _opts] {:status 200 :body {}})]+ (is (= 200 (:status (oidc.http/oidc-get "http://192.168.1.1/path"))))))))++(deftest oidc-get-merges-custom-opts-test+ (testing "Custom options are merged with defaults"+ (mt/with-temporary-setting-values [oidc-allowed-networks :allow-all]+ (with-redefs [http/get (fn [_url opts]+ ;; Verify custom opts are present alongside defaults+ (is (= :json (:as opts)))+ (is (= :json (:accept opts)))+ (is (= 5000 (:conn-timeout opts)))+ (is (false? (:throw-exceptions opts)))+ {:status 200 :body {}})]+ (oidc.http/oidc-get "https://example.com/path" {:accept :json})))))diff --git a/test/metabase/sso/oidc/tokens_test.clj b/test/metabase/sso/oidc/tokens_test.cljindex 72e0381f7af..9e98356242f 100644--- a/test/metabase/sso/oidc/tokens_test.clj+++ b/test/metabase/sso/oidc/tokens_test.clj@@ -343,37 +343,27 @@ h0ccjghRm1/Az8L/HL+gdQmtY0NdB4Ml2mZHCVsPYf5WzIirTpjY0EzKDA==;;; ================================================== SSRF Protection Tests ==================================================(deftest get-jwks-ssrf-protection-test- (testing "Respects oidc-allowed-networks if set"+ (testing "Respects oidc-allowed-networks if set — blocked requests return nil (no HTTP request made)"(mt/with-temporary-setting-values [oidc-allowed-networks :external-only](testing "Rejects internal addresses (localhost)"(oidc.tokens/clear-jwks-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid JWKS URI: address not allowed by network restrictions"- (oidc.tokens/get-jwks "http://localhost/jwks"))))+ (is (nil? (oidc.tokens/get-jwks "http://localhost/jwks"))))(testing "Rejects internal addresses (127.0.0.1)"(oidc.tokens/clear-jwks-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid JWKS URI: address not allowed by network restrictions"- (oidc.tokens/get-jwks "http://127.0.0.1/jwks"))))+ (is (nil? (oidc.tokens/get-jwks "http://127.0.0.1/jwks"))))(testing "Rejects cloud metadata endpoint"(oidc.tokens/clear-jwks-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid JWKS URI: address not allowed by network restrictions"- (oidc.tokens/get-jwks "http://169.254.169.254/jwks"))))+ (is (nil? (oidc.tokens/get-jwks "http://169.254.169.254/jwks"))))(testing "Rejects private network addresses (192.168.x.x)"(oidc.tokens/clear-jwks-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid JWKS URI: address not allowed by network restrictions"- (oidc.tokens/get-jwks "http://192.168.1.1/jwks"))))+ (is (nil? (oidc.tokens/get-jwks "http://192.168.1.1/jwks"))))(testing "Rejects private network addresses (10.x.x.x)"(oidc.tokens/clear-jwks-cache!)- (is (thrown-with-msg? clojure.lang.ExceptionInfo- #"Invalid JWKS URI: address not allowed by network restrictions"- (oidc.tokens/get-jwks "http://10.0.0.1/jwks")))))))+ (is (nil? (oidc.tokens/get-jwks "http://10.0.0.1/jwks")))))));;; ================================================== Algorithm Support Tests ==================================================