|
3 | 3 | # Licensed under the MIT License. See License.txt in the project root for license information. |
4 | 4 | # -------------------------------------------------------------------------------------------- |
5 | 5 |
|
| 6 | +import base64 |
| 7 | +from unittest import TestCase, mock |
| 8 | + |
| 9 | +from azure.storage.blob import BlobServiceClient |
| 10 | + |
6 | 11 | from azure.cli.testsdk import LiveScenarioTest, ResourceGroupPreparer, StorageAccountPreparer, JMESPathCheck |
| 12 | +from ...operations import blob as blob_operations |
7 | 13 | from ..storage_test_util import StorageScenarioMixin |
8 | 14 |
|
9 | 15 |
|
| 16 | +class StorageBlobCopySecurityTests(TestCase): |
| 17 | + def test_storage_blob_copy_preserves_source_sas_for_custom_endpoint(self): |
| 18 | + account_key = base64.b64encode(b'account-key').decode() |
| 19 | + service_client = BlobServiceClient( |
| 20 | + account_url='https://storage.internal.example', |
| 21 | + credential={'account_name': 'account', 'account_key': account_key}) |
| 22 | + destination_client = service_client.get_blob_client(container='dst', blob='output') |
| 23 | + source_url = 'https://storage.internal.example/src/input?sp=r&sig=existing' |
| 24 | + |
| 25 | + self.assertIsNone(destination_client.from_blob_url(source_url).account_name) |
| 26 | + |
| 27 | + with mock.patch.object(destination_client, 'upload_blob_from_url') as upload_blob_from_url: |
| 28 | + blob_operations.copy_blob( |
| 29 | + mock.MagicMock(), destination_client, source_url, |
| 30 | + requires_sync=False, destination_blob_type='BlockBlob') |
| 31 | + |
| 32 | + upload_blob_from_url.assert_called_once_with( |
| 33 | + source_url=source_url, overwrite=True, tags=None, destination_lease=None, |
| 34 | + standard_blob_tier=None, source_if_modified_since=None, source_if_unmodified_since=None, |
| 35 | + if_modified_since=None, if_unmodified_since=None, timeout=None) |
| 36 | + |
| 37 | + def test_storage_blob_copy_source_origin_normalization(self): |
| 38 | + equivalent_origins = [ |
| 39 | + ('https://account.blob.core.windows.net/container/source', |
| 40 | + 'https://ACCOUNT.blob.core.windows.net:443/container/destination'), |
| 41 | + ('https://account.blob.core.usgovcloudapi.net./container/source', |
| 42 | + 'https://account.blob.core.usgovcloudapi.net/container/destination'), |
| 43 | + ('https://account.blob.core.windows.net。/container/source', |
| 44 | + 'https://account.blob.core.windows.net/container/destination'), |
| 45 | + ('https://xn--bcher-kva.example/container/source', |
| 46 | + 'https://bücher.example:443/container/destination'), |
| 47 | + ('http://127.0.0.1:10000/account/container/source', |
| 48 | + 'http://127.0.0.1:10000/account/container/destination'), |
| 49 | + ('http://[::1]:10000/account/container/source', |
| 50 | + 'http://[0:0:0:0:0:0:0:1]:10000/account/container/destination'), |
| 51 | + ] |
| 52 | + for source_url, destination_url in equivalent_origins: |
| 53 | + with self.subTest(source_url=source_url, destination_url=destination_url): |
| 54 | + self.assertTrue(blob_operations._same_url_origin(source_url, destination_url)) |
| 55 | + |
| 56 | + different_origins = [ |
| 57 | + ('https://account.blob.core.windows.net.attacker.example/container/source', |
| 58 | + 'https://account.blob.core.windows.net/container/destination'), |
| 59 | + ('https://account.blob.core.windows.net@attacker.example/container/source', |
| 60 | + 'https://account.blob.core.windows.net/container/destination'), |
| 61 | + ('http://account.blob.core.windows.net/container/source', |
| 62 | + 'https://account.blob.core.windows.net/container/destination'), |
| 63 | + ('https://account.blob.core.windows.net:444/container/source', |
| 64 | + 'https://account.blob.core.windows.net/container/destination'), |
| 65 | + ('https://faß.example/container/source', |
| 66 | + 'https://fass.example/container/destination'), |
| 67 | + ('https://account.blob.core.windows.net/container/source', |
| 68 | + 'https://custom.example/container/destination'), |
| 69 | + ('not-a-url', 'https://account.blob.core.windows.net/container/destination'), |
| 70 | + ] |
| 71 | + for source_url, destination_url in different_origins: |
| 72 | + with self.subTest(source_url=source_url, destination_url=destination_url): |
| 73 | + self.assertFalse(blob_operations._same_url_origin(source_url, destination_url)) |
| 74 | + |
| 75 | + @mock.patch.object(blob_operations, 'generate_sas_blob_uri') |
| 76 | + def test_storage_blob_copy_does_not_reuse_credentials_for_untrusted_source(self, generate_sas): |
| 77 | + source_url = 'https://account.blob.core.windows.net.attacker.example/container/source' |
| 78 | + |
| 79 | + for blob_type in ('BlockBlob', 'AppendBlob', 'PageBlob'): |
| 80 | + with self.subTest(blob_type=blob_type): |
| 81 | + destination_client = mock.MagicMock() |
| 82 | + destination_client.account_name = 'account' |
| 83 | + destination_client.url = 'https://account.blob.core.windows.net/container/destination' |
| 84 | + destination_client.credential = mock.sentinel.destination_credential |
| 85 | + |
| 86 | + source_client = mock.MagicMock() |
| 87 | + source_client.account_name = 'account' |
| 88 | + source_client.get_blob_properties.return_value.size = 512 |
| 89 | + source_service_client = source_client._get_container_client.return_value. \ |
| 90 | + _get_blob_service_client.return_value |
| 91 | + source_service_client.credential = None |
| 92 | + destination_client.from_blob_url.return_value = source_client |
| 93 | + |
| 94 | + cmd = mock.MagicMock() |
| 95 | + blob_operations.copy_blob(cmd, destination_client, source_url, |
| 96 | + requires_sync=False, destination_blob_type=blob_type) |
| 97 | + |
| 98 | + self.assertTrue(all('credential' not in call.kwargs |
| 99 | + for call in destination_client.from_blob_url.call_args_list)) |
| 100 | + generate_sas.assert_not_called() |
| 101 | + generate_sas.reset_mock() |
| 102 | + |
| 103 | + @mock.patch.object(blob_operations, 'generate_sas_blob_uri') |
| 104 | + def test_storage_blob_copy_reuses_credentials_for_same_source_origin(self, generate_sas): |
| 105 | + source_url = 'https://ACCOUNT.blob.core.windows.net:443/container/source' |
| 106 | + signed_source_url = source_url + '?sig=generated' |
| 107 | + generate_sas.return_value = signed_source_url |
| 108 | + |
| 109 | + destination_client = mock.MagicMock() |
| 110 | + destination_client.account_name = 'account' |
| 111 | + destination_client.url = 'https://account.blob.core.windows.net/container/destination' |
| 112 | + destination_client.credential = mock.MagicMock(account_key='key') |
| 113 | + |
| 114 | + anonymous_source_client = mock.MagicMock() |
| 115 | + anonymous_source_client.account_name = 'ACCOUNT' |
| 116 | + authenticated_source_client = mock.MagicMock() |
| 117 | + authenticated_source_service = authenticated_source_client._get_container_client.return_value. \ |
| 118 | + _get_blob_service_client.return_value |
| 119 | + authenticated_source_service.credential = destination_client.credential |
| 120 | + destination_client.from_blob_url.side_effect = [anonymous_source_client, authenticated_source_client] |
| 121 | + |
| 122 | + cmd = mock.MagicMock() |
| 123 | + blob_operations.copy_blob(cmd, destination_client, source_url, |
| 124 | + requires_sync=False, destination_blob_type='BlockBlob') |
| 125 | + |
| 126 | + destination_client.from_blob_url.assert_has_calls([ |
| 127 | + mock.call(source_url), |
| 128 | + mock.call(source_url, credential=destination_client.credential) |
| 129 | + ]) |
| 130 | + generate_sas.assert_called_once() |
| 131 | + destination_client.upload_blob_from_url.assert_called_once_with( |
| 132 | + source_url=signed_source_url, overwrite=True, tags=None, destination_lease=None, |
| 133 | + standard_blob_tier=None, source_if_modified_since=None, source_if_unmodified_since=None, |
| 134 | + if_modified_since=None, if_unmodified_since=None, timeout=None) |
| 135 | + |
| 136 | + @mock.patch.object(blob_operations, 'generate_sas_blob_uri') |
| 137 | + def test_storage_blob_copy_reuses_credentials_for_validated_same_account_source(self, generate_sas): |
| 138 | + source_url = 'https://account.blob.core.windows.net/src/input' |
| 139 | + signed_source_url = source_url + '?sig=generated' |
| 140 | + generate_sas.return_value = signed_source_url |
| 141 | + |
| 142 | + destination_client = mock.MagicMock() |
| 143 | + destination_client.url = 'https://storage.internal.example/dst/output' |
| 144 | + destination_client.credential = mock.MagicMock(account_key='key') |
| 145 | + |
| 146 | + anonymous_source_client = mock.MagicMock() |
| 147 | + authenticated_source_client = mock.MagicMock() |
| 148 | + source_service_client = authenticated_source_client._get_container_client.return_value. \ |
| 149 | + _get_blob_service_client.return_value |
| 150 | + source_service_client.credential = destination_client.credential |
| 151 | + destination_client.from_blob_url.side_effect = [anonymous_source_client, authenticated_source_client] |
| 152 | + |
| 153 | + cmd = mock.MagicMock() |
| 154 | + blob_operations.copy_blob( |
| 155 | + cmd, destination_client, source_url, source_is_validated_same_account=True, |
| 156 | + requires_sync=False, destination_blob_type='BlockBlob') |
| 157 | + |
| 158 | + destination_client.from_blob_url.assert_has_calls([ |
| 159 | + mock.call(source_url), |
| 160 | + mock.call(source_url, credential=destination_client.credential) |
| 161 | + ]) |
| 162 | + generate_sas.assert_called_once() |
| 163 | + destination_client.upload_blob_from_url.assert_called_once_with( |
| 164 | + source_url=signed_source_url, overwrite=True, tags=None, destination_lease=None, |
| 165 | + standard_blob_tier=None, source_if_modified_since=None, source_if_unmodified_since=None, |
| 166 | + if_modified_since=None, if_unmodified_since=None, timeout=None) |
| 167 | + |
| 168 | + @mock.patch.object(blob_operations, 'generate_sas_blob_uri') |
| 169 | + def test_storage_blob_copy_does_not_reuse_credentials_across_path_style_accounts(self, generate_sas): |
| 170 | + source_url = 'http://127.0.0.1:10000/account2/src/input' |
| 171 | + |
| 172 | + destination_client = mock.MagicMock() |
| 173 | + destination_client.account_name = 'account1' |
| 174 | + destination_client.url = 'http://127.0.0.1:10000/account1/dst/output' |
| 175 | + destination_client.credential = mock.sentinel.destination_credential |
| 176 | + |
| 177 | + source_client = mock.MagicMock() |
| 178 | + source_client.account_name = 'account2' |
| 179 | + source_service_client = source_client._get_container_client.return_value. \ |
| 180 | + _get_blob_service_client.return_value |
| 181 | + source_service_client.credential = None |
| 182 | + destination_client.from_blob_url.return_value = source_client |
| 183 | + |
| 184 | + cmd = mock.MagicMock() |
| 185 | + blob_operations.copy_blob( |
| 186 | + cmd, destination_client, source_url, requires_sync=False, destination_blob_type='BlockBlob') |
| 187 | + |
| 188 | + destination_client.from_blob_url.assert_called_once_with(source_url) |
| 189 | + generate_sas.assert_not_called() |
| 190 | + |
| 191 | + |
10 | 192 | class StorageBlobCopyTests(StorageScenarioMixin, LiveScenarioTest): |
11 | 193 | @ResourceGroupPreparer() |
12 | 194 | @StorageAccountPreparer(parameter_name='source_account') |
|
0 commit comments