Skip to content

Commit

Permalink
fix handling of normal strings inside request payload
Browse files Browse the repository at this point in the history
  • Loading branch information
Bogdanp committed Mar 8, 2017
1 parent 16ae227 commit b0e5323
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
8 changes: 7 additions & 1 deletion markii/markii.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,12 @@ def dict_to_kv(d):
_ascii_range = range(0, 128)


def _b2i(b):
if isinstance(b, six.integer_types):
return b
return ord(b)


def sanitize(d):
"""Ensures that all values inside of the given dictionary are
represent valid ascii. Child dictionaries are sanitized
Expand All @@ -75,7 +81,7 @@ def sanitize(d):
else:
value = six.binary_type(value)

if not all(b in _ascii_range for b in value):
if all(_b2i(b) not in _ascii_range for b in value):
sanitized_d[key] = base64.b64encode(value)
else:
sanitized_d[key] = value
Expand Down
5 changes: 5 additions & 0 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ def f():
assert markii(e)


def test_rendering_normal_data_from_request():
assert "supercalifragilisticexpialidocious" in markii(
Exception("an error"), {"body": b"supercalifragilisticexpialidocious"})


def test_rendering_binary_data_from_request():
assert "gA==" in markii(Exception("an error"), {"body": b"\x80"})

Expand Down

0 comments on commit b0e5323

Please sign in to comment.