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 5c44755a2a XSSFPasswordHelper: parse spinCount as an unsignedInt 
(#1480)
5c44755a2a is described below

commit 5c44755a2a7569a5884dc8f1f680ca3a86fa245f
Author: PJ Fanning <[email protected]>
AuthorDate: Sun Oct 25 09:46:43 2026 +0000

    XSSFPasswordHelper: parse spinCount as an unsignedInt (#1480)
    
    spinCount is an xsd:unsignedInt, so values above Integer.MAX_VALUE made
    Integer.parseInt throw NumberFormatException instead of the
    EncryptedDocumentException used for other out-of-range spin counts.
    Parse as a long and report a non-numeric value as 
EncryptedDocumentException too.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../xssf/usermodel/helpers/XSSFPasswordHelper.java | 10 +++++++--
 .../apache/poi/xssf/usermodel/TestXSSFSheet.java   | 26 ++++++++++++++++++++++
 2 files changed, 34 insertions(+), 2 deletions(-)

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 94a06f5a72..bb3f5a4c7f 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
@@ -124,12 +124,18 @@ public final class XSSFPasswordHelper {
                 byte[] hash1 = dec64.decode(hashVal);
                 HashAlgorithm hashAlgo = HashAlgorithm.fromString(algoName);
                 byte[] salt = dec64.decode(saltVal);
-                int spinCnt = Integer.parseInt(spinCount);
+                // spinCount is an xsd:unsignedInt, so it may not fit in an int
+                long spinCnt;
+                try {
+                    spinCnt = Long.parseLong(spinCount);
+                } catch (NumberFormatException e) {
+                    throw new EncryptedDocumentException("Invalid SpinCount", 
e);
+                }
                 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);
+                byte[] hash2 = CryptoFunctions.hashPassword(password, 
hashAlgo, salt, (int) spinCnt, false);
                 return Arrays.equals(hash1, hash2);
             }
         }
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 f70bed2da6..29dadb5b0d 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
@@ -44,6 +44,8 @@ import java.util.function.Function;
 import java.util.stream.Collectors;
 import java.util.stream.StreamSupport;
 
+import javax.xml.namespace.QName;
+
 import org.apache.commons.io.output.UnsynchronizedByteArrayOutputStream;
 import org.apache.poi.EncryptedDocumentException;
 import org.apache.poi.common.usermodel.HyperlinkType;
@@ -68,6 +70,7 @@ import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.streaming.SXSSFWorkbook;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
+import org.apache.xmlbeans.XmlCursor;
 import org.junit.jupiter.api.Test;
 import org.junit.jupiter.api.Timeout;
 import org.junit.jupiter.params.ParameterizedTest;
@@ -1366,6 +1369,29 @@ public final class TestXSSFSheet extends BaseTestXSheet {
         }
     }
 
+    @Test
+    void protectSheet_spinCountAboveIntRange() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet xs = wb.createSheet();
+            xs.setSheetPassword("test", HashAlgorithm.sha512);
+            // max xsd:unsignedInt, which Integer.parseInt rejects
+            xs.getCTWorksheet().getSheetProtection().setSpinCount(4294967295L);
+            assertThrows(EncryptedDocumentException.class, () -> 
xs.validateSheetPassword("test"));
+        }
+    }
+
+    @Test
+    void protectSheet_spinCountNotANumber() throws IOException {
+        try (XSSFWorkbook wb = new XSSFWorkbook()) {
+            XSSFSheet xs = wb.createSheet();
+            xs.setSheetPassword("test", HashAlgorithm.sha512);
+            try (XmlCursor cur = 
xs.getCTWorksheet().getSheetProtection().newCursor()) {
+                cur.setAttributeText(new QName("spinCount"), "abc");
+            }
+            assertThrows(EncryptedDocumentException.class, () -> 
xs.validateSheetPassword("test"));
+        }
+    }
+
 
     @Test
     void bug49966() throws IOException {


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

Reply via email to