File size: 10,882 Bytes
4046a11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
"""Tests to ensure tokens never leak in logs, errors, or output."""

from __future__ import annotations

import os
from pathlib import Path
from unittest.mock import MagicMock, patch

import pytest
from click.testing import CliRunner

from src.uploader_cli.auth_service import AuthService
from src.uploader_cli.config import TOKEN_ENV_VAR
from src.uploader_cli.exceptions import AuthenticationError
from src.uploader_cli.main import cli


class TestTokenSecurityLogging:
    """Verify tokens never leak in error messages or logs."""

    def test_auth_error_does_not_expose_token(self):
        """Ensure error messages don't contain the actual token."""
        test_token = "hf_shouldNotAppearInError123456"
        
        with patch("src.uploader_cli.auth_service.HfApi") as mock_api:
            mock_api.side_effect = Exception("Invalid token")
            service = AuthService()
            
            with pytest.raises(AuthenticationError) as exc_info:
                service.authenticate(test_token)
            
            error_msg = str(exc_info.value)
            assert test_token not in error_msg, "Token leaked in error message"
            # Verify error message is still informative
            assert "Token validation failed" in error_msg

    def test_require_token_error_is_informative(self):
        """Ensure require_token error doesn't expose keyring backend details."""
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: ""}, clear=False):
            with patch("keyring.get_password", return_value=None):
                with pytest.raises(AuthenticationError) as exc_info:
                    service.require_token()
                
                error_msg = str(exc_info.value)
                assert "No stored token found" in error_msg
                assert "Run 'birdnet-uploader login'" in error_msg

    def test_env_var_token_not_logged_in_output(self, tmp_path):
        """Verify HF_TOKEN env var is never logged by the CLI."""
        runner = CliRunner()
        test_token = "hf_env_token_test_12345"
        segments_dir = tmp_path / "segments"
        segments_dir.mkdir()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: test_token}):
            with patch("src.uploader_cli.auth_service.AuthService.require_token", return_value=test_token):
                # Attempt a command that would use the token (but mock out the actual upload)
                with patch("src.uploader_cli.main.LocalScanner") as mock_scanner:
                    mock_scanner.return_value.scan_folder.return_value = {
                        "total_files": 0,
                        "total_size": 0,
                        "by_species": {},
                    }
                    
                    result = runner.invoke(
                        cli,
                        [
                            "upload",
                            "--repo-id", "test/repo",
                            "--segments", str(segments_dir),
                            "--dry-run",
                        ],
                        env={TOKEN_ENV_VAR: test_token},
                    )
                    
                    # Verify CLI executed successfully
                    assert result.exit_code == 0, f"CLI failed: {result.output}"
                    # Verify token not in output
                    assert test_token not in result.output, f"Token leaked in output: {result.output}"
                    assert "Dry run" in result.output

    def test_cli_token_option_not_exposed_on_error(self):
        """Verify --token CLI option is never shown in error output."""
        runner = CliRunner()
        test_token = "hf_cli_token_test_67890"
        
        with patch("src.uploader_cli.main.LocalScanner") as mock_scanner:
            mock_scanner.return_value.scan_folder.side_effect = RuntimeError("Scan error")
            
            result = runner.invoke(
                cli,
                [
                    "upload",
                    "--repo-id", "test/repo",
                    "--segments", "/tmp/segments",
                    "--token", test_token,
                ],
            )
            
            # Even though CLI failed, token should not appear in output
            assert test_token not in result.output, f"Token leaked in error output: {result.output}"
            # Verify an error was reported
            assert result.exit_code != 0

    def test_get_token_from_env_var(self):
        """Verify get_token() correctly reads HF_TOKEN env var."""
        test_token = "hf_env_token_123"
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: test_token}):
            token = service.get_token()
            assert token == test_token

    def test_get_token_env_var_takes_precedence(self):
        """Verify HF_TOKEN env var takes precedence over keyring."""
        env_token = "hf_from_env"
        keyring_token = "hf_from_keyring"
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: env_token}):
            with patch("keyring.get_password", return_value=keyring_token):
                token = service.get_token()
                assert token == env_token, "Env var should take precedence"

    def test_get_token_falls_back_to_keyring(self):
        """Verify get_token() falls back to keyring if HF_TOKEN not set."""
        keyring_token = "hf_from_keyring_only"
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: ""}, clear=False):
            with patch("keyring.get_password", return_value=keyring_token):
                token = service.get_token()
                assert token == keyring_token

    def test_token_whitespace_stripped(self):
        """Verify tokens are stripped of leading/trailing whitespace."""
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: "  hf_token_with_spaces  "}):
            token = service.get_token()
            assert token == "hf_token_with_spaces"
            assert token != "  hf_token_with_spaces  "

    def test_empty_env_var_falls_back_to_keyring(self):
        """Verify empty HF_TOKEN env var doesn't prevent keyring fallback."""
        keyring_token = "hf_from_keyring"
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: ""}):
            with patch("keyring.get_password", return_value=keyring_token):
                token = service.get_token()
                assert token == keyring_token

    def test_authentication_strips_token(self):
        """Verify authenticate() strips whitespace from tokens."""
        test_token = "  hf_clean_token  "
        service = AuthService()
        
        with patch("src.uploader_cli.auth_service.HfApi") as mock_api:
            mock_api_instance = MagicMock()
            mock_api_instance.whoami.return_value = {"name": "testuser"}
            mock_api.return_value = mock_api_instance
            
            with patch("keyring.set_password") as mock_keyring:
                service.authenticate(test_token)
                # Verify stripped token was passed to keyring
                mock_keyring.assert_called_once()
                args = mock_keyring.call_args[0]
                assert args[2] == "hf_clean_token"  # Third arg is the token

    def test_no_token_error_is_user_friendly(self):
        """Verify 'no token' error doesn't expose internal state."""
        service = AuthService()
        
        with patch.dict(os.environ, {TOKEN_ENV_VAR: ""}, clear=False):
            with patch("keyring.get_password", return_value=None):
                with pytest.raises(AuthenticationError) as exc_info:
                    service.require_token()
                
                error_msg = str(exc_info.value)
                # Should be user-friendly, not expose internals
                assert "birdnet-uploader login" in error_msg
                assert "keyring" not in error_msg.lower()


