Skip to content

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

Validate embedding-app-origins-interactive before it reaches headers (#81166)

Timofey Kachalov49af71fd44e6
diff --git a/src/metabase/server/middleware/security.clj b/src/metabase/server/middleware/security.clj
index ba4cd1ba774..f91c66c4097 100644
--- a/src/metabase/server/middleware/security.clj
+++ b/src/metabase/server/middleware/security.clj
@@ -347,12 +347,29 @@
:media-src ["www.metabase.com"]}]
(format "%s %s; " (name k) (str/join " " vs))))})
+(def ^:private csp-unsafe-char-re
+ "Chars that let an embedding-origin token escape the `frame-ancestors` directive: `;` (starts a
+ directive), `,` (starts a policy), and control chars (CR/LF header injection). Ordinary
+ whitespace is already removed by tokenizing on it."
+ #"[;,\p{Cntrl}]")
+
+(defn- valid-embedding-origins
+ "Drop any embedding-origin token with a CSP-structural char, so the rest can be spliced into
+ `frame-ancestors`/`X-Frame-Options` without injecting a directive. nil if none remain."
+ [raw]
+ (some->> (str/split (or raw "") #"\s+")
+ (remove str/blank?)
+ (remove #(re-find csp-unsafe-char-re %))
+ seq
+ (str/join " ")))
+
(defn- interactive-embedding-origins
- "The configured interactive-embedding app origins, when interactive embedding is
- enabled; otherwise nil."
+ "The configured interactive-embedding app origins (validated to structurally-safe origins),
+ when interactive embedding is enabled; otherwise nil."
[]
(and (setting/get-value-of-type :boolean :enable-embedding-interactive)
- (setting/get-value-of-type :string :embedding-app-origins-interactive)))
+ (valid-embedding-origins
+ (setting/get-value-of-type :string :embedding-app-origins-interactive))))
(defn- frame-ancestors-value
"The `frame-ancestors` CSP source-list for a given framing `mode`:
diff --git a/test/metabase/server/middleware/security_test.clj b/test/metabase/server/middleware/security_test.clj
index 14802271cf5..072c9106809 100644
--- a/test/metabase/server/middleware/security_test.clj
+++ b/test/metabase/server/middleware/security_test.clj
@@ -67,6 +67,29 @@
(is (= "frame-ancestors 'none'"
(csp-directive "frame-ancestors")))))))
+(deftest interactive-embedding-origins-cannot-inject-csp-directives-test
+ ;; `embedding-app-origins-interactive` is admin-writable and, when interactive embedding is
+ ;; on, its value becomes ordinary pages' `frame-ancestors`. It must stay confined to that
+ ;; directive: a `;` in the value must not break out and append further CSP directives. The
+ ;; worst is `script-src-elem` — the base policy omits it, so an injected one is honored and
+ ;; overrides the nonce/hash `script-src` allowlist the app relies on to block XSS.
+ (mt/with-premium-features #{:embedding}
+ (let [csp-directive-names
+ (fn [origins]
+ (mt/with-temporary-setting-values [enable-embedding-interactive true
+ embedding-app-origins-interactive origins]
+ (->> (str/split (get (mw.security/security-headers) "Content-Security-Policy") #";\s*")
+ (map str/trim)
+ (remove str/blank?)
+ (map #(first (str/split % #"\s+")))
+ set)))
+ injection "https://ok.example; script-src-elem https://evil.example"]
+ (testing "a `;` in the setting cannot change which CSP directives are present"
+ (is (= (csp-directive-names "https://ok.example")
+ (csp-directive-names injection))))
+ (testing "and script-src-elem specifically is never introduced"
+ (is (not (contains? (csp-directive-names injection) "script-src-elem")))))))
+
(deftest csp-header-iframe-hosts-tests
(testing "Allowed iframe hosts setting is used in the CSP frame-src directive."
(mt/with-temporary-setting-values [allowed-iframe-hosts "https://www.wikipedia.org, https://www.typescriptlang.org/ https://clojure.org"]