public final class JsonSanitizer extends Object
be conservative in what you do, be liberal in what you accept from others
Applied to JSON-like content from others, it will produce well-formed JSON that should satisfy any parser you use.
Applied to your output before you send, it will coerce minor mistakes in encoding and make it easier to embed your JSON in HTML and XML.
'...' Single quoted strings are converted to JSON strings.
\xAB Hex escapes are converted to JSON unicode escapes.
\012 Octal escapes are converted to JSON unicode escapes.
0xAB Hex integer literals are converted to JSON decimal numbers.
012 Octal integer literals are converted to JSON decimal numbers.
+.5 Decimal numbers are coerced to JSON's stricter format.
[0,,2] Elisions in arrays are filled with null.
[1,2,3,] Trailing commas are removed.
{foo:"bar"} Unquoted property names are quoted.
//comments JS style line and block comments are removed.
(...) Grouping parentheses are removed.
null is substituted.
"</script" so can be embedded inside an HTML script element without
further encoding.
"]]>" so can be
embedded inside an XML CDATA section without further encoding.eval builtin (after being wrapped in parentheses)
or by JSON.parse.
Specifically, the output will not contain any string literals with embedded
JS newlines (U+2028 Paragraph separator or U+2029 Line separator).
eval will
have no side-effects and no free variables, so is neither a code-injection
vector, nor a vector for exfiltration of secrets.
This library only ensures that the JSON string → Javascript object
phase has no side effects and resolves no free variables, and cannot control
how other client side code later interprets the resulting Javascript object.
So if client-side code takes a part of the parsed data that is controlled by
an attacker and passes it back through a powerful interpreter like
eval or innerHTML then that client-side code might suffer
unintended side-effects.
The sanitize method takes O(n) time where n is the length in UTF-16 code-units.
| Modifier and Type | Field and Description |
|---|---|
static int |
DEFAULT_NESTING_DEPTH
The default for the maximumNestingDepth constructor parameter.
|
static int |
MAXIMUM_NESTING_DEPTH
The maximum value for the maximumNestingDepth constructor parameter.
|
| Modifier and Type | Method and Description |
|---|---|
static String |
sanitize(String jsonish)
Given JSON-like content, produces a string of JSON that is safe to embed,
safe to pass to JavaScript's
eval operator. |
static String |
sanitize(String jsonish,
int maximumNestingDepth)
Same as
sanitize(String), but allows to set a custom
maximum nesting depth. |
String |
toString() |
public static final int DEFAULT_NESTING_DEPTH
public static final int MAXIMUM_NESTING_DEPTH
public static String sanitize(String jsonish)
eval operator.jsonish - JSON-like content.public static String sanitize(String jsonish, int maximumNestingDepth)
sanitize(String), but allows to set a custom
maximum nesting depth.jsonish - JSON-like content.maximumNestingDepth - maximum nesting depth.Copyright © 2017 OWASP. All rights reserved.