diff --git a/package.json b/package.json index 3d2f0990..1dc58858 100644 --- a/package.json +++ b/package.json @@ -85,6 +85,7 @@ "lodash": "4.17.11", "minimist": "1.2.0", "shelljs": "0.7.6", + "strip-bom": "3.0.0", "strip-json-comments": "2.0.1" }, "babel": { diff --git a/src/configLoader/getContent.js b/src/configLoader/getContent.js index a24cabfe..08918031 100644 --- a/src/configLoader/getContent.js +++ b/src/configLoader/getContent.js @@ -2,6 +2,8 @@ import fs from 'fs'; import path from 'path'; import stripJSONComments from 'strip-json-comments'; +import isUTF8 from 'is-utf8'; +import stripBom from 'strip-bom'; import { getNormalizedConfig } from '../configLoader'; @@ -17,7 +19,7 @@ export default getConfigContent; function readConfigContent (configPath) { const parsedPath = path.parse(configPath) const isRcFile = parsedPath.ext !== '.js' && parsedPath.ext !== '.json'; - const jsonString = fs.readFileSync(configPath, 'utf-8'); + const jsonString = readConfigFileContent(configPath); const parse = isRcFile ? (contents) => JSON.parse(stripJSONComments(contents)) : (contents) => JSON.parse(contents); @@ -61,3 +63,20 @@ function getConfigContent (configPath, baseDirectory) { const content = readConfigContent(resolvedPath); return getNormalizedConfig(configBasename, content); }; + +/** + * Read proper content from config file. + * If the chartset of the config file is not utf-8, one error will be thrown. + * @param {String} configPath + * @return {String} + */ +function readConfigFileContent (configPath) { + + let rawBufContent = fs.readFileSync(configPath); + + if (!isUTF8(rawBufContent)) { + throw new Error(`The config file at "${configPath}" contains invalid charset, expect utf8`); + } + + return stripBom(rawBufContent.toString("utf8")); +} diff --git a/test/fixtures/invalid-charset.json b/test/fixtures/invalid-charset.json new file mode 100644 index 00000000..9c8d9573 Binary files /dev/null and b/test/fixtures/invalid-charset.json differ diff --git a/test/tests/configLoader.js b/test/tests/configLoader.js index 0a33e684..df8892b2 100644 --- a/test/tests/configLoader.js +++ b/test/tests/configLoader.js @@ -13,6 +13,8 @@ describe('configLoader', function () { .to.throw(/parsing json at/i); expect(() => getContent('invalid-json-rc', fixturesPath)) .to.throw(/parsing json at/i); + expect(() => getContent('invalid-charset.json', fixturesPath)) + .to.throw(/contains invalid charset/i); }); it('parses json files with comments', function () {