|
| 1 | +"""Security regression tests for token-store file permissions. |
| 2 | +
|
| 3 | +Guards against GHSA-wjhr-76vg-2hvc: ``Client.dump()`` must write the token |
| 4 | +file (which holds the DI refresh token) owner-only (0o600) inside an |
| 5 | +owner-only directory (0o700), regardless of the process umask, so it can't be |
| 6 | +left world-readable on a shared host. |
| 7 | +
|
| 8 | +POSIX-only — file mode bits are not meaningful on Windows. |
| 9 | +""" |
| 10 | + |
| 11 | +import os |
| 12 | +import stat |
| 13 | +import sys |
| 14 | +from pathlib import Path |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +from garminconnect.client import Client |
| 19 | + |
| 20 | +pytestmark = pytest.mark.skipif(sys.platform == "win32", reason="POSIX file modes only") |
| 21 | + |
| 22 | + |
| 23 | +def _make_client() -> Client: |
| 24 | + c = Client() |
| 25 | + c.di_token = "ACCESS_TOKEN_EXAMPLE" |
| 26 | + c.di_refresh_token = "REFRESH_TOKEN_EXAMPLE" |
| 27 | + c.di_client_id = "CLIENT_ID_EXAMPLE" |
| 28 | + return c |
| 29 | + |
| 30 | + |
| 31 | +def _mode(path) -> int: |
| 32 | + return stat.S_IMODE(Path(path).stat().st_mode) |
| 33 | + |
| 34 | + |
| 35 | +def test_dump_creates_owner_only_token_file(tmp_path): |
| 36 | + """Under a permissive umask the token file must still be 0o600.""" |
| 37 | + old_umask = os.umask(0o022) |
| 38 | + try: |
| 39 | + token_dir = tmp_path / "tokens" |
| 40 | + _make_client().dump(str(token_dir)) |
| 41 | + token_file = token_dir / "garmin_tokens.json" |
| 42 | + |
| 43 | + assert _mode(token_file) == 0o600, oct(_mode(token_file)) |
| 44 | + assert _mode(token_dir) == 0o700, oct(_mode(token_dir)) |
| 45 | + # World/group bits must be clear |
| 46 | + assert not (_mode(token_file) & (stat.S_IRWXG | stat.S_IRWXO)) |
| 47 | + assert not (_mode(token_dir) & (stat.S_IRWXG | stat.S_IRWXO)) |
| 48 | + finally: |
| 49 | + os.umask(old_umask) |
| 50 | + |
| 51 | + |
| 52 | +def test_dump_tightens_preexisting_loose_permissions(tmp_path): |
| 53 | + """A pre-existing world-readable dir/file is tightened on dump.""" |
| 54 | + old_umask = os.umask(0o022) |
| 55 | + try: |
| 56 | + token_dir = tmp_path / "tokens" |
| 57 | + token_dir.mkdir(mode=0o755) |
| 58 | + token_file = token_dir / "garmin_tokens.json" |
| 59 | + token_file.write_text("{}") |
| 60 | + token_file.chmod(0o644) |
| 61 | + |
| 62 | + _make_client().dump(str(token_dir)) |
| 63 | + |
| 64 | + assert _mode(token_file) == 0o600, oct(_mode(token_file)) |
| 65 | + assert _mode(token_dir) == 0o700, oct(_mode(token_dir)) |
| 66 | + finally: |
| 67 | + os.umask(old_umask) |
| 68 | + |
| 69 | + |
| 70 | +def test_dump_explicit_json_path(tmp_path): |
| 71 | + """A direct ``*.json`` path is also written owner-only.""" |
| 72 | + old_umask = os.umask(0o022) |
| 73 | + try: |
| 74 | + token_file = tmp_path / "store" / "garmin_tokens.json" |
| 75 | + _make_client().dump(str(token_file)) |
| 76 | + assert _mode(token_file) == 0o600, oct(_mode(token_file)) |
| 77 | + finally: |
| 78 | + os.umask(old_umask) |
0 commit comments