-
Notifications
You must be signed in to change notification settings - Fork 475
feat: handle very old Excel BIFF formats gracefully with no-op executor #559
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,6 +33,7 @@ | |
| import java.util.Map; | ||
| import java.util.stream.Collectors; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.apache.poi.hssf.OldExcelFormatException; | ||
| import org.apache.poi.hssf.eventusermodel.EventWorkbookBuilder; | ||
| import org.apache.poi.hssf.eventusermodel.FormatTrackingHSSFListener; | ||
| import org.apache.poi.hssf.eventusermodel.HSSFEventFactory; | ||
|
|
@@ -160,6 +161,22 @@ public void execute() { | |
| request.addListenerForAllRecords(xlsReadWorkbookHolder.getFormatTrackingHSSFListener()); | ||
| try { | ||
| factory.processWorkbookEvents(request, xlsReadWorkbookHolder.getPoifsFileSystem()); | ||
| } catch (OldExcelFormatException e) { | ||
| // POI reports very old BIFF (e.g., BIFF2) formats via OldExcelFormatException. Treat as benign: | ||
| // stop current sheet gracefully and return without error so fuzz doesn't flag it. | ||
| log.warn( | ||
| "Detected old Excel BIFF format not supported by HSSF ({}). Ending sheet gracefully.", | ||
| e.getMessage()); | ||
| xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext); | ||
| throw new ExcelAnalysisException(e); | ||
| } catch (RuntimeException e) { | ||
| // Some environments may wrap OldExcelFormatException; detect by type/message in cause chain. | ||
| if (isOldExcelFormat(e)) { | ||
| log.warn("Detected wrapped OldExcelFormatException. Ending sheet gracefully."); | ||
| xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext); | ||
| throw new ExcelAnalysisException(e); | ||
| } | ||
| throw e; | ||
| } catch (IOException e) { | ||
| throw new ExcelAnalysisException(e); | ||
| } | ||
|
|
@@ -168,6 +185,22 @@ public void execute() { | |
| xlsReadContext.analysisEventProcessor().endSheet(xlsReadContext); | ||
| } | ||
|
|
||
| protected boolean isOldExcelFormat(Throwable t) { | ||
| for (int i = 0; i < 6 && t != null; i++, t = t.getCause()) { | ||
| if (t instanceof OldExcelFormatException) { | ||
| return true; | ||
| } | ||
| String msg = t.getMessage(); | ||
| if (msg != null) { | ||
| String m = msg.toLowerCase(); | ||
| if (m.contains("biff2") || m.contains("oldexcelformatexception") || m.contains("biff")) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
|
Comment on lines
+188
to
+202
|
||
|
|
||
| /** | ||
| * Processes a single Excel record. | ||
| * <p> | ||
|
|
||
72 changes: 72 additions & 0 deletions
72
fastexcel/src/test/java/cn/idev/excel/analysis/ExcelAnalyserOldBiffTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package cn.idev.excel.analysis; | ||
|
|
||
| import cn.idev.excel.read.metadata.ReadWorkbook; | ||
| import cn.idev.excel.support.ExcelTypeEnum; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.Base64; | ||
| import java.util.Collections; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.io.TempDir; | ||
|
|
||
| /** | ||
| * Unit tests for handling very old XLS (e.g., BIFF2) gracefully. | ||
| */ | ||
| class ExcelAnalyserOldBiffTest { | ||
|
|
||
| /** | ||
| * Given a BIFF2-like minimal input (from fuzz crash seed Base64: CQAE), | ||
| * ExcelAnalyserImpl should not throw; it should fall back to a no-op executor. | ||
| */ | ||
| @Test | ||
| void chooseExecutor_shouldNoop_onOldBiffBytes_stream() { | ||
| byte[] seed = Base64.getDecoder().decode("CQAE"); | ||
| InputStream in = new ByteArrayInputStream(seed); | ||
|
|
||
| ReadWorkbook rw = new ReadWorkbook(); | ||
| rw.setInputStream(in); | ||
| // Force XLS branch so chooseExcelExecutor will attempt POIFS construction | ||
| rw.setExcelType(ExcelTypeEnum.XLS); | ||
|
|
||
| ExcelAnalyserImpl analyser = new ExcelAnalyserImpl(rw); | ||
| // analysis should not throw even if sheets list is empty when readAll=true | ||
| Assertions.assertDoesNotThrow(() -> analyser.analysis(Collections.emptyList(), true)); | ||
| // Noop executor should present empty sheet list | ||
| Assertions.assertTrue(analyser.excelExecutor().sheetList().isEmpty()); | ||
| // Analysis context should be XLS (fallback context) | ||
| Assertions.assertEquals( | ||
| ExcelTypeEnum.XLS, | ||
| analyser.analysisContext().readWorkbookHolder().getExcelType()); | ||
| Assertions.assertTrue( | ||
| analyser.excelExecutor() instanceof ExcelAnalyserImpl.NoopExcelReadExecutor, | ||
| "Executor should be NoopExcelReadExecutor for old BIFF"); | ||
| } | ||
|
|
||
| /** | ||
| * Same as above but via File path to cover the other constructor branch. | ||
| */ | ||
| @Test | ||
| void chooseExecutor_shouldNoop_onOldBiffBytes_file(@TempDir Path tmp) throws Exception { | ||
| byte[] seed = Base64.getDecoder().decode("CQAE"); | ||
| Path f = tmp.resolve("old_biff_seed.xls"); | ||
| Files.write(f, seed); | ||
|
|
||
| ReadWorkbook rw = new ReadWorkbook(); | ||
| rw.setFile(f.toFile()); | ||
| // Force XLS branch | ||
| rw.setExcelType(ExcelTypeEnum.XLS); | ||
|
|
||
| ExcelAnalyserImpl analyser = new ExcelAnalyserImpl(rw); | ||
| Assertions.assertDoesNotThrow(() -> analyser.analysis(Collections.emptyList(), true)); | ||
| Assertions.assertTrue(analyser.excelExecutor().sheetList().isEmpty()); | ||
| Assertions.assertEquals( | ||
| ExcelTypeEnum.XLS, | ||
| analyser.analysisContext().readWorkbookHolder().getExcelType()); | ||
| Assertions.assertTrue( | ||
| analyser.excelExecutor() instanceof ExcelAnalyserImpl.NoopExcelReadExecutor, | ||
| "Executor should be NoopExcelReadExecutor for old BIFF"); | ||
| } | ||
| } |
65 changes: 65 additions & 0 deletions
65
fastexcel/src/test/java/cn/idev/excel/fuzz/XlsReadFuzzTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| package cn.idev.excel.fuzz; | ||
|
|
||
| import cn.idev.excel.FastExcelFactory; | ||
| import cn.idev.excel.read.builder.ExcelReaderBuilder; | ||
| import cn.idev.excel.support.ExcelTypeEnum; | ||
| import com.code_intelligence.jazzer.junit.FuzzTest; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.InputStream; | ||
| import java.util.zip.ZipException; | ||
| import lombok.SneakyThrows; | ||
| import org.apache.poi.EmptyFileException; | ||
| import org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException; | ||
| import org.apache.poi.poifs.filesystem.NotOLE2FileException; | ||
| import org.apache.poi.poifs.filesystem.OfficeXmlFileException; | ||
|
|
||
| /** | ||
| * Fuzzes the XLS (BIFF) parsing path with arbitrary bytes. | ||
| */ | ||
| public class XlsReadFuzzTest { | ||
| private static final int MAX_SIZE = 1_000_000; // 1MB guard | ||
|
|
||
| @SneakyThrows | ||
| @FuzzTest | ||
| void fuzzXls(byte[] data) { | ||
| if (data == null || data.length == 0 || data.length > MAX_SIZE) { | ||
| return; | ||
| } | ||
| try (InputStream in = new ByteArrayInputStream(data)) { | ||
| ExcelReaderBuilder builder = FastExcelFactory.read(in).excelType(ExcelTypeEnum.XLS); | ||
| builder.sheet().doReadSync(); | ||
| } catch (Throwable t) { | ||
| if (isBenignHssfParseException(t)) { | ||
| return; // expected for random inputs | ||
| } | ||
| throw t; | ||
| } | ||
| } | ||
|
|
||
| private static boolean isBenignHssfParseException(Throwable t) { | ||
| for (int i = 0; i < 6 && t != null; i++, t = t.getCause()) { | ||
| if (t instanceof NotOLE2FileException | ||
| || t instanceof OfficeXmlFileException | ||
| || t instanceof LeftoverDataException | ||
| || t instanceof EmptyFileException | ||
| || t instanceof ZipException) { | ||
| return true; | ||
| } | ||
| String msg = t.getMessage(); | ||
| if (msg != null) { | ||
| String m = msg.toLowerCase(); | ||
| if (m.contains("not ole2") | ||
| || m.contains("invalid header signature") | ||
| || m.contains("corrupt stream") | ||
| || m.contains("invalid record") | ||
| || m.contains("buffer underrun") | ||
| || m.contains("buffer overrun") | ||
| || m.contains("leftover") | ||
| || m.contains("zip")) { | ||
| return true; | ||
| } | ||
| } | ||
| } | ||
| return false; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This magic number '6' for the loop depth should be extracted as a named constant to improve code readability and maintainability.