Skip to content

A platform for community discussion. Free, open, simple.

FIX: possible ReDOS in markdown newline rule (#15646)

Régis Hanola582c49601ac
diff --git a/app/assets/javascripts/pretty-text/engines/discourse-markdown/newline.js b/app/assets/javascripts/pretty-text/engines/discourse-markdown/newline.js
index e01b7bb082d..7544a6c5727 100644
--- a/app/assets/javascripts/pretty-text/engines/discourse-markdown/newline.js
+++ b/app/assets/javascripts/pretty-text/engines/discourse-markdown/newline.js
@@ -8,6 +8,7 @@ function newline(state, silent) {
let token,
pmax,
max,
+ ws,
pos = state.pos;
if (state.src.charCodeAt(pos) !== 0x0a /* \n */) {
@@ -24,7 +25,14 @@ function newline(state, silent) {
if (!silent) {
if (pmax >= 0 && state.pending.charCodeAt(pmax) === 0x20) {
if (pmax >= 1 && state.pending.charCodeAt(pmax - 1) === 0x20) {
- state.pending = state.pending.replace(/ +$/, "");
+ // Find whitespaces tail of pending chars.
+ ws = pmax - 1;
+
+ while (ws >= 1 && state.pending.charCodeAt(ws - 1) === 0x20) {
+ ws--;
+ }
+
+ state.pending = state.pending.slice(0, ws);
token = state.push("hardbreak", "br", 0);
} else {
state.pending = state.pending.slice(0, -1);