Skip to content

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

Allow [::1] and 127.0.0.1 origins by default for cross-origin requests + support IPv6 origins (#68697)

Rodrigo López Dato5486894ab8f8
diff --git a/frontend/src/embedding-sdk-bundle/lib/get-is-localhost.ts b/frontend/src/embedding-sdk-bundle/lib/get-is-localhost.ts
index 25165f12976..85d5339f467 100644
--- a/frontend/src/embedding-sdk-bundle/lib/get-is-localhost.ts
+++ b/frontend/src/embedding-sdk-bundle/lib/get-is-localhost.ts
@@ -1,5 +1,7 @@
export const getIsLocalhost = () => {
const { hostname } = window.location;
- return hostname === "localhost" || hostname === "127.0.0.1";
+ return (
+ hostname === "localhost" || hostname === "127.0.0.1" || hostname === "[::1]"
+ );
};
diff --git a/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts b/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts
index fe205c28a49..ffaa43dd77f 100644
--- a/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts
+++ b/frontend/src/metabase/embedding/embedding-iframe-sdk/embed.ts
@@ -376,7 +376,11 @@ export abstract class MetabaseEmbedElement
console.error("unable to construct the URL:", error);
}
- return hostname === "localhost" || hostname === "127.0.0.1";
+ return (
+ hostname === "localhost" ||
+ hostname === "127.0.0.1" ||
+ hostname === "[::1]"
+ );
}
private _validateEmbedSettings(settings: SdkIframeEmbedElementSettings) {
diff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.clj
index 29b75625f4d..1b97992c7ad 100644
--- a/src/metabase/server/middleware/security.clj
+++ b/src/metabase/server/middleware/security.clj
@@ -64,7 +64,8 @@
[url]
(if (= url "*")
{:protocol nil :domain "*" :port "*"}
- (let [pattern #"^(?:(https?|app|capacitor)://)?([^:/]+)(?::(\d+|\*))?$"
+ ;; Pattern supports both regular hostnames/IPv4 and bracketed IPv6 addresses like [::1]
+ (let [pattern #"^(?:(https?|app|capacitor)://)?(\[[^\]]+\]|[^:/]+)(?::(\d+|\*))?$"
matches (re-matches pattern url)]
(if-not matches
(do (log/errorf "Invalid URL: %s" url) nil)
@@ -224,13 +225,18 @@
(let [urls (str/split approved-origins-raw #" +")]
(keep parse-url urls)))
+(def ^:private loopback-hosts
+ "Set of hostnames/IPs that represent loopback addresses.
+ Note: IPv6 addresses come from parse-url with brackets, e.g. [::1]"
+ #{"localhost" "127.0.0.1" "[::1]"})
+
(defn- localhost-origin?
- "Returns true if the origin is localhost (any port)"
+ "Returns true if the origin is a loopback address (localhost, 127.0.0.1, or ::1) on any port"
[raw-origin]
(when raw-origin
(let [origin (parse-url raw-origin)]
(and origin
- (= (u/lower-case-en (:domain origin)) "localhost")))))
+ (contains? loopback-hosts (u/lower-case-en (:domain origin)))))))
(mu/defn approved-origin?
"Returns true if `origin` should be allowed for CORS based on the `approved-origins`"
diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.clj
index df142789080..f3ea83bfb0e 100644
--- a/test/metabase/server/middleware/security_test.clj
+++ b/test/metabase/server/middleware/security_test.clj
@@ -159,16 +159,15 @@
(is (mw.security/approved-port? "8080" "*"))))
(deftest ^:parallel test-localhost-origin?
- (testing "Should identify localhost origins correctly"
- (is (#'mw.security/localhost-origin? "http://localhost"))
- (is (#'mw.security/localhost-origin? "https://localhost"))
- (is (#'mw.security/localhost-origin? "http://localhost:3000"))
- (is (#'mw.security/localhost-origin? "https://localhost:8080"))
- (is (#'mw.security/localhost-origin? "localhost:3000"))
- (is (#'mw.security/localhost-origin? "localhost")))
- (testing "Should not identify non-localhost origins as localhost"
+ (testing "Should identify loopback origins correctly"
+ (doseq [host @#'mw.security/loopback-hosts]
+ (is (#'mw.security/localhost-origin? (str "http://" host)))
+ (is (#'mw.security/localhost-origin? (str "https://" host)))
+ (is (#'mw.security/localhost-origin? (str "http://" host ":3000")))
+ (is (#'mw.security/localhost-origin? (str host ":8080")))
+ (is (#'mw.security/localhost-origin? host))))
+ (testing "Should not identify non-loopback origins as localhost"
(is (not (#'mw.security/localhost-origin? "http://example.com")))
- (is (not (#'mw.security/localhost-origin? "https://127.0.0.1")))
(is (not (#'mw.security/localhost-origin? "http://sub.localhost.com")))
(is (not (#'mw.security/localhost-origin? nil)))
(is (not (#'mw.security/localhost-origin? "")))))
@@ -177,11 +176,12 @@
(testing "Should return false if parameters are nil"
(is (not (mw.security/approved-origin? nil "example.com")))
(is (not (mw.security/approved-origin? "example.com" nil))))
- (testing "Should always approve localhost origins regardless of approved list"
- (is (mw.security/approved-origin? "http://localhost" ""))
- (is (mw.security/approved-origin? "http://localhost:3000" ""))
- (is (mw.security/approved-origin? "https://localhost:8080" nil))
- (is (mw.security/approved-origin? "localhost:3000" "example.com")))
+ (testing "Should always approve loopback origins regardless of approved list"
+ (doseq [host @#'mw.security/loopback-hosts]
+ (is (mw.security/approved-origin? (str "http://" host) ""))
+ (is (mw.security/approved-origin? (str "http://" host ":3000") ""))
+ (is (mw.security/approved-origin? (str "https://" host ":8080") nil))
+ (is (mw.security/approved-origin? (str host ":3000") "example.com"))))
(testing "Approved origins with exact protocol and port match"
(let [approved "http://example1.com http://example2.com:3000 https://example3.com"]
(is (mw.security/approved-origin? "http://example1.com" approved))
@@ -209,18 +209,21 @@
(is (mw.security/approved-origin? "http://example.com" " fpt://something ://123 4 http://example.com"))))
(deftest test-disable-cors-on-localhost-approved-origin
- (testing "Should approve localhost origins when disable-cors-on-localhost is false"
+ (testing "Should approve loopback origins when disable-cors-on-localhost is false"
(mt/with-temporary-setting-values [disable-cors-on-localhost false]
- (is (mw.security/approved-origin? "http://localhost" ""))
- (is (mw.security/approved-origin? "http://localhost:3000" ""))))
- (testing "Should reject localhost origins when disable-cors-on-localhost is true"
+ (doseq [host @#'mw.security/loopback-hosts]
+ (is (mw.security/approved-origin? (str "http://" host) ""))
+ (is (mw.security/approved-origin? (str "http://" host ":3000") "")))))
+ (testing "Should reject loopback origins when disable-cors-on-localhost is true"
(mt/with-temporary-setting-values [disable-cors-on-localhost true]
- (is (not (mw.security/approved-origin? "http://localhost" "")))
- (is (not (mw.security/approved-origin? "http://localhost:3000" "")))))
- (testing "Should allow localhost origins when explicitly added to approved origins even with disable-cors-on-localhost true"
+ (doseq [host @#'mw.security/loopback-hosts]
+ (is (not (mw.security/approved-origin? (str "http://" host) "")))
+ (is (not (mw.security/approved-origin? (str "http://" host ":3000") ""))))))
+ (testing "Should allow loopback origins when explicitly added to approved origins even with disable-cors-on-localhost true"
(mt/with-temporary-setting-values [disable-cors-on-localhost true]
- (is (mw.security/approved-origin? "http://localhost" "localhost"))
- (is (mw.security/approved-origin? "http://localhost:3000" "localhost:*")))))
+ (doseq [host @#'mw.security/loopback-hosts]
+ (is (mw.security/approved-origin? (str "http://" host) host))
+ (is (mw.security/approved-origin? (str "http://" host ":3000") (str host ":*")))))))
(deftest test-disable-cors-on-localhost-access-control-headers
(testing "Should allow CORS headers for localhost when disable-cors-on-localhost is false"