From 4ec8cafc4bbdf628d609f26d664abfc7a2dc383c Mon Sep 17 00:00:00 2001 From: "Bryan D. Payne" Date: Mon, 11 Nov 2013 14:41:58 -0800 Subject: [PATCH] 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 --- barbican/crypto/extension_manager.py | 5 ++++- barbican/tests/crypto/test_extension_manager.py | 7 +++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/barbican/crypto/extension_manager.py b/barbican/crypto/extension_manager.py index 9033ac063..207d0ccd4 100644 --- a/barbican/crypto/extension_manager.py +++ b/barbican/crypto/extension_manager.py @@ -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: diff --git a/barbican/tests/crypto/test_extension_manager.py b/barbican/tests/crypto/test_extension_manager.py index 277f8594f..cceab2c25 100644 --- a/barbican/tests/crypto/test_extension_manager.py +++ b/barbican/tests/crypto/test_extension_manager.py @@ -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):