class TestTokenSecurityEnvironment:
    """Test token handling with various environment setups."""

    def test_cli_token_option_preferred_over_env(self, tmp_path):
        """Verify --token CLI option takes precedence."""
        runner = CliRunner()
        cli_token = "hf_from_cli"
        env_token = "hf_from_env"
        segments_dir = tmp_path / "segments"
        segments_dir.mkdir()
        
        with patch("src.uploader_cli.main.AuthService") as mock_auth:
            with patch("src.uploader_cli.main.LocalScanner") as mock_scanner:
                mock_scanner.return_value.scan_folder.return_value = {
                    "total_files": 0,
                    "total_size": 0,
                    "by_species": {},
                }
                
                result = runner.invoke(
                    cli,
                    [
                        "upload",
                        "--repo-id", "test/repo",
                        "--segments", str(segments_dir),
                        "--token", cli_token,
                        "--dry-run",
                    ],
                    env={TOKEN_ENV_VAR: env_token},
                )
                
                # CLI should use provided token, not call require_token()
                assert result.exit_code == 0
                # require_token should NOT have been called
                mock_auth.return_value.require_token.assert_not_called()

    def test_upload_cli_uses_token_env_as_fallback(self, tmp_path):
        """Verify upload command uses HF_TOKEN env var when --token not provided."""
        runner = CliRunner()
        env_token = "hf_from_env_fallback"
        segments_dir = tmp_path / "segments"
        segments_dir.mkdir()
        
        with patch("src.uploader_cli.main.LocalScanner") as mock_scanner:
            mock_scanner.return_value.scan_folder.return_value = {
                "total_files": 0,
                "total_size": 0,
                "by_species": {},
            }
            
            # Mock AuthService to verify token resolution order
            with patch("src.uploader_cli.main.AuthService") as mock_auth:
                mock_auth_instance = MagicMock()
                mock_auth_instance.require_token.return_value = env_token
                mock_auth.return_value = mock_auth_instance
                
                result = runner.invoke(
                    cli,
                    [
                        "upload",
                        "--repo-id", "test/repo",
                        "--segments", str(segments_dir),
                        "--dry-run",
                    ],
                    env={TOKEN_ENV_VAR: env_token},
                )
                
                # Should succeed
                assert result.exit_code == 0