Skip to content

Add the ability to bail out of parsing if the number of columns exceeds a given limit #776

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 2 commits into from
May 3, 2019
Merged
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
6 changes: 5 additions & 1 deletion lib/xlsx/xform/sheet/row-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ var BaseXform = require('../base-xform');

var CellXform = require('./cell-xform');

var RowXform = module.exports = function() {
var RowXform = module.exports = function(options) {
this.maxItems = options && options.maxItems;
this.map = {
c: new CellXform()
};
Expand Down Expand Up @@ -119,6 +120,9 @@ utils.inherits(RowXform, BaseXform, {
if (this.parser) {
if (!this.parser.parseClose(name)) {
this.model.cells.push(this.parser.model);
if (this.maxItems && this.model.cells.length > this.maxItems) {
throw new Error('Max column count exceeded');
}
this.parser = undefined;
}
return true;
Expand Down
3 changes: 2 additions & 1 deletion lib/xlsx/xform/sheet/worksheet-xform.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ var RowBreaksXform = require('./row-breaks-xform');

var WorkSheetXform = module.exports = function(options) {
var maxRows = options && options.maxRows;
var maxCols = options && options.maxCols;
this.map = {
sheetPr: new SheetPropertiesXform(),
dimension: new DimensionXform(),
sheetViews: new ListXform({tag: 'sheetViews', count: false, childXform: new SheetViewXform()}),
sheetFormatPr: new SheetFormatPropertiesXform(),
cols: new ListXform({tag: 'cols', count: false, childXform: new ColXform()}),
sheetData: new ListXform({tag: 'sheetData', count: false, empty: true, childXform: new RowXform(), maxItems: maxRows}),
sheetData: new ListXform({tag: 'sheetData', count: false, empty: true, childXform: new RowXform({maxItems: maxCols}), maxItems: maxRows}),
autoFilter: new AutoFilterXform(),
mergeCells: new ListXform({tag: 'mergeCells', count: true, childXform: new MergeCellXform()}),
rowBreaks: new RowBreaksXform(),
Expand Down
Binary file added spec/integration/data/many-columns.xlsx
Binary file not shown.
Binary file removed spec/integration/data/~$fibonacci.xlsx
Binary file not shown.
29 changes: 29 additions & 0 deletions spec/integration/workbook-xlsx-reader.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,35 @@ describe('WorkbookReader', function() {
return workbook.xlsx.readFile('./spec/integration/data/fibonacci.xlsx', {maxRows: 20});
});
});

describe('Column limit', function() {
it('should bail out if the file contains more cells than the limit', function() {
const workbook = new Excel.Workbook();
// The many-columns sheet has 20 columns in row 2
return workbook.xlsx.readFile('./spec/integration/data/many-columns.xlsx', {maxCols: 15})
.then(function() {
throw new Error('Promise unexpectedly fulfilled');
}, function(err) {
expect(err.message).to.equal('Max column count exceeded');
});
});

it('should fail fast on a huge file', function() {
this.timeout(20000);
const workbook = new Excel.Workbook();
return workbook.xlsx.readFile('./spec/integration/data/huge.xlsx', {maxCols: 10})
.then(function() {
throw new Error('Promise unexpectedly fulfilled');
}, function(err) {
expect(err.message).to.equal('Max column count exceeded');
});
});

it('should parse fine if the limit is not exceeded', function() {
const workbook = new Excel.Workbook();
return workbook.xlsx.readFile('./spec/integration/data/many-columns.xlsx', {maxCols: 40});
});
});
});

describe('#read', function() {
Expand Down