diff --git a/__tests__/build-commit.test.js b/__tests__/build-commit.test.js index 72aaea3..876ff2d 100644 --- a/__tests__/build-commit.test.js +++ b/__tests__/build-commit.test.js @@ -98,4 +98,11 @@ line 2`; expect(buildCommit(answersNoScope, options)).toEqual(expecteMessage); }); }); + + it('should escape harmful characters', () => { + const altAnswers = { ...answers, subject: 'th"is i\'s a n`ew $ fture &' }; + + // eslint-disable-next-line prettier/prettier, no-useless-escape + expect(buildCommit(altAnswers, {})).toEqual('feat(app): th\\\"is i\'s a n\\`ew \\\\$ f\\ture \\&'); + }); }); diff --git a/lib/build-commit.js b/lib/build-commit.js index a82cd70..84650f4 100644 --- a/lib/build-commit.js +++ b/lib/build-commit.js @@ -48,17 +48,17 @@ const addFooter = (footer, config) => { return `\n\n${footerPrefix} ${addBreaklinesIfNeeded(footer, config.breaklineChar)}`; }; -const escapeSpecialChars = result => { - // eslint-disable-next-line no-useless-escape - const specialChars = ['`']; - +const escapeSpecialChars = (result) => { + // eslint-disable-next-line no-useless-escape, prettier/prettier + const specialChars = ['`', '"', '\\$', '!', '<', '>', '&']; let newResult = result; - // eslint-disable-next-line array-callback-return - specialChars.map(item => { - // If user types "feat: `string`", the commit preview should show "feat: `\string\`". - // Don't worry. The git log will be "feat: `string`" - newResult = result.replace(new RegExp(item, 'g'), '\\`'); + + specialChars.forEach((item) => { + // If user types `feat: "string"`, the commit preview should show `feat: \"string\"`. + // Don't worry. The git log will be `feat: "string"` + newResult = newResult.replace(new RegExp(item, 'g'), `\\${item}`); }); + return newResult; };