-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathtest_handlers.py
More file actions
366 lines (287 loc) · 14.1 KB
/
test_handlers.py
File metadata and controls
366 lines (287 loc) · 14.1 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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
"""Tests for AI guardrails handlers."""
from typing import Any
from unittest.mock import MagicMock, patch
import pytest
import typer
from cycode.cli.apps.ai_guardrails.scan.handlers import (
handle_before_mcp_execution,
handle_before_read_file,
handle_before_submit_prompt,
)
from cycode.cli.apps.ai_guardrails.scan.payload import AIHookPayload
from cycode.cli.apps.ai_guardrails.scan.types import AIHookOutcome, BlockReason
@pytest.fixture
def mock_ctx() -> MagicMock:
"""Create a mock Typer context."""
ctx = MagicMock(spec=typer.Context)
ctx.obj = {
'ai_security_client': MagicMock(),
'scan_type': 'secret',
}
return ctx
@pytest.fixture
def mock_payload() -> AIHookPayload:
"""Create a mock AIHookPayload."""
return AIHookPayload(
event_name='prompt',
conversation_id='test-conv-id',
generation_id='test-gen-id',
ide_user_email='test@example.com',
model='gpt-4',
ide_provider='cursor',
ide_version='1.0.0',
prompt='Test prompt',
)
@pytest.fixture
def default_policy() -> dict[str, Any]:
"""Create a default policy dict."""
return {
'mode': 'block',
'fail_open': True,
'secrets': {'max_bytes': 200000},
'prompt': {'enabled': True, 'action': 'block'},
'file_read': {'enabled': True, 'action': 'block', 'scan_content': True, 'deny_globs': []},
'mcp': {'enabled': True, 'action': 'block', 'scan_arguments': True},
}
# Tests for handle_before_submit_prompt
def test_handle_before_submit_prompt_disabled(
mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict[str, Any]
) -> None:
"""Test that disabled prompt scanning allows the prompt."""
default_policy['prompt']['enabled'] = False
result = handle_before_submit_prompt(mock_ctx, mock_payload, default_policy)
assert result == {'continue': True}
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
mock_ctx.obj['ai_security_client'].create_conversation.assert_not_called()
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_submit_prompt_no_secrets(
mock_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict[str, Any]
) -> None:
"""Test that prompt with no secrets is allowed."""
mock_scan.return_value = (None, 'scan-id-123')
result = handle_before_submit_prompt(mock_ctx, mock_payload, default_policy)
assert result == {'continue': True}
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
mock_ctx.obj['ai_security_client'].create_conversation.assert_not_called()
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
# outcome is arg[2], scan_id and block_reason are kwargs
assert call_args.args[2] == AIHookOutcome.ALLOWED
assert call_args.kwargs['scan_id'] == 'scan-id-123'
assert call_args.kwargs['block_reason'] is None
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_submit_prompt_with_secrets_blocked(
mock_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict[str, Any]
) -> None:
"""Test that prompt with secrets is blocked."""
mock_scan.return_value = ('Found 1 secret: API key', 'scan-id-456')
result = handle_before_submit_prompt(mock_ctx, mock_payload, default_policy)
assert result['continue'] is False
assert 'Found 1 secret: API key' in result['user_message']
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.BLOCKED
assert call_args.kwargs['block_reason'] == BlockReason.SECRETS_IN_PROMPT
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_submit_prompt_with_secrets_warned(
mock_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict[str, Any]
) -> None:
"""Test that prompt with secrets in warn mode is allowed."""
default_policy['prompt']['action'] = 'warn'
mock_scan.return_value = ('Found 1 secret: API key', 'scan-id-789')
result = handle_before_submit_prompt(mock_ctx, mock_payload, default_policy)
assert result == {'continue': True}
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.WARNED
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_submit_prompt_scan_failure_fail_open(
mock_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict[str, Any]
) -> None:
"""Test that scan failure with fail_open=True allows the prompt."""
mock_scan.side_effect = RuntimeError('Scan failed')
default_policy['fail_open'] = True
with pytest.raises(RuntimeError):
handle_before_submit_prompt(mock_ctx, mock_payload, default_policy)
# Event should be tracked even on exception
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.ALLOWED
# block_reason is set for tracking even when fail_open allows the action
assert call_args.kwargs['block_reason'] == BlockReason.SCAN_FAILURE
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_submit_prompt_scan_failure_fail_closed(
mock_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict[str, Any]
) -> None:
"""Test that scan failure with fail_open=False blocks the prompt."""
mock_scan.side_effect = RuntimeError('Scan failed')
default_policy['fail_open'] = False
with pytest.raises(RuntimeError):
handle_before_submit_prompt(mock_ctx, mock_payload, default_policy)
# Event should be tracked even on exception
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.BLOCKED
assert call_args.kwargs['block_reason'] == BlockReason.SCAN_FAILURE
# Tests for handle_before_read_file
def test_handle_before_read_file_disabled(mock_ctx: MagicMock, default_policy: dict[str, Any]) -> None:
"""Test that disabled file read scanning allows the file."""
default_policy['file_read']['enabled'] = False
payload = AIHookPayload(
event_name='file_read',
ide_provider='cursor',
file_path='/path/to/file.txt',
)
result = handle_before_read_file(mock_ctx, payload, default_policy)
assert result == {'permission': 'allow'}
@patch('cycode.cli.apps.ai_guardrails.scan.handlers.is_denied_path')
def test_handle_before_read_file_sensitive_path(
mock_is_denied: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that sensitive path is blocked."""
mock_is_denied.return_value = True
payload = AIHookPayload(
event_name='file_read',
ide_provider='cursor',
file_path='/path/to/.env',
)
result = handle_before_read_file(mock_ctx, payload, default_policy)
assert result['permission'] == 'deny'
assert '.env' in result['user_message']
mock_ctx.obj['ai_security_client'].create_event.assert_called_once()
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.BLOCKED
assert call_args.kwargs['block_reason'] == BlockReason.SENSITIVE_PATH
assert call_args.kwargs['file_path'] == '/path/to/.env'
@patch('cycode.cli.apps.ai_guardrails.scan.handlers.is_denied_path')
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_path_for_secrets')
def test_handle_before_read_file_no_secrets(
mock_scan: MagicMock, mock_is_denied: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that file with no secrets is allowed."""
mock_is_denied.return_value = False
mock_scan.return_value = (None, 'scan-id-123')
payload = AIHookPayload(
event_name='file_read',
ide_provider='cursor',
file_path='/path/to/file.txt',
)
result = handle_before_read_file(mock_ctx, payload, default_policy)
assert result == {'permission': 'allow'}
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.ALLOWED
assert call_args.kwargs['file_path'] == '/path/to/file.txt'
@patch('cycode.cli.apps.ai_guardrails.scan.handlers.is_denied_path')
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_path_for_secrets')
def test_handle_before_read_file_with_secrets(
mock_scan: MagicMock, mock_is_denied: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that file with secrets is blocked."""
mock_is_denied.return_value = False
mock_scan.return_value = ('Found 1 secret: password', 'scan-id-456')
payload = AIHookPayload(
event_name='file_read',
ide_provider='cursor',
file_path='/path/to/file.txt',
)
result = handle_before_read_file(mock_ctx, payload, default_policy)
assert result['permission'] == 'deny'
assert 'Found 1 secret: password' in result['user_message']
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.BLOCKED
assert call_args.kwargs['block_reason'] == BlockReason.SECRETS_IN_FILE
assert call_args.kwargs['file_path'] == '/path/to/file.txt'
@patch('cycode.cli.apps.ai_guardrails.scan.handlers.is_denied_path')
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_path_for_secrets')
def test_handle_before_read_file_scan_disabled(
mock_scan: MagicMock, mock_is_denied: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that file is allowed when content scanning is disabled."""
mock_is_denied.return_value = False
default_policy['file_read']['scan_content'] = False
payload = AIHookPayload(
event_name='file_read',
ide_provider='cursor',
file_path='/path/to/file.txt',
)
result = handle_before_read_file(mock_ctx, payload, default_policy)
assert result == {'permission': 'allow'}
mock_scan.assert_not_called()
# Tests for handle_before_mcp_execution
def test_handle_before_mcp_execution_disabled(mock_ctx: MagicMock, default_policy: dict[str, Any]) -> None:
"""Test that disabled MCP scanning allows the execution."""
default_policy['mcp']['enabled'] = False
payload = AIHookPayload(
event_name='mcp_execution',
ide_provider='cursor',
mcp_tool_name='test_tool',
mcp_arguments={'arg1': 'value1'},
)
result = handle_before_mcp_execution(mock_ctx, payload, default_policy)
assert result == {'permission': 'allow'}
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_mcp_execution_no_secrets(
mock_scan: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that MCP execution with no secrets is allowed."""
mock_scan.return_value = (None, 'scan-id-123')
payload = AIHookPayload(
event_name='mcp_execution',
ide_provider='cursor',
mcp_tool_name='test_tool',
mcp_arguments={'arg1': 'value1'},
)
result = handle_before_mcp_execution(mock_ctx, payload, default_policy)
assert result == {'permission': 'allow'}
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.ALLOWED
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_mcp_execution_with_secrets_blocked(
mock_scan: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that MCP execution with secrets is blocked."""
mock_scan.return_value = ('Found 1 secret: token', 'scan-id-456')
payload = AIHookPayload(
event_name='mcp_execution',
ide_provider='cursor',
mcp_tool_name='test_tool',
mcp_arguments={'arg1': 'secret_token_12345'},
)
result = handle_before_mcp_execution(mock_ctx, payload, default_policy)
assert result['permission'] == 'deny'
assert 'Found 1 secret: token' in result['user_message']
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.BLOCKED
assert call_args.kwargs['block_reason'] == BlockReason.SECRETS_IN_MCP_ARGS
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_mcp_execution_with_secrets_warned(
mock_scan: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that MCP execution with secrets in warn mode asks permission."""
mock_scan.return_value = ('Found 1 secret: token', 'scan-id-789')
default_policy['mcp']['action'] = 'warn'
payload = AIHookPayload(
event_name='mcp_execution',
ide_provider='cursor',
mcp_tool_name='test_tool',
mcp_arguments={'arg1': 'secret_token_12345'},
)
result = handle_before_mcp_execution(mock_ctx, payload, default_policy)
assert result['permission'] == 'ask'
assert 'Found 1 secret: token' in result['user_message']
call_args = mock_ctx.obj['ai_security_client'].create_event.call_args
assert call_args.args[2] == AIHookOutcome.WARNED
@patch('cycode.cli.apps.ai_guardrails.scan.handlers._scan_text_for_secrets')
def test_handle_before_mcp_execution_scan_disabled(
mock_scan: MagicMock, mock_ctx: MagicMock, default_policy: dict[str, Any]
) -> None:
"""Test that MCP execution is allowed when argument scanning is disabled."""
default_policy['mcp']['scan_arguments'] = False
payload = AIHookPayload(
event_name='mcp_execution',
ide_provider='cursor',
mcp_tool_name='test_tool',
mcp_arguments={'arg1': 'value1'},
)
result = handle_before_mcp_execution(mock_ctx, payload, default_policy)
assert result == {'permission': 'allow'}
mock_scan.assert_not_called()