Skip to content

[BUG]: apply_patch internally reads files, allowing AI to patch unread files blind #30770

Closed

Description

Description

apply_patch's "update" handler (apply-patch.ts:125) internally reads the file content:

const source = yield* fs.readFile(target.canonical)

This means the AI can apply a patch to a file it has never read. The tool reads internally just for diff application — the AI still doesn't know the file's full content. This allows the AI to submit patches that conflict with content it's unaware of.

If the AI hasn't read the file, it doesn't know:

  • The exact whitespace/indentation style
  • Other code nearby that the patch might interact with
  • Whether the file even contains what the AI assumes

The writeIfUnchanged check at line 155 does catch if content changed between the internal read and write, but the AI never got a chance to see the full file before deciding what to patch.

Chain from #30376.

Plugins

N/A (source code analysis)

OpenCode version

main

Steps to reproduce

  1. AI has never read src/foo.ts
  2. AI calls apply_patch with a patch for src/foo.ts
  3. Patch is applied without AI ever seeing the file's content

Screenshot and/or share link

N/A (we read the source code)

Operating System

N/A (code analysis, not environment-specific)

Terminal

N/A

Activity

  1. github-actions commented on Jun 4, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of an existing issue. Please check:

    If the intent is to track this as a separate, more focused sub-issue, please clarify how it differs from the coverage in #30376, or consider adding this detail as a comment there instead.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions