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 cbcff4059f WordToHtmlConverter: don't link HYPERLINK fields with 
script targets (#1455)
cbcff4059f is described below

commit cbcff4059ffb9eca4143b4fd9089bf2e1f9c4411
Author: Nayyar <[email protected]>
AuthorDate: Fri Oct 23 00:54:52 2026 +0530

    WordToHtmlConverter: don't link HYPERLINK fields with script targets (#1455)
---
 .../poi/hwpf/converter/WordToHtmlConverter.java    |  8 ++-
 .../apache/poi/hwpf/converter/WordToHtmlUtils.java | 41 +++++++++++++++
 .../hwpf/converter/TestWordToHtmlConverter.java    | 61 ++++++++++++++++++++++
 3 files changed, 108 insertions(+), 2 deletions(-)

diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
index a1f3a4fb14..d8bb7dbf53 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlConverter.java
@@ -316,8 +316,12 @@ public class WordToHtmlConverter extends 
AbstractWordConverter
             Element currentBlock, Range textRange, int currentTableLevel,
             String hyperlink )
     {
-        Element basicLink = htmlDocumentFacade.createHyperlink( hyperlink );
-        currentBlock.appendChild( basicLink );
+        // keep the text of a link to e.g. a javascript: target, but don't 
make it a link
+        Element basicLink = currentBlock;
+        if ( WordToHtmlUtils.isSafeHyperlink( hyperlink ) ) {
+            basicLink = htmlDocumentFacade.createHyperlink( hyperlink );
+            currentBlock.appendChild( basicLink );
+        }
 
         if ( textRange != null ) {
             processCharacters( wordDocument, currentTableLevel, textRange,
diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java
 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java
index 84cbec14e8..58bccd1dd8 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToHtmlUtils.java
@@ -16,6 +16,9 @@
 ==================================================================== */
 package org.apache.poi.hwpf.converter;
 
+import java.util.Locale;
+import java.util.Set;
+
 import org.apache.poi.hwpf.usermodel.BorderCode;
 import org.apache.poi.hwpf.usermodel.CharacterRun;
 import org.apache.poi.hwpf.usermodel.Paragraph;
@@ -27,6 +30,9 @@ import org.w3c.dom.Element;
 @Beta
 public class WordToHtmlUtils extends AbstractWordUtils
 {
+    private static final Set<String> SAFE_HYPERLINK_SCHEMES = Set.of(
+            "http", "https", "ftp", "mailto", "file", "news", "tel" );
+
     public static void addBold( final boolean bold, StringBuilder style )
     {
         style.append("font-weight:").append(bold ? "bold" : 
"normal").append(";");
@@ -175,6 +181,41 @@ public class WordToHtmlUtils extends AbstractWordUtils
         return true;
     }
 
+    /**
+     * The target of a HYPERLINK field comes straight from the document and 
ends up in the
+     * {@code href} of a link, where a scheme like {@code javascript:} runs 
script in the page
+     * when the link is followed. Targets without a scheme (relative paths, 
fragments, UNC
+     * paths), Windows drive paths and the common document link schemes are 
accepted. The
+     * scheme is looked up the way browsers parse it, i.e. after dropping tabs 
and line breaks
+     * anywhere and control characters and spaces at the start.
+     */
+    static boolean isSafeHyperlink( final String hyperlink )
+    {
+        StringBuilder target = new StringBuilder( hyperlink.length() );
+        for ( int i = 0; i < hyperlink.length(); i++ )
+        {
+            char c = hyperlink.charAt( i );
+            if ( c != '\t' && c != '\n' && c != '\r' && ( c > ' ' || 
target.length() > 0 ) )
+                target.append( c );
+        }
+
+        // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." ) ":"
+        int end = 0;
+        while ( end < target.length() && isSchemeChar( target.charAt( end ), 
end == 0 ) )
+            end++;
+        // no scheme at all, or a single letter, i.e. a drive like C:\
+        if ( end < 2 || end == target.length() || target.charAt( end ) != ':' )
+            return true;
+        return SAFE_HYPERLINK_SCHEMES.contains( target.substring( 0, end 
).toLowerCase( Locale.ROOT ) );
+    }
+
+    private static boolean isSchemeChar( final char c, final boolean first )
+    {
+        if ( ( c >= 'a' && c <= 'z' ) || ( c >= 'A' && c <= 'Z' ) )
+            return true;
+        return !first && ( ( c >= '0' && c <= '9' ) || c == '+' || c == '-' || 
c == '.' );
+    }
+
     public static void addFontSize( final int fontSize, StringBuilder style )
     {
         style.append("font-size:").append(fontSize).append("pt;");
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java
 
b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java
index 694a116825..fd9d0a3ae5 100644
--- 
a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToHtmlConverter.java
@@ -26,6 +26,7 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.ByteArrayInputStream;
 import java.io.IOException;
+import java.io.InputStream;
 import java.io.StringWriter;
 import java.nio.charset.StandardCharsets;
 import java.time.Duration;
@@ -41,6 +42,7 @@ import org.apache.poi.POIDataSamples;
 import org.apache.poi.POITestCase;
 import org.apache.poi.hwpf.HWPFDocument;
 import org.apache.poi.poifs.filesystem.POIFSFileSystem;
+import org.apache.poi.util.IOUtils;
 import org.apache.poi.util.XMLHelper;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -180,6 +182,41 @@ public class TestWordToHtmlConverter {
         }
     }
 
+    @Test
+    void testScriptHyperlink() throws Exception {
+        // the target of a HYPERLINK field is copied into the href of the 
link, so a
+        // javascript: target ran script in the page when the link was followed
+        try (POIFSFileSystem fs = new 
POIFSFileSystem(SAMPLES.openResourceAsStream("hyperlink.doc"))) {
+            replaceText(fs, "http://testuri.org/";, "javascript:alert(1)");
+
+            try (HWPFDocument hwpfDocument = new HWPFDocument(fs)) {
+                String result = getHtmlText(hwpfDocument, true);
+                assertNotContains(result, "javascript:");
+                assertContains(result, "<span class=\"s1\">Hyperlink 
text</span>");
+            }
+        }
+    }
+
+    @Test
+    void testIsSafeHyperlink() {
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("http://testuri.org/";));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("HTTPS://testuri.org/"));
+        
assertTrue(WordToHtmlUtils.isSafeHyperlink("mailto:[email protected]";));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("file:///C:/docs/a.doc"));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("C:\\docs\\a.doc"));
+        
assertTrue(WordToHtmlUtils.isSafeHyperlink("\\\\server\\share\\a.doc"));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("docs/a.doc"));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("#_Toc1"));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink("a.doc#x:y"));
+        assertTrue(WordToHtmlUtils.isSafeHyperlink(""));
+        assertFalse(WordToHtmlUtils.isSafeHyperlink("javascript:alert(1)"));
+        assertFalse(WordToHtmlUtils.isSafeHyperlink("JavaScript:alert(1)"));
+        assertFalse(WordToHtmlUtils.isSafeHyperlink(" 
\u0001javascript:alert(1)"));
+        
assertFalse(WordToHtmlUtils.isSafeHyperlink("java\tscr\nipt:alert(1)"));
+        assertFalse(WordToHtmlUtils.isSafeHyperlink("vbscript:msgbox(1)"));
+        
assertFalse(WordToHtmlUtils.isSafeHyperlink("data:text/html,<script>alert(1)</script>"));
+    }
+
     @Test
     void testCssFontFamily() {
         assertEquals("Times New Roman", WordToHtmlUtils.cssFontFamily("Times 
New Roman"));
@@ -213,6 +250,30 @@ public class TestWordToHtmlConverter {
         throw new IllegalStateException("font " + oldName + " not found");
     }
 
+    /**
+     * Overwrites every occurrence of a text in the WordDocument stream, in 
place: the new
+     * text must have the same length as the old one.
+     */
+    private static void replaceText(POIFSFileSystem fs, String oldText, String 
newText) throws IOException {
+        byte[] oldBytes = oldText.getBytes(StandardCharsets.UTF_16LE);
+        byte[] newBytes = newText.getBytes(StandardCharsets.UTF_16LE);
+        assertEquals(oldBytes.length, newBytes.length);
+
+        byte[] stream;
+        try (InputStream is = fs.createDocumentInputStream("WordDocument")) {
+            stream = IOUtils.toByteArray(is);
+        }
+        boolean found = false;
+        for (int i = 0; i + oldBytes.length <= stream.length; i++) {
+            if (Arrays.equals(oldBytes, Arrays.copyOfRange(stream, i, i + 
oldBytes.length))) {
+                System.arraycopy(newBytes, 0, stream, i, newBytes.length);
+                found = true;
+            }
+        }
+        assertTrue(found, "text " + oldText + " not found");
+        fs.createOrUpdateDocument(new ByteArrayInputStream(stream), 
"WordDocument");
+    }
+
     private static String getHtmlText(final String sampleFileName, boolean 
emulatePictureStorage) throws Exception {
         try (HWPFDocument hwpfDocument = new 
HWPFDocument(SAMPLES.openResourceAsStream(sampleFileName))) {
             return getHtmlText(hwpfDocument, emulatePictureStorage);


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

Reply via email to