Skip to content

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

Escape and anchor the www. strip in CSP host allowlist (#81182)

Timofey Kachalov08b90a304c02
diff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.clj
index f91c66c4097..58fafefe479 100644
--- a/src/metabase/server/middleware/security.clj
+++ b/src/metabase/server/middleware/security.clj
@@ -131,20 +131,20 @@
So, we'll double things up and include both the wildcard and non-wildcard entry. We still keep the logic of not adding a wildcard when a
subdomain is already specified because we want to treat this case as the user being more specific and thus intentionally less permissive."
[domain-or-url]
- (let [cleaned-domain (-> domain-or-url
- (str/replace #"/$" "")
- (str/replace #"www." ""))
- {:keys [protocol domain port]} (parse-url cleaned-domain)]
- (when domain
- (let [split-domain (str/split domain #"\.")
+ (let [{:keys [protocol domain port]} (parse-url (str/replace domain-or-url #"/$" ""))
+ ;; Strip only a *leading* `www.` label (anchored, escaped dot). An unescaped, unanchored
+ ;; `#"www."` ate `www` + the next char — mangling `wwwevil.com` and dropping `www2.*` hosts.
+ base-domain (some-> domain (str/replace #"^www\." ""))]
+ (when base-domain
+ (let [split-domain (str/split base-domain #"\.")
new-domains (cond-> (if (= (count split-domain) 2)
- [domain (format "*.%s" domain)]
- [domain])
- (str/includes? domain-or-url "www.") (conj (format "www.%s" domain)))]
+ [base-domain (format "*.%s" base-domain)]
+ [base-domain])
+ (str/starts-with? domain "www.") (conj (format "www.%s" base-domain)))]
(for [new-domain new-domains]
(str (when protocol (format "%s://" protocol))
new-domain
- (when (and port (not= domain "*")) (format ":%s" port))))))))
+ (when (and port (not= base-domain "*")) (format ":%s" port))))))))
(def ^:private always-allowed-iframe-hosts
["'self'"
diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.clj
index 072c9106809..46d9cc5f1b6 100644
--- a/test/metabase/server/middleware/security_test.clj
+++ b/test/metabase/server/middleware/security_test.clj
@@ -102,6 +102,26 @@
(let [hosts (-> (csp-directive "frame-src") (str/split #"\s+") set)]
(is (contains? hosts "'self'") "frame-src hosts does not include 'self'"))))
+(deftest csp-www-strip-keeps-the-admins-registrable-domain-test
+ ;; `add-wildcard-entries` strips a leading `www.` label with a regex whose `.` is unescaped,
+ ;; so it eats `www` + the next character instead of the literal `www.`. An entry of
+ ;; `https://wwwacme.com` must keep the admin's actual domain — not become a different
+ ;; registrable domain (`cme.com`) plus a `*.cme.com` wildcard the admin never entered.
+ (testing "a www<char> host is not rewritten to a different registrable domain + wildcard"
+ (mt/with-temporary-setting-values [allowed-iframe-hosts "https://wwwacme.com"]
+ (let [hosts (-> (csp-directive "frame-src") (str/split #"\s+") set)]
+ (is (contains? hosts "https://wwwacme.com")
+ "the admin's actual domain must be present")
+ (is (not (contains? hosts "https://cme.com"))
+ "a different registrable domain must not appear")
+ (is (not (contains? hosts "https://*.cme.com"))
+ "a wildcard over a domain the admin never entered must not appear"))))
+ (testing "a legitimate www2 host is not silently dropped"
+ (mt/with-temporary-setting-values [allowed-iframe-hosts "https://www2.example.com"]
+ (let [hosts (-> (csp-directive "frame-src") (str/split #"\s+") set)]
+ (is (contains? hosts "https://www2.example.com")
+ "a www2.* entry must survive, not vanish")))))
+
(deftest xframeoptions-header-tests
(mt/with-premium-features #{:embedding}
(testing "`DENY` when embedding is disabled"