|
| 1 | +from unittest.mock import MagicMock, patch |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +from azure.ai.ml.exceptions import MlException |
| 6 | + |
| 7 | +_CLOUD = {"storage_endpoint": "core.windows.net"} |
| 8 | + |
| 9 | + |
| 10 | +def _named(name, is_directory=False): |
| 11 | + item = MagicMock() |
| 12 | + item.name = name |
| 13 | + item.is_directory = is_directory |
| 14 | + return item |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.unittest |
| 18 | +class TestStorageDownloadTraversal: |
| 19 | + """Server-controlled blob/file names with ``..`` segments must not escape the destination.""" |
| 20 | + |
| 21 | + def test_blob_download_rejects_path_traversal(self, tmp_path): |
| 22 | + from azure.ai.ml._artifacts._blob_storage_helper import BlobStorageClient |
| 23 | + |
| 24 | + with patch("azure.ai.ml._artifacts._blob_storage_helper.BlobServiceClient"): |
| 25 | + client = BlobStorageClient( |
| 26 | + credential="cred", account_url="https://acct.blob.core.windows.net", container_name="c" |
| 27 | + ) |
| 28 | + client.container_client = MagicMock() |
| 29 | + client.container_client.list_blobs.return_value = [_named("asset/../escaped.txt")] |
| 30 | + blob_content = client.container_client.download_blob.return_value |
| 31 | + blob_content.size = 1 |
| 32 | + blob_content.content_as_bytes.return_value = b"data" |
| 33 | + |
| 34 | + dest = tmp_path / "dest" |
| 35 | + dest.mkdir() |
| 36 | + with patch("azure.ai.ml._artifacts._blob_storage_helper._blob_is_hdi_folder", return_value=False), patch( |
| 37 | + "azure.ai.ml._artifacts._blob_storage_helper._get_cloud_details", return_value=_CLOUD |
| 38 | + ): |
| 39 | + with pytest.raises(MlException): |
| 40 | + client.download(starts_with="asset/", destination=str(dest)) |
| 41 | + assert not (tmp_path / "escaped.txt").exists() |
| 42 | + |
| 43 | + def test_gen2_download_rejects_path_traversal(self, tmp_path): |
| 44 | + from azure.ai.ml._artifacts._gen2_storage_helper import Gen2StorageClient |
| 45 | + |
| 46 | + with patch("azure.ai.ml._artifacts._gen2_storage_helper.DataLakeServiceClient"): |
| 47 | + client = Gen2StorageClient( |
| 48 | + credential="cred", file_system="fs", account_url="https://acct.dfs.core.windows.net" |
| 49 | + ) |
| 50 | + client.file_system_client = MagicMock() |
| 51 | + client.file_system_client.get_paths.return_value = [_named("asset/../escaped.txt")] |
| 52 | + file_client = client.file_system_client.get_file_client.return_value |
| 53 | + file_client.get_file_properties.return_value.size = 1 |
| 54 | + file_client.download_file.return_value.readall.return_value = b"data" |
| 55 | + |
| 56 | + dest = tmp_path / "dest" |
| 57 | + dest.mkdir() |
| 58 | + with patch("azure.ai.ml._artifacts._gen2_storage_helper._get_cloud_details", return_value=_CLOUD): |
| 59 | + with pytest.raises(MlException): |
| 60 | + client.download(starts_with="asset/", destination=str(dest)) |
| 61 | + assert not (tmp_path / "escaped.txt").exists() |
| 62 | + |
| 63 | + def test_fileshare_recursive_download_rejects_path_traversal(self, tmp_path): |
| 64 | + from azure.ai.ml._artifacts._fileshare_storage_helper import recursive_download |
| 65 | + |
| 66 | + client = MagicMock() |
| 67 | + client.list_directories_and_files.return_value = [{"name": "../escaped.txt", "is_directory": False}] |
| 68 | + client.get_file_client.return_value.download_file.return_value.readall.return_value = b"data" |
| 69 | + |
| 70 | + dest = tmp_path / "dest" |
| 71 | + dest.mkdir() |
| 72 | + with pytest.raises(MlException): |
| 73 | + recursive_download(client, destination=str(dest), max_concurrency=1) |
| 74 | + assert not (tmp_path / "escaped.txt").exists() |
0 commit comments