Quality: Potential ReDoS vulnerability in regex-based CSS function parsing#627
Open
tomaioo wants to merge 1 commit into
Open
Conversation
In `lib/variables.js`, the `replaceVariables` function uses a regex `/([a-z\-]+)\s*\(\s*([^\(\)]*?)\s*(?:,\s*([^\(\)]*?)\s*)?\s*\)/i` to parse CSS function calls. This regex contains nested quantifiers and backreferences that could be exploited with maliciously crafted CSS input, causing catastrophic backtracking and denial of service. The regex is applied in a `while` loop with `exec`, compounding the risk. The `[^\(\)]*?` pattern with the surrounding optional groups creates multiple paths for exponential backtracking when encountering nested or malformed parentheses. Signed-off-by: tomaioo <203048277+tomaioo@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Quality: Potential ReDoS vulnerability in regex-based CSS function parsing
Problem
Severity:
High| File:lib/variables.js:L12In
lib/variables.js, thereplaceVariablesfunction uses a regex/([a-z\-]+)\s*\(\s*([^\(\)]*?)\s*(?:,\s*([^\(\)]*?)\s*)?\s*\)/ito parse CSS function calls. This regex contains nested quantifiers and backreferences that could be exploited with maliciously crafted CSS input, causing catastrophic backtracking and denial of service. The regex is applied in awhileloop withexec, compounding the risk. The[^\(\)]*?pattern with the surrounding optional groups creates multiple paths for exponential backtracking when encountering nested or malformed parentheses.Solution
Replace the hand-rolled regex parser with a proper CSS value tokenizer, or at minimum add input length limits and timeout protection. Consider using a well-tested CSS parsing library instead of regex for this task. If regex must be used, simplify the pattern to avoid nested quantifiers and test with ReDoS detection tools.
Changes
lib/variables.js(modified)