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 SVG handling (#80249)
Nathan Voxlandc24f084ae63c
diff --git a/src/metabase/channel/render/js/svg.clj b/src/metabase/channel/render/js/svg.cljindex 2615cca5a0a..2557b956749 100644--- a/src/metabase/channel/render/js/svg.clj+++ b/src/metabase/channel/render/js/svg.clj@@ -5,6 +5,7 @@protocol function, and rasterize the resulting SVG to PNG bytes via Batik."(:require[clojure.string :as str]+ [hiccup.util :as hiccup.util][metabase.appearance.core :as appearance][metabase.channel.render.image-buffer :as image-buffer][metabase.channel.render.js.protocol :as js.protocol]@@ -81,7 +82,8 @@(let [s (sanitize-svg s)factory (SAXSVGDocumentFactory. "org.apache.xerces.parsers.SAXParser")](with-open [is (ByteArrayInputStream. (.getBytes ^String s StandardCharsets/UTF_8))]- (.createDocument factory "file:///fake.svg" is))))+ ;; The document deliberately gets no base URI so that Batik will not fetch any external references or local files+ (.createDocument factory nil is))))(def ^:dynamic ^:private *svg-render-width*"Width to render svg images. Intentionally large to improve quality. Consumers should be aware and resize as@@ -207,8 +209,12 @@:bell "M14.254 5.105c-7.422.874-8.136 7.388-8.136 11.12 0 4.007 0 5.61-.824 6.411-.549.535-1.647.802-3.294.802v4.006h28v-4.006c-1.647 0-2.47 0-3.294-.802-.55-.534-.824-3.205-.824-8.013-.493-5.763-3.205-8.936-8.136-9.518a2.365 2.365 0 0 0 .725-1.701C18.47 2.076 17.364 1 16 1s-2.47 1.076-2.47 2.404c0 .664.276 1.266.724 1.7zM11.849 29c.383 1.556 1.793 2.333 4.229 2.333s3.845-.777 4.229-2.333h-8.458z"})(defn- icon-svg-string+ "Raw SVG for `icon-name` filled with `color`. Both are escaped: `color` is the whitelabel brand color, which is a free+ string an admin can set to anything, and unescaped it can close the `fill` attribute and inject elements of its own+ (SEC-722)."[icon-name color]- (str "<svg xmlns=\"http://www.w3.org/2000/svg\"><path d=\"" (get icon-paths icon-name) "\" fill=\"" color "\"/></svg>"))+ (str "<svg xmlns=\"http://www.w3.org/2000/svg\"><path d=\"" (hiccup.util/escape-html (get icon-paths icon-name))+ "\" fill=\"" (hiccup.util/escape-html color) "\"/></svg>"))(defn icon"Entrypoint for rendering an SVG icon as a PNG, with a specific color"diff --git a/test/metabase/channel/render/js/svg_test.clj b/test/metabase/channel/render/js/svg_test.cljindex 876e42443ee..fa57df9202d 100644--- a/test/metabase/channel/render/js/svg_test.clj+++ b/test/metabase/channel/render/js/svg_test.clj@@ -6,10 +6,17 @@the svg png renderer does not understand nested html elements so we ensure that there are no divs, spans, etc in theresulting svg."(:require+ [clojure.java.io :as io][clojure.test :refer :all][metabase.channel.render.js.svg :as js.svg])(:import+ (java.awt Color)+ (java.awt.image BufferedImage)+ (java.io ByteArrayInputStream ByteArrayOutputStream File)+ (java.util Base64)+ (javax.imageio ImageIO)(org.apache.batik.anim.dom SVGOMDocument)+ (org.apache.batik.transcoder TranscoderException)(org.w3c.dom Element Node)))(set! *warn-on-reflection* true)@@ -48,6 +55,59 @@(is (= "0.0"(.getAttribute line "fill-opacity")))))+(deftest ^:parallel icon-svg-string-escapes-color-test+ (testing "a color full of xml metacharacters cannot inject markup into the icon svg (SEC-722)"+ (let [payload (str "red\"/><image xmlns:xlink=\"http://www.w3.org/1999/xlink\" "+ "xlink:href=\"file:///etc/passwd\" width=\"33\" height=\"33\"/><path d=\"")+ ^SVGOMDocument document (parse-svg (#'js.svg/icon-svg-string :bell payload))+ ^Element path (.. document (getDocumentElement) (getChildNodes) (item 0))+ nodes (atom [])]+ (#'js.svg/post-process document (fn [^Node node] (swap! nodes conj (.getNodeName node))))+ (is (= ["svg" "path"] @nodes)+ "the payload must not add any elements to the document")+ (is (= payload (.getAttribute path "fill"))+ "the color survives escaping as a plain attribute value"))))++(defn- solid-png-bytes+ "PNG bytes of a 2x2 image filled with `color`."+ ^bytes [^Color color]+ (let [image (BufferedImage. 2 2 BufferedImage/TYPE_INT_RGB)]+ (doseq [x (range 2)+ y (range 2)]+ (.setRGB image x y (.getRGB color)))+ (with-open [os (ByteArrayOutputStream.)]+ (ImageIO/write image "png" os)+ (.toByteArray os))))++(defn- image-svg+ "An svg whose entire canvas is an `<image>` pointing at `href`."+ [href]+ (str "<svg xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" width=\"10\" height=\"10\">"+ "<image xlink:href=\"" href "\" x=\"0\" y=\"0\" width=\"10\" height=\"10\"/></svg>"))++(defn- render-center-pixel+ "Rasterize `svg-string` and return the `[r g b]` of the center pixel, or `::blocked` if Batik refused to render it."+ [svg-string]+ (binding [js.svg/*chart-size* {:width 20 :height 20}]+ (try+ (let [image (ImageIO/read (ByteArrayInputStream. (js.svg/svg-string->bytes svg-string)))+ argb (.getRGB image (quot (.getWidth image) 2) (quot (.getHeight image) 2))]+ [(bit-and (bit-shift-right argb 16) 0xFF)+ (bit-and (bit-shift-right argb 8) 0xFF)+ (bit-and argb 0xFF)])+ (catch TranscoderException _ ::blocked))))++(deftest ^:parallel svg-string->bytes-refuses-external-resources-test+ (testing "an svg may not pull in a resource from outside the document (SEC-722)"+ (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")))+ (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)))]+ (is (= [0 255 0] (render-center-pixel (image-svg data-uri)))))))+(deftest ^:parallel parse-svg-sanitizes-characters-test(testing "Characters discouraged or not permitted by the xml 1.0 specification are removed. (#"(#'js.svg/parse-svg-string