Skip to content

Commit

Permalink
fix: shell special characters escaping (#108)
Browse files Browse the repository at this point in the history
ISSUES CLOSED: #107
  • Loading branch information
dudekaa authored Jul 15, 2022
1 parent 0ad552e commit d9fd78a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
7 changes: 7 additions & 0 deletions __tests__/build-commit.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 $ f<ea>ture &' };

// eslint-disable-next-line prettier/prettier, no-useless-escape
expect(buildCommit(altAnswers, {})).toEqual('feat(app): th\\\"is i\'s a n\\`ew \\\\$ f\\<ea\\>ture \\&');
});
});
18 changes: 9 additions & 9 deletions lib/build-commit.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down

0 comments on commit d9fd78a

Please sign in to comment.