Executive brief
Terser is a widely-used JavaScript minification and compression tool. The tool contains insecure regular expressions that can be exploited to cause a denial-of-service condition, where processing of specially-crafted JavaScript code causes the tool to consume excessive CPU and become unresponsive. An attacker could exploit this by providing malicious code to be minified, causing service disruption for any system using Terser as part of its build pipeline.
Technical details
The vulnerability is a Regular Expression Denial of Service (ReDoS) attack, classified as CWE-1333, arising from insecure use of regular expressions in Terser's expression evaluation component (lib/compress/evaluate.js). The root cause is catastrophic backtracking in a vulnerable regex pattern similar to /A(B|C+)+D/, where the nested quantifiers cause exponential time complexity when matching fails. An attacker can trigger this by providing JavaScript code containing specially-crafted string patterns to the Terser minification tool with the unsafe=true compression option enabled. The attack requires no authentication or user interaction beyond submitting code for minification; processing a 30-character malicious string causes the regex engine to consume several seconds of CPU. Patches are available in versions 4.8.1 (for the 4.x branch) and 5.14.2 (for the 5.x branch).
Affected products
- Terser Terser <4.8.1, >=5.0.0 and <5.14.2
Timeline
- 2022-05-02: disclosed
- 2022-07-15: advisory: NVD published
- 2022-07-16: advisory: GHSA published
- 2022-07-20: patched: Patches available in 4.8.1 and 5.14.2
References
- https://github.com/terser/terser/commit/a4da7349fdc92c05094f41d33d06d8cd4e90e76b
- https://github.com/terser/terser/commit/d8cc5691be980d663c29cc4d5ce67e852d597012
- https://github.com/terser/terser
- https://github.com/terser/terser/blob/master/lib/compress/evaluate.js%23L135
- https://snyk.io/vuln/SNYK-JAVA-ORGWEBJARSNPM-2949722
- https://snyk.io/vuln/SNYK-JS-TERSER-2806366