I am working on a site and I would like to make a user able to enter custom CSS into that will be publicly displayed.
However, seeing as a good deal of XSS attacks can be preformed through CSS, I would like to be able to find a way to “clean” the CSS output, similar to how HTML Purifier works, by parsing the CSS, running the parsed CSS against a whitelist, and then outputting a new stylesheet based on the parsed and whitelisted CSS.
Is there already a library like this out there? If not, is there a CSS parsing library that can be used to create a custom implementation?
I guess you’re going to write your own CSS parser and filter, so here’s what I’d consider, although I’ve never done such a thing:
color,font-family.background, at least in the beginning, so that you can easily parse the values. Require that they explicitly writebackground-color,background-image.When parsing, the hardest part would be the parsing of complex CSS selectors. But you can impose your own subset here too.
Here’s some (pseudo)code, maybe it will help you somehow: