DmitrySoshnikov / regexp-tree

Regular expressions processor in JavaScript
MIT License
400 stars 45 forks source link

Optimizer creates unsafe Regex? #236

Open Uzlopak opened 2 years ago

Uzlopak commented 2 years ago

Example: /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[1-9])\.)(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){2}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/i is optimized to

/^(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|[1-9])\.(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)\.){2}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]\d|\d)$/i

The first is considered a safe regex by safe-regex, the second one not.

DmitrySoshnikov commented 2 years ago

Hi @Uzlopak, thanks for reporting. I'm curious which specific part is considered unsafe here? First we need to check if a valid regexp is generated as part of the optimizer, and then see the unsafe part and how we can transform it better. I'll appreciate a PR for this issue.