From a08015cefd064006bc75e309c75c2bdd35bc9b81 Mon Sep 17 00:00:00 2001
From: David R Newman <drn@ecs.soton.ac.uk>
Date: Mon, 23 Nov 2026 15:13:06 +0000
Subject: [PATCH] Fixes XXE injection still possible when importing a .docx
 file (#646)

---
 .../pub_lib/plugins/EPrints/Plugin/Import/OpenXML.pm  | 10 +++++-----
 perl_lib/EPrints/XML/LibXML.pm                        | 11 +++++++++--
 2 files changed, 14 insertions(+), 7 deletions(-)

diff --git a/flavours/pub_lib/plugins/EPrints/Plugin/Import/OpenXML.pm b/flavours/pub_lib/plugins/EPrints/Plugin/Import/OpenXML.pm
index cc253b53..ce2470fb 100644
--- a/flavours/pub_lib/plugins/EPrints/Plugin/Import/OpenXML.pm
+++ b/flavours/pub_lib/plugins/EPrints/Plugin/Import/OpenXML.pm
@@ -143,7 +143,7 @@ sub _extract_bibl
 
 		next if !-e "$custom_dir/item$idx.xml";
 
-		my $doc = eval { $xml->parse_file( "$custom_dir/$fn" ) };
+		my $doc = eval { $xml->parse_file( "$custom_dir/$fn", no_expand => 1 ) };
 		next if !defined $doc;
 
 		my( $schemaRef ) = $doc->documentElement->getElementsByTagNameNS(
@@ -158,7 +158,7 @@ sub _extract_bibl
 		next if !defined $uri;
 		next if $uri ne "http://schemas.openxmlformats.org/officeDocument/2006/bibliography";
 
-		$doc = eval { $xml->parse_file( "$custom_dir/item$idx.xml" ) };
+		$doc = eval { $xml->parse_file( "$custom_dir/item$idx.xml", no_expand => 1 ) };
 		next if !defined $doc;
 
 		$self->_extract_references( $bibl_file, $doc );
@@ -294,7 +294,7 @@ sub _parse_dc
 	my $epdata = $opts{epdata};
 	my $xml = $self->{session}->xml;
 
-	my $dom_doc = eval { $xml->parse_file( "$dir/docProps/core.xml" ) };
+	my $dom_doc = eval { $xml->parse_file( "$dir/docProps/core.xml", no_expand => 1 ) };
 	return if !defined $dom_doc;
 	my $root = $dom_doc->documentElement;
 
@@ -328,7 +328,7 @@ sub _parse_dc
 			$values->[0];
 	}
 
-	$dom_doc = eval { $xml->parse_file( "$dir/word/document.xml" ) };
+	$dom_doc = eval { $xml->parse_file( "$dir/word/document.xml", no_expand => 1 ) };
 
 	if( defined $dom_doc )
 	{
@@ -365,7 +365,7 @@ sub _parse_dc
 
 		next if !-e "$custom_dir/item$idx.xml";
 
-		my $dom_doc = eval { $xml->parse_file( "$custom_dir/$fn" ) };
+		my $dom_doc = eval { $xml->parse_file( "$custom_dir/$fn", no_expand => 1 ) };
 		next if !defined $dom_doc;
 	
 		$root = $dom_doc->documentElement;
diff --git a/perl_lib/EPrints/XML/LibXML.pm b/perl_lib/EPrints/XML/LibXML.pm
index 4de5a70a..02ac062e 100644
--- a/perl_lib/EPrints/XML/LibXML.pm
+++ b/perl_lib/EPrints/XML/LibXML.pm
@@ -109,7 +109,7 @@ sub _parse_url
 	my $parser = $PARSER;
 	if ( $no_expand )
 	{
-    $parser = XML::LibXML->new( expand_entities => 0, load_ext_dtd => 0 );
+		$parser = XML::LibXML->new( expand_entities => 0, load_ext_dtd => 0 );
 	}
 
 	return $parser->parse_file( "$url" ) if substr( $url, 0, 6 ) ne "https:";
@@ -134,9 +134,16 @@ sub parse_xml
 		EPrints::abort( "Can't read XML file: '$file'" );
 	}
 
+    # Use non-expanding parser if expressly asked.
+    my $parser = $PARSER;
+	if ( $no_expand )
+    {
+		$parser = XML::LibXML->new( expand_entities => 0, load_ext_dtd => 0 );
+    }
+
 	open(my $fh, "<", $file) or die "Error opening $file: $!";
 	my $doc; 
-	eval { $doc = $PARSER->parse_fh( $fh, $basepath ) };
+	eval { $doc = $parser->parse_fh( $fh, $basepath ) };
 	close($fh);
 
 	return $doc;
