Skip to content

CM-77702: Scan images attached to Copilot prompts for secrets - #590

Merged
Altruistus merged 7 commits into
mainfrom
CM-77702-block-secrets-in-attached-images
Nov 16, 2026
Merged

Altruistus merged 7 commits into
mainfrom
CM-77702-block-secrets-in-attached-images

Conversation

@Altruistus

@Altruistus Altruistus commented Nov 10, 2026 •

Copy link
Copy Markdown
Collaborator

What

The Copilot UserPromptSubmit hook now also scans images attached to the prompt, so a secret in a pasted screenshot blocks the prompt (or is reported), just like a secret in the prompt text.

Jira: CM-77702

Why

The hook payload carries only the prompt text. In live probes (VS Code 1.145.2, Copilot CLI 1.0.107), Copilot had already saved every attached image to disk by the time UserPromptSubmit ran, so the hook can find the images and block the turn. userPromptTransformed can't block, so it isn't used.

Where to look

  • ides/copilot.py → Copilot.collect_prompt_images gathers images from three sources, in order:

    1. Copilot CLI @path mentions, resolved against cwd.
    2. Copilot CLI pasted images: the [image: <name>] placeholder names a file in the temp directory.
    3. VS Code agent sessions' send-time copies: <user data>/agentSessionData/<chat id>/attachments/*/*, for Code and Code - Insiders.

    The VS Code folder is named after VS Code's chat id, not the hook's session_id, so the chat is the one whose session.db records the hook's session_id (sdkSessionId, read-only). Its files saved since the session's previous prompt are taken. Other chats' attachments never count, and a Copilot CLI prompt gets no VS Code attachments. VS Code's local chat (not an agent session) is not covered.

  • scan/prompt_images.py is the IDE-agnostic part:

    • Per-session prompt marks in ~/.cycode/ai-guardrails-prompt-marks.json. A first prompt looks back 600 s; session start isn't used because VS Code fires it together with the first prompt, after its attachments were saved.
    • Filters: PNG/JPEG by signature, regular files only, 5 MB each, de-duplicated by sha256, at most 5 per prompt.
  • scan/scan_command.py: images are collected for every prompt event, after the detach decision, so a report-mode child still finds them. Collection is best-effort, and the text is always scanned. The images are always sent; the server decides whether to scan them.

  • scan/handlers.py: one scan with prompt-content.txt plus one bytes document per image (image-<n>-<name>), built from the bytes read at collection, so each image is read once. Prompt, text and file-read scans share one scan helper. The verdict, block, timeout and fail-open behaviour are unchanged.

  • scan/utils.py: a finding in an image reads … in image "<name>", so the developer knows which attachment to remove.

  • Bytes support only where image documents go: Document.content: str | bytes with text_content, InMemoryZip.append and batch sizing.

  • README: the data-collected section says that attached images are sent with the prompt.

Tests

  • pytest tests: 1512 passed, 1 skipped (an existing Windows-only test).
  • New test_prompt_images.py and test_document.py, plus additions to test_copilot.py (including session.db reads against a real SQLite file), test_handlers.py, test_scan_command.py and test_in_memory_zip.py.
  • ruff check . and ruff format --check . are clean.

Release

The CLI always sends the images, so release this only after the server side that scans them is live in every region.

🤖 Generated with Claude Code

Comment thread README.md Resolved
@Altruistus
Altruistus force-pushed the CM-77702-block-secrets-in-attached-images branch from 7aa3375 to b4f7600 Compare November 16, 2026 08:36
Ilanlido
Ilanlido previously approved these changes Nov 16, 2026
Altruistus and others added 5 commits November 16, 2026 11:04
The UserPromptSubmit hook gets only the prompt text, but by the time it
runs Copilot has saved every attached image to disk. The Copilot
integration now finds them (@path mentions and pasted images in Copilot
CLI; send-time copies and pastes in VS Code, saved since the session's
previous prompt), and the prompt handler sends them with the text in one
scan, so a secret in a screenshot blocks the prompt like a secret in its
text.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A mention whose quote was left open read to the next quote anywhere in the
prompt, so the image mentions in between went unscanned.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The server decides whether to scan them, so the CLI no longer reads an
image setting from the guardrail config.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…chat

Every VS Code chat's attachments saved since the session's previous prompt
were taken, so a prompt could be blocked by an image attached in another
window, and a Copilot CLI prompt could pick up VS Code attachments. The
chat is now the one whose session database records the hook's session id.

The shared folder of pasted images is no longer read: agent sessions copy
every sent attachment into the chat's folder, and that folder also holds
images that were pasted but never sent.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Altruistus
Altruistus force-pushed the CM-77702-block-secrets-in-attached-images branch from b4f7600 to 4160741 Compare November 16, 2026 10:04

@omer-roth omer-roth left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

KISS / DRY / YAGNI pass: three medium findings, inline.

Altruistus and others added 2 commits November 16, 2026 12:10
The file excluder, its string helpers and the console printers never see an
image document, so they are back to their text-only versions.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Collecting the images already reads and checks every file, so the bytes now
travel with the path to the scan instead of being read and checked again.
Prompt, text and file-read scans also share one scan helper.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@Altruistus
Altruistus merged commit d7b9f61 into main Nov 16, 2026
25 checks passed
@Altruistus
Altruistus deleted the CM-77702-block-secrets-in-attached-images branch November 16, 2026 11:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants