Skip to content

Commit 395668f

Browse files
waleedlatif1claude
andcommitted
fix(security): decode entities before tag stripping and cap loop iterations
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 0fdb2be commit 395668f

File tree

1 file changed

+12
-5
lines changed

1 file changed

+12
-5
lines changed

apps/sim/lib/mothership/inbox/format.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,16 @@ function isForwardedEmail(subject: string | null, body: string | null): boolean
9999
* Prevents incomplete sanitization from nested/overlapping patterns
100100
* like `<scr<script>ipt>`.
101101
*/
102-
export function replaceUntilStable(input: string, pattern: RegExp, replacement: string): string {
102+
export function replaceUntilStable(
103+
input: string,
104+
pattern: RegExp,
105+
replacement: string,
106+
maxIterations = 100
107+
): string {
103108
let prev = input
104109
let next = prev.replace(pattern, replacement)
105-
while (next !== prev) {
110+
let iterations = 0
111+
while (next !== prev && iterations++ < maxIterations) {
106112
prev = next
107113
next = prev.replace(pattern, replacement)
108114
}
@@ -134,6 +140,9 @@ function extractTextFromHtml(html: string | null): string | null {
134140
if (!html) return null
135141

136142
let text = html
143+
144+
text = decodeHtmlEntities(text)
145+
137146
text = replaceUntilStable(text, /<style[^>]*>[\s\S]*?<\/style\s*>/gi, '')
138147
text = replaceUntilStable(text, /<script[^>]*>[\s\S]*?<\/script\s*>/gi, '')
139148

@@ -145,9 +154,7 @@ function extractTextFromHtml(html: string | null): string | null {
145154

146155
text = replaceUntilStable(text, /<[^>]+>/g, '')
147156

148-
text = decodeHtmlEntities(text)
149-
.replace(/\n{3,}/g, '\n\n')
150-
.trim()
157+
text = text.replace(/\n{3,}/g, '\n\n').trim()
151158

152159
return text
153160
}

0 commit comments

Comments
 (0)