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

commit b66dd87478b0a6455cea03d70bae54659ccb444b
Author: PJ Fanning <[email protected]>
AuthorDate: Thu Oct 22 23:21:38 2026 +0100

    WordToFoConverter: don't link HYPERLINK fields with script targets (#1460)
    
    Apply the scheme check from #1455 to the external-destination of
    fo:basic-link too. A target like javascript: keeps its text but is not
    emitted as a link.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../poi/hwpf/converter/WordToFoConverter.java      |  9 +++++---
 .../poi/hwpf/converter/TestWordToFoConverter.java  | 27 ++++++++++++++++++++++
 .../hwpf/converter/TestWordToHtmlConverter.java    |  2 +-
 3 files changed, 34 insertions(+), 4 deletions(-)

diff --git 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java
 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java
index 7520558f0e..8db4722795 100644
--- 
a/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java
+++ 
b/poi-scratchpad/src/main/java/org/apache/poi/hwpf/converter/WordToFoConverter.java
@@ -340,9 +340,12 @@ public class WordToFoConverter extends 
AbstractWordConverter
             Element currentBlock, Range textRange, int currentTableLevel,
             String hyperlink )
     {
-        Element basicLink = foDocumentFacade
-                .createBasicLinkExternal( 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 = foDocumentFacade.createBasicLinkExternal( hyperlink );
+            currentBlock.appendChild( basicLink );
+        }
 
         if ( textRange != null )
             processCharacters( wordDocument, currentTableLevel, textRange,
diff --git 
a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToFoConverter.java
 
b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToFoConverter.java
index 62c1c955e0..bcca9c1d3d 100644
--- 
a/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToFoConverter.java
+++ 
b/poi-scratchpad/src/test/java/org/apache/poi/hwpf/converter/TestWordToFoConverter.java
@@ -19,6 +19,7 @@
 package org.apache.poi.hwpf.converter;
 
 import static org.apache.poi.POITestCase.assertContains;
+import static org.apache.poi.POITestCase.assertNotContains;
 
 import java.io.StringWriter;
 
@@ -28,6 +29,7 @@ import javax.xml.transform.stream.StreamResult;
 
 import org.apache.poi.POIDataSamples;
 import org.apache.poi.hwpf.HWPFDocument;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
 import org.apache.poi.util.XMLHelper;
 import org.junit.jupiter.api.Test;
 
@@ -41,7 +43,12 @@ public class TestWordToFoConverter
     {
         HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples
                 .getDocumentInstance().openResourceAsStream( sampleFileName ) 
);
+        return getFoText( hwpfDocument );
+    }
 
+    private static String getFoText( final HWPFDocument hwpfDocument )
+            throws Exception
+    {
         WordToFoConverter wordToFoConverter = new WordToFoConverter(
                 XMLHelper.newDocumentBuilder().newDocument() );
         wordToFoConverter.processDocument( hwpfDocument );
@@ -105,6 +112,26 @@ public class TestWordToFoConverter
         assertContains( result, "Hyperlink text" );
     }
 
+    @Test
+    void testScriptHyperlink() throws Exception
+    {
+        // the target of a HYPERLINK field is copied into the 
external-destination of the
+        // link, which a renderer may hand on to a viewer that runs a 
javascript: target
+        try ( POIFSFileSystem fs = new POIFSFileSystem( POIDataSamples
+                .getDocumentInstance().openResourceAsStream( "hyperlink.doc" ) 
) )
+        {
+            TestWordToHtmlConverter.replaceText( fs, "http://testuri.org/";, 
"javascript:alert(1)" );
+
+            try ( HWPFDocument hwpfDocument = new HWPFDocument( fs ) )
+            {
+                String result = getFoText( hwpfDocument );
+                assertNotContains( result, "javascript:" );
+                assertNotContains( result, "external-destination" );
+                assertContains( result, "Hyperlink text" );
+            }
+        }
+    }
+
     @Test
     void testInnerTable() throws Exception
     {
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 fd9d0a3ae5..7bce7bbe29 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
@@ -254,7 +254,7 @@ public class TestWordToHtmlConverter {
      * 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 {
+    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);


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

Reply via email to