@devries-cameron20 / composio Mirror · read-only
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 03:37 UTC

next
py 251 lines 8.7 KB
Raw
1 """Tests for telemetry secret redaction."""
2
3 import json
4 from typing import NamedTuple
5
6 import pytest
7
8 from composio.utils.redaction import redact_sensitive_text, redact_sensitive_value
9
10
11 class _RequestContext(NamedTuple):
12 request_id: str
13 api_key: str
14
15
16 class _RenderedContext:
17 def __repr__(self) -> str:
18 return "RenderedContext(api_key='object_test_secret')"
19
20
21 def test_redacts_url_queries_authorization_and_secret_pairs() -> None:
22 redacted = redact_sensitive_text(
23 "https://example.com/file?signature=secret Bearer token123 api_key=abc password: hunter2"
24 )
25
26 assert "signature=secret" not in redacted
27 assert "token123" not in redacted
28 assert "api_key=abc" not in redacted
29 assert "password: hunter2" not in redacted
30 assert redacted.count("[REDACTED]") == 4
31
32
33 @pytest.mark.parametrize(
34 ("text", "secret"),
35 [
36 ('{"api_key": "sk-live-abc123"}', "sk-live-abc123"),
37 ('{"api_key":"sk-live-abc123"}', "sk-live-abc123"),
38 ('{"api_key" : "sk-live-abc123"}', "sk-live-abc123"),
39 ('{"refresh_token":"rt-abc.def-123"}', "rt-abc.def-123"),
40 ('{"auth":"app-key:pusher-secret"}', "app-key:pusher-secret"),
41 ('{"COMPOSIO_API_KEY":"prefixed-double-secret"}', "prefixed-double-secret"),
42 ("{'OPENAI_API_KEY': 'prefixed single secret'}", "prefixed single secret"),
43 ('{"x-api-key":"sk-hdr","user":"bob"}', "sk-hdr"),
44 ("{'client_secret': 'cs-live-abc123'}", "cs-live-abc123"),
45 ('{"password": "hunter2"}', "hunter2"),
46 (
47 '{"password": "correct horse battery staple"}',
48 "correct horse battery staple",
49 ),
50 ("{'client_secret': 'multi word secret'}", "multi word secret"),
51 ],
52 )
53 def test_redacts_secrets_in_json_and_dict_reprs(text: str, secret: str) -> None:
54 """Serialized payloads are the common shape in exception text.
55
56 The key's own closing quote sits between the name and the colon, so a
57 pattern anchored on ``name`` followed directly by ``:`` never matches.
58 """
59 redacted = redact_sensitive_text(text)
60
61 assert secret not in redacted
62 assert "[REDACTED]" in redacted
63
64
65 def test_redacts_secret_in_serialized_error_payload() -> None:
66 """The shape that reaches the telemetry error field via a tool failure."""
67 text = (
68 "Error executing tool: request body was rejected: "
69 '{"toolkit": "GMAIL", "arguments": {"api_key": "sk-live-CUSTOMER", "to": "x@y.z"}}'
70 )
71
72 redacted = redact_sensitive_text(text)
73
74 assert "sk-live-CUSTOMER" not in redacted
75 assert "GMAIL" in redacted # non-secret context is preserved
76
77
78 def test_json_keys_are_preserved() -> None:
79 """Only the value is replaced; the key and quoting survive intact."""
80 assert (
81 redact_sensitive_text('{"api_key": "sk-live-abc123"}')
82 == '{"api_key": "[REDACTED]"}'
83 )
84
85
86 def test_redacts_secret_without_consuming_the_adjacent_field() -> None:
87 assert (
88 redact_sensitive_text('{"api_key":"secret","user":"bob"}')
89 == '{"api_key":"[REDACTED]","user":"bob"}'
90 )
91
92
93 def test_redacts_escaped_quoted_secret_in_serialized_message() -> None:
94 source = json.dumps({"error": 'password: "secret"'})
95 expected = json.dumps({"error": 'password: "[REDACTED]"'})
96
97 assert redact_sensitive_text(source) == expected
98 assert redact_sensitive_text("password: \\'secret\\'") == (
99 "password: \\'[REDACTED]\\'"
100 )
101
102 double_encoded = json.dumps({"body": json.dumps({"api_key": "secret"})})
103 double_encoded_expected = json.dumps(
104 {"body": json.dumps({"api_key": "[REDACTED]"})}
105 )
106 assert redact_sensitive_text(double_encoded) == double_encoded_expected
107
108
109 def test_preserves_serialized_adjacent_keys_with_escapes() -> None:
110 source = json.dumps(
111 {"error": "unknown field auth:", 'quoted"key': "safe", "path\\key": "safe"}
112 )
113
114 assert redact_sensitive_text(source) == source
115
116
117 def test_redacts_unquoted_secrets_containing_backslashes() -> None:
118 assert redact_sensitive_text("password=abc\\def") == "password=[REDACTED]"
119 assert redact_sensitive_text("password=\\leading") == "password=[REDACTED]"
120
121
122 def test_redacts_many_quoted_secrets_in_one_pass() -> None:
123 source = " ".join(f'password: "secret-{index}"' for index in range(1_000))
124 expected = " ".join('password: "[REDACTED]"' for _ in range(1_000))
125
126 assert redact_sensitive_text(source) == expected
127
128
129 @pytest.mark.parametrize(
130 "text",
131 [
132 "the password field is required",
133 "TypeError: cannot read property secret of undefined",
134 'no separator here "api_key" and nothing else',
135 ],
136 )
137 def test_leaves_benign_text_untouched(text: str) -> None:
138 """A key name with no value attached must not trigger redaction."""
139 assert redact_sensitive_text(text) == text
140
141
142 @pytest.mark.parametrize(
143 ("text", "secret"),
144 [
145 ("COMPOSIO_API_KEY=sk_live_9f3c", "sk_live_9f3c"),
146 ("OPENAI_API_KEY=sk_live_9f3c", "sk_live_9f3c"),
147 ('export COMPOSIO_API_KEY="sk_live_9f3c"', "sk_live_9f3c"),
148 ],
149 )
150 def test_redacts_env_style_prefixed_api_keys(text: str, secret: str) -> None:
151 """Underscore before api_key is a word char, so a leading \\b would miss these."""
152 redacted = redact_sensitive_text(text)
153
154 assert secret not in redacted
155 assert "[REDACTED]" in redacted
156
157
158 def test_does_not_match_secret_name_embedded_in_letters() -> None:
159 """Lookbehind still refuses letter-prefixed collisions like myapikey=x."""
160 assert redact_sensitive_text("myapikey=sk_live_9f3c") == "myapikey=sk_live_9f3c"
161
162
163 @pytest.mark.parametrize(
164 "text",
165 [
166 '{"error": "unknown field auth:", "user": "bob"}',
167 "{'note': 'unknown field auth:', \"user\": 'bob'}",
168 '{"error": "unknown field auth:", "$schema": "safe"}',
169 '{"error": "unknown field auth:", "@type": "safe"}',
170 '{"error": "unknown field auth:", "üser": "safe"}',
171 '{"error": "unknown field auth:", ".well-known": "safe"}',
172 '{"error": "unknown field auth:", ":type": "safe"}',
173 '{"error": "unknown field auth:", "?field": "safe"}',
174 ],
175 )
176 def test_does_not_consume_adjacent_fields_after_key_like_text(text: str) -> None:
177 assert redact_sensitive_text(text) == text
178
179
180 @pytest.mark.parametrize(
181 ("text", "secret"),
182 [
183 ('password: "punctuated secret";', "punctuated secret"),
184 ("client_secret='period secret'.", "period secret"),
185 ('api_key = "prose secret" followed by context', "prose secret"),
186 ('password: "escaped \\"secret\\" value"', 'escaped \\"secret\\" value'),
187 ('can\'t connect password: "secret"', "secret"),
188 ('users\' password: "secret"', "secret"),
189 ('Chris\' password: "secret"', "secret"),
190 ('Andrés\' password: "secret"', "secret"),
191 ('{"error":"request failed password: \'secret\'"}', "secret"),
192 ('5" from target password: "secret"', "secret"),
193 ('unmatched " before password: "secret"', "secret"),
194 ("unmatched ' before password: 'secret'", "secret"),
195 ('password: "}abc"', "}abc"),
196 ('api_key: ",abc"', ",abc"),
197 ],
198 )
199 def test_redacts_bare_quoted_values_before_punctuation_or_prose(
200 text: str, secret: str
201 ) -> None:
202 redacted = redact_sensitive_text(text)
203
204 assert secret not in redacted
205 assert "[REDACTED]" in redacted
206
207
208 def test_structured_redaction_bounds_recursive_metadata() -> None:
209 cyclic: dict[str, object] = {}
210 cyclic["self"] = cyclic
211
212 deeply_nested: object = "safe"
213 for _ in range(40):
214 deeply_nested = {"next": deeply_nested}
215
216 assert redact_sensitive_value(cyclic) == {"self": "[REDACTED]"}
217 assert "[REDACTED]" in str(redact_sensitive_value(deeply_nested))
218
219
220 def test_structured_redaction_handles_sequences_and_rendered_objects() -> None:
221 context = _RequestContext("request-1", "namedtuple_test_secret")
222 redacted = redact_sensitive_value(
223 {
224 "items": [{"access_token": "list_test_secret"}],
225 "tuple": ("Authorization: Bearer bearer_value_secret",),
226 "context": context,
227 "rendered": _RenderedContext(),
228 "binary": b"api_key=binary_test_secret",
229 }
230 )
231
232 assert isinstance(redacted["context"], _RequestContext)
233 assert redacted["context"].request_id == "request-1"
234 rendered = str(redacted)
235 for secret in (
236 "list_test_secret",
237 "bearer_value_secret",
238 "namedtuple_test_secret",
239 "object_test_secret",
240 "binary_test_secret",
241 ):
242 assert secret not in rendered
243
244
245 def test_structured_redaction_stops_at_the_node_budget() -> None:
246 assert redact_sensitive_value(list(range(10_001))) == "[REDACTED]"
247 wide_mapping = redact_sensitive_value(
248 {f"field_{index}": index for index in range(10_001)}
249 )
250 assert isinstance(wide_mapping, dict)
251 assert len(wide_mapping) < 10_001