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 8390dfe22d ToHtml example: escape cell text (#1461)
8390dfe22d is described below

commit 8390dfe22d7130d756c01e74c0db18eab9a39fd6
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Oct 22 23:31:09 2026 +0100

    ToHtml example: escape cell text (#1461)
    
    * ToHtml example: escape cell text
    
    The formatted cell value was written into the table cell as it is, so
    markup in a cell, e.g. <script> or a javascript: link, ended up in the
    generated page. Escape it with XMLHelper.escapeXml.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    * ToHtml example: ship excelStyle.css as a resource
    
    The style sheet lived in src/main/java, so neither the Gradle nor the Ant
    build put it in the output and printPage() failed with an NPE when
    loading it. Move it to src/main/resources and add the examples resources
    to the Ant jar.
    
    Co-Authored-By: Claude Opus 5.5 <[email protected]>
    
    ---------
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 build.xml                                          |  4 ++
 .../org/apache/poi/examples/ss/html/ToHtml.java    |  4 +-
 .../org/apache/poi/examples/ss/html/excelStyle.css |  0
 .../apache/poi/examples/ss/html/TestToHtml.java    | 64 ++++++++++++++++++++++
 4 files changed, 71 insertions(+), 1 deletion(-)

diff --git a/build.xml b/build.xml
index caa3ce40bc..3043f692ee 100644
--- a/build.xml
+++ b/build.xml
@@ -133,6 +133,7 @@ under the License.
 
     <!-- Examples: -->
     <property name="examples.src" location="poi-examples/src/main/java"/>
+    <property name="examples.resources.main" 
value="poi-examples/src/main/resources"/>
     <property name="examples.output.dir" 
location="poi-examples/build/classes/ant/java"/>
 
     <!-- OOXML support: -->
@@ -1276,6 +1277,9 @@ under the License.
                 <pathelement path="lib/main"/>
                 <pathelement path="lib/ooxml"/>
             </lib>
+            <resources>
+                <fileset dir="${examples.resources.main}"/>
+            </resources>
         </compile-jar>
     </target>
 
diff --git 
a/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java 
b/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java
index 004400c1ec..b84108ddbc 100644
--- a/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java
+++ b/poi-examples/src/main/java/org/apache/poi/examples/ss/html/ToHtml.java
@@ -48,6 +48,7 @@ import org.apache.poi.ss.usermodel.VerticalAlignment;
 import org.apache.poi.ss.usermodel.Workbook;
 import org.apache.poi.ss.usermodel.WorkbookFactory;
 import org.apache.poi.util.IOUtils;
+import org.apache.poi.util.XMLHelper;
 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
 
 /**
@@ -473,7 +474,8 @@ public final class ToHtml {
                         CellFormat cf = CellFormat.getInstance(
                                 style.getDataFormatString());
                         CellFormatResult result = cf.apply(cell);
-                        content = result.text; //never null
+                        //the text comes from the workbook, so escape it to 
keep markup in it as text
+                        content = XMLHelper.escapeXml(result.text); //never 
null
                         if (content.isEmpty()) {
                             content = "&nbsp;";
                         }
diff --git 
a/poi-examples/src/main/java/org/apache/poi/examples/ss/html/excelStyle.css 
b/poi-examples/src/main/resources/org/apache/poi/examples/ss/html/excelStyle.css
similarity index 100%
rename from 
poi-examples/src/main/java/org/apache/poi/examples/ss/html/excelStyle.css
rename to 
poi-examples/src/main/resources/org/apache/poi/examples/ss/html/excelStyle.css
diff --git 
a/poi-examples/src/test/java/org/apache/poi/examples/ss/html/TestToHtml.java 
b/poi-examples/src/test/java/org/apache/poi/examples/ss/html/TestToHtml.java
new file mode 100644
index 0000000000..58c1a223e9
--- /dev/null
+++ b/poi-examples/src/test/java/org/apache/poi/examples/ss/html/TestToHtml.java
@@ -0,0 +1,64 @@
+/* ====================================================================
+   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.examples.ss.html;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.containsString;
+import static org.hamcrest.Matchers.not;
+
+import java.io.IOException;
+
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
+import org.apache.poi.ss.usermodel.Row;
+import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+import org.junit.jupiter.api.Test;
+
+class TestToHtml {
+    @Test
+    void cellTextIsEscapedHSSF() throws IOException {
+        try (Workbook wb = new HSSFWorkbook()) {
+            assertCellTextIsEscaped(wb);
+        }
+    }
+
+    @Test
+    void cellTextIsEscapedXSSF() throws IOException {
+        try (Workbook wb = new XSSFWorkbook()) {
+            assertCellTextIsEscaped(wb);
+        }
+    }
+
+    private static void assertCellTextIsEscaped(Workbook wb) throws 
IOException {
+        Row row = wb.createSheet().createRow(0);
+        row.createCell(0).setCellValue("<script>alert(1)</script>");
+        row.createCell(1).setCellValue("<a href=\"javascript:alert(1)\">x</a> 
& y");
+
+        StringBuilder sb = new StringBuilder();
+        ToHtml.create(wb, sb).printPage();
+        String html = sb.toString();
+
+        // the base style sheet is included
+        assertThat(html, containsString("<style type=\"text/css\">"));
+        assertThat(html, containsString(".excelDefaults {"));
+        assertThat(html, not(containsString("<script>")));
+        assertThat(html, not(containsString("<a href")));
+        assertThat(html, 
containsString("&lt;script&gt;alert(1)&lt;/script&gt;"));
+        assertThat(html, containsString("&lt;a 
href=&quot;javascript:alert(1)&quot;&gt;x&lt;/a&gt; &amp; y"));
+    }
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to