@@ -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 , / < s t y l e [ ^ > ] * > [ \s \S ] * ?< \/ s t y l e \s * > / gi, '' )
138147 text = replaceUntilStable ( text , / < s c r i p t [ ^ > ] * > [ \s \S ] * ?< \/ s c r i p t \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