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 05cd05e545 Document spin count limit on protection password validation 
(#1479)
05cd05e545 is described below

commit 05cd05e545e1bade669f1ba35081d641cb9b07b0
Author: PJ Fanning <[email protected]>
AuthorDate: Sun Oct 25 09:45:45 2026 +0000

    Document spin count limit on protection password validation (#1479)
    
    Follow-up to #1478: add @throws to the validate*Password methods, note in
    AgileEncryptionVerifier that the max spin count also covers protection
    passwords, and test that a cryptSpinCount wrapping to a small int is 
rejected.
    
    Co-authored-by: Claude Opus 5.5 <[email protected]>
---
 .../main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java    |  2 ++
 .../main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java |  4 ++++
 .../apache/poi/xssf/usermodel/helpers/XSSFPasswordHelper.java |  2 ++
 .../main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java |  2 ++
 .../main/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java |  2 ++
 .../test/java/org/apache/poi/xwpf/TestDocumentProtection.java | 11 +++++++++++
 .../apache/poi/poifs/crypt/agile/AgileEncryptionVerifier.java |  9 ++++++---
 7 files changed, 29 insertions(+), 3 deletions(-)

diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
index ffc1a2401b..6148e9976c 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
@@ -1465,6 +1465,8 @@ public class XSSFSheet extends POIXMLDocumentPart 
implements Sheet, OoxmlSheetEx
      * Validate the password against the stored hash, the hashing method will 
be determined
      *  by the existing password attributes
      * @return true, if the hashes match (... though original password may 
differ ...)
+     * @throws org.apache.poi.EncryptedDocumentException if the stored spin 
count is outside the range allowed by
+     *  {@link 
org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier#getMaxSpinCount()}
      */
     public boolean validateSheetPassword(String password) {
         if (!isSheetProtectionEnabled()) {
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java 
b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
index d2ed147da3..2e06ba6f7c 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xssf/usermodel/XSSFWorkbook.java
@@ -2310,6 +2310,8 @@ public class XSSFWorkbook extends POIXMLDocument 
implements Workbook, Date1904Su
      * Validate the password against the stored hash, the hashing method will 
be determined
      *  by the existing password attributes
      * @return true, if the hashes match (... though original password may 
differ ...)
+     * @throws org.apache.poi.EncryptedDocumentException if the stored spin 
count is outside the range allowed by
+     *  {@link 
org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier#getMaxSpinCount()}
      */
     public boolean validateWorkbookPassword(String password) {
         if (!workbookProtectionPresent()) {
@@ -2336,6 +2338,8 @@ public class XSSFWorkbook extends POIXMLDocument 
implements Workbook, Date1904Su
      * Validate the password against the stored hash, the hashing method will 
be determined
      *  by the existing password attributes
      * @return true if the hashes match (... though original password may 
differ ...)
+     * @throws org.apache.poi.EncryptedDocumentException if the stored spin 
count is outside the range allowed by
+     *  {@link 
org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier#getMaxSpinCount()}
      */
     public boolean validateRevisionsPassword(String password) {
         if (!workbookProtectionPresent()) {
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 ee9f7c3e5f..94a06f5a72 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
@@ -97,6 +97,8 @@ public final class XSSFPasswordHelper {
      * @param prefix the prefix of the password attributes, may be null
      * 
      * @return true, if the hashes match
+     * @throws EncryptedDocumentException if the stored spin count is outside 
the range allowed by
+     *  {@link AgileEncryptionVerifier#getMaxSpinCount()}
      */
     public static boolean validatePassword(XmlObject xobj, String password, 
String prefix) {
         // TODO: is "velvetSweatshop" the default password?
diff --git 
a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java 
b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
index 4493742829..c99b752ca4 100644
--- a/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
+++ b/poi-ooxml/src/main/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
@@ -1445,6 +1445,8 @@ public class XWPFDocument extends POIXMLDocument 
implements Document, IBody {
      * Validates the existing password
      *
      * @return true, only if password was set and equals, false otherwise
+     * @throws org.apache.poi.EncryptedDocumentException if the stored spin 
count is outside the range allowed by
+     *  {@link 
org.apache.poi.poifs.crypt.agile.AgileEncryptionVerifier#getMaxSpinCount()}
      */
     public boolean validateProtectionPassword(String password) {
         return settings.validateProtectionPassword(password);
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 e0fbb16270..873d9f2393 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
@@ -296,6 +296,8 @@ public class XWPFSettings extends POIXMLDocumentPart {
      *
      * @param password the password
      * @return true, only if password was set and equals, false otherwise
+     * @throws EncryptedDocumentException if the stored spin count is outside 
the range allowed by
+     *  {@link AgileEncryptionVerifier#getMaxSpinCount()}
      */
     public boolean validateProtectionPassword(String password) {
         BigInteger sid = safeGetDocumentProtection().getCryptAlgorithmSid();
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 e978ef29e5..4323bb66f6 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
@@ -210,4 +210,15 @@ class TestDocumentProtection {
             assertThrows(EncryptedDocumentException.class, () -> 
doc.validateProtectionPassword("Example"));
         }
     }
+
+    @Test
+    void spinCountOverflowsInt() throws IOException {
+        try (XWPFDocument doc = new XWPFDocument()) {
+            doc.enforceCommentsProtection("Example", HashAlgorithm.sha512);
+            // 2^32 + 100000 wraps to 100000 (the spin count actually used) 
when narrowed to int
+            BigInteger spinCount = 
BigInteger.ONE.shiftLeft(32).add(BigInteger.valueOf(100000));
+            
doc.getSettings().getCTSettings().getDocumentProtection().setCryptSpinCount(spinCount);
+            assertThrows(EncryptedDocumentException.class, () -> 
doc.validateProtectionPassword("Example"));
+        }
+    }
 }
diff --git 
a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionVerifier.java
 
b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionVerifier.java
index e63ac9f848..899a0b17ec 100644
--- 
a/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionVerifier.java
+++ 
b/poi/src/main/java/org/apache/poi/poifs/crypt/agile/AgileEncryptionVerifier.java
@@ -31,7 +31,8 @@ public class AgileEncryptionVerifier extends 
EncryptionVerifier {
 
     /**
      * Maximum allowed spinCount to prevent CPU-exhaustion DoS via crafted 
documents.
-     * Values beyond this are rejected during parsing.
+     * Values beyond this are rejected when parsing agile encryption info and 
when validating
+     * XSSF sheet/workbook protection and XWPF document protection passwords.
      */
     private static int MAX_SPIN_COUNT = DEFAULT_MAX_SPIN_COUNT;
 
@@ -40,7 +41,8 @@ public class AgileEncryptionVerifier extends 
EncryptionVerifier {
 
     /**
      * Maximum allowed spinCount to prevent CPU-exhaustion DoS via crafted 
documents.
-     * Values beyond this are rejected during parsing.
+     * Values beyond this are rejected when parsing agile encryption info and 
when validating
+     * XSSF sheet/workbook protection and XWPF document protection passwords.
      * @since 6.0.0
      */
     public static int getMaxSpinCount() {
@@ -49,7 +51,8 @@ public class AgileEncryptionVerifier extends 
EncryptionVerifier {
 
     /**
      * Maximum allowed spinCount to prevent CPU-exhaustion DoS via crafted 
documents.
-     * Values beyond this are rejected during parsing.
+     * Values beyond this are rejected when parsing agile encryption info and 
when validating
+     * XSSF sheet/workbook protection and XWPF document protection passwords.
      * @since 6.0.0
      */
     public static void setMaxSpinCount(final int maxSpinCount) {


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

Reply via email to