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

Conversation

EelcoLos
Copy link
Contributor

@EelcoLos EelcoLos commented Jan 31, 2024

This pull request refactors the subject pattern validation in the validatePrTitle.js file. It introduces a whitelist of allowed special characters and escapes all special characters that are not in the whitelist. This ensures that the subject pattern is properly validated and improves the overall code quality.

This part of the code is being noticed by Github security checks as "Regular expression injection".
Github states on that:

Constructing a regular expression with unsanitized user input is dangerous as a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack.

I tried the normal sanitation rules, which broke some tests. this solution doesn't break any of the current tests.

Demo PR

Brink-Software#25

Copy link
Owner

@amannn amannn left a comment

Choose a reason for hiding this comment

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

Please see the inline comment. Apart from this, restricting which regex values are allowed would be a breaking change and would also introduce maintenance effort. I'm therefore a bit hesitant to move forward with this, but if you can provide some examples of regex values that you find concerning might help to discuss this.

Generally, in regard to this analysis:

Constructing a regular expression with unsanitized user input is dangerous as a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack.

We're using a regex string that the user provides via configuration to the GitHub action, so if this would cause a DoS, it's caused by the developers themselves. Same as if you'd add an infinite loop to code. Therefore I'm not sure if this is something that needs to be handled by this action.

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.

@EelcoLos
Copy link
Contributor Author

EelcoLos commented Feb 2, 2024

Please see the inline comment. Apart from this, restricting which regex values are allowed would be a breaking change and would also introduce maintenance effort. I'm therefore a bit hesitant to move forward with this, but if you can provide some examples of regex values that you find concerning might help to discuss this.

Generally, in regard to this analysis:

Constructing a regular expression with unsanitized user input is dangerous as a malicious user may be able to modify the meaning of the expression. In particular, such a user may be able to provide a regular expression fragment that takes exponential time in the worst case, and use that to perform a Denial of Service attack.

We're using a regex string that the user provides via configuration to the GitHub action, so if this would cause a DoS, it's caused by the developers themselves. Same as if you'd add an infinite loop to code. Therefore I'm not sure if this is something that needs to be handled by this action.

I see and acknowledge that this would be more maintenance. The first change I made locally broke 6 of the tests. Therefore, at first, I was wondering whether I should make this PR in the first place.
But then again, I felt like: I'd rather hear your opinion then just not making the PR in the first place.
If the maintenance would be too high, I'd rather close this PR and consider the matter resolved.

@amannn
Copy link
Owner

amannn commented Feb 2, 2024

I see, yes—thanks for starting the discussion! Based on the warning in the safe-regex packages, it seems like it's hard to get rid of all false positives. As we're not creating regexes from arbitrary user input but from a configuration that was made by developers, I currently don't see this as an issue.

@EelcoLos
Copy link
Contributor Author

EelcoLos commented Feb 2, 2024

as discussed, we've decided to not go through with this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants