Skip to content

Commit

Permalink
fix: Ignore tag style, if sanitize cant parse CSS inside it #506 (#507)
Browse files Browse the repository at this point in the history
  • Loading branch information
shevnv authored Sep 17, 2024
1 parent 3c37ab2 commit 182985d
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/transform/sanitize.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,7 +560,9 @@ function sanitizeStyleTags(dom: cheerio.CheerioAPI, cssWhiteList: CssWhiteList)
});

dom(element).text(css.stringify(parsedCSS));
} catch {}
} catch {
dom(element).remove();
}
});
}

Expand Down
15 changes: 10 additions & 5 deletions test/__snapshots__/xss.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -284,11 +284,7 @@ exports[` meta 1`] = `""`;
exports[` style sheet 1`] = `""`;
exports[` style tag 1`] = `
<style type="text/javascript">
alert('XSS');
</style>
`;
exports[` style tag 1`] = `""`;
exports[` style tag using background 1`] = `
<style type="text/css">
Expand All @@ -311,3 +307,12 @@ exports[` style tags with broken up JavaScript for XSS 1`] = `
`;
exports[` style tags with broken up JavaScript for XSS part 2 1`] = `<img>`;
exports[` svg with style tag and foreignObject inside 1`] = `
<p>
<svg>
<style>
</style>
</svg>
</p>
`;
4 changes: 4 additions & 0 deletions test/xss.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ const ckecks = [
'style tag using background',
`<style type="text/css">body{background:url("javascript:alert('XSS')")}</style>`,
],
[
'svg with style tag and foreignObject inside',
'<svg><style><foreignObject><img src="a" onerror=alert(1)/></foreignObject></style></svg>',
],
['Anonymous HTML with style attribute', `<xss style="xss:expression(alert('XSS'))">`],
['Local htc file', `<xss style="behavior: url(xss.htc);">`],
['US-ASCII encoding', `¼script¾alert(¢XSS¢)¼/script¾`],
Expand Down

0 comments on commit 182985d

Please sign in to comment.