From 21085d073205401dc30520b5e498ba7ea73ea1f8 Mon Sep 17 00:00:00 2001 From: Princi Vershwal Date: Fri, 23 Jun 2023 12:01:16 +0530 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Handled=20BOM=20character=20for?= =?UTF-8?q?=20Unicode=20encoded=20file=20uploads=20(#17104)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fixes https://github.com/TryGhost/Ghost/issues/16917 refs https://github.com/TryGhost/Ghost/issues/16917#issuecomment-1602984601 Co-authored-by: Princi Vershwal --- ghost/members-csv/lib/parse.js | 6 +++--- .../fixtures/single-column-with-header-bom.csv | 3 +++ ghost/members-csv/test/parse.test.js | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+), 3 deletions(-) create mode 100644 ghost/members-csv/test/fixtures/single-column-with-header-bom.csv diff --git a/ghost/members-csv/lib/parse.js b/ghost/members-csv/lib/parse.js index 9723109ef1..6c1311fd87 100644 --- a/ghost/members-csv/lib/parse.js +++ b/ghost/members-csv/lib/parse.js @@ -15,11 +15,11 @@ module.exports = (path, headerMapping, defaultLabels = []) => { const csvParserStream = papaparse.parse(papaparse.NODE_STREAM_INPUT, { header: true, transformHeader(_header) { - if (!headerMapping || !Reflect.has(headerMapping, _header)) { + const cleanHeader = _header.replace(papaparse.BYTE_ORDER_MARK, ''); //Removing BOM characters for Unicode-based encodings + if (!headerMapping || !Reflect.has(headerMapping, cleanHeader)) { return undefined; } - - return headerMapping[_header]; + return headerMapping[cleanHeader]; }, transform(value, header) { if (header === 'labels') { diff --git a/ghost/members-csv/test/fixtures/single-column-with-header-bom.csv b/ghost/members-csv/test/fixtures/single-column-with-header-bom.csv new file mode 100644 index 0000000000..137635654c --- /dev/null +++ b/ghost/members-csv/test/fixtures/single-column-with-header-bom.csv @@ -0,0 +1,3 @@ +email +jbloggs@example.com +test@example.com \ No newline at end of file diff --git a/ghost/members-csv/test/parse.test.js b/ghost/members-csv/test/parse.test.js index e725e8808e..a124b23fcf 100644 --- a/ghost/members-csv/test/parse.test.js +++ b/ghost/members-csv/test/parse.test.js @@ -35,6 +35,24 @@ describe('parse', function () { assert.equal(result.length, 0); }); + it('one column with bom', async function () { + const filePath = csvPath + 'single-column-with-header-bom.csv'; + const result = await parse(filePath, DEFAULT_HEADER_MAPPING); + + assert.ok(result); + assert.equal(result.length, 2); + assert.equal(result[0].email, 'jbloggs@example.com'); + assert.equal(result[1].email, 'test@example.com'); + }); + + it('one column with bom and without header mapping returns empty result', async function () { + const filePath = csvPath + 'single-column-with-header-bom.csv'; + const result = await parse(filePath); + + assert.ok(result); + assert.equal(result.length, 0); + }); + it('two columns, 1 filter', async function () { const filePath = csvPath + 'two-columns-with-header.csv'; const result = await parse(filePath, DEFAULT_HEADER_MAPPING);