fix catastrophic backtracking when removing HTML comments (#40223)

* fix catastrophic backtracking when removing HTML comments

The regexp `/<!--(.|\s)*?-->/g` can experience catastrophic backtracking while matching a string like `<!--                                           -- >` (notice how it does not end with `-->`).    

[You can see the catastrophic backtracking in action here](https://regex101.com/r/2jvwFi/1/debugger). 

This is because both `.` and `\s` matches the space character, and there are therefore a large number of possible ways for `(.|\s)*` to match a long sequence of spaces.  
The regexp evaluator is greedy, and the problem therefore only exists when the string does not match the regexp. 

The fix is simply to remove the ambiguity such that there is only one possible way for the regexp to match a sequence of white-space.

* Update client/src/utils/curriculum-helpers.js

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>

Co-authored-by: Oliver Eyton-Williams <ojeytonwilliams@gmail.com>
This commit is contained in:
Erik Krogh Kristensen
2020-11-12 17:20:34 +01:00
committed by GitHub
parent f353278905
commit 11dfe9274f

View File

@ -1,7 +1,7 @@
import { parse } from '@babel/parser'; import { parse } from '@babel/parser';
import generate from '@babel/generator'; import generate from '@babel/generator';
const removeHtmlComments = str => str.replace(/<!--(.|\s)*?-->/g, ''); const removeHtmlComments = str => str.replace(/<!--.*?-->/gs, '');
const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, ''); const removeCssComments = str => str.replace(/\/\*[\s\S]+?\*\//g, '');