Gracefully handle request for binary data as plain

This will return a 406 instead of a 500 in the event that a user
requests binary data but provides text/plain as the content type
that they accept.

Change-Id: I003c8306138db7d8d53d30c2e83f29a51aa3759d
Closes-Bug: #1220957
This commit is contained in:
Bryan D. Payne 2013-11-11 14:41:58 -08:00
parent 0dfb842b54
commit 4ec8cafc4b
2 changed files with 11 additions and 1 deletions

View File

@ -202,7 +202,10 @@ def denormalize_after_decryption(unencrypted, content_type):
# Process plain-text type.
if content_type in mime_types.PLAIN_TEXT:
# normalize text to binary string
unencrypted = unencrypted.decode('utf-8')
try:
unencrypted = unencrypted.decode('utf-8')
except UnicodeDecodeError:
raise CryptoAcceptNotSupportedException(content_type)
# Process binary type.
elif content_type not in mime_types.BINARY:

View File

@ -175,6 +175,13 @@ class WhenTestingDenormalizeAfterDecryption(unittest.TestCase):
em.denormalize_after_decryption(self.unencrypted,
self.content_type)
def test_decrypt_fail_binary_as_plain(self):
self.unencrypted = '\xff'
self.content_type = 'text/plain'
with self.assertRaises(em.CryptoAcceptNotSupportedException):
em.denormalize_after_decryption(self.unencrypted,
self.content_type)
class WhenTestingCryptoExtensionManager(unittest.TestCase):