Repository navigation
Read message attachments through an Attachments folder grant - #274
Merged
Merged
Conversation
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
Contributor
There was a problem hiding this comment.
🟡 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.
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.
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 |
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.


Fixes #130
What changed in the sandbox (the #247 blocker, solved)
messages_attachment_readwas 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:chat.db,chat.db-wal,chat.db-shmStickerCache/subtree (2 levels deep)Attachments/— listingAttachments/**— any file, by exact pathAttachments/Findings along the way:
com.apple.quarantine(present on the Attachments dir and on each received file, tagMessages) is not the blocker — stripping it changes nothing.temporary-exception.files.absolute-path.read-writeentitlement for/Users/*/Library/Messages/that has shipped since the initial commit is dead letter on this macOS: without a grant, evenchat.dbis denied.~/Library/Messages/Attachmentsitself lifts the wall completely. No Full Disk Access, no entitlement changes.What this PR does
messages_attachment_read({id, maxBytes} → base64 + name/type/size), unchanged from Messages: list attachments in messages_fetch and add messages_attachment_read #244;maxBytesdeclares the 1…100 MiB bounds the handler enforces.me.mattt.iMCP.messagesAttachmentsBookmark), mirroring the existing bookmark handling. The Attachments panel only enables the Attachments folder itself.readAttachmentreads 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).Grant lifecycle
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.maxBytesbudget re-checks the size afterwards; an attachment still downloading that grew past the limit reportstooLargeinstead of serving a silently truncated payload.Testing
swift format+xcodebuildclean.NSOpenPanelpowerbox →.withSecurityScopebookmark →startAccessingSecurityScopedResource): with both grants the Attachments tree lists (257 subdirectories) and a 3.3 MB PNG reads to the last byte.cc @patp (your #244 tool is back, and now it can actually read the files)