Skip to content

Read message attachments through an Attachments folder grant - #274

Merged
mattt merged 11 commits into
mattt:mainfrom
bradhallett:attachment-read-spike
Oct 13, 2026
Merged

mattt merged 11 commits into
mattt:mainfrom
bradhallett:attachment-read-spike

Conversation

@bradhallett

@bradhallett bradhallett commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #130

What changed in the sandbox (the #247 blocker, solved)

messages_attachment_read was reverted in #247 with "until we know what grants access to attachment files". I ran that down on macOS 27.2 with a standalone sandboxed probe app (app-sandbox + files.user-selected.read-write) holding the exact Messages folder grant iMCP stores:

Path Messages-folder grant, scope active
chat.db, chat.db-wal, chat.db-shm ✅ readable
StickerCache/ subtree (2 levels deep) ✅ listable + readable
Attachments/ — listing ❌ EPERM
Attachments/** — any file, by exact path ❌ EPERM (surfacing as "no such file")
Same paths with a direct grant on Attachments/ ✅ everything reads

Findings along the way:

  • The wall is specific to the Attachments subtree; folder grants otherwise propagate.
  • com.apple.quarantine (present on the Attachments dir and on each received file, tag Messages) is not the blocker — stripping it changes nothing.
  • The temporary-exception.files.absolute-path.read-write entitlement for /Users/*/Library/Messages/ that has shipped since the initial commit is dead letter on this macOS: without a grant, even chat.db is denied.
  • A powerbox grant on ~/Library/Messages/Attachments itself lifts the wall completely. No Full Disk Access, no entitlement changes.

What this PR does

  1. Reverts Remove messages_attachment_read until attachment files are readable #247, restoring messages_attachment_read ({id, maxBytes} → base64 + name/type/size), unchanged from Messages: list attachments in messages_fetch and add messages_attachment_read #244; maxBytes declares the 1…100 MiB bounds the handler enforces.
  2. Activation asks for the Attachments folder right after the Messages folder and stores a second read-only security-scoped bookmark (me.mattt.iMCP.messagesAttachmentsBookmark), mirroring the existing bookmark handling. The Attachments panel only enables the Attachments folder itself.
  3. readAttachment reads files through that scope; declining the extra grant only affects this one tool, which then returns an error saying what to grant (switch Messages off and on in the menu).
  4. Development builds on the default path read without any grant, as before.

Grant lifecycle

  • The off/on remediation actually reaches the picker: while the Messages folder grant is held but the Attachments grant is missing, the service reports itself as not fully activated (same convention as the legacy chat.db-file grant), so the toggle keeps running activation and offering the Attachments folder. A declined first offer can still be granted later; a stored grant never re-prompts.
  • A stale bookmark refreshes in place; an unresolvable one is dropped with an error pointing at the re-grant path, and the next toggle offers the folder again.
  • The missing-grant probe distinguishes a walled Attachments folder from a deleted one (the wall leaves the directory entry stat-able while denying the subtree): a deleted folder reports the file as missing, a walled one reports the missing grant — each error points at the action that actually fixes it.
  • A read that fills the entire maxBytes budget re-checks the size afterwards; an attachment still downloading that grew past the limit reports tooLarge instead of serving a silently truncated payload.

Testing

  • swift format + xcodebuild clean.
  • The grant matrix above is from live runs of the probe app (same mechanics iMCP uses: NSOpenPanel powerbox → .withSecurityScope bookmark → startAccessingSecurityScopedResource): with both grants the Attachments tree lists (257 subdirectories) and a 3.3 MB PNG reads to the last byte.
  • Note: local Debug builds of iMCP itself are not sandboxed, so the sandbox-dependent behavior can't be exercised through the app in development — hence the standalone probe. Signing a Release-style build should show the grant prompt pair on first activation.

cc @patp (your #244 tool is back, and now it can actually read the files)

Restores messages_attachment_read (reverted in mattt#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.
- 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
- 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
- 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
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.
- 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

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.

🟡 Changes recommended

Attachment reads can truncate data, and invalid limits or stale grants are not handled correctly.

3 open findings
What changed in this PR

Restores message attachment retrieval using a dedicated security-scoped Attachments folder grant.

Changes:

  • Adds bounded, base64 attachment reads.
  • Adds Attachments grant activation and bookmark lifecycle.
  • Documents the additional sandbox permission.
File Description
App/​Services/​Messages.swift Implements attachment reads and grant handling.
README.md Documents attachment access requirements.

🧠 Review effort: Balanced


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

Comment thread App/Services/Messages.swift Resolved
Comment thread App/Services/Messages.swift Resolved
Comment thread App/Services/Messages.swift Resolved
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.
…-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.
@bradhallett

Copy link
Copy Markdown
Contributor Author

Follow-up in f072af3: when a file grows past maxBytes mid-read, the error now reports the re-statted file size instead of data.count, which the read loop caps at the limit.

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.

🟡 Changes recommended

Symlink validation occurs before the required sandbox scope, weakening the intended path boundary.

1 open finding
3 resolved since last review

🧠 Review effort: Balanced

Comment thread App/Services/Messages.swift Resolved
bradhallett and others added 2 commits October 13, 2026 07:42
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.
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.

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.

🔵 Needs a closer look

Sandbox-specific permissions and file-boundary enforcement lack end-to-end automated validation.

0 open findings

1 resolved since last review

🧠 Review effort: Balanced

@mattt
mattt merged commit 2c54e21 into mattt:main Oct 13, 2026
2 checks passed
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.

Feature Request: Add messages_attachment_fetch tool

3 participants