Skip to content

Commit 7a0bd5d

Browse files
committed
fix: Ignore tag style, if sanitize cant parse CSS inside it #506
1 parent 62e861b commit 7a0bd5d

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

src/transform/sanitize.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -560,7 +560,9 @@ function sanitizeStyleTags(dom: cheerio.CheerioAPI, cssWhiteList: CssWhiteList)
560560
});
561561

562562
dom(element).text(css.stringify(parsedCSS));
563-
} catch {}
563+
} catch {
564+
dom(element).remove();
565+
}
564566
});
565567
}
566568

test/__snapshots__/xss.test.ts.snap

+10-5
Original file line numberDiff line numberDiff line change
@@ -284,11 +284,7 @@ exports[` meta 1`] = `""`;
284284
285285
exports[` style sheet 1`] = `""`;
286286
287-
exports[` style tag 1`] = `
288-
<style type="text/javascript">
289-
alert('XSS');
290-
</style>
291-
`;
287+
exports[` style tag 1`] = `""`;
292288
293289
exports[` style tag using background 1`] = `
294290
<style type="text/css">
@@ -311,3 +307,12 @@ exports[` style tags with broken up JavaScript for XSS 1`] = `
311307
`;
312308
313309
exports[` style tags with broken up JavaScript for XSS part 2 1`] = `<img>`;
310+
311+
exports[` svg with style tag and foreignObject inside 1`] = `
312+
<p>
313+
<svg>
314+
<style>
315+
</style>
316+
</svg>
317+
</p>
318+
`;

test/xss.test.ts

+4
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,10 @@ const ckecks = [
112112
'style tag using background',
113113
`<style type="text/css">body{background:url("javascript:alert('XSS')")}</style>`,
114114
],
115+
[
116+
'svg with style tag and foreignObject inside',
117+
'<svg><style><foreignObject><img src="a" onerror=alert(1)/></foreignObject></style></svg>',
118+
],
115119
['Anonymous HTML with style attribute', `<xss style="xss:expression(alert('XSS'))">`],
116120
['Local htc file', `<xss style="behavior: url(xss.htc);">`],
117121
['US-ASCII encoding', `¼script¾alert(¢XSS¢)¼/script¾`],

0 commit comments

Comments
 (0)