This is an automated email from the ASF dual-hosted git repository.
pjfanning pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/poi.git
The following commit(s) were added to refs/heads/trunk by this push:
new 41a1bc2f0e SVGImageRenderer: only load embedded resources from SVG
pictures (#1452)
41a1bc2f0e is described below
commit 41a1bc2f0e0c1e6b7b5611c0e54244d4a16b8ec2
Author: Nayyar <[email protected]>
AuthorDate: Fri Oct 23 04:41:23 2026 +0530
SVGImageRenderer: only load embedded resources from SVG pictures (#1452)
* SVGImageRenderer: only load embedded resources from SVG pictures
* SVGImageRenderer: report blocked SVG references as IOException
Batik's BridgeException is a RuntimeException, so a refused or broken
external reference aborted the whole slide instead of skipping the picture.
Also skip TestSVGImageRenderer when Batik isn't on the module path (Ant
build)
and drop a stale TODO - Batik already parses securely.
Co-Authored-By: Claude Opus 5.5 <[email protected]>
---------
Co-authored-by: PJ Fanning <[email protected]>
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/poi/xslf/draw/SVGImageRenderer.java | 9 ++-
.../org/apache/poi/xslf/draw/SVGUserAgent.java | 12 ++++
.../apache/poi/xslf/draw/TestSVGImageRenderer.java | 84 ++++++++++++++++++++++
3 files changed, 103 insertions(+), 2 deletions(-)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGImageRenderer.java
b/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGImageRenderer.java
index 7b99086dd3..e6ce2e8b8b 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGImageRenderer.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGImageRenderer.java
@@ -30,6 +30,7 @@ import java.lang.ref.WeakReference;
import org.apache.batik.anim.dom.SAXSVGDocumentFactory;
import org.apache.batik.bridge.BridgeContext;
+import org.apache.batik.bridge.BridgeException;
import org.apache.batik.bridge.DocumentLoader;
import org.apache.batik.bridge.GVTBuilder;
import org.apache.batik.ext.awt.RenderingHintsKeyExt;
@@ -52,7 +53,6 @@ public class SVGImageRenderer implements ImageRenderer {
public SVGImageRenderer() {
String parser = XMLResourceDescriptor.getXMLParserClassName();
- // TOOO: tell the batik guys to use secure parsing feature
svgFact = new SAXSVGDocumentFactory(parser);
SVGUserAgent agent = new SVGUserAgent();
@@ -65,7 +65,12 @@ public class SVGImageRenderer implements ImageRenderer {
public void loadImage(InputStream data, String contentType) throws
IOException {
SVGDocument document = (SVGDocument)svgFact.createDocument("", data);
((SVGUserAgent)context.getUserAgent()).initViewbox(document);
- builder.build(context, document);
+ try {
+ builder.build(context, document);
+ } catch (BridgeException e) {
+ // e.g. a blocked or broken external reference - skip this picture
instead of the whole slide
+ throw new IOException("Unable to render SVG image", e);
+ }
}
@Override
diff --git a/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGUserAgent.java
b/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGUserAgent.java
index 4fb1f6cde1..b061d28e90 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGUserAgent.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xslf/draw/SVGUserAgent.java
@@ -20,12 +20,15 @@ package org.apache.poi.xslf.draw;
import java.awt.geom.Dimension2D;
import java.awt.geom.Rectangle2D;
+import org.apache.batik.bridge.EmbededExternalResourceSecurity;
+import org.apache.batik.bridge.ExternalResourceSecurity;
import org.apache.batik.bridge.UserAgentAdapter;
import org.apache.batik.bridge.ViewBox;
import org.apache.batik.parser.DefaultLengthHandler;
import org.apache.batik.parser.LengthHandler;
import org.apache.batik.parser.LengthParser;
import org.apache.batik.parser.ParseException;
+import org.apache.batik.util.ParsedURL;
import org.apache.batik.util.SVGConstants;
import org.apache.logging.log4j.Logger;
import org.apache.poi.logging.PoiLogManager;
@@ -95,6 +98,15 @@ public class SVGUserAgent extends UserAgentAdapter {
return val[0];
}
+ /**
+ * The SVG comes from inside the document and has no location of its own,
+ * so only allow resources embedded as data: URLs and not local files
+ */
+ @Override
+ public ExternalResourceSecurity getExternalResourceSecurity(ParsedURL
resourceURL, ParsedURL docURL) {
+ return new EmbededExternalResourceSecurity(resourceURL);
+ }
+
@Override
public void displayMessage(String message) {
LOG.atInfo().log(message);
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/xslf/draw/TestSVGImageRenderer.java
b/poi-ooxml/src/test/java/org/apache/poi/xslf/draw/TestSVGImageRenderer.java
new file mode 100644
index 0000000000..f525747240
--- /dev/null
+++ b/poi-ooxml/src/test/java/org/apache/poi/xslf/draw/TestSVGImageRenderer.java
@@ -0,0 +1,84 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+
+package org.apache.poi.xslf.draw;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.junit.jupiter.api.Assumptions.assumeFalse;
+
+import java.awt.image.BufferedImage;
+import java.io.File;
+import java.io.IOException;
+import java.nio.charset.StandardCharsets;
+import java.nio.file.Files;
+import java.util.Base64;
+
+import org.apache.poi.util.Dimension2DDouble;
+import org.apache.poi.util.TempFile;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.Test;
+
+class TestSVGImageRenderer {
+ private static final String SVG_START =
+ "<svg xmlns='http://www.w3.org/2000/svg'
xmlns:xlink='http://www.w3.org/1999/xlink' width='10' height='10'>";
+ private static final String RED_RECT =
+ SVG_START + "<rect id='r' width='10' height='10' fill='red'/></svg>";
+
+ @BeforeAll
+ static void checkBatik() {
+ try {
+ Class.forName("org.apache.batik.bridge.BridgeContext");
+ } catch (ClassNotFoundException | NoClassDefFoundError e) {
+ assumeFalse(true, "Batik isn't available on the module-path");
+ }
+ }
+
+ @Test
+ void externalFileReferencesAreNotLoaded() throws IOException {
+ File local = TempFile.createTempFile("local", ".svg");
+ try {
+ Files.write(local.toPath(),
RED_RECT.getBytes(StandardCharsets.UTF_8));
+ String url = local.toURI().toString();
+
+ assertThrows(IOException.class, () ->
+ render(SVG_START + "<use xlink:href='" + url + "#r'/></svg>"));
+ assertThrows(IOException.class, () ->
+ render(SVG_START + "<image width='10' height='10'
xlink:href='" + url + "'/></svg>"));
+ } finally {
+ assertTrue(local.delete());
+ }
+ }
+
+ @Test
+ void embeddedReferencesAreLoaded() throws IOException {
+ assertEquals(0xffff0000, render(SVG_START + "<defs><rect id='r'
width='10' height='10' fill='red'/></defs>" +
+ "<use xlink:href='#r'/></svg>").getRGB(5, 5));
+
+ String data = "data:image/svg+xml;base64," +
+
Base64.getEncoder().encodeToString(RED_RECT.getBytes(StandardCharsets.UTF_8));
+ assertEquals(0xffff0000, render(SVG_START + "<image width='10'
height='10' xlink:href='" + data + "'/></svg>")
+ .getRGB(5, 5));
+ }
+
+ private static BufferedImage render(String svg) throws IOException {
+ SVGImageRenderer renderer = new SVGImageRenderer();
+ renderer.loadImage(svg.getBytes(StandardCharsets.UTF_8),
"image/svg+xml");
+ return renderer.getImage(new Dimension2DDouble(10, 10));
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]