Interface SafeStyles
-
- All Superinterfaces:
java.io.Serializable
- All Known Implementing Classes:
SafeStylesString
public interface SafeStyles extends java.io.Serializable
An object that implements this interface encapsulates zero or more CSS properties that are guaranteed to be safe to use (with respect to potential Cross-Site-Scripting vulnerabilities) in a CSS (Cascading Style Sheet) attribute context. A CSS attribute context can be inside of a CSS rule in astyle
element, or inside thestyle
attribute of a DOM element.Note on usage:
SafeStyles
should be used to ensure user input is not executed in the browser.SafeStyles
should not be used to sanitize input before sending it to the server: The server cannot rely on the type contract ofSafeStyles
values received from clients, because a malicious client could provide maliciously crafted serialized forms of implementations of this type that violate the type contract.All implementing classes must maintain the class invariant (by design and implementation and/or convention of use), that invoking
asString()
on any instance will return a string that is safe to assign to a CSS attribute in a browser, in the sense that doing so must not cause execution of script in the browser. Generally,SafeStyles
should be of the formcssPropertyName:value;
, where neither the name nor the value contain malicious scripts.SafeStyles
may never contain literal angle brackets. Otherwise, it could be unsafe to place aSafeStyles
into a <style> tag (where it can't be HTML escaped). For example, if theSafeStyles
containing "font: 'foo <style><script>evil</script>
'" is used in a style sheet in a <style> tag, this could then break out of the style context into HTML.SafeStyles
may contain literal single or double quotes, and as such the entire style string must be escaped when used in a style attribute (if this were not the case, the string could contain a matching quote that would escape from the style attribute).Furthermore, values of this type must be composable, i.e. for any two values
A
andB
of this type,A.asString() + B.asString()
must itself be a value that satisfies theSafeStyles
type constraint. This requirement implies that for any valueA
of this type,A.asString()
must not end in a "CSS value" or "CSS name" context. For example, a value ofbackground:url("
orfont-
would not satisfy theSafeStyles
contract. This is because concatenating such strings with a second value that itself does not contain unsafe CSS can result in an overall string that does. For example, ifjavascript:evil())"
is appended tobackground:url("
, the resulting string may result in the execution of a malicious script.The following example values comply with this type's contract:
width: 1em;
height:1em;
width: 1em;height: 1em;
background:url('http://url');
The following example values do not comply with this type's contract:
background: red
(missing a trailing semi-colon)background:
(missing a value and a trailing semi-colon)1em
(missing an attribute name, which provides context for the value)
All implementations must implement equals() and hashCode() to behave consistently with the result of asString().equals() and asString.hashCode().
Implementations must not return
null
fromasString()
.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description java.lang.String
asString()
Returns this object's contained CSS as a string.boolean
equals(java.lang.Object anObject)
Compares this string to the specified object.int
hashCode()
Returns a hash code for this string.
-
-
-
Method Detail
-
asString
java.lang.String asString()
Returns this object's contained CSS as a string.Based on this class' contract, the returned value will be non-null and a string that is safe to use in an CSS attribute context.
- Returns:
- the contents as a String
-
equals
boolean equals(java.lang.Object anObject)
Compares this string to the specified object. Must be equal to asString().equals().- Overrides:
equals
in classjava.lang.Object
- Parameters:
anObject
- the object to compare to
-
hashCode
int hashCode()
Returns a hash code for this string. Must be equal to asString().hashCode().- Overrides:
hashCode
in classjava.lang.Object
-
-