You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@fesod.apache.org by "alaahong (via GitHub)" <gi...@apache.org> on 2026/07/19 02:06:31 UTC

[PR] fix: XLS BIFF8 encryption not applied due to premature password clearing [fesod]

alaahong opened a new pull request, #959:
URL: https://github.com/apache/fesod/pull/959

   
   
   <!--
   Thanks very much for contributing to Apache Fesod (Incubating)! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   Closed: #ISSUE
   Related: #ISSUE
   
   -->
   
   ## Purpose of the pull request
   
   The refactor in 7fe23f0 added a finally block in WorkBookUtil.createWorkBook() that clears the Biff8EncryptionKey ThreadLocal immediately after setting it. However, workbook.write() (which applies BIFF8 encryption) runs later in WriteContextImpl.finish(). At that point the password is already null, so the file content is never encrypted - only a write-protection flag is set.
   
   Below two files, XLSX can protect the content, but xls only provide write permission protection.
   [sample-xls-password-buggy.xls](https://github.com/user-attachments/files/30159464/sample-xls-password-buggy.xls)
   <img width="149" height="96" alt="image" src="https://github.com/user-attachments/assets/1fba1a95-67a6-42b9-a153-3c639512b07b" /> 
   
   [sample-xlsx-password-encrypted.xlsx](https://github.com/user-attachments/files/30159465/sample-xlsx-password-encrypted.xlsx)
   <img width="159" height="83" alt="image" src="https://github.com/user-attachments/assets/53d04ab3-b661-4b07-9867-f3df06facc39" />
   
   
   Since code fixed, it also protect the content as below
   <img width="155" height="84" alt="image" src="https://github.com/user-attachments/assets/c0bbf3c6-dca4-4f3d-a016-df9435feba67" />
   
   
   ## What's changed?
   
   
   Fix: Remove the premature clearing. The password is correctly cleared by WriteContextImpl.clearEncrypt03() after workbook.write() completes.
   
   Tests:
   - Update WorkBookUtilTest to verify password remains set after createWorkBook()
   - Add EncryptDataTest.xlsPasswordWrite_isActuallyEncrypted to verify that reading an encrypted XLS without a password fails
   
   
   ## Checklist
   
   - [X] I have read the [Contributor Guide](https://fesod.apache.org/community/contribution/).
   - [X] I have written the necessary doc or comment.
   - [X] I have added the necessary unit tests and all cases have passed.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] fix: XLS BIFF8 encryption not applied due to premature password clearing [fesod]

Posted by "Copilot (via GitHub)" <gi...@apache.org>.
Copilot commented on code in PR #959:
URL: https://github.com/apache/fesod/pull/959#discussion_r3609619218


##########
fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java:
##########
@@ -93,12 +93,8 @@ public static void createWorkBook(WriteWorkbookHolder writeWorkbookHolder) throw
                 writeWorkbookHolder.setCachedWorkbook(hssfWorkbook);
                 writeWorkbookHolder.setWorkbook(hssfWorkbook);
                 if (writeWorkbookHolder.getPassword() != null) {
-                    try {
-                        Biff8EncryptionKey.setCurrentUserPassword(writeWorkbookHolder.getPassword());
-                        hssfWorkbook.writeProtectWorkbook(writeWorkbookHolder.getPassword(), StringUtils.EMPTY);
-                    } finally {
-                        Biff8EncryptionKey.setCurrentUserPassword(null);
-                    }
+                    Biff8EncryptionKey.setCurrentUserPassword(writeWorkbookHolder.getPassword());
+                    hssfWorkbook.writeProtectWorkbook(writeWorkbookHolder.getPassword(), StringUtils.EMPTY);
                 }

Review Comment:
   The new behavior relies on WriteContextImpl.finish() to clear the BIFF8 encryption ThreadLocal, but this block currently sets the password whenever it is non-null (including the empty string). WriteContextImpl.clearEncrypt03() returns early for empty passwords, so an empty password would leave Biff8EncryptionKey set and leak across subsequent writes on the same thread.



##########
fesod-sheet/src/test/java/org/apache/fesod/sheet/readwrite/EncryptDataTest.java:
##########
@@ -104,4 +105,37 @@ private void readAndWrite(
         Assertions.assertEquals(10, dataList.size());
         Assertions.assertNotNull(dataList.get(0).getName());
     }
+
+    /**
+     * Verifies that an XLS file written with a password is actually encrypted at the BIFF8 record level,
+     * not merely flagged as write-protected. Without the correct password, reading the file content
+     * must fail.
+     */
+    @Test
+    void xlsPasswordWrite_isActuallyEncrypted() throws Exception {
+        File file = createTempFile("enc-verify", ExcelFormat.XLS);
+
+        // Write an encrypted XLS file
+        FesodSheet.write(file, SimpleData.class)
+                .excelType(ExcelTypeEnum.XLS)
+                .password(PASSWORD)
+                .sheet()
+                .doWrite(TestDataBuilder.simpleData(10));
+
+        // Reading without the password must fail because the content is BIFF8-encrypted
+        Assertions.assertThrows(
+                Exception.class,
+                () -> FesodSheet.read(file, SimpleData.class, new CollectingReadListener<SimpleData>())
+                        .excelType(ExcelTypeEnum.XLS)
+                        .sheet()
+                        .doReadSync());

Review Comment:
   assertThrows(Exception.class, ...) is very broad and can make this test pass for unrelated failures (e.g., parsing regressions). Since the intent is to verify encryption behavior, assert a more specific failure type such as POI's EncryptedDocumentException when reading the XLS without a password.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] fix: XLS BIFF8 encryption not applied due to premature password clearing [fesod]

Posted by "alaahong (via GitHub)" <gi...@apache.org>.
alaahong commented on PR #959:
URL: https://github.com/apache/fesod/pull/959#issuecomment-5075980882

   > Hi, @alaahong
   > 
   > Regarding the `sample-xls-password-buggy.xls` file you provided, could you please provide the test case code?
   > 
   > Is this related to the operating environment? I haven't been able to reproduce this issue yet.
   > 
   > * Java: OpenJDK1.8
   > * OS: macOS Tahoe(26.5.2)
   > * Fesod Version: 2.0.2-incubating
   > 
   > **Test**
   > 
   > ```java
   > @Test
   > void writeTest() {
   >     File testFile = TestFileUtil.createNewFile(TestFileUtil.getPath() + "/fesod/test.xls");
   > 
   >     FesodSheet.write(testFile, DemoData.class)
   >             .excelType(ExcelTypeEnum.XLS)
   >             .password("1112222")
   >             .sheet()
   >             .doWrite(demoData(2));
   > }
   > ```
   > 
   > **Result** [test.xls](https://github.com/user-attachments/files/30351225/test.xls)
   
   @delei   Thanks for your double checking, here is the sample code, seems get diff result than you provided.
   
   
   
   <img width="1084" height="741" alt="image" src="https://github.com/user-attachments/assets/3ba3fee5-ca3f-45e6-b98e-6143b9bbc6c2" />
   <img width="953" height="614" alt="image" src="https://github.com/user-attachments/assets/d24812f7-0a5d-46f9-b756-bb4be85cb3a0" />
   
   [biff8.xls](https://github.com/user-attachments/files/30365932/biff8.xls)
   
   Java: temurin-25
   OS:  Windows 11 (26200.8875)
   Fesod Version: current main on top of 2.0.2-incubating
   
   Sample Test
   ```
       @Test
       void writeBiff8Test(@TempDir Path tempDir) {
           String fileName = getTempOutputPath(tempDir, "biff8.xls");
           List<DemoData> data = new ArrayList<>();
           for (int i = 0; i < 10; i++) {
               DemoData d = new DemoData();
               d.setString("String" + i);
               d.setDate(new Date());
               d.setDoubleData(0.56);
               data.add(d);
           }
           FesodSheet.write(fileName, DemoData.class)
                   .excelType(ExcelTypeEnum.XLS)
                   .password("1112222")
                   .sheet()
                   .doWrite(data);
   
           System.out.println(fileName);
           System.out.println();
       }
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] fix: XLS BIFF8 encryption not applied due to premature password clearing [fesod]

Posted by "alaahong (via GitHub)" <gi...@apache.org>.
alaahong commented on PR #959:
URL: https://github.com/apache/fesod/pull/959#issuecomment-5076048227

   @delei  it's a funny issue.
   I try to rerun the same code on Mac, it got different file than Windows.
   <img width="1920" height="1080" alt="d6bedb5fca6a08c2470edfd24658b219" src="https://github.com/user-attachments/assets/7a0adb8b-c8fa-4785-8b99-02972ee25ea2" />
   
   
   So, both Windows and Mac open my given buggy file, will show readonly or password.
   But only Windows can generate such file?


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] fix: XLS BIFF8 encryption not applied due to premature password clearing [fesod]

Posted by "delei (via GitHub)" <gi...@apache.org>.
delei merged PR #959:
URL: https://github.com/apache/fesod/pull/959


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org


Re: [PR] fix: XLS BIFF8 encryption not applied due to premature password clearing [fesod]

Posted by "delei (via GitHub)" <gi...@apache.org>.
delei commented on PR #959:
URL: https://github.com/apache/fesod/pull/959#issuecomment-5071001599

   Hi, @alaahong 
   
   Regarding the `sample-xls-password-buggy.xls` file you provided, could you please provide the test case code? 
   
   Is this related to the operating environment? I haven't been able to reproduce this issue yet.
   
   - Java: OpenJDK1.8
   - OS: macOS Tahoe(26.5.2)
   - Fesod Version: 2.0.2-incubating
   
   **Test**
   
   ```java
   @Test
   void writeTest() {
       File testFile = TestFileUtil.createNewFile(TestFileUtil.getPath() + "/fesod/test.xls");
   
       FesodSheet.write(testFile, DemoData.class)
               .excelType(ExcelTypeEnum.XLS)
               .password("1112222")
               .sheet()
               .doWrite(demoData(2));
   }
   ```
   
   **Result**
   [test.xls](https://github.com/user-attachments/files/30351225/test.xls)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@fesod.apache.org
For additional commands, e-mail: commits-help@fesod.apache.org