diff --git a/sdk/storage/azure-storage-blob/azure/storage/blob/_shared/base_client.py b/sdk/storage/azure-storage-blob/azure/storage/blob/_shared/base_client.py index 9ad754b92b14..7ae31d69679c 100644 --- a/sdk/storage/azure-storage-blob/azure/storage/blob/_shared/base_client.py +++ b/sdk/storage/azure-storage-blob/azure/storage/blob/_shared/base_client.py @@ -317,9 +317,10 @@ def format_shared_key_credential(account, credential): def parse_connection_str(conn_str, credential, service): conn_str = conn_str.rstrip(";") - conn_settings = dict( # pylint: disable=consider-using-dict-comprehension - [s.split("=", 1) for s in conn_str.split(";")] - ) + conn_settings = [s.split("=", 1) for s in conn_str.split(";")] + if any(len(tup) != 2 for tup in conn_settings): + raise ValueError("Connection string is either blank or malformed.") + conn_settings = dict(conn_settings) endpoints = _SERVICE_PARAMS[service] primary = None secondary = None diff --git a/sdk/storage/azure-storage-blob/tests/test_blob_client.py b/sdk/storage/azure-storage-blob/tests/test_blob_client.py index 9c1393206320..18a4dcd38bf1 100644 --- a/sdk/storage/azure-storage-blob/tests/test_blob_client.py +++ b/sdk/storage/azure-storage-blob/tests/test_blob_client.py @@ -559,5 +559,20 @@ def callback(response): custom_headers = {'User-Agent': 'customer_user_agent'} service.get_service_properties(raw_response_hook=callback, headers=custom_headers) + def test_error_with_malformed_conn_str(self): + # Arrange + for conn_str in ["", "foobar", "foo;bar;baz", ";", "foobar=baz=foo" , "foo=;bar=;", "=", "=;=="]: + for service_type in SERVICES.items(): + # Act + with self.assertRaises(ValueError) as e: + service = service_type[0].from_connection_string(conn_str, blob_name="test", container_name="foo/bar") + + if conn_str in("", "foobar", "foo;bar;baz", ";"): + self.assertEqual( + str(e.exception), "Connection string is either blank or malformed.") + elif conn_str in ("foobar=baz=foo" , "foo=;bar=;", "=", "=;=="): + self.assertEqual( + str(e.exception), "Connection string missing required connection details.") + # ------------------------------------------------------------------------------ diff --git a/sdk/storage/azure-storage-file-share/azure/storage/fileshare/_shared/base_client.py b/sdk/storage/azure-storage-file-share/azure/storage/fileshare/_shared/base_client.py index 9ad754b92b14..7ae31d69679c 100644 --- a/sdk/storage/azure-storage-file-share/azure/storage/fileshare/_shared/base_client.py +++ b/sdk/storage/azure-storage-file-share/azure/storage/fileshare/_shared/base_client.py @@ -317,9 +317,10 @@ def format_shared_key_credential(account, credential): def parse_connection_str(conn_str, credential, service): conn_str = conn_str.rstrip(";") - conn_settings = dict( # pylint: disable=consider-using-dict-comprehension - [s.split("=", 1) for s in conn_str.split(";")] - ) + conn_settings = [s.split("=", 1) for s in conn_str.split(";")] + if any(len(tup) != 2 for tup in conn_settings): + raise ValueError("Connection string is either blank or malformed.") + conn_settings = dict(conn_settings) endpoints = _SERVICE_PARAMS[service] primary = None secondary = None diff --git a/sdk/storage/azure-storage-file-share/tests/test_file_client.py b/sdk/storage/azure-storage-file-share/tests/test_file_client.py index 2a0cf4e43db7..b2139249a434 100644 --- a/sdk/storage/azure-storage-file-share/tests/test_file_client.py +++ b/sdk/storage/azure-storage-file-share/tests/test_file_client.py @@ -401,6 +401,21 @@ def callback(response): custom_headers = {'User-Agent': 'customer_user_agent'} service.get_service_properties(raw_response_hook=callback, headers=custom_headers) + def test_error_with_malformed_conn_str(self): + # Arrange + + for conn_str in ["", "foobar", "foobar=baz=foo", "foo;bar;baz", "foo=;bar=;", "=", ";", "=;=="]: + for service_type in SERVICES.items(): + # Act + with self.assertRaises(ValueError) as e: + service = service_type[0].from_connection_string(conn_str, share_name="test", directory_path="foo/bar", file_path="temp/dat") + + if conn_str in("", "foobar", "foo;bar;baz", ";"): + self.assertEqual( + str(e.exception), "Connection string is either blank or malformed.") + elif conn_str in ("foobar=baz=foo" , "foo=;bar=;", "=", "=;=="): + self.assertEqual( + str(e.exception), "Connection string missing required connection details.") # ------------------------------------------------------------------------------ if __name__ == '__main__': diff --git a/sdk/storage/azure-storage-queue/azure/storage/queue/_shared/base_client.py b/sdk/storage/azure-storage-queue/azure/storage/queue/_shared/base_client.py index 9ad754b92b14..7ae31d69679c 100644 --- a/sdk/storage/azure-storage-queue/azure/storage/queue/_shared/base_client.py +++ b/sdk/storage/azure-storage-queue/azure/storage/queue/_shared/base_client.py @@ -317,9 +317,10 @@ def format_shared_key_credential(account, credential): def parse_connection_str(conn_str, credential, service): conn_str = conn_str.rstrip(";") - conn_settings = dict( # pylint: disable=consider-using-dict-comprehension - [s.split("=", 1) for s in conn_str.split(";")] - ) + conn_settings = [s.split("=", 1) for s in conn_str.split(";")] + if any(len(tup) != 2 for tup in conn_settings): + raise ValueError("Connection string is either blank or malformed.") + conn_settings = dict(conn_settings) endpoints = _SERVICE_PARAMS[service] primary = None secondary = None diff --git a/sdk/storage/azure-storage-queue/tests/test_queue_client.py b/sdk/storage/azure-storage-queue/tests/test_queue_client.py index 47c36d7e2339..e4b21e8120c4 100644 --- a/sdk/storage/azure-storage-queue/tests/test_queue_client.py +++ b/sdk/storage/azure-storage-queue/tests/test_queue_client.py @@ -458,6 +458,23 @@ def test_create_queue_client_with_complete_queue_url(self, resource_group, locat # Assert self.assertEqual(service.scheme, 'https') self.assertEqual(service.queue_name, 'bar') + + def test_error_with_malformed_conn_str(self): + # Arrange + + for conn_str in ["", "foobar", "foobar=baz=foo", "foo;bar;baz", "foo=;bar=;", "=", ";", "=;=="]: + for service_type in SERVICES.items(): + # Act + with self.assertRaises(ValueError) as e: + service = service_type[0].from_connection_string(conn_str, queue_name="test") + + if conn_str in("", "foobar", "foo;bar;baz", ";"): + self.assertEqual( + str(e.exception), "Connection string is either blank or malformed.") + elif conn_str in ("foobar=baz=foo" , "foo=;bar=;", "=", "=;=="): + self.assertEqual( + str(e.exception), "Connection string missing required connection details.") + # ------------------------------------------------------------------------------ if __name__ == '__main__': unittest.main()