Skip to content

Commit 95f6d80

Browse files
authored
Merge branch 'main' into add-asf-allowlist-check
2 parents b5b9614 + d80dc96 commit 95f6d80

3 files changed

Lines changed: 41 additions & 8 deletions

File tree

fesod-sheet/src/main/java/org/apache/fesod/sheet/util/WorkBookUtil.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,9 @@ public static void createWorkBook(WriteWorkbookHolder writeWorkbookHolder) throw
9292
}
9393
writeWorkbookHolder.setCachedWorkbook(hssfWorkbook);
9494
writeWorkbookHolder.setWorkbook(hssfWorkbook);
95-
if (writeWorkbookHolder.getPassword() != null) {
96-
try {
97-
Biff8EncryptionKey.setCurrentUserPassword(writeWorkbookHolder.getPassword());
98-
hssfWorkbook.writeProtectWorkbook(writeWorkbookHolder.getPassword(), StringUtils.EMPTY);
99-
} finally {
100-
Biff8EncryptionKey.setCurrentUserPassword(null);
101-
}
95+
if (!StringUtils.isEmpty(writeWorkbookHolder.getPassword())) {
96+
Biff8EncryptionKey.setCurrentUserPassword(writeWorkbookHolder.getPassword());
97+
hssfWorkbook.writeProtectWorkbook(writeWorkbookHolder.getPassword(), StringUtils.EMPTY);
10298
}
10399
return;
104100
case CSV:

fesod-sheet/src/test/java/org/apache/fesod/sheet/readwrite/EncryptDataTest.java

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,10 @@
3939
import org.apache.fesod.sheet.testkit.models.SimpleData;
4040
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
4141
import org.apache.fesod.sheet.write.builder.ExcelWriterBuilder;
42+
import org.apache.poi.EncryptedDocumentException;
4243
import org.junit.jupiter.api.Assertions;
4344
import org.junit.jupiter.api.Tag;
45+
import org.junit.jupiter.api.Test;
4446
import org.junit.jupiter.params.ParameterizedTest;
4547

4648
/**
@@ -104,4 +106,36 @@ private void readAndWrite(
104106
Assertions.assertEquals(10, dataList.size());
105107
Assertions.assertNotNull(dataList.get(0).getName());
106108
}
109+
110+
/**
111+
* Verifies that an XLS file written with a password is actually encrypted at the BIFF8 record level,
112+
* not merely flagged as write-protected. Without the correct password, reading the file content
113+
* must fail.
114+
*/
115+
@Test
116+
void xlsPasswordWrite_isActuallyEncrypted() throws Exception {
117+
File file = createTempFile("enc-verify", ExcelFormat.XLS);
118+
119+
// Write an encrypted XLS file
120+
FesodSheet.write(file, SimpleData.class)
121+
.excelType(ExcelTypeEnum.XLS)
122+
.password(PASSWORD)
123+
.sheet()
124+
.doWrite(TestDataBuilder.simpleData(10));
125+
126+
// Reading without the password must fail because the content is BIFF8-encrypted
127+
Assertions.assertThrows(EncryptedDocumentException.class, () -> FesodSheet.read(
128+
file, SimpleData.class, new CollectingReadListener<SimpleData>())
129+
.excelType(ExcelTypeEnum.XLS)
130+
.sheet()
131+
.doReadSync());
132+
133+
// Reading with the correct password must succeed
134+
List<SimpleData> dataList = FesodSheet.read(file, SimpleData.class, new CollectingReadListener<SimpleData>())
135+
.excelType(ExcelTypeEnum.XLS)
136+
.password(PASSWORD)
137+
.sheet()
138+
.doReadSync();
139+
Assertions.assertEquals(10, dataList.size());
140+
}
107141
}

fesod-sheet/src/test/java/org/apache/fesod/sheet/util/WorkBookUtilTest.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ void test_createWorkBook_XLS_Password() throws IOException {
231231

232232
// Verify
233233
Mockito.verify(writeWorkbookHolder).setWorkbook(Mockito.any(HSSFWorkbook.class));
234-
Assertions.assertNull(Biff8EncryptionKey.getCurrentUserPassword());
234+
// The BIFF8 encryption password must remain set after createWorkBook() so that
235+
// workbook.write() (called later in WriteContextImpl.finish()) can apply encryption.
236+
// WriteContextImpl.clearEncrypt03() clears it after writing completes.
237+
Assertions.assertEquals("123456", Biff8EncryptionKey.getCurrentUserPassword());
235238
}
236239

237240
@Test

0 commit comments

Comments
 (0)