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 4e986a26bd WordToFoConverter: allow a custom hyperlink validator
(#1464)
4e986a26bd is described below
commit 4e986a26bd94f92f8cde9bae876487fd13ce17d0
Author: PJ Fanning <[email protected]>
AuthorDate: Fri Oct 23 01:55:04 2026 +0100
WordToFoConverter: allow a custom hyperlink validator (#1464)
Same as WordToHtmlConverter.setHyperlinkValidator (#1459): an extra check on
HYPERLINK field targets, applied after WordToHtmlUtils.isSafeHyperlink.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../poi/hwpf/converter/WordToFoConverter.java | 32 +++++++++++++-
.../apache/poi/hwpf/converter/WordToHtmlUtils.java | 5 ++-
.../poi/hwpf/converter/TestWordToFoConverter.java | 49 ++++++++++++++++++++++
3 files changed, 83 insertions(+), 3 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 8db4722795..039481d8d0 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
@@ -23,8 +23,10 @@ import java.io.File;
import java.util.ArrayList;
import java.util.LinkedHashSet;
import java.util.List;
+import java.util.Objects;
import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;
+import java.util.function.Predicate;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.transform.Transformer;
@@ -109,6 +111,8 @@ public class WordToFoConverter extends AbstractWordConverter
private Set<String> usedIds = new LinkedHashSet<>();
+ private Predicate<String> hyperlinkValidator = hyperlink -> true;
+
/**
* Creates new instance of {@link WordToFoConverter}. Can be used for
output
* several {@link HWPFDocument}s into single FO document.
@@ -194,6 +198,32 @@ public class WordToFoConverter extends
AbstractWordConverter
return outputCharactersLanguage;
}
+ /**
+ * @return the extra check a HYPERLINK field target has to pass to be
written as a link
+ * @see #setHyperlinkValidator(Predicate)
+ * @since 6.0.1
+ */
+ public Predicate<String> getHyperlinkValidator()
+ {
+ return hyperlinkValidator;
+ }
+
+ /**
+ * Sets an extra check a HYPERLINK field target has to pass to be written
as the
+ * {@code external-destination} of a link, e.g. to only link to some
hosts. The validator
+ * is only consulted for targets that pass {@link
WordToHtmlUtils#isSafeHyperlink(String)},
+ * so it can reject standard URLs but never let a {@code javascript:} or
other script
+ * target through. The text of a field whose target is rejected is kept,
but it is not
+ * linked. The default accepts every target that passes the built-in check.
+ *
+ * @param hyperlinkValidator returns true for the targets to write as links
+ * @since 6.0.1
+ */
+ public void setHyperlinkValidator( Predicate<String> hyperlinkValidator )
+ {
+ this.hyperlinkValidator = Objects.requireNonNull( hyperlinkValidator,
"hyperlinkValidator" );
+ }
+
@Override
protected void outputCharacters( Element block, CharacterRun characterRun,
String text )
@@ -342,7 +372,7 @@ public class WordToFoConverter extends AbstractWordConverter
{
// 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 ) ) {
+ if ( WordToHtmlUtils.isSafeHyperlink( hyperlink ) &&
hyperlinkValidator.test( hyperlink ) ) {
basicLink = foDocumentFacade.createBasicLinkExternal( hyperlink );
currentBlock.appendChild( basicLink );
}
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 31989cbabf..a876ce3871 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
@@ -189,8 +189,9 @@ public class WordToHtmlUtils extends AbstractWordUtils
* 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.
* <p>
- * {@link WordToHtmlConverter} always applies this check, before any
validator set with
- * {@link
WordToHtmlConverter#setHyperlinkValidator(java.util.function.Predicate)}.
+ * {@link WordToHtmlConverter} and {@link WordToFoConverter} always apply
this check, before
+ * any validator set with {@link
WordToHtmlConverter#setHyperlinkValidator(java.util.function.Predicate)}
+ * or {@link
WordToFoConverter#setHyperlinkValidator(java.util.function.Predicate)}.
*
* @param hyperlink the target of a HYPERLINK field
* @return true if the target can be written as the {@code href} of a link
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 bcca9c1d3d..e6e5f9e70b 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
@@ -22,6 +22,7 @@ import static org.apache.poi.POITestCase.assertContains;
import static org.apache.poi.POITestCase.assertNotContains;
import java.io.StringWriter;
+import java.util.function.Predicate;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.DOMSource;
@@ -48,9 +49,22 @@ public class TestWordToFoConverter
private static String getFoText( final HWPFDocument hwpfDocument )
throws Exception
+ {
+ return getFoText( hwpfDocument, null );
+ }
+
+ /**
+ * @param hyperlinkValidator the validator to set on the converter, or
null for the default
+ */
+ private static String getFoText( final HWPFDocument hwpfDocument,
+ final Predicate<String> hyperlinkValidator ) throws Exception
{
WordToFoConverter wordToFoConverter = new WordToFoConverter(
XMLHelper.newDocumentBuilder().newDocument() );
+ if ( hyperlinkValidator != null )
+ {
+ wordToFoConverter.setHyperlinkValidator( hyperlinkValidator );
+ }
wordToFoConverter.processDocument( hwpfDocument );
StringWriter stringWriter = new StringWriter();
@@ -132,6 +146,41 @@ public class TestWordToFoConverter
}
}
+ @Test
+ void testCustomHyperlinkValidator() throws Exception
+ {
+ // a validator only applies to standard URLs, it can't let a
javascript: target through
+ 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, h -> true );
+ assertNotContains( result, "javascript:" );
+ assertNotContains( result, "external-destination" );
+ assertContains( result, "Hyperlink text" );
+ }
+ }
+
+ try ( HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples
+ .getDocumentInstance().openResourceAsStream( "hyperlink.doc" )
) )
+ {
+ String result = getFoText( hwpfDocument, h -> h.startsWith(
"http://testuri.org/" ) );
+ assertContains( result,
+ "<fo:basic-link
external-destination=\"http://testuri.org/\">" );
+ }
+
+ try ( HWPFDocument hwpfDocument = new HWPFDocument( POIDataSamples
+ .getDocumentInstance().openResourceAsStream( "hyperlink.doc" )
) )
+ {
+ String result = getFoText( hwpfDocument, h -> false );
+ assertNotContains( result, "external-destination" );
+ assertContains( result, "Hyperlink text" );
+ }
+ }
+
@Test
void testInnerTable() throws Exception
{
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]