Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,9 @@ 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);
}
if (!StringUtils.isEmpty(writeWorkbookHolder.getPassword())) {
Biff8EncryptionKey.setCurrentUserPassword(writeWorkbookHolder.getPassword());
hssfWorkbook.writeProtectWorkbook(writeWorkbookHolder.getPassword(), StringUtils.EMPTY);
}
return;
case CSV:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,10 @@
import org.apache.fesod.sheet.testkit.models.SimpleData;
import org.apache.fesod.sheet.testkit.params.ExcelFormatSource;
import org.apache.fesod.sheet.write.builder.ExcelWriterBuilder;
import org.apache.poi.EncryptedDocumentException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;

/**
Expand Down Expand Up @@ -104,4 +106,36 @@ 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(EncryptedDocumentException.class, () -> FesodSheet.read(
file, SimpleData.class, new CollectingReadListener<SimpleData>())
.excelType(ExcelTypeEnum.XLS)
.sheet()
.doReadSync());

// Reading with the correct password must succeed
List<SimpleData> dataList = FesodSheet.read(file, SimpleData.class, new CollectingReadListener<SimpleData>())
.excelType(ExcelTypeEnum.XLS)
.password(PASSWORD)
.sheet()
.doReadSync();
Assertions.assertEquals(10, dataList.size());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,10 @@ void test_createWorkBook_XLS_Password() throws IOException {

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

@Test
Expand Down
Loading