The attachment reader accepts temp directories as allowed roots and matches by bare prefix #2

Closed
opened 2026-11-12 20:18:14 +00:00 by svc-agent-writer · 2 comments

Found by a read of main at 5bc2758 on 2026-11-13, during the verification for dev/messages-mind-mcp Phase A.

What is wrong

The check that decides whether an attachment path is under an allowed root accepts the system temp directories in production code, and it compares by string prefix. A prefix match accepts a sibling directory whose name starts with an allowed root's name. The attachment path comes from a row in the Messages history and must be treated as untrusted. Whether a remote sender can control the stored filename has not been established.

What to decide and fix

  • Whether the temp directories are needed outside tests. If not, remove them from the production list.
  • Compare resolved path components, not string prefixes, and resolve symbolic links before the comparison.

Acceptance criteria

  • A test proves that a path in a sibling directory with a shared name prefix is refused.
  • A test proves that a symbolic link that leaves the allowed root is refused.
  • The production root list holds no temp directory, or the reason for one is written beside it.

Blocked by

  • none

Filed under: operator word "go" sha256:4cd0e21a9a07

Triage — 2026-11-16

Disposition: confirmed bug; ready-for-agent. Priority: high, because the attachment reader can return bytes outside the promised root. No human design decision or dependency is outstanding. This ticket authorizes preparation of an implementation; it does not itself authorize dispatch or release.

Evidence

Verified against main at ba2f5dbc1a069dd9dc6e23de035c9c949c98d6f6.

  • Sources/AppleMessagesBlade/AttachmentReader.swift:24–30 checks the expanded filename before opening it; it never resolves symlinks. Data(contentsOf:) then follows them.
  • Sources/AppleMessagesBlade/AttachmentReader.swift:64–67 admits /private/tmp/ and /tmp/ unconditionally and uses hasPrefix for the configured attachment root. The default root has no trailing slash, so Attachments-other/file passes its check.
  • MessagesBladeConfig already accepts an explicit attachmentsRoot. Fixture tests can set their own root; a production-wide temp exception is unnecessary.
  • Existing AttachmentPathTests check an ordinary descendant, unrelated paths, and a missing temp file. They do not cover temp-file reads, prefix siblings, or symlink escapes.

A throwaway Swift harness compiled the current reader with the current config, errors and attachment model declarations. It used synthetic files only and did not open chat.db or real attachments. Result: ordinary file under an explicit root passed; default-config temp-file rejection failed (bytes returned); default-root sibling predicate rejection failed (accepted); explicit-root symlink escape rejection failed (bytes returned). The symlink fixture and target were both under temp, so that read demonstrates the current failure to confine to the configured root, rather than isolating it from the broad temp allowance. No remote-sender exploit was tested or established.

Implementation brief

  1. Make config.attachmentsRoot the only attachment byte root. Remove the unconditional temp roots. Tests that need temp files must pass their own isolated attachment root explicitly.
  2. Expand tilde and canonicalize both root and candidate, including symlink resolution. Compare path components and require a file strictly below the root; do not use an unbounded string prefix. Handle roots with and without a trailing separator consistently.
  3. Read the same canonical candidate that passed validation; do not validate one path and then reopen the original unresolved path. Preserve attachmentNotFound for missing files, directories and rejected paths, and preserve the existing attachment size cap and response shape.
  4. Keep this change in attachment-byte access. Do not change MessagesCore store-path policy, add an attachment reader there, or broaden the public API unnecessarily.

Required verification

  • Use synthetic files and exercise AttachmentReader.read(meta:), checking the specific rejection error, not just any thrown error.
  • A normal descendant returns the expected bytes with an explicit fixture root.
  • An existing temp file is rejected by the default production config.
  • An existing sibling with a shared root-name prefix is rejected, including root spellings with and without a trailing separator. Do not rely only on the predicate test.
  • Both a file symlink and an intermediate directory symlink leaving the configured root are rejected. A normal descendant reached through a configured root that itself resolves through a symlink remains usable after canonicalization.
  • Missing files, directories and oversized files retain their existing behavior. Run AttachmentPathTests and the AppleMessagesBladeTests target after the change.

The three original acceptance criteria remain in force, with the temp-directory decision resolved to removal of implicit exceptions. Blocked by: none. No implementation was changed during triage.

