Skip to content

Commit 0fdd0bb

Browse files
committed
fix: resolve issue with csv injection option
1 parent dd154d6 commit 0fdd0bb

4 files changed

Lines changed: 25 additions & 4 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ Returns the CSV `string` or rejects with an `Error` if there was an issue.
197197
* Note: If selected, values will be converted using `toLocaleString()` rather than `toString()`
198198
* `wrapBooleans` - Boolean - Should boolean values be wrapped in wrap delimiters to prevent Excel from converting them to Excel's TRUE/FALSE Boolean values.
199199
* Default: `false`
200-
* `preventCsvInjection` - Boolean - Should CSV injection be prevented by left trimming these characters: Equals (=), Plus (+), Minus (-), At (@), Tab (0x09), Carriage return (0x0D).
200+
* `preventCsvInjection` - Boolean - Should CSV injection be prevented by left trimming these characters, including when they appear after leading spaces: Equals (=), Plus (+), Minus (-), At (@), Tab (0x09), Carriage return (0x0D).
201201
* Default: `false`
202202

203203

@@ -324,4 +324,4 @@ $ npm run coverage
324324
* `json2csv test.json -o output.csv -W -k arrayOfStrings -o output.csv`
325325
* Empty field value option (as of 3.1.0)
326326
* TypeScript typings included (as of 3.4.0) - thanks to [@GabrielCastro](https://github.com/GabrielCastro)!
327-
* Synchronous use case support (as of 5.0.0) - thanks to [@Nokel81](https://github.com/Nokel81)
327+
* Synchronous use case support (as of 5.0.0) - thanks to [@Nokel81](https://github.com/Nokel81)

src/json2csv.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ export const Json2Csv = function (options: DefaultJson2CsvOptions) {
407407
if (Array.isArray(fieldValue)) {
408408
return fieldValue.map(preventCsvInjection);
409409
} else if (typeof fieldValue === 'string' && !utils.isNumber(fieldValue)) {
410-
return fieldValue.replace(/^[=+\-@\t\r]+/g, '');
410+
return fieldValue.replace(/^[ \t\r]*[=+\-@\t\r]+/g, '');
411411
}
412412
return fieldValue;
413413
}

src/types.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ interface SharedConverterOptions {
5757

5858
/**
5959
* Should CSV injection be prevented by left trimming these characters:
60-
* Equals (=), Plus (+), Minus (-), At (@), Tab (0x09), Carriage return (0x0D).
60+
* Equals (=), Plus (+), Minus (-), At (@), Tab (0x09), Carriage return (0x0D),
61+
* including when they appear after leading spaces.
6162
* @default false
6263
*/
6364
preventCsvInjection?: boolean;

test/json2csv.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,6 +721,26 @@ export function runTests() {
721721
assert.equal(csv, expectedCsv);
722722
});
723723

724+
it('should left trim csv injection characters after leading whitespace if preventCsvInjection is specified', () => {
725+
const csv = json2csv([{ name: ' =SUM(B1:B2)' }], {
726+
preventCsvInjection: true
727+
});
728+
729+
const expectedCsv = 'name\nSUM(B1:B2)';
730+
731+
assert.equal(csv, expectedCsv);
732+
});
733+
734+
it('should not left trim leading spaces without csv injection characters if preventCsvInjection is specified', () => {
735+
const csv = json2csv([{ name: ' Bob' }], {
736+
preventCsvInjection: true
737+
});
738+
739+
const expectedCsv = 'name\n Bob';
740+
741+
assert.equal(csv, expectedCsv);
742+
});
743+
724744
it('should not alter numbers by removing minus (-) even if preventCsvInjection is specified', () => {
725745
const csv = json2csv([{ temperature: -10 }], {
726746
preventCsvInjection: true

0 commit comments

Comments
 (0)