Skip to content

Remove messages_attachment_read until attachment files are readable - #247

Merged
mattt merged 1 commit into
mainfrom
mattt/remove-attachment-read
Sep 26, 2026
Merged

mattt merged 1 commit into
mainfrom
mattt/remove-attachment-read

Conversation

@mattt

@mattt mattt commented Sep 26, 2026

Copy link
Copy Markdown
Owner

This PR removes the messages_attachment_read tool added in #244, so it does not ship in 1.6.0.

In a signed, sandboxed build of main, iMCP reads chat.db through the Messages folder grant, but opening any file under ~/Library/Messages/Attachments fails with "Operation not permitted". That includes a screenshot sent seconds earlier. It fails with the folder grant's security scope active and after adding the app to Full Disk Access, and no sandbox or TCC denial is logged. So the tool reported every attachment as missing.

messages_fetch with attachments: true still lists each attachment's name, type, size, and @id from chat.db. Reading the files can come back once we know what grants access to them.

In the signed, sandboxed app, iMCP can read chat.db through the
Messages folder grant, but opening a file under
~/Library/Messages/Attachments fails with "Operation not permitted",
even with the grant's security scope active and with Full Disk Access.
The tool reported every attachment as missing. messages_fetch keeps
listing attachments from chat.db.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

The reviewed changes consistently remove unsupported functionality while preserving attachment metadata.

Review effort: Lite
Findings: None

What changed in this PR

Removes the unavailable messages_attachment_read tool while preserving attachment metadata in messages_fetch.

Changes:

  • Removes attachment file-reading logic and size limits.
  • Updates documentation for metadata-only attachment handling.
File Description
App/​Services/​Messages.swift Removes the attachment-reading tool and implementation.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@mattt
mattt merged commit 37145ce into main Sep 26, 2026
2 checks passed
@mattt
mattt deleted the mattt/remove-attachment-read branch September 26, 2026 03:09
mattt added a commit that referenced this pull request Oct 13, 2026
* Revert "Remove messages_attachment_read until attachment files are readable (#247)"

This reverts commit 37145ce.

* Read message attachments through an Attachments folder grant

