Skip to content

Commit fafd3fb

Browse files
authored
🐛 Fixed ReDoS susceptible regexs (#470)
fixes TryGhost/Product#3544 The regexs for counting words and counting images were susceptible to ReDoS and could be problematic in Ghost when the a post's HTML contains a tag with a very large attribute (i.e an image src being a base64 encoded image). This commit tweaks these regexs to make them more efficient and circumvent the problem.
1 parent b88a59a commit fafd3fb

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

packages/helpers/lib/utils/count-images.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,5 @@ export default function countImages(html) {
1212
if (Object.prototype.hasOwnProperty.call(html, 'string')) {
1313
html = html.string;
1414
}
15-
return (html.match(/<img(.|\n)*?>/g) || []).length;
15+
return (html.match(/<img("[^"]*"|'[^']*'|[^'">])+\/?>/g) || []).length;
1616
}

packages/helpers/lib/utils/count-words.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default function countWords(text) {
1515
text = text.string;
1616
}
1717

18-
text = text.replace(/<(.|\n)*?>/g, ' '); // strip any HTML tags
18+
text = text.replace(/<("[^"]*"|'[^']*'|[^'">])+\/?>/g, ' '); // strip any HTML tags
1919

2020
const pattern = /[a-zA-ZÀ-ÿ0-9_\u0392-\u03c9\u0410-\u04F9]+|[\u4E00-\u9FFF\u3400-\u4dbf\uf900-\ufaff\u3040-\u309f\uac00-\ud7af]+/g;
2121

0 commit comments

Comments
 (0)