Merge pull request #1880 from mhils/1877

Fix #1877
This commit is contained in:
Maximilian Hils 2016-12-19 19:04:45 +01:00 committed by GitHub
commit 77cd9224f9
2 changed files with 14 additions and 1 deletions

View File

@ -103,7 +103,11 @@ class Message(serializable.Serializable):
ce = self.headers.get("content-encoding")
if ce:
try:
return encoding.decode(self.raw_content, ce)
content = encoding.decode(self.raw_content, ce)
# A client may illegally specify a byte -> str encoding here (e.g. utf8)
if isinstance(content, str):
raise ValueError("Invalid Content-Encoding: {}".format(ce))
return content
except ValueError:
if strict:
raise

View File

@ -141,6 +141,15 @@ class TestMessageContentEncoding:
assert r.headers["content-encoding"]
assert r.get_content(strict=False) == b"foo"
def test_utf8_as_ce(self):
r = tutils.tresp()
r.headers["content-encoding"] = "utf8"
r.raw_content = b"foo"
with tutils.raises(ValueError):
assert r.content
assert r.headers["content-encoding"]
assert r.get_content(strict=False) == b"foo"
def test_cannot_decode(self):
r = tutils.tresp()
r.encode("gzip")