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 fc8f32b93e XSSF/XWPF: cap spinCount when validating protection 
passwords (#1478)
fc8f32b93e is described below

commit fc8f32b93e9d0c4cce7ed1ee722f90575b43a3c8
Author: Nayyar <[email protected]>
AuthorDate: Sun Oct 25 14:54:22 2026 +0530

    XSSF/XWPF: cap spinCount when validating protection passwords (#1478)
    
    cap spinCount when validating xssf/xwpf protection passwords
---
 .../poi/xssf/usermodel/helpers/XSSFPasswordHelper.java     |  6 ++++++
 .../java/org/apache/poi/xwpf/usermodel/XWPFSettings.java   |  6 ++++++
 .../java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java  | 12 ++++++++++++
 .../java/org/apache/poi/xwpf/TestDocumentProtection.java   | 14 ++++++++++++++
 4 files changed, 38 insertions(+)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java
 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java
index ab9caf5d5b..ee9f7c3e5f 100644
--- 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java
+++ 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java
@@ -25,8 +25,10 @@ import java.util.Locale;
 
 import javax.xml.namespace.QName;
 
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.poifs.crypt.CryptoFunctions;
 import org.apache.poi.poifs.crypt.HashAlgorithm;
+import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.RandomSingleton;
 import org.apache.poi.util.StringUtil;
@@ -121,6 +123,10 @@ public final class XSSFPasswordHelper {
                 HashAlgorithm hashAlgo = HashAlgorithm.fromString(algoName);
                 byte[] salt = dec64.decode(saltVal);
                 int spinCnt = Integer.parseInt(spinCount);
+                if (spinCnt < 0 || spinCnt > 
AgileEncryptionVerifier.getMaxSpinCount()) {
+                    throw new EncryptedDocumentException("SpinCount " + 
spinCnt +
+                            " is out of the allowed range [0, " + 
AgileEncryptionVerifier.getMaxSpinCount() + "]");
+                }
                 byte[] hash2 = CryptoFunctions.hashPassword(password, 
hashAlgo, salt, spinCnt, false);
                 return Arrays.equals(hash1, hash2);
             }
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
index 6c01a43b94..e0fbb16270 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
@@ -32,6 +32,7 @@ import org.apache.poi.ooxml.util.POIXMLUnits;
 import org.apache.poi.openxml4j.opc.PackagePart;
 import org.apache.poi.poifs.crypt.CryptoFunctions;
 import org.apache.poi.poifs.crypt.HashAlgorithm;
+import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.RandomSingleton;
 import org.apache.xmlbeans.XmlOptions;
@@ -304,6 +305,11 @@ public class XWPFSettings extends POIXMLDocumentPart {
 
         if (sid == null || hash == null || salt == null || spinCount == null) 
return false;
 
+        if (spinCount.signum() < 0 || 
spinCount.compareTo(BigInteger.valueOf(AgileEncryptionVerifier.getMaxSpinCount()))
 > 0) {
+            throw new EncryptedDocumentException("SpinCount " + spinCount +
+                    " is out of the allowed range [0, " + 
AgileEncryptionVerifier.getMaxSpinCount() + "]");
+        }
+
         HashAlgorithm hashAlgo;
         switch (sid.intValue()) {
             case 1:
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java 
b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
index fcf194fb41..f70bed2da6 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
@@ -45,11 +45,13 @@ import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
 
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.common.usermodel.HyperlinkType;
 import org.apache.poi.hssf.HSSFTestDataSamples;
 import org.apache.poi.ooxml.POIXMLException;
 import org.apache.poi.poifs.crypt.CryptoFunctions;
 import org.apache.poi.poifs.crypt.HashAlgorithm;
+import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier;
 import org.apache.poi.ss.tests.usermodel.BaseTestXSheet;
 import org.apache.poi.ss.usermodel.*;
 import org.apache.poi.ss.util.AreaReference;
@@ -1354,6 +1356,16 @@ public final class TestXSSFSheet extends BaseTestXSheet {
         }
     }
 
+    @Test
+    void protectSheet_spinCountTooLarge() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet xs = wb.createSheet();
+            xs.setSheetPassword("test", HashAlgorithm.sha512);
+            
xs.getCTWorksheet().getSheetProtection().setSpinCount(AgileEncryptionVerifier.getMaxSpinCount()
 + 1L);
+            assertThrows(EncryptedDocumentException.class, () -> 
xs.validateSheetPassword("test"));
+        }
+    }
+
 
     @Test
     void bug49966() throws IOException {
diff --git 
a/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestDocumentProtection.java 
b/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestDocumentProtection.java
index 1f7aca347f..e978ef29e5 100644
--- a/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestDocumentProtection.java
+++ b/poi-ooxml/src/test/java/org/apache/poi/xwpf/TestDocumentProtection.java
@@ -18,15 +18,19 @@ package org.apache.poi.xwpf;
 
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.math.BigInteger;
 
+import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.poifs.crypt.CryptoFunctions;
 import org.apache.poi.poifs.crypt.HashAlgorithm;
+import org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier;
 import org.apache.poi.util.TempFile;
 import org.apache.poi.xwpf.usermodel.XWPFDocument;
 import org.apache.poi.xwpf.usermodel.XWPFParagraph;
@@ -196,4 +200,14 @@ class TestDocumentProtection {
         assertTrue(isValid);
         doc2.close();
     }
+
+    @Test
+    void spinCountTooLarge() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            doc.enforceCommentsProtection("Example", HashAlgorithm.sha512);
+            doc.getSettings().getCTSettings().getDocumentProtection()
+                
.setCryptSpinCount(BigInteger.valueOf(AgileEncryptionVerifier.getMaxSpinCount() 
+ 1L));
+            assertThrows(EncryptedDocumentException.class, () -> 
doc.validateProtectionPassword("Example"));
+        }
+    }
 }


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

Reply via email to