From 1c32f5c5f67e8bc01453d8a2190f6464a618f121 Mon Sep 17 00:00:00 2001 From: Nikita Fishbakh Date: Thu, 19 Nov 2026 12:47:18 +0100 Subject: [PATCH 1/2] CM-77676: Scan the text in image files an agent reads A PNG or JPEG file an agent reads with its file-read tool is sent as its bytes, under a name with its format's extension, so the server reads the text in it, as it does for the images attached to a prompt. Any other file is still sent as text. One change in the shared file-read handler covers Claude Code, Cursor and GitHub Copilot. Co-Authored-By: Claude Opus 5.5 --- .../cli/apps/ai_guardrails/scan/handlers.py | 23 +++- .../ai_guardrails/scan/test_handlers.py | 118 ++++++++++++++++++ 2 files changed, 136 insertions(+), 5 deletions(-) diff --git a/cycode/cli/apps/ai_guardrails/scan/handlers.py b/cycode/cli/apps/ai_guardrails/scan/handlers.py index a3505dac..2a2dd44c 100644 --- a/cycode/cli/apps/ai_guardrails/scan/handlers.py +++ b/cycode/cli/apps/ai_guardrails/scan/handlers.py @@ -27,7 +27,7 @@ from cycode.cli.apps.ai_guardrails.scan.mcp_server_status import is_enforced, load_mcp_server_statuses from cycode.cli.apps.ai_guardrails.scan.payload import AIHookPayload from cycode.cli.apps.ai_guardrails.scan.policy import get_policy_value -from cycode.cli.apps.ai_guardrails.scan.prompt_images import PromptImage +from cycode.cli.apps.ai_guardrails.scan.prompt_images import PromptImage, read_prompt_image from cycode.cli.apps.ai_guardrails.scan.types import ( SECRETS_BLOCK_REASON_BY_EVENT_TYPE, AiHookEventType, @@ -586,6 +586,22 @@ def _scan_text_for_secrets( return _scan_documents_for_secrets(ctx, documents, timeout_ms, payload, event_type) +def _file_document(file_path: str, max_bytes: int) -> Document: + """The file to scan as a document: an image as its bytes, anything else as its text, up to ``max_bytes``. + + An image is what a prompt's attachment is scanned as (read_prompt_image). Its name keeps its format's extension + (PromptImage.file_name), as the server picks the files to read text out of by extension as well as signature. + """ + image_content = read_prompt_image(file_path) + if image_content is not None: + image = PromptImage(path=file_path, content=image_content) + return Document(path=image.file_name, content=image.content, is_git_diff_format=False, absolute_path=file_path) + + with open(file_path, encoding='utf-8', errors='replace') as f: + content = f.read(max_bytes) + return Document(path=os.path.basename(file_path), content=content, is_git_diff_format=False) + + def _scan_path_for_secrets( ctx: typer.Context, file_path: str, @@ -601,13 +617,10 @@ def _scan_path_for_secrets( return NO_SCAN max_bytes = get_policy_value(policy, 'secrets', 'max_bytes', default=200000) - - with open(file_path, encoding='utf-8', errors='replace') as f: - content = f.read(max_bytes) + document = _file_document(file_path, max_bytes) timeout_ms = get_policy_value(policy, 'secrets', 'timeout_ms', default=30000) - document = Document(path=os.path.basename(file_path), content=content, is_git_diff_format=False) return _scan_documents_for_secrets( ctx, [document], timeout_ms, payload, AiHookEventType.FILE_READ, paths=(file_path,) ) diff --git a/tests/cli/commands/ai_guardrails/scan/test_handlers.py b/tests/cli/commands/ai_guardrails/scan/test_handlers.py index 44f4bb60..c12ba9cd 100644 --- a/tests/cli/commands/ai_guardrails/scan/test_handlers.py +++ b/tests/cli/commands/ai_guardrails/scan/test_handlers.py @@ -666,6 +666,124 @@ def test_blocked_prompt_names_the_image_the_secret_is_in( assert mock_ctx.obj['ai_security_client'].create_event.call_args.args[2] == AIHookOutcome.BLOCKED +# Tests for image files the agent reads + + +@patch('cycode.cli.apps.ai_guardrails.scan.handlers._perform_scan', return_value=ScanOutcome(scan_id='scan-id-1')) +def test_file_read_of_an_image_sends_its_bytes( + mock_perform_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict, fs: Any +) -> None: + fs.create_file('/project/shot.png', contents=_PNG) + + _scan_path_for_secrets(mock_ctx, '/project/shot.png', default_policy, payload=mock_payload) + + (document,) = _scanned_documents(mock_perform_scan) + assert document.path == 'shot.png' + assert document.content == _PNG + assert document.absolute_path == '/project/shot.png' + assert mock_perform_scan.call_args.args[2]['metadata']['ai_guardrails']['detection_source'] == 'secrets_in_file' + + +@patch('cycode.cli.apps.ai_guardrails.scan.handlers._perform_scan', return_value=ScanOutcome(scan_id='scan-id-1')) +def test_file_read_of_an_image_without_its_extension_is_named_with_it( + mock_perform_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict, fs: Any +) -> None: + """The server picks the files to read text out of by extension as well as signature.""" + fs.create_file('/project/Screenshot', contents=_JPEG) + + _scan_path_for_secrets(mock_ctx, '/project/Screenshot', default_policy, payload=mock_payload) + + (document,) = _scanned_documents(mock_perform_scan) + assert document.path == 'Screenshot.jpg' + assert document.content == _JPEG + + +@pytest.mark.parametrize( + ('file_name', 'contents'), + [ + ('notes.txt', 'password=hunter2'), + ('fake.png', 'password=hunter2'), + ('animation.gif', 'GIF89a password=hunter2'), + ], + ids=['text file', 'text file named as an image', 'image format the server is not sent'], +) +@patch('cycode.cli.apps.ai_guardrails.scan.handlers._perform_scan', return_value=ScanOutcome(scan_id='scan-id-1')) +def test_file_read_of_anything_but_a_png_or_jpeg_sends_its_text( + mock_perform_scan: MagicMock, + mock_ctx: MagicMock, + mock_payload: AIHookPayload, + default_policy: dict, + fs: Any, + file_name: str, + contents: str, +) -> None: + fs.create_file(f'/project/{file_name}', contents=contents) + + _scan_path_for_secrets(mock_ctx, f'/project/{file_name}', default_policy, payload=mock_payload) + + (document,) = _scanned_documents(mock_perform_scan) + assert document.path == file_name + assert document.content == contents + + +@patch('cycode.cli.apps.ai_guardrails.scan.handlers._perform_scan', return_value=ScanOutcome(scan_id='scan-id-1')) +def test_file_read_of_an_image_over_the_size_limit_sends_its_text( + mock_perform_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict, fs: Any +) -> None: + fs.create_file('/project/huge.png', contents=_PNG + b'x' * 64) + + with patch('cycode.cli.apps.ai_guardrails.scan.prompt_images.FILE_MAX_SIZE_LIMIT_IN_BYTES', len(_PNG)): + _scan_path_for_secrets(mock_ctx, '/project/huge.png', default_policy, payload=mock_payload) + + (document,) = _scanned_documents(mock_perform_scan) + assert document.path == 'huge.png' + assert isinstance(document.content, str) + + +def _image_file_finding_scan(batch: list[Document]) -> tuple[str, None, LocalScanResult]: + """A server answer with one Block-verdict secret in the image file read.""" + (image_document,) = batch + detection = Detection( + detection_type_id='type-id', + type='GitHub Token', + message='Hardcoded secret', + detection_details={'sha512': 'sha-image', 'line': 0, 'start_position': 0, 'length': 4}, + detection_rule_id='rule-id', + severity='High', + ) + return ( + 'scan-id-1', + None, + LocalScanResult( + scan_id='scan-id-1', + report_url=None, + document_detections=[DocumentDetections(document=image_document, detections=[detection])], + issue_detected=True, + detections_count=1, + relevant_detections_count=1, + verdict='Block', + ), + ) + + +def test_blocked_file_read_names_the_image_the_secret_is_in(mock_ctx: MagicMock, default_policy: dict, fs: Any) -> None: + fs.create_file('/project/Screenshot', contents=_PNG) + payload = AIHookPayload(event_name='FileRead', ide_provider='claude-code', file_path='/project/Screenshot') + + with patch( + 'cycode.cli.apps.ai_guardrails.scan.handlers._get_scan_documents_thread_func', + return_value=_image_file_finding_scan, + ): + result = handle_before_read_file(mock_ctx, payload, default_policy) + + assert result.action == DecisionAction.DENY + assert result.user_message.startswith('Cycode blocked reading /project/Screenshot because it contains a secret.') + # Positions in an image point into text the server read out of it: the developer is told the file instead + assert result.user_message.endswith('\n - GitHub Token in image "Screenshot"') + assert 'ignore --by-sha sha-image' in result.agent_message + assert mock_ctx.obj['ai_security_client'].create_event.call_args.args[2] == AIHookOutcome.BLOCKED + + def test_violation_summary_lists_a_value_once_per_document() -> None: """The same value in the prompt and in an image is two places to remove it from, but one value to ignore.""" local_scan_result = _local_scan_result_with_detections('sha-aaa') From 332d927acd68fa51cdd8989c873d16d336fc4e8f Mon Sep 17 00:00:00 2001 From: Nikita Fishbakh Date: Thu, 19 Nov 2026 13:29:23 +0100 Subject: [PATCH 2/2] CM-77676: Word the image file name comments like the existing ones Co-Authored-By: Claude Opus 5.5 --- cycode/cli/apps/ai_guardrails/scan/handlers.py | 2 +- tests/cli/commands/ai_guardrails/scan/test_handlers.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/cycode/cli/apps/ai_guardrails/scan/handlers.py b/cycode/cli/apps/ai_guardrails/scan/handlers.py index 2a2dd44c..19c45408 100644 --- a/cycode/cli/apps/ai_guardrails/scan/handlers.py +++ b/cycode/cli/apps/ai_guardrails/scan/handlers.py @@ -590,7 +590,7 @@ def _file_document(file_path: str, max_bytes: int) -> Document: """The file to scan as a document: an image as its bytes, anything else as its text, up to ``max_bytes``. An image is what a prompt's attachment is scanned as (read_prompt_image). Its name keeps its format's extension - (PromptImage.file_name), as the server picks the files to read text out of by extension as well as signature. + (PromptImage.file_name), as the server picks the files to scan by extension as well as signature. """ image_content = read_prompt_image(file_path) if image_content is not None: diff --git a/tests/cli/commands/ai_guardrails/scan/test_handlers.py b/tests/cli/commands/ai_guardrails/scan/test_handlers.py index c12ba9cd..05ee778a 100644 --- a/tests/cli/commands/ai_guardrails/scan/test_handlers.py +++ b/tests/cli/commands/ai_guardrails/scan/test_handlers.py @@ -688,7 +688,7 @@ def test_file_read_of_an_image_sends_its_bytes( def test_file_read_of_an_image_without_its_extension_is_named_with_it( mock_perform_scan: MagicMock, mock_ctx: MagicMock, mock_payload: AIHookPayload, default_policy: dict, fs: Any ) -> None: - """The server picks the files to read text out of by extension as well as signature.""" + """The server picks the files to scan by extension as well as signature.""" fs.create_file('/project/Screenshot', contents=_JPEG) _scan_path_for_secrets(mock_ctx, '/project/Screenshot', default_policy, payload=mock_payload)