Skip to content
This repository was archived by the owner on May 16, 2019. It is now read-only.

Commit 8abe141

Browse files
committed
2 parents 3fa6c4f + ed99246 commit 8abe141

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

api/restapi.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from market.btcprice import BtcPrice
2929
from net.upnp import PortMapper
3030
from api import ALLOWED_TAGS, ALLOWED_ATTRIBUTES, ALLOWED_STYLES
31+
from api.utils import sanitize_html
3132

3233
DEFAULT_RECORDS_COUNT = 20
3334
DEFAULT_RECORDS_OFFSET = 0
@@ -494,7 +495,7 @@ def get_contract(self, request):
494495
def parse_contract(contract):
495496
if contract is not None:
496497
request.setHeader('content-type', "application/json")
497-
request.write(bleach.clean(json.dumps(contract, indent=4), tags=ALLOWED_TAGS).encode("utf-8"))
498+
request.write(json.dumps(sanitize_html(contract), indent=4))
498499
request.finish()
499500
else:
500501
request.write(json.dumps({}))

api/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
import bleach
2+
3+
from api import ALLOWED_TAGS, ALLOWED_ATTRIBUTES, ALLOWED_STYLES
4+
15
# pylint: disable=W1402
26
def smart_unicode(s, encoding='utf8'):
37
""" Convert str to unicode. If s is unicode, return itself.
@@ -33,3 +37,13 @@ def smart_str(s, encoding='utf8'):
3337
if isinstance(s, str):
3438
return s
3539
return s.encode(encoding)
40+
41+
def sanitize_html(value):
42+
""" Recursively sanitize all strings within a data structure. """
43+
if isinstance(value, dict):
44+
value = {k:sanitize_html(v) for k, v in value.iteritems()}
45+
elif isinstance(value, list):
46+
value = [sanitize_html(v) for v in value]
47+
elif isinstance(value, basestring):
48+
value = bleach.clean(value, tags=ALLOWED_TAGS, attributes=ALLOWED_ATTRIBUTES, styles=ALLOWED_STYLES)
49+
return value

0 commit comments

Comments
 (0)