2019-11-06 15:55:03 +01:00
|
|
|
from typing import Any
|
|
|
|
|
2017-01-20 11:34:05 +01:00
|
|
|
import bleach
|
|
|
|
|
2019-11-06 15:55:03 +01:00
|
|
|
from .rest_api import ValidationError
|
|
|
|
|
2018-07-09 23:22:26 +02:00
|
|
|
|
2017-01-20 11:34:05 +01:00
|
|
|
allowed_tags = [
|
2019-01-06 16:22:33 +01:00
|
|
|
"a",
|
|
|
|
"img", # links and images
|
|
|
|
"br",
|
|
|
|
"p",
|
|
|
|
"span",
|
|
|
|
"blockquote", # text layout
|
|
|
|
"strike",
|
2019-09-24 12:53:15 +02:00
|
|
|
"del",
|
|
|
|
"ins",
|
2019-01-06 16:22:33 +01:00
|
|
|
"strong",
|
|
|
|
"u",
|
|
|
|
"em",
|
|
|
|
"sup",
|
|
|
|
"sub",
|
|
|
|
"pre", # text formatting
|
|
|
|
"h1",
|
|
|
|
"h2",
|
|
|
|
"h3",
|
|
|
|
"h4",
|
|
|
|
"h5",
|
|
|
|
"h6", # headings
|
|
|
|
"ol",
|
|
|
|
"ul",
|
|
|
|
"li", # lists
|
|
|
|
"table",
|
|
|
|
"caption",
|
|
|
|
"thead",
|
|
|
|
"tbody",
|
|
|
|
"th",
|
|
|
|
"tr",
|
|
|
|
"td", # tables
|
2017-01-20 11:34:05 +01:00
|
|
|
]
|
|
|
|
allowed_attributes = {
|
2019-01-06 16:22:33 +01:00
|
|
|
"*": ["class", "style"],
|
|
|
|
"img": ["alt", "src", "title"],
|
|
|
|
"a": ["href", "title"],
|
|
|
|
"th": ["scope"],
|
|
|
|
"ol": ["start"],
|
2017-01-20 11:34:05 +01:00
|
|
|
}
|
|
|
|
allowed_styles = [
|
2019-01-06 16:22:33 +01:00
|
|
|
"color",
|
|
|
|
"background-color",
|
|
|
|
"height",
|
|
|
|
"width",
|
|
|
|
"text-align",
|
|
|
|
"float",
|
|
|
|
"padding",
|
2019-03-08 10:00:26 +01:00
|
|
|
"text-decoration",
|
2017-01-20 11:34:05 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
2017-08-24 12:26:55 +02:00
|
|
|
def validate_html(html: str) -> str:
|
2017-01-20 11:34:05 +01:00
|
|
|
"""
|
|
|
|
This method takes a string and escapes all non-whitelisted html entries.
|
|
|
|
Every field of a model that is loaded trusted in the DOM should be validated.
|
2018-03-24 08:08:21 +01:00
|
|
|
During copy and paste from Word maybe some tabs are spread over the html. Remove them.
|
2017-01-20 11:34:05 +01:00
|
|
|
"""
|
2019-01-06 16:22:33 +01:00
|
|
|
html = html.replace("\t", "")
|
2017-08-24 12:26:55 +02:00
|
|
|
return bleach.clean(
|
2019-01-06 16:22:33 +01:00
|
|
|
html, tags=allowed_tags, attributes=allowed_attributes, styles=allowed_styles
|
|
|
|
)
|
2019-11-06 15:55:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
def validate_json(json: Any, max_depth: int) -> Any:
|
|
|
|
"""
|
|
|
|
Traverses through the JSON structure (dicts and lists) and runs
|
|
|
|
validate_html on every found string.
|
|
|
|
|
|
|
|
Give max-depth to protect against stack-overflows. This should be the
|
|
|
|
maximum nested depth of the object expected.
|
|
|
|
"""
|
|
|
|
|
|
|
|
if max_depth == 0:
|
|
|
|
raise ValidationError({"detail": "The JSON is too nested."})
|
|
|
|
|
|
|
|
if isinstance(json, dict):
|
|
|
|
return {key: validate_json(value, max_depth - 1) for key, value in json.items()}
|
|
|
|
if isinstance(json, list):
|
|
|
|
return [validate_json(item, max_depth - 1) for item in json]
|
|
|
|
if isinstance(json, str):
|
|
|
|
return validate_html(json)
|
|
|
|
|
|
|
|
return json
|