-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathtest_refresh_auth_security.py
More file actions
124 lines (103 loc) · 3.98 KB
/
test_refresh_auth_security.py
File metadata and controls
124 lines (103 loc) · 3.98 KB
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
"""Security tests for tp_refresh_auth tool.
These tests verify that cookie values can NEVER leak into tool output.
"""
from tp_mcp.tools.refresh_auth import _sanitize_result
class TestSanitizeResult:
"""Test the result sanitization function."""
def test_removes_cookie_key(self):
"""Cookie values must never appear in output."""
result = {
"success": True,
"cookie": "SENSITIVE_VALUE_12345",
"message": "OK",
}
sanitized = _sanitize_result(result)
assert "cookie" not in sanitized
assert "SENSITIVE_VALUE" not in str(sanitized)
def test_removes_auth_token_key(self):
"""Auth-related keys must be stripped."""
result = {
"success": True,
"auth_token": "secret123",
"token": "secret456",
"message": "OK",
}
sanitized = _sanitize_result(result)
assert "auth_token" not in sanitized
assert "token" not in sanitized
assert "secret" not in str(sanitized).lower()
def test_removes_credential_key(self):
"""Credential keys must be stripped."""
result = {
"success": True,
"credential": "mycred",
"user_credential": "othercred",
"message": "OK",
}
sanitized = _sanitize_result(result)
assert "credential" not in sanitized
assert "user_credential" not in sanitized
def test_preserves_safe_keys(self):
"""Safe keys should be preserved."""
result = {
"success": True,
"message": "Authentication refreshed",
"athlete_id": 12345,
"email": "test@example.com",
"browser": "chrome",
}
sanitized = _sanitize_result(result)
assert sanitized == result
def test_case_insensitive_filtering(self):
"""Filtering should be case-insensitive."""
result = {
"success": True,
"COOKIE": "value1",
"Cookie": "value2",
"AUTH_TOKEN": "value3",
"message": "OK",
}
sanitized = _sanitize_result(result)
assert "COOKIE" not in sanitized
assert "Cookie" not in sanitized
assert "AUTH_TOKEN" not in sanitized
class TestCredentialResultRepr:
"""Test that CredentialResult doesn't leak cookies in repr."""
def test_repr_hides_cookie(self):
"""Cookie value must not appear in repr."""
from tp_mcp.auth.keyring import CredentialResult
result = CredentialResult(
success=True,
message="Credential retrieved",
cookie="SUPER_SECRET_VALUE_67890",
)
repr_str = repr(result)
assert "SUPER_SECRET" not in repr_str
assert "67890" not in repr_str
assert "cookie=<present>" in repr_str
def test_repr_shows_none_for_missing_cookie(self):
from tp_mcp.auth.keyring import CredentialResult
result = CredentialResult(success=False, message="No cred")
repr_str = repr(result)
assert "cookie=<None>" in repr_str
class TestBrowserCookieResultRepr:
"""Test that BrowserCookieResult doesn't leak cookies in repr."""
def test_repr_hides_cookie_value(self):
"""Cookie value must not appear in repr."""
from tp_mcp.auth.browser import BrowserCookieResult
result = BrowserCookieResult(
success=True,
cookie="SUPER_SECRET_COOKIE_VALUE_12345",
browser="chrome",
message="Found cookie",
)
repr_str = repr(result)
assert "SUPER_SECRET" not in repr_str
assert "12345" not in repr_str
assert "cookie=<present>" in repr_str
def test_repr_shows_none_for_missing_cookie(self):
"""Repr should indicate when cookie is None."""
from tp_mcp.auth.browser import BrowserCookieResult
result = BrowserCookieResult(success=False, message="Not found")
repr_str = repr(result)
assert "cookie=<None>" in repr_str