Found by a read of `main` at `5bc2758` on 2026-11-13, during the verification for `dev/messages-mind-mcp` Phase A. ## What is wrong The check that decides whether an attachment path is under an allowed root accepts the system temp directories in production code, and it compares by string prefix. A prefix match accepts a sibling directory whose name starts with an allowed root's name. The attachment path comes from a row in the Messages history and must be treated as untrusted. Whether a remote sender can control the stored filename has not been established. ## What to decide and fix - Whether the temp directories are needed outside tests. If not, remove them from the production list. - Compare resolved path components, not string prefixes, and resolve symbolic links before the comparison. ## Acceptance criteria - A test proves that a path in a sibling directory with a shared name prefix is refused. - A test proves that a symbolic link that leaves the allowed root is refused. - The production root list holds no temp directory, or the reason for one is written beside it. ## Blocked by - none Filed under: operator word "go" sha256:4cd0e21a9a07 ## Triage — 2026-11-16 **Disposition: confirmed bug; ready-for-agent. Priority: high, because the attachment reader can return bytes outside the promised root.** No human design decision or dependency is outstanding. This ticket authorizes preparation of an implementation; it does not itself authorize dispatch or release. ### Evidence Verified against main at `ba2f5dbc1a069dd9dc6e23de035c9c949c98d6f6`. - `Sources/AppleMessagesBlade/AttachmentReader.swift:24–30` checks the expanded filename before opening it; it never resolves symlinks. `Data(contentsOf:)` then follows them. - `Sources/AppleMessagesBlade/AttachmentReader.swift:64–67` admits `/private/tmp/` and `/tmp/` unconditionally and uses `hasPrefix` for the configured attachment root. The default root has no trailing slash, so `Attachments-other/file` passes its check. - `MessagesBladeConfig` already accepts an explicit `attachmentsRoot`. Fixture tests can set their own root; a production-wide temp exception is unnecessary. - Existing `AttachmentPathTests` check an ordinary descendant, unrelated paths, and a missing temp file. They do not cover temp-file reads, prefix siblings, or symlink escapes. A throwaway Swift harness compiled the current reader with the current config, errors and attachment model declarations. It used synthetic files only and did not open chat.db or real attachments. Result: ordinary file under an explicit root passed; default-config temp-file rejection failed (bytes returned); default-root sibling predicate rejection failed (accepted); explicit-root symlink escape rejection failed (bytes returned). The symlink fixture and target were both under temp, so that read demonstrates the current failure to confine to the configured root, rather than isolating it from the broad temp allowance. No remote-sender exploit was tested or established. ### Implementation brief 1. Make `config.attachmentsRoot` the only attachment byte root. Remove the unconditional temp roots. Tests that need temp files must pass their own isolated attachment root explicitly. 2. Expand tilde and canonicalize both root and candidate, including symlink resolution. Compare path components and require a file strictly below the root; do not use an unbounded string prefix. Handle roots with and without a trailing separator consistently. 3. Read the same canonical candidate that passed validation; do not validate one path and then reopen the original unresolved path. Preserve `attachmentNotFound` for missing files, directories and rejected paths, and preserve the existing attachment size cap and response shape. 4. Keep this change in attachment-byte access. Do not change `MessagesCore` store-path policy, add an attachment reader there, or broaden the public API unnecessarily. ### Required verification - Use synthetic files and exercise `AttachmentReader.read(meta:)`, checking the specific rejection error, not just any thrown error. - A normal descendant returns the expected bytes with an explicit fixture root. - An existing temp file is rejected by the default production config. - An existing sibling with a shared root-name prefix is rejected, including root spellings with and without a trailing separator. Do not rely only on the predicate test. - Both a file symlink and an intermediate directory symlink leaving the configured root are rejected. A normal descendant reached through a configured root that itself resolves through a symlink remains usable after canonicalization. - Missing files, directories and oversized files retain their existing behavior. Run `AttachmentPathTests` and the `AppleMessagesBladeTests` target after the change. The three original acceptance criteria remain in force, with the temp-directory decision resolved to removal of implicit exceptions. `Blocked by`: none. No implementation was changed during triage.
Collaborator

⏸ SVD architect gate: dev/apple-messages-blade-mcp#2 the-attachment-reader-accepts-temp-directories-as-allowed-ro

Status: CLEAN after 1 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity standard · dispatch effort medium

Run: 20261116T111805Z-1cde15e3

