Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 344611
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:00:24+00:00 2026-05-12T11:00:24+00:00

I am working on a site and I would like to make a user

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:00:24+00:00Added an answer on May 12, 2026 at 11:00 am

    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:

    • Make a (white) list of acceptable CSS properties that your users can use. Like: color, font-family.
    • I believe it might be better to not allow short-hand forms such as background, at least in the beginning, so that you can easily parse the values. Require that they explicitly write background-color, background-image.
    • If you want URLs, only allow relative URLs, and discard everything that doesn’t even closely look like a URL. Log these issues anyway, so that you can improve your parser and validator.
    • Be very strict in your parsing, discard everything that your parser doesn’t understand even if it would be valid CSS. In other words, make your own CSS subset.

    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:

    <?php
    
    function tokenizeCSS() {
        return array(
            array(
                'selector'   => '#foo .bar',
                'properties' => array(
                    'background-color' => 'transparent',
                    'color'            => '#fff',
                ),
            );
        );
    }
    
    function colorValidator($color)
    {}
    
    /**
     * This is basically the white list. Keys are accepted CSS properties
     * and values are the validator callbacks.
     */
    $propertyValidators = array(
        'background-color' => 'colorValidator',
        'color'            => 'colorValidator',
    );
    
    $filteredRules = array();
    
    foreach (tokenizeCSS() as $rule) {
        if (! validSelector($rule['selector'])) {
            continue;
        }
    
        foreach ($rule['properties'] as $property => $value) {
            /**
             * Check property is in white list
             */
            if (! isset($propertyValidators[$property]) {
                continue;
            }
    
            /**
             * Check property is valid
             */
            if (! $propertyValidators[$property]($value)) {
                continue;
            }
    
            /**
             * Valid rule
             */
            $filteredRules[$rule['selector']][$property] = $value;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working with ASP.Net MVC and I would like to make a web site
I am working on a new site and would like it to be able
I am working on a site that depends on user generated content, and it
I am working on a webby, which would need unique user identification like IP/MAC
I'm working on a redesign of my website, and would like to make a
I'm working with a partner site that does not have bandwidth to make customizations
I have to make some mods to an existing (and working) magento site. So
I'm working on a site that pulls various public RSS feeds down. I want
I am working on a site that require a php script running on a
I'm working on a site here where I include parts of the site that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.