Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What characters are safe to not escape with CSS value context?

I have a string that arrives from the user, and is then inserted into a large CSS block using a CSS parser.

CSS escaping can be done with \C (where C is a character), \HexOfC  (with a space) or \6DigitHexOfC.

Generally, all characters can be escaped safely, and CSS would still run as expected. The following works:

div {
  background: \23 f66;
}
<div>Test</div>

However, I still want the CSS properties to be as "clean" as possible, because I want, for example, to be able to view URLs and rules cleanly with the inspector.


There are characters which are obviously bad. {};\* should all be escaped because they can be used to break from the current rule. I am managing a whitelist (everything gets escaped, except for what's allowed) of characters (as opposed to a blacklist where everything is allowed, except for what's not). The whitelisted characters I currently have is

'#', ',', '.', '(', ')', '-', '%', '+', '=', '/', ' ', ':', '\'', '"', '\n', '\r'

Are there dangerous characters here? Anything that can be used to break out of a rule and affect the rest of the CSS block. Are there characters which aren't here which would get unneededly escaped? (Alphanumeric characters are not escaped by default).

like image 317
Madara's Ghost Avatar asked Oct 02 '14 09:10

Madara's Ghost


People also ask

How do you escape a character in CSS?

CSS escapes. CSS represents escaped characters in a different way. Escapes start with a backslash followed by the hexadecimal number that represents the character's hexadecimal Unicode code point value. If there is a following character that is not in the range A–F, a–f or 0–9, that is all you need.

What is HTML &GT?

&gt; and &lt; is a character entity reference for the > and < character in HTML. It is not possible to use the less than (<) or greater than (>) signs in your file, because the browser will mix them with tags. for these difficulties you can use entity names( &gt; ) and entity numbers( &#60; ).

What is contextual escaping in Java?

In this context, escaping means to replace them with HTML character entities. For example, < can be replaced with &lt; . Another character entity that's occasionally useful is &nbsp; (the non-breaking space).


1 Answers

Instead of sanitizing input, you could simply allow elements to be transfered.

Basically a client generated structur file:

[
    MyDiv: { # Key
        background: "#FFFFFF" # Element
    }
]

In this case you just have to create a File.

Dummy Code:

StringBuilder sb = new StringBuilder();
foreach(String key: structure.getKeys()) {
    final List<Element> e = structure.getElements(key);
    sb.append(".") // This may be changed of course
      .append(key) // ID or class based on type above
      .append("{")
      // Append Elements
      .append("}");
}

Generating Elements should be easy.

Each element is just

S := element-key : element-value;

Then you'd be able to whitelist special commands too.

If you wish to keep sanitizing, take a look here: http://www.w3.org/TR/CSS21/grammar.html#scanner

like image 171
manf Avatar answered Sep 16 '22 11:09

manf