🐛 Handled BOM character for Unicode encoded file uploads (#17104)

fixes https://github.com/TryGhost/Ghost/issues/16917
refs https://github.com/TryGhost/Ghost/issues/16917#issuecomment-1602984601

Co-authored-by: Princi Vershwal <princi.vershwal@Princis-MacBook-Pro.local>
This commit is contained in:
Princi Vershwal 2023-06-23 12:01:16 +05:30 committed by GitHub
parent 9e6c800e38
commit 21085d0732
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 24 additions and 3 deletions

View File

@ -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') {

View File

@ -0,0 +1,3 @@
email
jbloggs@example.com
test@example.com
1 email
2 jbloggs@example.com
3 test@example.com

View File

@ -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);