-
Notifications
You must be signed in to change notification settings - Fork 14
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
Update code to allow better static analysis #408
base: main
Are you sure you want to change the base?
Conversation
/** | ||
* Match `re` and return captures. | ||
* Update position and css string. Return the matches | ||
*/ | ||
function match(re: RegExp) { | ||
const m = re.exec(css); | ||
if (!m) { | ||
return; | ||
} | ||
function processMatch(m: RegExpExecArray) { | ||
const str = m[0]; | ||
updatePosition(str); | ||
css = css.slice(str.length); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the bulk of the changes. The rest is just adaptation for this internal API change.
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( | ||
css, | ||
); |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High
regular expression
library input
This
regular expression
library input
This
regular expression
library input
); | ||
let value = ''; | ||
const matchVal = | ||
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 7 days ago
To fix the problem, we need to modify the regular expression to remove the ambiguity that causes exponential backtracking. Specifically, we can refactor the pattern to avoid nested quantifiers and ambiguous alternations. The goal is to ensure that each part of the regular expression has a clear and unambiguous match.
In this case, we can break down the regular expression into smaller, more manageable parts and use non-capturing groups to avoid ambiguity. We will replace the problematic part of the regular expression with a more efficient version.
-
Copy modified line R332
@@ -331,3 +331,3 @@ | ||
const matchVal = | ||
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( | ||
/^((?:'(?:\\'|[^'\\])*'|"(?:\\"|[^"\\])*"|\((?:'(?:\\'|[^'\\])*'|"(?:\\"|[^"\\])*"|[^)])*\)|[^};])+)/.exec( | ||
css, |
); | ||
let value = ''; | ||
const matchVal = | ||
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 7 days ago
To fix the problem, we need to remove the ambiguity in the regular expression that causes exponential backtracking. Specifically, we should modify the sub-expression (?:\\'|.)*?
to ensure that it does not have overlapping matches. We can achieve this by explicitly excluding the single quote character from the .
match using a negated character class.
- Modify the regular expression on line 332 to replace
(?:\\'|.)*?
with(?:\\'|[^'])*?
. - This change ensures that the
.
match does not include the single quote character, thus removing the ambiguity and preventing exponential backtracking.
-
Copy modified line R332
@@ -331,3 +331,3 @@ | ||
const matchVal = | ||
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( | ||
/^((?:'(?:\\'|[^'])*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|[^'])*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( | ||
css, |
); | ||
let value = ''; | ||
const matchVal = | ||
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( |
Check failure
Code scanning / CodeQL
Inefficient regular expression High
Show autofix suggestion
Hide autofix suggestion
Copilot Autofix AI 7 days ago
To fix the problem, we need to modify the regular expression to remove the ambiguity that causes exponential backtracking. Specifically, we should replace the sub-expression (?:\\"|.)*?
with a more efficient pattern that avoids ambiguity. One way to achieve this is to use a character class that excludes the characters we want to match separately, thus eliminating the need for nested quantifiers.
In this case, we can replace (?:\\"|.)*?
with [^"\\]*
to match any sequence of characters that are not double quotes or backslashes. This change will ensure that the regular expression performs efficiently without changing its functionality.
-
Copy modified line R332
@@ -331,3 +331,3 @@ | ||
const matchVal = | ||
/^((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|\((?:'(?:\\'|.)*?'|"(?:\\"|.)*?"|[^)])*?\)|[^};])+)/.exec( | ||
/^((?:'(?:\\'|[^'])*?'|"(?:\\"|[^"])*?"|\((?:'(?:\\'|[^'])*?'|"(?:\\"|[^"])*?"|[^)])*?\)|[^};])+)/.exec( | ||
css, |
@@ -573,15 +603,16 @@ | |||
*/ | |||
function atcustommedia(): CssCustomMediaAST | void { | |||
const pos = position(); | |||
const m = match(/^@custom-media\s+(--\S+)\s*([^{;\s][^{;]*);/); | |||
const m = /^@custom-media\s+(--\S+)\s*([^{;\s][^{;]*);/.exec(css); |
Check failure
Code scanning / CodeQL
Polynomial regular expression used on uncontrolled data High
Description
Related Issue
Motivation and Context
How Has This Been Tested?
Screenshots (if appropriate):
Types of changes
Checklist: