Skip to content

getStyleModel(id) function throws runtime errors because some expected properties are missing. #37

@PaulChinta7

Description

@PaulChinta7

🐛 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:

    this.model.styles[id];

    throws TypeError: Cannot read properties of undefined.

  • this.index does not always contain a model property, so:

    this.index.model[id];

    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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions