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
Improve PDF/PNG link handling (#82755)
Nathan Voxland21a74d4d8758
diff --git a/src/metabase/channel/render/png.clj b/src/metabase/channel/render/png.cljindex 567140b2c14..883c2b3e811 100644--- a/src/metabase/channel/render/png.clj+++ b/src/metabase/channel/render/png.clj@@ -11,19 +11,22 @@[hiccup.core :refer [html]][metabase.channel.render.body :as body][metabase.channel.render.style :as style]+ [metabase.util :as u]+ [metabase.util.http :as u.http][metabase.util.log :as log][metabase.util.malli :as mu])(:import- (cz.vutbr.web.css MediaSpec)+ (cz.vutbr.web.css CSSFactory MediaSpec NetworkProcessor)(java.awt Font GraphicsEnvironment Graphics2D RenderingHints)(java.awt.image BufferedImage)- (java.io ByteArrayInputStream ByteArrayOutputStream)+ (java.io ByteArrayInputStream ByteArrayOutputStream IOException)+ (java.net URL)(java.nio.charset StandardCharsets)(javax.imageio ImageIO)(org.fit.cssbox.awt GraphicsEngine)(org.fit.cssbox.css CSSNorm DOMAnalyzer DOMAnalyzer$Origin)- (org.fit.cssbox.io DefaultDOMSource StreamDocumentSource)- (org.fit.cssbox.layout Dimension)+ (org.fit.cssbox.io DefaultDOMSource DefaultDocumentSource DocumentSource StreamDocumentSource)+ (org.fit.cssbox.layout BrowserConfig Dimension)(org.w3c.dom Document)))(set! *warn-on-reflection* true)@@ -44,6 +47,47 @@(.addStyleSheet nil (CSSNorm/formsStyleSheet) DOMAnalyzer$Origin/AGENT).getStyleSheets))+;;; CSSBox's default `BrowserConfig` resolves every `<img src>` with a bare `URL.openConnection`, and a+;;; `view_as: "image"` table cell puts an attacker-chosen query result in that attribute. So the+;;; config below honors only our own inline `data:` chart images plus `https:` through the SSRF-hardened+;;; [[u.http/fetch-bytes]]; anything else throws `IOException`, which CSSBox catches and logs.++(defonce ^{:private true+ :doc "jStyleParser retrieves `<link rel=stylesheet>` and `@import` URLs itself, with a bare+ `URL.openConnection` that [[browser-config]] never sees. Our documents only ever carry+ inline styles, so refuse those outright."+ :arglists '([])} refuse-external-stylesheets!+ (let [refused (delay (CSSFactory/setNetworkProcessor+ (reify NetworkProcessor+ (fetch [_ url]+ (throw (IOException. (str "Refusing to load stylesheet: " url)))))))]+ (fn [] @refused)))++(def ^:private allowed-image-content-types #{"image/png" "image/jpeg" "image/gif"})++(defn- https-image-source+ ^DocumentSource [^URL url]+ (if-let [{:keys [content-type], image-bytes :bytes} (u.http/fetch-bytes+ (str url)+ {:allowed-content-types allowed-image-content-types})]+ (StreamDocumentSource. (ByteArrayInputStream. ^bytes image-bytes) url content-type)+ (throw (IOException. (str "Refusing to load image: " url)))))++(defn- image-document-source+ ^DocumentSource [^URL url]+ (case (some-> url .getProtocol u/lower-case-en)+ "data" (DefaultDocumentSource. url)+ "https" (https-image-source url)+ (throw (IOException. (str "Unsupported image URL scheme: " url)))))++(defn- browser-config+ "CSSBox config whose only route to an image is a `data:` URI or [[u.http/fetch-bytes]]."+ ^BrowserConfig []+ (proxy [BrowserConfig] []+ (createDocumentSource+ ([^URL url] (image-document-source url))+ ([^URL url ^String _content-type] (image-document-source url)))))+(defn- scale-px"`px` scaled by `scale`, rounded *up* to a whole pixel so the scaled box never falls short of thecontent it has to hold."@@ -78,6 +122,7 @@(render-to-png html width 1.0))(^java.awt.image.BufferedImage [^String html width scale](style/register-fonts-if-needed!)+ (refuse-external-stylesheets!)(with-open [is (ByteArrayInputStream. (.getBytes html StandardCharsets/UTF_8))doc-source (StreamDocumentSource. is nil "text/html; charset=utf-8")];; `setupGraphics` runs for the measuring layout and again for the redraw, but a function `scale` can't be@@ -100,6 +145,7 @@RenderingHints/VALUE_TEXT_ANTIALIAS_GASP)(.setRenderingHint RenderingHints/KEY_FRACTIONALMETRICSRenderingHints/VALUE_FRACTIONALMETRICS_ON))))]+ (.setConfig graphics-engine (browser-config))(.createLayout graphics-engine dimension)(let [base (.getImage graphics-engine)viewport (.getViewport graphics-engine)diff --git a/src/metabase/channel/render/table.clj b/src/metabase/channel/render/table.cljindex 08183b31d86..411b90fd835 100644--- a/src/metabase/channel/render/table.clj+++ b/src/metabase/channel/render/table.clj@@ -185,6 +185,12 @@[:td {:style (style/style {:width (format "%s%%" pct-left) :padding "0"})}]]]]])]]]]])(h val)))+(defn- image-url?+ "Whether a `view_as: \"image\"` cell may become an `<img src>`: only http(s), never an attacker-supplied+ `file:`/`javascript:`/`data:`. Matches `(str cell)`, which is what `h` puts in the attribute."+ [cell]+ (boolean (re-matches #"(?i)https?://\S+" (str cell))))+(defn- render-table-body"Renders the body (<tbody>) of an HTML table as a Hiccup data structure.@@ -226,7 +232,8 @@(render-minibar cell (get-in minibar-col [:fingerprint :type :type/Number]) (get col->styles (:name column)));; View as image- (= (get col-settings ::mb.viz/view-as) "image")+ (and (= (get col-settings ::mb.viz/view-as) "image")+ (image-url? cell))[:img {:src (h cell):style (style/style style/view-as-img-style)}]diff --git a/test/metabase/channel/render/js/svg_test.clj b/test/metabase/channel/render/js/svg_test.cljindex fa57df9202d..883065cf8d0 100644--- a/test/metabase/channel/render/js/svg_test.clj+++ b/test/metabase/channel/render/js/svg_test.clj@@ -102,7 +102,9 @@(let [secret (doto ^File (File/createTempFile "metabase-svg-test" ".png") (.deleteOnExit))](io/copy (solid-png-bytes Color/RED) secret)(is (= ::blocked (render-center-pixel (image-svg (str "file://" (.getAbsolutePath secret)))))- "a file: reference must not be read off the server's disk and rasterized into the output")))+ "a file: reference must not be read off the server's disk and rasterized into the output"))+ (is (= ::blocked (render-center-pixel (image-svg "https://example.com/x.png")))+ "nor may an absolute http(s) reference be fetched server-side"))(testing "images embedded in the document itself still render"(let [data-uri (str "data:image/png;base64,"(.encodeToString (Base64/getEncoder) (solid-png-bytes Color/GREEN)))]diff --git a/test/metabase/channel/render/png_test.clj b/test/metabase/channel/render/png_test.cljindex 8001484adb8..6abe1dee39b 100644--- a/test/metabase/channel/render/png_test.clj+++ b/test/metabase/channel/render/png_test.clj@@ -1,13 +1,17 @@(ns metabase.channel.render.png-test(:require+ [clojure.java.io :as io][clojure.test :refer :all][hiccup.core :as hiccup][metabase.channel.render.png :as png]- [metabase.channel.render.style :as style])+ [metabase.channel.render.style :as style]+ [metabase.test :as mt]+ [metabase.util.http :as u.http])(:import- (java.awt Font GraphicsEnvironment)+ (java.awt Color Font GraphicsEnvironment)(java.awt.image BufferedImage)- (java.io ByteArrayInputStream)+ (java.io ByteArrayInputStream ByteArrayOutputStream)+ (java.util Base64)(javax.imageio ImageIO)))(set! *warn-on-reflection* true)@@ -129,3 +133,108 @@(testing (str "scale " factor)(is (= #{255}(edge-alphas (render-with-wrapping content 233 {:channel.render/scale factor})))))))))++(defn- red-png-bytes+ "A 20x20 solid #FF0000 PNG."+ ^bytes []+ (let [img (BufferedImage. 20 20 BufferedImage/TYPE_INT_RGB)]+ (doto (.createGraphics img)+ (.setColor Color/RED)+ (.fillRect 0 0 20 20)+ .dispose)+ (with-open [os (ByteArrayOutputStream.)]+ (ImageIO/write img "png" os)+ (.toByteArray os))))++(defn- red-pixel?+ [rgb]+ (let [r (bit-and (bit-shift-right rgb 16) 0xFF)+ g (bit-and (bit-shift-right rgb 8) 0xFF)+ b (bit-and rgb 0xFF)]+ (and (> r 200) (< g 60) (< b 60))))++(defn- has-red-pixel?+ "Whether any of `img`'s pixels came from the red test image -- i.e. whether the `<img>` actually loaded."+ [^BufferedImage img]+ (boolean (some red-pixel?+ (for [x (range (.getWidth img))+ y (range (.getHeight img))]+ (.getRGB img x y)))))++(defn- render-img+ "Render a lone `<img>` of `src` at its natural 20x20 size."+ ^BufferedImage [src]+ (#'png/render-to-png (str "<html><body style=\"margin: 0; padding: 0;\">"+ "<img src=\"" src "\" width=\"20\" height=\"20\">"+ "</body></html>")+ 100))++(defn- render-with-head+ "Render a 20x20 div, with `head` injected into the document's `<head>`."+ ^BufferedImage [head]+ (#'png/render-to-png (str "<html><head>" head "</head>"+ "<body style=\"margin: 0; padding: 0;\">"+ "<div style=\"width: 20px; height: 20px;\">x</div>"+ "</body></html>")+ 100))++(defn- red-data-uri []+ (str "data:image/png;base64," (.encodeToString (Base64/getEncoder) (red-png-bytes))))++(deftest data-uri-image-renders-test+ (testing "a `data:` image URI -- how our own chart images are embedded -- still renders"+ (is (true? (has-red-pixel? (render-img (red-data-uri)))))))++(deftest file-scheme-image-not-loaded-test+ (testing "a `file:` image URL is never read off the server's disk"+ (mt/with-temp-file [path "sec-872-red.png"]+ (with-open [os (io/output-stream path)]+ (.write os (red-png-bytes)))+ (is (false? (has-red-pixel? (render-img (str "file://" path))))))))++(deftest https-image-goes-through-hardened-fetch-test+ (testing "an `https:` image URL is fetched only through the SSRF-hardened fetcher"+ (let [calls (atom [])]+ (mt/with-dynamic-fn-redefs [u.http/fetch-bytes (fn [url opts]+ (swap! calls conj [url opts])+ {:bytes (red-png-bytes) :content-type "image/png"})]+ (is (true? (has-red-pixel? (render-img "https://example.com/red.png"))))+ (is (= ["https://example.com/red.png"] (mapv first @calls)))+ (is (contains? (:allowed-content-types (second (first @calls))) "image/png"))))))++(deftest non-https-schemes-never-fetched-test+ (testing "no other scheme reaches the network or the filesystem"+ (mt/with-temp-file [path "sec-872-red-2.png"]+ (with-open [os (io/output-stream path)]+ (.write os (red-png-bytes)))+ (doseq [src ["http://example.com/red.png"+ (str "file://" path)+ (str "jar:file://" path "!/red.png")+ "ftp://example.com/red.png"+ "cid:red.png"]]+ (testing src+ (let [calls (atom [])]+ (mt/with-dynamic-fn-redefs [u.http/fetch-bytes (fn [url opts]+ (swap! calls conj [url opts])+ {:bytes (red-png-bytes) :content-type "image/png"})]+ (is (false? (has-red-pixel? (render-img src))))+ (is (= [] @calls)))))))))++(deftest https-image-refused-renders-without-it-test+ (testing "when the hardened fetch refuses the URL the render succeeds with no image"+ (mt/with-dynamic-fn-redefs [u.http/fetch-bytes (constantly nil)]+ (let [^BufferedImage img (render-img "https://10.0.0.1/red.png")]+ (is (false? (has-red-pixel? img)))+ (is (pos? (.getWidth img)))))))++(deftest external-stylesheet-not-loaded-test+ (testing "a stylesheet URL is never fetched -- jStyleParser retrieves those itself, outside the BrowserConfig"+ (mt/with-temp-file [path "sec-872-style.css"]+ (spit path "div { background-color: #FF0000; }")+ (let [url (str "file://" path)]+ (testing "an inline <style> block still applies, so the probe below can tell loading from not loading"+ (is (true? (has-red-pixel? (render-with-head "<style>div { background-color: #FF0000; }</style>")))))+ (testing "<link rel=stylesheet>"+ (is (false? (has-red-pixel? (render-with-head (str "<link rel=\"stylesheet\" href=\"" url "\">"))))))+ (testing "@import inside a <style> block"+ (is (false? (has-red-pixel? (render-with-head (str "<style>@import url(\"" url "\");</style>"))))))))))diff --git a/test/metabase/channel/render/table_test.clj b/test/metabase/channel/render/table_test.cljindex a776498aceb..d3c67c4c618 100644--- a/test/metabase/channel/render/table_test.clj+++ b/test/metabase/channel/render/table_test.clj@@ -344,3 +344,32 @@(let [s (get (table/column->viz-setting-styles columns viz) "desc")](is (contains? s :min-width))(is (not (contains? s :width))))))))++(deftest view-as-image-only-renders-http-urls-test+ (testing "a `view_as: image` cell only becomes an <img> when it holds an http(s) URL"+ (let [columns [{:name "url"}]+ query-results {:cols columns+ :rows [["https://example.com/a.png"]+ ["http://example.com/b.png"]+ ["file:///etc/passwd"]+ ["javascript:alert(1)"]+ ["data:text/html,<script>alert(1)</script>"]]}+ viz-settings {::mb.viz/column-settings {{::mb.viz/column-name "url"} {::mb.viz/view-as "image"}}}+ rendered (#'table/render-table (select-keys query-results [:cols :rows])+ {:col-names ["url"] :cols-for-color-lookup ["url"]}+ (query-results->header+rows query-results)+ columns viz-settings nil)]+ (testing "the http(s) cells, and only those, are images"+ (is (= ["https://example.com/a.png" "http://example.com/b.png"]+ (postwalk-collect (fn [node] (and (vector? node) (= :img (first node))))+ (fn [node] (:src (second node)))+ rendered))))+ (testing "the other cells render as escaped text instead"+ (is (= ["file:///etc/passwd"+ "javascript:alert(1)"+ "data:text/html,<script>alert(1)</script>"]+ (->> (find-table-body rendered)+ (postwalk-collect (fn [node] (and (vector? node) (= :td (first node))))+ last)+ (filter string?)+ (remove #(str/starts-with? % "http")))))))))