Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: shell special characters escaping #108

Merged
merged 1 commit into from
Jul 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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