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: Refactor subject pattern validation in validatePrTitle.js #251

Closed
wants to merge 1 commit into from
Closed
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
30 changes: 29 additions & 1 deletion src/validatePrTitle.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,35 @@ module.exports = async function validatePrTitle(
}

if (subjectPattern) {
const match = result.subject.match(new RegExp(subjectPattern));
// eslint-disable-next-line no-inner-declarations

// Define a whitelist of allowed special characters
const allowedSpecialChars = [
'.',
'*',
'+',
'?',
'^',
'$',
'{',
'}',
'(',
')',
'|',
'[',
']',
'\\'
];

// Escape all special characters that are not in the whitelist
const sanitizedPattern = subjectPattern.replace(
/([.*+?^${}()|[\]\\])/g,
(match) => (allowedSpecialChars.includes(match) ? match : `\\${match}`)
);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide an example of a regex that would be changed with this logic? From what I can tell the regex matches all chars that are allowed, therefore I'm wondering what would change based on this?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Related to this, I found these npm libraries:

Note that both claim that:

WARNING: This module has both false positives and false negatives. It is not meant as a full checker, but it detect basic cases.


const regex = new RegExp(sanitizedPattern);

const match = result.subject.match(regex);

if (!match) {
throwSubjectPatternError(
Expand Down
Loading