|
| 1 | +"""Regression tests for GHSA-prmg-4fr3-mm6x. |
| 2 | +
|
| 3 | +The token cache holds a long-lived Meta access token (~60 days). It was written |
| 4 | +with a bare `open(path, "w")` into a directory created by `mkdir()` with no |
| 5 | +mode, so under the usual 0022 umask it landed at 0644 inside 0755 — readable by |
| 6 | +every other local user on a shared host. |
| 7 | +""" |
| 8 | + |
| 9 | +import json |
| 10 | +import os |
| 11 | +import platform |
| 12 | +import stat |
| 13 | +import time |
| 14 | + |
| 15 | +import pytest |
| 16 | + |
| 17 | +from meta_ads_mcp.core.auth import ( |
| 18 | + TOKEN_CACHE_DIR_MODE, |
| 19 | + TOKEN_CACHE_FILE_MODE, |
| 20 | + AuthManager, |
| 21 | + TokenInfo, |
| 22 | + _restrict_permissions, |
| 23 | +) |
| 24 | + |
| 25 | +pytestmark = pytest.mark.skipif( |
| 26 | + platform.system() == "Windows", |
| 27 | + reason="POSIX permission bits do not apply on Windows", |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def _mode(path) -> int: |
| 32 | + return stat.S_IMODE(os.stat(path).st_mode) |
| 33 | + |
| 34 | + |
| 35 | +@pytest.fixture |
| 36 | +def home(tmp_path, monkeypatch): |
| 37 | + """Point the platform cache lookup at a scratch HOME.""" |
| 38 | + monkeypatch.setenv("HOME", str(tmp_path)) |
| 39 | + monkeypatch.setenv("APPDATA", str(tmp_path)) |
| 40 | + monkeypatch.setattr("pathlib.Path.home", lambda: tmp_path) |
| 41 | + (tmp_path / "Library" / "Application Support").mkdir(parents=True, exist_ok=True) |
| 42 | + (tmp_path / ".config").mkdir(parents=True, exist_ok=True) |
| 43 | + return tmp_path |
| 44 | + |
| 45 | + |
| 46 | +@pytest.fixture |
| 47 | +def manager(home): |
| 48 | + auth_manager = AuthManager(app_id="test-app-id") |
| 49 | + auth_manager.token_info = TokenInfo( |
| 50 | + access_token="EAAG" + "x" * 60, |
| 51 | + expires_in=60 * 24 * 3600, |
| 52 | + user_id="123", |
| 53 | + ) |
| 54 | + return auth_manager |
| 55 | + |
| 56 | + |
| 57 | +def test_token_cache_file_is_owner_only(manager): |
| 58 | + manager._save_token_to_cache() |
| 59 | + cache_path = manager._get_token_cache_path() |
| 60 | + |
| 61 | + assert _mode(cache_path) == TOKEN_CACHE_FILE_MODE |
| 62 | + assert not _mode(cache_path) & 0o077, "group/other can read the Meta token" |
| 63 | + |
| 64 | + |
| 65 | +def test_token_cache_directory_is_owner_only(manager): |
| 66 | + cache_dir = manager._get_token_cache_path().parent |
| 67 | + |
| 68 | + assert _mode(cache_dir) == TOKEN_CACHE_DIR_MODE |
| 69 | + assert not _mode(cache_dir) & 0o077 |
| 70 | + |
| 71 | + |
| 72 | +def test_permissions_hold_under_a_permissive_umask(manager): |
| 73 | + """0644 came from the umask, so the umask is what the fix must survive.""" |
| 74 | + previous = os.umask(0o000) |
| 75 | + try: |
| 76 | + manager._save_token_to_cache() |
| 77 | + finally: |
| 78 | + os.umask(previous) |
| 79 | + |
| 80 | + assert _mode(manager._get_token_cache_path()) == TOKEN_CACHE_FILE_MODE |
| 81 | + |
| 82 | + |
| 83 | +def test_existing_world_readable_cache_is_narrowed_on_save(manager): |
| 84 | + cache_path = manager._get_token_cache_path() |
| 85 | + cache_path.write_text("{}") |
| 86 | + os.chmod(cache_path, 0o644) |
| 87 | + |
| 88 | + manager._save_token_to_cache() |
| 89 | + |
| 90 | + assert _mode(cache_path) == TOKEN_CACHE_FILE_MODE |
| 91 | + |
| 92 | + |
| 93 | +def test_existing_world_readable_cache_is_narrowed_on_load(manager, home): |
| 94 | + """A cache written by an older version is repaired without re-authenticating.""" |
| 95 | + cache_path = manager._get_token_cache_path() |
| 96 | + cache_path.write_text( |
| 97 | + json.dumps( |
| 98 | + { |
| 99 | + "access_token": "EAAG" + "y" * 60, |
| 100 | + "expires_in": 60 * 24 * 3600, |
| 101 | + "created_at": int(time.time()), |
| 102 | + "user_id": "123", |
| 103 | + } |
| 104 | + ) |
| 105 | + ) |
| 106 | + os.chmod(cache_path, 0o644) |
| 107 | + |
| 108 | + assert AuthManager(app_id="test-app-id")._load_cached_token() is True |
| 109 | + assert _mode(cache_path) == TOKEN_CACHE_FILE_MODE |
| 110 | + |
| 111 | + |
| 112 | +def test_saved_token_is_still_readable_by_its_owner(manager): |
| 113 | + manager._save_token_to_cache() |
| 114 | + |
| 115 | + data = json.loads(manager._get_token_cache_path().read_text()) |
| 116 | + |
| 117 | + assert data["access_token"] == manager.token_info.access_token |
| 118 | + |
| 119 | + |
| 120 | +def test_restrict_permissions_survives_a_missing_file(tmp_path): |
| 121 | + """Best effort: a vanished path must not raise into the caller.""" |
| 122 | + _restrict_permissions(tmp_path / "gone.json", TOKEN_CACHE_FILE_MODE) |
0 commit comments