Skip to content

Commit b928e2f

Browse files
fix(mobile): scan inline code delimiters linearly to prevent ReDoS UI stalls
The unanchored _inlineCode regex repeatedly scanned the remaining line on messages containing pairs of backticks separated by ordinary characters, causing quadratic work synchronously on the mobile UI isolate whenever a message preview was mounted. Replace the regex with a single-pass linear delimiter scanner in _withoutCode: - Scan consecutive backticks and match single-backtick openers to the next closing single backtick without backtracking. - Multi-backtick runs and unclosed spans are preserved in outsideCode so the existing fallback retains the original message. - Add regression test asserting linear processing time on an adversarial payload with 20,000 double-backticks. Signed-off-by: presentyourlove <[email protected]>
1 parent 2d29185 commit b928e2f

2 files changed

Lines changed: 77 additions & 3 deletions

File tree

‎mobile/lib/features/channels/message_content/preview_source.dart‎

Lines changed: 65 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,75 @@ String markdownPreviewSource(String content, int maxLines) {
6060
// at a line holding nothing but the closing backticks.
6161
final _fenceOpen = RegExp(r'^ *```');
6262
final _fenceClose = RegExp(r'^\s*```$');
63-
final _inlineCode = RegExp(r'`(?!`)(.+?)(?<!`)`(?!`)');
6463

6564
/// The prose lines without their inline code spans, since delimiters inside
6665
/// a span cannot pair with ones outside it. A backtick left over means a span
6766
/// this cannot place, and the caller keeps the whole message.
68-
String _withoutCode(List<String> prose) =>
69-
prose.map((line) => line.replaceAll(_inlineCode, '')).join('\n');
67+
///
68+
/// Uses a single-pass linear delimiter scanner instead of an unanchored regex
69+
/// to avoid quadratic scanning on adversarial inputs with paired backticks.
70+
String _withoutCode(List<String> prose) {
71+
final buffer = StringBuffer();
72+
for (var p = 0; p < prose.length; p++) {
73+
if (p > 0) buffer.write('\n');
74+
final line = prose[p];
75+
var i = 0;
76+
final len = line.length;
77+
while (i < len) {
78+
if (line.codeUnitAt(i) == 96) {
79+
// Count consecutive backticks.
80+
var runEnd = i + 1;
81+
while (runEnd < len && line.codeUnitAt(runEnd) == 96) {
82+
runEnd++;
83+
}
84+
final runLen = runEnd - i;
85+
if (runLen == 1) {
86+
// A single backtick may open an inline code span. Look linearly
87+
// forward for the closing single backtick.
88+
var closeStart = runEnd;
89+
var found = false;
90+
while (closeStart < len) {
91+
if (line.codeUnitAt(closeStart) == 96) {
92+
var closeEnd = closeStart + 1;
93+
while (closeEnd < len && line.codeUnitAt(closeEnd) == 96) {
94+
closeEnd++;
95+
}
96+
if (closeEnd - closeStart == 1) {
97+
i = closeEnd;
98+
found = true;
99+
break;
100+
}
101+
closeStart = closeEnd;
102+
} else {
103+
closeStart++;
104+
}
105+
}
106+
if (found) {
107+
continue;
108+
}
109+
// No closing single backtick on this line; append the rest and
110+
// finish.
111+
buffer.write(line.substring(i));
112+
break;
113+
} else {
114+
// Multi-backtick run (e.g. ``): keep in prose for outsideCode to
115+
// detect.
116+
buffer.write(line.substring(i, runEnd));
117+
i = runEnd;
118+
}
119+
} else {
120+
final nextBacktick = line.indexOf('`', i);
121+
if (nextBacktick == -1) {
122+
buffer.write(line.substring(i));
123+
break;
124+
}
125+
buffer.write(line.substring(i, nextBacktick));
126+
i = nextBacktick;
127+
}
128+
}
129+
}
130+
return buffer.toString();
131+
}
70132

71133
bool _closesInline(String text) {
72134
bool even(Pattern pattern) => pattern.allMatches(text).length.isEven;

‎mobile/test/features/channels/message_content/preview_source_test.dart‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,4 +201,16 @@ void main() {
201201
await tester.pumpWidget(_testable(MessageContent(content: content)));
202202
expect(find.textContaining('TAILMARK', findRichText: true), findsWidgets);
203203
});
204+
205+
test(
206+
'adversarial input with 20,000 double backticks processes in linear time',
207+
() {
208+
final payload = '${'``x' * 20000}\nline 1\nline 2\nline 3';
209+
final stopwatch = Stopwatch()..start();
210+
final result = markdownPreviewSource(payload, 2);
211+
stopwatch.stop();
212+
expect(result, payload);
213+
expect(stopwatch.elapsedMilliseconds, lessThan(100));
214+
},
215+
);
204216
}

0 commit comments

Comments
 (0)