-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[7.x] [Reporting]: Check if CSV cells (including headers) start with …
…known formula characters (#37930) (#40081) * [Reporting]: Check if CSV cells (including headers) start with known formula characters (#37930) * Re-working csv injection issue into master * Config flag for checking if CSV's contain formulas * Fixing snapshots * Fixing bad merge conflict with get_document_payload
- Loading branch information
Joel Griffith
authored
Jul 1, 2019
1 parent
ea8ea2b
commit 92ac121
Showing
14 changed files
with
371 additions
and
74 deletions.
There are no files selected for viewing
4 changes: 4 additions & 0 deletions
4
x-pack/legacy/plugins/reporting/__snapshots__/index.test.js.snap
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 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
96 changes: 96 additions & 0 deletions
96
x-pack/legacy/plugins/reporting/export_types/csv/server/lib/check_cells_for_formulas.test.ts
This file contains 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,96 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import { checkIfRowsHaveFormulas } from './check_cells_for_formulas'; | ||
|
||
const formulaValues = ['=', '+', '-', '@']; | ||
const nonRows = [null, undefined, 9, () => {}]; | ||
|
||
describe(`Check CSV Injected values`, () => { | ||
it(`returns 'false' when there's no formula values in cells`, () => { | ||
expect( | ||
checkIfRowsHaveFormulas( | ||
{ | ||
_doc: 'foo-bar', | ||
value: 'cool', | ||
title: 'nice', | ||
}, | ||
['_doc', 'value', 'title'] | ||
) | ||
).toBe(false); | ||
}); | ||
|
||
formulaValues.forEach(formula => { | ||
it(`returns 'true' when cells start with "${formula}"`, () => { | ||
expect( | ||
checkIfRowsHaveFormulas( | ||
{ | ||
_doc: 'foo-bar', | ||
value: formula, | ||
title: 'nice', | ||
}, | ||
['_doc', 'value', 'title'] | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
it(`returns 'false' when cells start with "${formula}" but aren't selected`, () => { | ||
expect( | ||
checkIfRowsHaveFormulas( | ||
{ | ||
_doc: 'foo-bar', | ||
value: formula, | ||
title: 'nice', | ||
}, | ||
['_doc', 'title'] | ||
) | ||
).toBe(false); | ||
}); | ||
}); | ||
|
||
formulaValues.forEach(formula => { | ||
it(`returns 'true' when headers start with "${formula}"`, () => { | ||
expect( | ||
checkIfRowsHaveFormulas( | ||
{ | ||
_doc: 'foo-bar', | ||
[formula]: 'baz', | ||
title: 'nice', | ||
}, | ||
['_doc', formula, 'title'] | ||
) | ||
).toBe(true); | ||
}); | ||
|
||
it(`returns 'false' when headers start with "${formula}" but aren't selected in fields`, () => { | ||
expect( | ||
checkIfRowsHaveFormulas( | ||
{ | ||
_doc: 'foo-bar', | ||
[formula]: 'baz', | ||
title: 'nice', | ||
}, | ||
['_doc', 'title'] | ||
) | ||
).toBe(false); | ||
}); | ||
}); | ||
|
||
nonRows.forEach(nonRow => { | ||
it(`returns false when there's "${nonRow}" for rows`, () => { | ||
expect( | ||
checkIfRowsHaveFormulas( | ||
{ | ||
_doc: 'foo-bar', | ||
// @ts-ignore need to assert non-string values still return false | ||
value: nonRow, | ||
title: 'nice', | ||
}, | ||
['_doc', 'value', 'title'] | ||
) | ||
).toBe(false); | ||
}); | ||
}); | ||
}); |
20 changes: 20 additions & 0 deletions
20
x-pack/legacy/plugins/reporting/export_types/csv/server/lib/check_cells_for_formulas.ts
This file contains 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,20 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import * as _ from 'lodash'; | ||
|
||
const formulaValues = ['=', '+', '-', '@']; | ||
|
||
interface IFlattened { | ||
[header: string]: string; | ||
} | ||
|
||
export const checkIfRowsHaveFormulas = (flattened: IFlattened, fields: string[]) => { | ||
const pruned = _.pick(flattened, fields); | ||
const csvValues = [..._.keys(pruned), ...(_.values(pruned) as string[])]; | ||
|
||
return _.some(csvValues, cell => _.some(formulaValues, char => _.startsWith(cell, char))); | ||
}; |
This file contains 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 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 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 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 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
Oops, something went wrong.