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 fe6d94cb9a OOXMLURIDereferencer: check the content type a signature
reference was made for (#1472)
fe6d94cb9a is described below
commit fe6d94cb9a3b60d84c1e4a65be2206d9073f52a4
Author: Nayyar <[email protected]>
AuthorDate: Sun Oct 25 04:21:22 2026 +0530
OOXMLURIDereferencer: check the content type a signature reference was made
for (#1472)
check the signed content type of OOXML signature references
OOXMLURIDereferencer resolved /part?ContentType=... references by path
only, so changing a part's content type in the unsigned
[Content_Types].xml left the signature valid. Compare the ContentType
query with the part's content type and reject the reference on mismatch.
---
.../poi/poifs/crypt/dsig/OOXMLURIDereferencer.java | 12 +++++++
.../poi/poifs/crypt/dsig/TestSignatureInfo.java | 41 ++++++++++++++++++++++
2 files changed, 53 insertions(+)
diff --git
a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
index 2a2c1a461a..ae57aa41ca 100644
---
a/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
+++
b/poi-ooxml/src/main/java/org/apache/poi/poifs/crypt/dsig/OOXMLURIDereferencer.java
@@ -51,6 +51,7 @@ import org.apache.poi.openxml4j.opc.PackagingURIHelper;
public class OOXMLURIDereferencer implements URIDereferencer {
private static final Logger LOG =
PoiLogManager.getLogger(OOXMLURIDereferencer.class);
+ private static final String CONTENT_TYPE_QUERY = "ContentType=";
private SignatureInfo signatureInfo;
private URIDereferencer baseUriDereferencer;
@@ -88,6 +89,17 @@ public class OOXMLURIDereferencer implements URIDereferencer
{
return baseUriDereferencer.dereference(uriReference, context);
}
+ // [Content_Types].xml is not signed, so a signed reference carries
the content type of
+ // the part in its query - the signature only covers the part if the
content type matches
+ String query = uri.getQuery();
+ if (query != null && query.startsWith(CONTENT_TYPE_QUERY)) {
+ String signedContentType =
query.substring(CONTENT_TYPE_QUERY.length());
+ if (!signedContentType.equalsIgnoreCase(part.getContentType())) {
+ throw new URIReferenceException("content type of " +
part.getPartName() + " is " +
+ part.getContentType() + ", but the signature references "
+ signedContentType);
+ }
+ }
+
InputStream dataStream = null;
try {
dataStream = part.getInputStream();
diff --git
a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
index 0cc4e24e16..63cb6c32eb 100644
---
a/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
+++
b/poi-ooxml/src/test/java/org/apache/poi/poifs/crypt/dsig/TestSignatureInfo.java
@@ -26,7 +26,9 @@ package org.apache.poi.poifs.crypt.dsig;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTimeoutPreemptively;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeFalse;
@@ -60,9 +62,13 @@ import java.util.List;
import java.util.Locale;
import java.util.function.BiFunction;
import java.util.function.Supplier;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipInputStream;
+import java.util.zip.ZipOutputStream;
import javax.security.auth.x500.X500Principal;
import javax.xml.crypto.MarshalException;
+import javax.xml.crypto.URIReferenceException;
import javax.xml.crypto.dsig.CanonicalizationMethod;
import javax.xml.crypto.dsig.XMLSignatureException;
import javax.xml.crypto.dsig.dom.DOMSignContext;
@@ -83,6 +89,7 @@ import
org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.openxml4j.opc.PackageAccess;
import org.apache.poi.openxml4j.opc.PackageRelationshipTypes;
+import org.apache.poi.openxml4j.opc.PackagingURIHelper;
import org.apache.poi.poifs.crypt.HashAlgorithm;
import org.apache.poi.poifs.crypt.dsig.DummyKeystore.KeyCertPair;
import org.apache.poi.poifs.crypt.dsig.facets.EnvelopedSignatureFacet;
@@ -352,6 +359,40 @@ class TestSignatureInfo {
}
}
+ @Test
+ void contentTypeChangeInvalidatesSignature() throws Exception {
+ // the signed references carry the part content type as
"?ContentType=...", because
+ // [Content_Types].xml itself is not signed
+ final String docType =
"application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";
+ final String macroType =
"application/vnd.ms-word.document.macroEnabled.main+xml";
+
+ final UnsynchronizedByteArrayOutputStream bos =
UnsynchronizedByteArrayOutputStream.builder().get();
+ try (InputStream is =
testdata.openResourceAsStream("hello-world-signed.docx");
+ ZipInputStream zis = new ZipInputStream(is);
+ ZipOutputStream zos = new ZipOutputStream(bos)) {
+ for (ZipEntry ze; (ze = zis.getNextEntry()) != null; ) {
+ byte[] data = IOUtils.toByteArray(zis);
+ if ("[Content_Types].xml".equals(ze.getName())) {
+ String types = new String(data, StandardCharsets.UTF_8);
+ assertTrue(types.contains(docType));
+ data = types.replace(docType,
macroType).getBytes(StandardCharsets.UTF_8);
+ }
+ zos.putNextEntry(new ZipEntry(ze.getName()));
+ zos.write(data);
+ zos.closeEntry();
+ }
+ }
+
+ try (OPCPackage pkg = OPCPackage.open(bos.toInputStream())) {
+ assertEquals(macroType,
pkg.getPart(PackagingURIHelper.createPartName("/word/document.xml")).getContentType());
+ SignatureInfo si = new SignatureInfo();
+ si.setOpcPackage(pkg);
+ si.setSignatureConfig(new SignatureConfig());
+ EncryptedDocumentException ex =
assertThrows(EncryptedDocumentException.class, si::verifySignature);
+ assertInstanceOf(URIReferenceException.class,
ex.getCause().getCause());
+ }
+ }
+
@Test
void getMultiSigners() throws Exception {
String testFile = "hello-world-signed-twice.docx";
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]