|
| 1 | +"""Regression tests for GHSA-2h5x-4qc8-3x27. |
| 2 | +
|
| 3 | +`save_ad_image_locally` built its destination from two caller-supplied tool |
| 4 | +arguments with no validation: `os.path.join(output_dir, f"{ad_id}_{hash}.jpg")`. |
| 5 | +join() returns an absolute second argument unchanged and does not neutralize |
| 6 | +"..", and `os.makedirs(output_dir)` would build whatever tree was needed, so a |
| 7 | +caller could drop the downloaded image anywhere the process could write. |
| 8 | +
|
| 9 | +`resolve_ad_image_save_path` now decides the path, and it is called before |
| 10 | +anything touches the disk. |
| 11 | +""" |
| 12 | + |
| 13 | +import os |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from meta_ads_mcp.core.ads import UnsafeSavePathError, resolve_ad_image_save_path |
| 18 | + |
| 19 | +AD_ID = "120250589548040262" |
| 20 | +IMAGE_HASH = "60dff700e54f59a38ec6e626c7b2f3f5" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def base_dir(tmp_path, monkeypatch): |
| 25 | + """Point the allowed base at a scratch directory.""" |
| 26 | + base = tmp_path / "workdir" |
| 27 | + base.mkdir() |
| 28 | + monkeypatch.setenv("META_ADS_IMAGE_OUTPUT_DIR", str(base)) |
| 29 | + return base |
| 30 | + |
| 31 | + |
| 32 | +def test_default_output_dir_lands_under_the_base(base_dir): |
| 33 | + path = resolve_ad_image_save_path(AD_ID, IMAGE_HASH, "ad_images") |
| 34 | + |
| 35 | + assert path == str(base_dir / "ad_images" / f"{AD_ID}_{IMAGE_HASH}.jpg") |
| 36 | + |
| 37 | + |
| 38 | +def test_nested_subdirectories_are_allowed(base_dir): |
| 39 | + path = resolve_ad_image_save_path(AD_ID, IMAGE_HASH, "creatives/2026/q3") |
| 40 | + |
| 41 | + assert path.startswith(str(base_dir) + os.sep) |
| 42 | + |
| 43 | + |
| 44 | +def test_empty_output_dir_writes_into_the_base(base_dir): |
| 45 | + path = resolve_ad_image_save_path(AD_ID, IMAGE_HASH, "") |
| 46 | + |
| 47 | + assert path == str(base_dir / f"{AD_ID}_{IMAGE_HASH}.jpg") |
| 48 | + |
| 49 | + |
| 50 | +@pytest.mark.parametrize( |
| 51 | + "output_dir", |
| 52 | + [ |
| 53 | + "/etc/cron.d", # absolute path discards the base in join() |
| 54 | + "/tmp", |
| 55 | + "../escaped", # traversal |
| 56 | + "../../../../tmp/evil", |
| 57 | + "ad_images/../../outside", # traversal after a legitimate-looking prefix |
| 58 | + ], |
| 59 | +) |
| 60 | +def test_output_dir_escapes_are_rejected(base_dir, output_dir): |
| 61 | + with pytest.raises(UnsafeSavePathError): |
| 62 | + resolve_ad_image_save_path(AD_ID, IMAGE_HASH, output_dir) |
| 63 | + |
| 64 | + |
| 65 | +@pytest.mark.parametrize( |
| 66 | + "ad_id", |
| 67 | + [ |
| 68 | + "/tmp/PWNED", # absolute id discards the directory |
| 69 | + "../../../../tmp/evil", # traversal through the filename |
| 70 | + "123/../../etc/passwd", |
| 71 | + "not-an-id", |
| 72 | + "", |
| 73 | + "12 3", |
| 74 | + ], |
| 75 | +) |
| 76 | +def test_non_numeric_ad_ids_are_rejected(base_dir, ad_id): |
| 77 | + with pytest.raises(UnsafeSavePathError): |
| 78 | + resolve_ad_image_save_path(ad_id, IMAGE_HASH, "ad_images") |
| 79 | + |
| 80 | + |
| 81 | +@pytest.mark.parametrize("image_hash", ["../../evil", "a/b", "", "hash with spaces"]) |
| 82 | +def test_unsafe_image_hashes_are_rejected(base_dir, image_hash): |
| 83 | + with pytest.raises(UnsafeSavePathError): |
| 84 | + resolve_ad_image_save_path(AD_ID, image_hash, "ad_images") |
| 85 | + |
| 86 | + |
| 87 | +def test_symlink_out_of_the_base_is_rejected(base_dir, tmp_path): |
| 88 | + """realpath resolution is what catches this, not string matching.""" |
| 89 | + outside = tmp_path / "outside" |
| 90 | + outside.mkdir() |
| 91 | + (base_dir / "escape").symlink_to(outside) |
| 92 | + |
| 93 | + with pytest.raises(UnsafeSavePathError): |
| 94 | + resolve_ad_image_save_path(AD_ID, IMAGE_HASH, "escape") |
| 95 | + |
| 96 | + |
| 97 | +def test_rejection_creates_nothing_on_disk(base_dir): |
| 98 | + """The path is resolved before any makedirs, so a rejected call is inert.""" |
| 99 | + before = sorted(os.listdir(base_dir)) |
| 100 | + |
| 101 | + with pytest.raises(UnsafeSavePathError): |
| 102 | + resolve_ad_image_save_path(AD_ID, IMAGE_HASH, "../../../../tmp/evil-tree") |
| 103 | + |
| 104 | + assert sorted(os.listdir(base_dir)) == before |
| 105 | + assert not os.path.exists("/tmp/evil-tree") |
| 106 | + |
| 107 | + |
| 108 | +def test_base_defaults_to_the_working_directory(tmp_path, monkeypatch): |
| 109 | + monkeypatch.delenv("META_ADS_IMAGE_OUTPUT_DIR", raising=False) |
| 110 | + monkeypatch.chdir(tmp_path) |
| 111 | + |
| 112 | + path = resolve_ad_image_save_path(AD_ID, IMAGE_HASH, "ad_images") |
| 113 | + |
| 114 | + assert path == os.path.join(os.path.realpath(tmp_path), "ad_images", f"{AD_ID}_{IMAGE_HASH}.jpg") |
0 commit comments