Restores messages_attachment_read (reverted in #247) on top of what
that revert was waiting for: a way to grant access to the files.

A sandboxed app cannot read ~/Library/Messages/Attachments through the
Messages folder grant. Verified on macOS 27.2 with a sandboxed probe
app holding that exact grant:

- Everything else the grant covers reads fine: chat.db and its
  write-ahead log, and other subtrees like StickerCache, two levels
  deep
- The Attachments subtree is denied outright — listing and reading,
  with deep paths surfacing as 'no such file'
- The com.apple.quarantine xattrs on the directory and on received
  files are not the blocker; stripping them changes nothing
- A powerbox grant on ~/Library/Messages/Attachments itself lifts the
  wall: the whole tree lists and reads

So activation now asks for the Attachments folder right after the
Messages folder and stores a second, read-only, security-scoped
bookmark. Declining only affects this tool, which then returns an
error explaining what to grant. Development builds on the default
path keep reading without a grant.

* Harden attachments grant path and read limits

- Existing installs holding the Messages folder grant are offered the
  Attachments grant when Messages is toggled off and on, so the
  remediation in the grant-missing error is now real
- The Attachments picker accepts exactly ~/Library/Messages/Attachments
  (standardized path) and enables that folder for its panel alone;
  dismissing keeps messages working, a wrong selection is logged
- maxBytes is validated against a 100 MB ceiling that matches the cost
  of a base64 response, and a file whose size cannot be read fails
  honestly instead of passing as empty
- Attachment paths resolve symlinks before the Messages-folder prefix
  check; grantScopeFailed now says how to fix it

* Make the Attachments grant actually carry reads

- The no-grant-needed shortcut probes the Attachments folder itself.
  chat.db is readable through the Messages folder grant's scope, which
  tool calls open around the database, so the old probe skipped the
  Attachments grant entirely in sandboxed installs and every read
  failed as fileMissing
- A stale Attachments bookmark is re-stored from its resolved URL while
  it still works, instead of failing later with a misleading error
- The Attachments grant is offered only in user-initiated flows: a tool
  call that just walked the Messages picker no longer stacks a second
  modal panel
- Attachment files are read through a bounded FileHandle read, so a
  file still downloading cannot grow past maxBytes between the size
  check and the read
- A wrong selection in the Attachments picker reports an Attachments
  error, not the Messages-folder one; README documents the grant

* Keep the Attachments grant recoverable and reads honest

- A bookmark that no longer resolves, or whose scope cannot start, is
  dropped instead of kept: the re-grant path the errors point at only
  offers the picker when no bookmark is stored, so keeping it dead-ended
  the remediation
- A stored grant takes precedence over the development shortcut, so a
  concurrent call's scope can no longer make the probe pass while our
  own scope never starts
- read(upToCount:) reports end-of-file as nil; a readable zero-byte
  attachment returns empty content instead of failing as missing

* Distinguish a deleted Attachments folder from the sandbox wall

The wall denies the Attachments subtree but leaves the directory entry
stat-able, so existence separates the two states when no grant is
stored: a deleted folder now surfaces as fileMissing from the per-file
checks instead of grantMissing, whose remediation panel has nothing
left to select.

A full-budget read also re-checks the size afterwards; a file still
downloading could grow past the limit between the check and the read,
and the truncated chunk would otherwise pass as complete content.

* Make the Attachments re-grant reachable and the probe honest about walls

- isActivated reports false while the Messages folder grant is held
  but the Attachments grant is missing, following the chat.db-file
  precedent: the toggle then keeps calling activate(), whose directory
  branch offers the grant. Previously the remediation every read
  error and the README point at — switch Messages off and on — was a
  no-op once the folder grant existed, so a declined first offer
  could never be granted afterwards
- The deleted-folder probe distinguishes walls from deletion by the
  Messages folder's own reachability: under a grant on chat.db alone
  that folder is unreachable too, so the missing Attachments folder
  is walled, not deleted, and grantMissing (whose remediation fixes
  it) is the honest error again
- maxBytes declares the 1…100 MiB bounds the handler enforces,
  matching every other bounded integer in the services
- The Attachments panel no longer offers the Messages folder itself
  as a selectable target, which only ever produced wrongFolderSelected

* Keep attachment reads honest from limit to grant

A present maxBytes of the wrong shape fell back to the 25 MiB default;
it is now rejected (integral doubles still count as integers, as JSON
Schema does). Attachment reads loop to EOF or one byte past the limit
so a short read(upToCount:) can no longer serve silently truncated
content, and a file that grows past the limit mid-read is still
rejected. A stored Attachments bookmark that no longer makes the
folder readable is dropped after its scope starts, so the re-grant
path stays reachable instead of a dead grant masking it.

* Report the re-statted size when an attachment grows past maxBytes mid-read

The read loop stops one byte past the limit, so the overflow error
named data.count, which is capped there. Re-stat the file so the
message carries a size the caller can act on.

* Resolve attachment symlinks inside the Attachments scope

resolvingSymlinksInPath() and the Messages-prefix check ran before the
Attachments security scope started. In the sandbox state this path
handles, that subtree is not yet inspectable, so a symlink planted
under Attachments could survive resolution and pass the check by its
/Messages/Attachments/... spelling alone; FileHandle would then follow
it after the scopes went active, defeating the boundary. The URL is
now resolved and validated inside the scope, before anything opens,
and the resolved lastPathComponent is carried out of the scope for the
attachment name.

* Check the opened attachment file against the Messages folder

The path is resolved and checked before FileHandle opens it,
so a link swapped in between the check and the open
was still followed out of the Messages folder.
Read the opened descriptor's path with F_GETPATH
and refuse the read unless it is inside the Messages folder.

---------

Co-authored-by: mattt <[email protected]>
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.

2 participants