Skip to content

Commit

Permalink
Optimize the performance of parsoid regular expressions (#1323)
Browse files Browse the repository at this point in the history
* Sets length ranges for a few regular expressions that may
  result in polynomial-time performance in worst-case
  scenarios.  A max range length of 128 seems reasonable
  based upon a limited analysis of available patterns.

Bug: T337274
  • Loading branch information
sbassett29 committed May 31, 2023
1 parent edb41fc commit a1edf24
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions sys/parsoid.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const spec = HyperSwitch.utils.loadSpec(`${__dirname}/parsoid.yaml`);
function normalizeHtml(html) {
return html && html.toString &&
html.toString()
.replace(/ about="[^"]+"(?=[/> ])|<meta property="mw:TimeUuid"[^>]+>/g, '');
.replace(/ about="[^"]+"(?=[/> ])|<meta property="mw:TimeUuid"[^>]{0,128}>/g, '');
}
function sameHtml(a, b) {
return normalizeHtml(a) === normalizeHtml(b);
Expand All @@ -30,8 +30,8 @@ function sameHtml(a, b) {
* @return {string} modified html
*/
function insertTidMeta(html, tid) {
if (!/<meta property="mw:TimeUuid" [^>]+>/.test(html)) {
return html.replace(/(<head [^>]+>)/,
if (!/<meta property="mw:TimeUuid" [^>]{0,128}>/.test(html)) {
return html.replace(/(<head [^>]{0,128}>)/,
`$1<meta property="mw:TimeUuid" content="${tid}"/>`);
}
return html;
Expand Down

0 comments on commit a1edf24

Please sign in to comment.