🐛 Bug Report
Description
File:
exceljs/lib/xlsx/xform/style/styles-xform.js
Function:
getStyleModel(id)
When reading certain corrupted or malformed Excel files, the getStyleModel(id) function throws runtime errors because some expected properties are missing.
Specifically:
-
this.model does not always contain a styles property, so:
throws TypeError: Cannot read properties of undefined.
-
this.index does not always contain a model property, so:
also throws a TypeError.
Because of this, execution stops instead of handling the missing style gracefully.
Steps To Reproduce
I cannot share the original file because it contains sensitive work data.
The issue occurs when using WorkbookReader to stream-read a file:
while await (const row of workbookReader) {
// process row
}
While processing rows, the reader internally calls getStyleModel(id), which throws when styles or model is undefined.
Basic workbook usage works normally:
const wb = new ExcelJS.Workbook();
const ws = wb.addWorksheet('XYZ');
ws.getCell('A1').value = 7;
expect(ws.getCell('A1').value).to.equal(7);
The issue specifically appears when reading certain corrupted files.
Expected Behavior
The library should not crash when style metadata is missing or malformed.
Instead, it should:
- Safely handle missing
styles or model properties
- Return
null or a default value
- Continue processing without throwing an uncaught exception
Possible Solution
Add defensive checks before accessing nested properties:
// if the style doesn't exist return null
const style = this.model && this.model.styles ? this.model.styles[id] : null;
if (!style) return null;
// have we built this model before?
let model = this.index && this.index.model ? this.index.model[id] : null;
if (model) return model;
This would prevent runtime errors when processing corrupted or incomplete files and allow the reader to fail gracefully instead of terminating execution.
🐛 Bug Report
Description
File:
exceljs/lib/xlsx/xform/style/styles-xform.jsFunction:
getStyleModel(id)When reading certain corrupted or malformed Excel files, the
getStyleModel(id)function throws runtime errors because some expected properties are missing.Specifically:
this.modeldoes not always contain astylesproperty, so:throws
TypeError: Cannot read properties of undefined.this.indexdoes not always contain amodelproperty, so:also throws a
TypeError.Because of this, execution stops instead of handling the missing style gracefully.
Steps To Reproduce
I cannot share the original file because it contains sensitive work data.
The issue occurs when using
WorkbookReaderto stream-read a file:While processing rows, the reader internally calls
getStyleModel(id), which throws whenstylesormodelis undefined.Basic workbook usage works normally:
The issue specifically appears when reading certain corrupted files.
Expected Behavior
The library should not crash when style metadata is missing or malformed.
Instead, it should:
stylesormodelpropertiesnullor a default valuePossible Solution
Add defensive checks before accessing nested properties:
This would prevent runtime errors when processing corrupted or incomplete files and allow the reader to fail gracefully instead of terminating execution.