Held because: CLEAN but 4 open risk(s) (2 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim:

Open risks:

  1. No Swift build or test could run in the Half-A sandbox, so realpath(3) behaviour in Swift, the red-first failures and the table's Base column are unmeasured here. On the host: run swift test --filter AppleMessagesBladeTests on base 4669119 and record the failure set. With only the read(meta:) tests applied, the rows marked red (AC 1-3, RV 3, 4, 5a, 5b, 5c) must fail and the rows marked green must pass. After the fix, every scenario row passes under its own filter, and the target failure set is otherwise unchanged. [verification]
  2. Should the residual check-then-use window between realpath and Data(contentsOf:) be closed, for example with open(O_NOFOLLOW), fstat and a read from the descriptor? Lean: no. Triage requires reading the validated canonical path, which this spec does, and exploiting the window needs same-user write access inside the attachments root. [decision]
  3. Should MessagesCore Config.validate(storePath:) (Config.swift:84-105), which has the same bare-prefix and unconditional /tmp shape, get a separate follow-up issue? Triage item 4 keeps it out of this unit. Lean: file a separate issue. [decision]
  4. Out-of-repo consumers (StallariKit / stallari-harness) that build MessagesBladeConfig with temp attachment fixtures cannot be checked from this sandbox. Before the next pin bump, grep the consumer for AttachmentReader and attachmentsRoot usages. [verification]

Change: Keep the read(meta:) shape at AttachmentReader.swift:22-54 and replace only the confinement step. isUnderAllowedRoot (:64-68: a bare hasPrefix check over [root, "/private/tmp/", "/tmp/"]) becomes static canonicalPath (tilde-expand, then realpath(3)), isStrictlyBelow (compares pathComponents) and confinedPath(raw, config) helpers, with config.attachmentsRoot as the only root. Every later fileExists, attributesOfItem and Data(contentsOf:) call uses the canonical path. The filename stays lastPathComponent of the display path, and errors are unchanged (attachmentNotFound and attachmentTooLarge). Extend AttachmentPathTests (UnitTests.swift:208-230) with a per-test UUID fixture under temporaryDirectory and explicit MessagesBladeConfig(attachmentsRoot:). Every scenario goes through read(meta:) with specific error equality. Add a CHANGELOG Security entry.

Files: Sources/AppleMessagesBlade/AttachmentReader.swift · Tests/AppleMessagesBladeTests/UnitTests.swift · CHANGELOG.md

Decision: accept the 4 open risk(s) above and dispatch this spec (svd:go), or park (svd:skip) / re-spec (svd:respec)?

Your call (when this issue is held at the gate via svd:hold):

  • add label svd:go — approve; Half-B dispatch accepts exactly this spec (open risks accepted)
  • add label svd:fire beside svd:go — approve AND authorise the Half-B fire from the phone; never on its own
  • add label svd:skip — park it; dispatch refuses it and the issue stays for a human
  • add label svd:respec — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context

Spec: /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-16-the-attachment-reader-accepts-temp-directories-as-allowed-ro.md — next: codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-16-the-attachment-reader-accepts-temp-directories-as-allowed-ro.md

### ⏸ SVD architect gate: dev/apple-messages-blade-mcp#2 the-attachment-reader-accepts-temp-directories-as-allowed-ro **Status:** CLEAN after 1 rework round(s) · 0 blocking defect(s) · 0 lens(es) INCONCLUSIVE (could not run) · 0 lens(es) DEGRADED (deterministic denial) · complexity `standard` · dispatch effort `medium` **Run:** `20261116T111805Z-1cde15e3` **Held because:** CLEAN but 4 open risk(s) (2 verification · 2 decision) — HOLD-ALL parks everything. The risks, verbatim: **Open risks:** 1. No Swift build or test could run in the Half-A sandbox, so realpath(3) behaviour in Swift, the red-first failures and the table's Base column are unmeasured here. On the host: run `swift test --filter AppleMessagesBladeTests` on base 4669119 and record the failure set. With only the read(meta:) tests applied, the rows marked red (AC 1-3, RV 3, 4, 5a, 5b, 5c) must fail and the rows marked green must pass. After the fix, every scenario row passes under its own filter, and the target failure set is otherwise unchanged. [verification] 2. Should the residual check-then-use window between realpath and Data(contentsOf:) be closed, for example with open(O_NOFOLLOW), fstat and a read from the descriptor? Lean: no. Triage requires reading the validated canonical path, which this spec does, and exploiting the window needs same-user write access inside the attachments root. [decision] 3. Should MessagesCore Config.validate(storePath:) (Config.swift:84-105), which has the same bare-prefix and unconditional /tmp shape, get a separate follow-up issue? Triage item 4 keeps it out of this unit. Lean: file a separate issue. [decision] 4. Out-of-repo consumers (StallariKit / stallari-harness) that build MessagesBladeConfig with temp attachment fixtures cannot be checked from this sandbox. Before the next pin bump, grep the consumer for AttachmentReader and attachmentsRoot usages. [verification] **Change:** Keep the read(meta:) shape at AttachmentReader.swift:22-54 and replace only the confinement step. isUnderAllowedRoot (:64-68: a bare hasPrefix check over [root, "/private/tmp/", "/tmp/"]) becomes static canonicalPath (tilde-expand, then realpath(3)), isStrictlyBelow (compares pathComponents) and confinedPath(raw, config) helpers, with config.attachmentsRoot as the only root. Every later fileExists, attributesOfItem and Data(contentsOf:) call uses the canonical path. The filename stays lastPathComponent of the display path, and errors are unchanged (attachmentNotFound and attachmentTooLarge). Extend AttachmentPathTests (UnitTests.swift:208-230) with a per-test UUID fixture under temporaryDirectory and explicit MessagesBladeConfig(attachmentsRoot:). Every scenario goes through read(meta:) with specific error equality. Add a CHANGELOG Security entry. **Files:** `Sources/AppleMessagesBlade/AttachmentReader.swift` · `Tests/AppleMessagesBladeTests/UnitTests.swift` · `CHANGELOG.md` **Decision:** accept the 4 open risk(s) above and dispatch this spec (`svd:go`), or park (`svd:skip`) / re-spec (`svd:respec`)? **Your call** (when this issue is held at the gate via `svd:hold`): - add label `svd:go` — approve; Half-B dispatch accepts exactly this spec (open risks accepted) - add label `svd:fire` **beside** `svd:go` — approve AND authorise the Half-B fire from the phone; never on its own - add label `svd:skip` — park it; dispatch refuses it and the issue stays for a human - add label `svd:respec` — discard this spec; the next Half-A run re-produces it with this record as prior-attempt context <sub>Spec: `/Users/piers/.local/state/exhaust/groupthink/specs/2026-11-16-the-attachment-reader-accepts-temp-directories-as-allowed-ro.md` — next: `codex-svd dispatch --args '<same-json>' --approved-spec-path /Users/piers/.local/state/exhaust/groupthink/specs/2026-11-16-the-attachment-reader-accepts-temp-directories-as-allowed-ro.md`</sub>
Collaborator

spec-verify-dispatch Half B for dev/apple-messages-blade-mcp#2 the-attachment-reader-accepts-temp-directories-as-allowed-ro completed integration verify.

  • PR: #10
  • Branch: feat/svd-dev-apple-messages-blade-mcp-2-the-attachment-reader-8827a41a
  • Commit: 0675815f98
  • Build: green
  • Test: green
  • Reader audit clean: True
  • Spec fidelity: True
  • Ready for merge: True
  • Check commit statuses on PR head 0675815: absent
  • Checks: PR #10 head 0675815: local build green. AppleMessagesBladeTests passed (38 tests, 3 skipped, 0 failures). AttachmentPathTests passed (10 of 10). Remote CI: no statuses (absent). No blockers.
  • Warning: No remote CI statuses exist on PR head 0675815; remote CI truth is absent, not green.
  • Warning: make lint was reported failing by the subagent on the repo's pre-existing swift-format indentation baseline; it was not re-run here. The spec asks for lint to stay clean on touched files, so confirm the baseline claim.
  • Warning: no CI for commit statuses on PR head 0675815: Forgejo returned no commit statuses for head 0675815 and no CI context has reported. No tracking pointer was found. — reported as absent, not blocking (gh#633)
  • Decision: review the PR and merge? (codex-svd ship --run-id 20261116T114111Z-8827a41a --confirm-merge)
spec-verify-dispatch Half B for dev/apple-messages-blade-mcp#2 the-attachment-reader-accepts-temp-directories-as-allowed-ro completed integration verify. - PR: https://git.groupthink.asia/dev/apple-messages-blade-mcp/pulls/10 - Branch: feat/svd-dev-apple-messages-blade-mcp-2-the-attachment-reader-8827a41a - Commit: 0675815f98f7caa6777ed75cefa2fb77286bad8b - Build: green - Test: green - Reader audit clean: True - Spec fidelity: True - Ready for merge: True - Check commit statuses on PR head 0675815: absent - Checks: PR #10 head 0675815: local build green. AppleMessagesBladeTests passed (38 tests, 3 skipped, 0 failures). AttachmentPathTests passed (10 of 10). Remote CI: no statuses (absent). No blockers. - Warning: No remote CI statuses exist on PR head 0675815; remote CI truth is absent, not green. - Warning: make lint was reported failing by the subagent on the repo's pre-existing swift-format indentation baseline; it was not re-run here. The spec asks for lint to stay clean on touched files, so confirm the baseline claim. - Warning: no CI for commit statuses on PR head 0675815: Forgejo returned no commit statuses for head 0675815 and no CI context has reported. No tracking pointer was found. — reported as absent, not blocking (gh#633) - Decision: review the PR and merge? (`codex-svd ship --run-id 20261116T114111Z-8827a41a --confirm-merge`)
Sign in to join this conversation.
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
dev/apple-messages-blade-mcp#2
No description provided.