# Keep Clipboard Data Separate in Mac Automation

Canonical: https://figment.so/blog/mac-agent-clipboard-isolation

Capture clipboard data immediately, clear it with an EXIT trap, and pass secrets through chmod-600 temp files. Sequential single-user jobs fit a Figment Intel Mac VM ($25 a month); separate classes need separate users or Macs.

Stop clipboard leaks between Mac automation jobs with one rule: **capture immediately, then clear**. macOS exposes one general pasteboard per login session, shared by every process in that session. When a job leaves a parsed token, a customer name or a temporary credential on the clipboard, it stays there until something overwrites it, and a parallel job, a Shortcut or any GUI app can read it with `pbpaste`, with no error, no log entry and no TCC prompt. If your automation is single-user and sequential, a Figment Intel Mac VM ($25 a month) is enough. Clipboard isolation is a session-level discipline and applies on every plan.

## Which Figment plan fits clipboard-safe automation?

The [Intel Mac VM](/mac-hosting#intel-vm) ($25 a month, $2 a day, $0.15 an hour) is enough for shell-driven automation, `pbcopy` and `pbpaste` work, and Intel-compatible Xcode toolchains. It has 4 shared vCPUs, 4GB of RAM and an 80GB SSD, and runs macOS Sequoia, where Apple lists Xcode 26.0 to 26.3 (see Apple's [porting guidance](https://developer.apple.com/documentation/apple-silicon/porting-your-macos-apps-to-apple-silicon)). Outbound CI agents can connect to your runner service from the VM.

If two job classes must never see each other's clipboard, run them under separate macOS user accounts or on separate machines. You have administrator access to your own Mac or VM, so you can create users yourself. Pick the plan by how hard the separation must be:

- **Sequential jobs under one user:** the Intel Mac VM ($25 a month).
- **arm64 tools, or a second isolated user on Apple silicon:** the [Apple Silicon Mac VM](/mac-hosting#apple-silicon-vm) ($39 a month, $6 a day), with 4 shared vCPUs and 4GB of RAM.
- **Job classes that must not share a machine at all:** the [Dedicated M4 Pro 48 GB](/mac-hosting#dedicated-m4-pro) ($349 a month, or $299 a month on a 12-month term), a whole Mac with an M4 Pro chip, 48GB of RAM and 512GB of storage. Or run each class on its own VM.

Figment runs the Mac and network layer. Your scripts, users and secrets are yours to manage.

## Why is the clipboard a leakage surface?

The general pasteboard (`NSPasteboard.general`) is reachable by any process in the same session through `pbpaste`, without a permission prompt. These are the typical exposure points:

| Scenario | Data at risk |
|---|---|
| OCR step stages text on the clipboard for the next step | Prior extraction readable by the next job |
| Script writes an API token via `pbcopy` for one command | Token readable by any parallel script |
| Shortcut uses an internal Copy to Clipboard action | Output persists after the shortcut returns |
| UI automation pastes text via simulated keystrokes | Injected text lingers on the pasteboard |
| Script reads clipboard expecting fresh input | Reads stale data from a prior job |

Recent macOS releases added an interactive paste-protection prompt for foreground apps, but it does not intercept `pbpaste` in headless scripts. Verify the behaviour on the exact macOS version you deploy to. The Intel Mac VM runs macOS Sequoia.

## How do I capture and clear the clipboard in every script?

Add these wrapper functions and an `EXIT` trap at the top of every job script. Never leave job data on the pasteboard across step boundaries.

```bash
#!/usr/bin/env bash
set -euo pipefail

# Read the clipboard and immediately clear it
clipboard_take() {
    local value
    value=$(pbpaste 2>/dev/null || true)
    printf '' | pbcopy
    printf '%s' "$value"
}

# Clear the clipboard unconditionally
clipboard_clear() {
    printf '' | pbcopy
}

# Always clear on normal exit or trappable signal
trap clipboard_clear EXIT
```

The `EXIT` trap fires on normal exit, on `set -e` failures and on trappable signals. It does not fire on `SIGKILL` or on sudden power loss. Apple's [launchd job guide](https://developer.apple.com/library/archive/documentation/MacOSX/Conceptual/BPSystemStartup/Chapters/CreatingLaunchdJobs.html) notes that launchd issues `SIGTERM` for graceful shutdown of daemons and user agents, so your trap runs during a normal unload. Keep the capture-and-clear window short anyway. Use `printf ''` rather than `echo` so no trailing newline lands on the pasteboard.

This is an untested example. Run it against your own project and confirm its behaviour before trusting it in production.

## How do I pass a secret between steps without the clipboard?

Use a restricted temp file, not the pasteboard:

```bash
TMPFILE=$(mktemp -t job_data) || exit 1
chmod 600 "$TMPFILE"
cleanup() { rm -f "$TMPFILE"; clipboard_clear; }
trap cleanup EXIT
printf '%s' "$API_TOKEN" > "$TMPFILE"
# downstream step in the SAME shell reads it:
TOKEN=$(cat "$TMPFILE")
```

A separate CI step runs in a separate shell and will not know this temp path unless you persist it explicitly and safely. For real secrets, keep the value in your CI secret manager (GitHub Actions secrets, Vault, 1Password CLI) and read it directly into an environment variable inside the step that needs it.

## How do I stop Shortcuts from using the clipboard?

Prefer explicit output redirection over any internal Copy to Clipboard action. The [`shortcuts run` command](https://support.apple.com/guide/shortcuts-mac/run-shortcuts-from-the-command-line-apd455c82f02/mac) accepts `-o` or `--output-path` to write output to a file and supports piping to another command:

```bash
shortcuts run "Extract Invoice Data" \
    -i ~/invoice-spool/invoice_042.pdf \
    -o ~/results/invoice_042.json
```

If a Shortcut you do not control uses Copy to Clipboard internally, call `clipboard_clear` immediately after `shortcuts run` returns.

## What should a per-job clipboard checklist cover?

Use this checklist in code review:

```
Before job starts:
  [ ] trap clipboard_clear EXIT set at top of script
  [ ] If reading from clipboard, call clipboard_take() once, then
      work from the captured variable

During job:
  [ ] No secrets or PII passed through pbcopy/pbpaste
  [ ] Sensitive intermediates live in chmod-600 temp files
  [ ] shortcuts run uses -o; no internal Copy to Clipboard action
  [ ] UI steps that paste text clear the pasteboard afterward

After job (enforced by EXIT trap):
  [ ] Clipboard cleared (printf '' | pbcopy)
  [ ] Temp files removed (rm -f)

Verification:
  [ ] pbpaste returns empty after the job completes
```

Run the verification as a CI post-step:

```bash
clipboard_clear
REMAINING=$(pbpaste 2>/dev/null || true)
if [[ -n "$REMAINING" ]]; then
    echo "WARNING: clipboard not empty after job: $(printf '%s' "$REMAINING" | wc -c) bytes" >&2
fi
```

## What if the trap does not fire, or jobs race?

Handle each failure like this:

**`SIGKILL` or power loss before the trap fires.** The trap does not run. Minimise the window: read, clear, then work from variables or files for the rest of the job.

**Parallel jobs racing on the pasteboard.** Two jobs under the same user share one pasteboard. The reliable fix is not to use the clipboard as a handoff channel at all. Use temp files or pipes. For strong separation between job classes, run them as separate macOS users.

**Audit trail for compliance.** Log a byte count or hash, never the content, to a user-writable path:

```bash
AUDIT_LOG="${HOME}/Library/Logs/clipboard_audit.log"
clipboard_take_logged() {
    local value
    value=$(pbpaste 2>/dev/null || true)
    printf '' | pbcopy
    printf '%s CLIPBOARD_READ job=%s bytes=%d\n' \
        "$(date -u +%Y-%m-%dT%H:%M:%SZ)" "${JOB_NAME:-unknown}" \
        "$(printf '%s' "$value" | wc -c)" >> "$AUDIT_LOG"
    printf '%s' "$value"
}
```

## What does clipboard isolation cost?

It adds no runtime cost. The sizing question is session separation: if sequential jobs under one user are acceptable, the Intel Mac VM at $25 a month is enough. If you need Apple silicon (for arm64 execution, or because your signing policy requires it), the Apple Silicon Mac VM is $39 a month, and a whole dedicated Mac is $349 a month. Idle jobs do not pause billing.

## How do I roll this out on an existing set of scripts?

1. Pick a plan on the [Mac hosting page](/mac-hosting).
2. Audit existing scripts: `grep -rn 'pbcopy\|pbpaste' ~/scripts/`.
3. Add `trap clipboard_clear EXIT` and the `clipboard_take` wrapper to each script found.
4. Replace clipboard-based handoffs with chmod-600 temp files in the same shell, or move the handoff into your CI secret manager.
5. Run two jobs back-to-back and confirm `pbpaste` in the second returns an empty string.
6. Add the checklist above to code review.

**Related guides:** [Task queue for Mac agents](/blog/mac-agent-task-queue) · [Idempotent jobs for Mac agents](/blog/mac-agent-idempotent-jobs) · [Self-hosted GitHub Actions runner on Mac](/blog/mac-github-actions-self-hosted-runner)
