Skip to content

Add privacy-bounded Messages 2FA provider - #49

Open
LivioGama wants to merge 5 commits into
fdussert:mainfrom
LivioGama:codex/messages-2fa-core-pr
Open

LivioGama wants to merge 5 commits into
fdussert:mainfrom
LivioGama:codex/messages-2fa-core-pr

Conversation

@LivioGama

@LivioGama LivioGama commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Adds a default-off, local-only messages-2fa provider for a built-in widget request. The provider reads only incoming Messages from the last five minutes (at most 100 rows) through read-only SQLite with a timeout, and copies a context-matched code on the host. The widget receives only { ok: true }; database and pasteboard errors stay generic. The command is built-in-only and requires the separate Messages 2FA privacy setting, which defaults off.

The pasteboard writer uses a fixed JXA script and sends the code over stdin, not argv. It marks the entry concealed, transient, and current-host-only. The writer captures the change count returned by prepareForNewContentsWithOptions(CurrentHostOnly), checks the string and both metadata writes, verifies the same count before returning, and passes that captured count to delayed cleanup. Cleanup compares changeCount before clearing, but the comparison and clear are separate AppKit calls; cleanup is best-effort, and real macOS/JXA behavior remains unverified.

The matcher prefers verification-context numbers, handles English and French wording, skips date-shaped and account-ending numbers, excludes group chats, and rate-limits requests. Contact exclusion is not implemented; the provider does not request Contacts access.

This PR supplies the core provider only. The separate Sietch widget PR #16 remains outside this repository; marketplace widgets cannot invoke this built-in-only command, so trusted integration and the Sietch widget outcome remain unresolved.

Clipboard ownership race correction: commit 261592f83a32106b625daaadb7ef359e5876e052 preserves the acquired pasteboard token and refuses to arm cleanup after failed metadata writes or detected ownership changes.

Verification at 261592f83a32106b625daaadb7ef359e5876e052: focused Messages 2FA tests passed (21/21); full server suite passed (134 files, 2,699 tests, 2 skipped); server source and test TypeScript checks passed; git diff --check passed. Real Messages, pasteboard, JXA, or end-to-end widget behavior was not tested.

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks a lot for this, Livio — the provider itself is carefully written: constant SQL, -readonly + query_only, a timeout, a row cap and a time window. It fails closed, the code goes through pbcopy's stdin rather than argv, errors are generic, nothing is logged, and paired devices are refused. That's the hard part done well. Before it can land in core, though, the threat model needs a few changes:

1. The channel should be built-in only. Right now any registry widget that lists messages-2fa in commands can call copyLatest whenever it likes; nothing ties the call to a real tap, and the consent dialog only says "controls messages-2fa". That widget can't read the code (clipboard is built-in only and the frame has no clipboard permission), but it can push a fresh OTP onto the pasteboard at a moment of its choosing, overwrite whatever the user just copied in a loop, and learn from ok vs "no code" that a sign-in is happening. Once the code is on the pasteboard, Universal Clipboard, cloud-synced clipboard managers and Fremkit's own clipboard widget all read it. Please add messages-2fa to BUILTIN_ONLY_CHANNELS. That means the widget ships in widgets/ rather than on the registry.

2. It needs its own opt-in. Full Disk Access may already be granted for the disk map, and with this PR that grant silently enables reading Messages. SECURITY.md currently says FDA only benefits the disk map. Please gate the command on a privacy setting that is off by default (like claudeAccountUsage), explain the FDA scope next to it, and update SECURITY.md. That includes the "same-user process is out of scope" paragraph: a process without FDA could now get Messages codes through Fremkit.

3. Mark the pasteboard write as concealed and transient. A bare pbcopy makes the OTP an ordinary entry. Fremkit's clipboard history records it unmasked (6 digits is under the secret heuristic's length) and shows it on the Edge and on a paired device whose screen holds that widget. Suggestion: a constant /usr/bin/osascript -l JavaScript script that reads the code from stdin, so it never appears on argv, and that:

  • declares org.nspasteboard.ConcealedType and TransientType;
  • uses currentHostOnly so the code doesn't go to other devices;
  • clears the pasteboard after ~60 s if changeCount hasn't moved.

4. Extraction picks up numbers that aren't OTPs. With a synthetic fake runner:

  • "a sign-in to the account ending 4821 … Your code: 739201" copies 4821;
  • "New login on 19/10/2026 … Verification code 482913" copies 2026;
  • a friend's "le digicode c'est 4821, et le login wifi…" copies 4821;
  • "Votre code de connexion / de sécurité est 482913" copies nothing.

Ideas:

  • prefer the number closest to "code" / "is your", or the one right after it;
  • skip numbers after "ending" and date-shaped runs;
  • exclude group chats, and ideally contacts;
  • add French keywords (connexion, sécurité, accès, confirmation).

5. Housekeeping:

  • Please rebase onto current main (0.5.0). app.ts conflicts; providers are now registered in registerRealProviders(), and demo mode needs a fake that answers without touching the Mac.
  • CommandContext now requires paired, so pnpm typecheck fails on the test file.
  • A rate limit on copyLatest would help, since each call spawns sqlite3.
  • Please return error codes translated with tr() instead of English strings the widget has to match.
  • A CHANGELOG line under Unreleased would be welcome.
  • Tested with pnpm: the focused suite passes (9/9), and the full suite passes on the PR's base.

Happy to help with any of these. The core of the work is solid, and this is mostly about who is allowed to pull the trigger.

@LivioGama
LivioGama force-pushed the codex/messages-2fa-core-pr branch from a66fcb7 to c496de6 Compare October 19, 2026 20:10
@LivioGama

Copy link
Copy Markdown
Contributor Author

Thanks for the review. The updated candidate is c496de6.

  1. Built-in-only trigger: messages-2fa is restricted to built-in widgets, and the marketplace manifest/catalog/install/consent paths reject this command for registry widgets. This prevents Sietch PR Expand Mac system metrics and uptime tiles #16 from invoking the provider today; the widget integration remains a separate unresolved deliverable.
  2. Separate opt-in: the Messages 2FA privacy setting defaults off and gates the provider independently of Full Disk Access. The settings text and SECURITY.md explain the additional Messages access and the same-user-process boundary.
  3. Concealed, transient pasteboard write: a fixed JXA script receives the code on stdin, marks the pasteboard entry concealed, transient, and current-host-only, then schedules a 60-second clear guarded by changeCount. If cleaner startup fails, the writer attempts one immediate guarded clear and returns a generic copy failure. If the started cleaner later fails, it attempts one immediate guarded clear and logs a fixed generic error only if that fallback also fails. If macOS refuses both attempts, the code can remain beyond 60 seconds; real macOS/JXA expiry was not tested. The guard preserves a newer clipboard value, so cleanup does not clear unconditionally or retry indefinitely.
  4. Code selection: contextual English/French markers, date/account-ending exclusions, and synthetic regression cases address the reported false positives; group chats are excluded. Contacts are deliberately not queried, avoiding new Contacts permission and keeping the provider bounded to Messages data.
  5. Housekeeping: the branch is based on current main (6eaf545); provider registration, demo behavior, paired-context tests, a five-second request cooldown, tr() error messages, and the Unreleased changelog entry are included. Focused validation passed: 5 server test files / 149 tests, server typecheck, UI typecheck, and git diff --check.

This PR contains the core provider, not the Sietch widget. Marketplace widgets still cannot invoke a built-in-only command, and the Sietch integration card remains open while a trusted packaging/integration path is resolved. The PR remains a draft; this source-level response does not claim runtime clipboard expiry or end-to-end widget integration is verified.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LivioGama

Copy link
Copy Markdown
Contributor Author

Follow-up for the clipboard ownership race, fixed in commit 261592f83a32106b625daaadb7ef359e5876e052.

The writer now captures the count returned by prepareForNewContentsWithOptions(CurrentHostOnly), requires success from the string write and both ConcealedType/TransientType writes, then confirms the board still has that same count before returning it to the delayed cleaner. The cleaner receives the captured token rather than sampling a later owner’s count. Four synthetic ownership interleavings failed against the prior implementation (4 failed / 17 passed) and pass with the fix; unrelated synthetic clipboard content stays untouched and no cleaner is started on those failure paths.

Validation at this commit: focused Messages 2FA tests 21/21; server suite 134 files, 2,699 passed and 2 skipped; source and test TypeScript checks exit 0; diff check passes. Cleanup uses a separate count comparison and clear call, so this is not an atomic compare-and-clear guarantee. Real macOS/JXA/pasteboard behavior remains UNVERIFIED.

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks Livio — this is a big step forward, and the write path is now exactly the shape we hoped for. I replayed 261592f onto current main (only a CHANGELOG conflict with #55; no interaction with the new clipboard masking, which skips concealed entries anyway): pnpm typecheck is clean and pnpm test passes (server 2702 passed / 2 skipped, UI 874). Going through the five points:

1. Built-in only — fixed. messages-2fa is in BUILTIN_ONLY_CHANNELS (server/src/widgets/manifest.ts:185), so install, catalogue and consent all refuse it for a registry package. (Listing it in BUILTIN_ONLY_COMMANDS too is redundant but harmless.) Note for whoever merges: that file is vendored byte for byte in the registry's tools/vendor/widgets/manifest.ts, so it needs re-copying there afterwards.

2. Opt-in — fixed. privacy.messages2FA defaults to false in the schema, the v3 reader and the demo seed, a restore keeps this Mac's privacy, the provider checks it at command time (app.ts:312, messages-2fa.ts:295), and SECURITY.md now covers both FDA and the same-user case. One small gap: the Settings search doesn't know the new label yet — it needs adding to the Privacy entry's labels in ui/src/admin/settingsWindow.ts:77.

3. Concealed/transient write — mostly fixed, with one real bug. The writer is right: a constant script, code on stdin only, CurrentHostOnly, both markers, and the change count captured at write time. I ran WRITE_CODE_SCRIPT against a private named pasteboard (not the general one) on macOS and it produced public.utf8-plain-text + ConcealedType + TransientType and printed the count. The clear scripts don't work as written, though. In JXA a zero-argument Objective-C method is called by property access, so board.clearContents() first clears (via board.clearContents), then tries to call the returned integer and throws TypeError: board.clearContents is not a function (messages-2fa.ts:45 and :116). The pasteboard does get cleared, but only by accident. Every successful cleaner exits non-zero, which fires the exit fallback and a second osascript, and the start-failure path logs a cleanup failure after a clear that actually worked. The unit tests assert the board.clearContents() string (server/test/messages-2fa.test.ts:343,362), which is why nothing caught it. Please:

  • use board.clearContents; in both scripts;
  • add a macOS-only test (skipped elsewhere) that runs the three scripts against NSPasteboard.pasteboardWithName(...) with the board injected, then releaseGlobally — it's how I checked them, and it would have caught this;
  • call /usr/bin/osascript by absolute path at :51 and :62, as the rest of the server now does for system tools.

4. Extraction — partly fixed. The four cases I reported are fixed, and so are the French phrasings and group chats. But the generic markers (your code, code … is/est within 18 characters) still copy codes that people send each other in one-to-one chats:

  • "Le code de la porte est 4821, je serai en retard" → 4821
  • "Your code for the gym locker is 4821" → 4821
  • "Ton code wifi est 48213579" → 48213579
  • "Mon code PIN est 1234 lol" → 1234

Skipping Contacts makes sense. A cheaper signal is the sender's handle: short codes and alphanumeric senders (handle.id that is neither a phone number nor an e-mail) are where OTPs come from. One option is to accept the loose markers only from those senders and require the strict ones (verification/security/one-time code, code de vérification/connexion/sécurité, passcode, OTP) from a phone number or iMessage address. Small one: "code d'accès" is still missed because of the apostrophe.

5. Housekeeping — fixed. Rebased, registered in registerRealProviders(), demo fake, paired in the test contexts, a 5 s cooldown, tr() messages, CHANGELOG. Two notes:

  • The CHANGELOG line is written for developers and points to a registry widget that can no longer install. It should say what a user can do, once they can do it (next point).
  • The Sietch widget still matches the old English strings ('Available only from this Mac.' etc.), which no longer match what tr() returns, and it has no case for "disabled" or "rate limited". A built-in widget can simply show result.error, which is already localized.

What's left for merge. With built-in-only in place, nothing can call the provider yet: the admin would show a privacy switch for a feature no widget uses. So I'd like the widget to move into this PR as widgets/messages-2fa: the Sietch widget ported to a built-in, French and English strings, pnpm widget:preview for a screenshot, and the demo fake in place. Then the CHANGELOG line can describe the feature. Together with the JXA fix, the absolute path, the extraction tightening and the search label, I think this is ready.

Requesting changes for those, but this is close — thank you for taking every point seriously, the pasteboard ownership handling in particular is careful work.

A macOS denial on chat.db surfaced as the generic "Messages could not
be read", indistinguishable from a schema or spawn failure. Tag the
permission case in the sqlite runner and return a dedicated error that
tells the user to enable Full Disk Access for Fremkit.

Co-Authored-By: Claude <noreply@anthropic.com>

@fdussert fdussert left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

Thanks Livio — the Full Disk Access message is a nice touch, and telling the user what's missing is much better than a generic read failure. I merged current main into 9fe2188 locally: only a CHANGELOG conflict, pnpm typecheck is clean and pnpm test passes. The points from my last review are still open, though, so here's where things stand:

  • JXA clear (still the blocker). board.clearContents() is still in both clear scripts (messages-2fa.ts:45, :116). I re-ran it on a private named pasteboard and it still throws TypeError: board.clearContents is not a function, so every cleaner exits non-zero, the fallback fires, and each successful copy ends with a "cleanup failed" log line. Please switch both scripts to board.clearContents;, update the assertions in messages-2fa.test.ts:353,372, and add the macOS-only test that runs the three scripts against NSPasteboard.pasteboardWithName(...) and then releaseGlobally.
  • Absolute path. osascript is still a bare name at :51 and :62. Please use /usr/bin/osascript.
  • Extraction. All four one-to-one examples still copy (4821, 4821, 48213579, 1234), and "code d'accès" with a straight apostrophe is still missed. Gating the loose markers on the sender's handle (short codes and alphanumeric senders) and requiring the strict ones from phone numbers and e-mail addresses would cover it.
  • Settings search. admin.inspector.screen.messages2FA still needs adding to the Privacy entry's labels in settingsWindow.ts.
  • The widget. It needs to come into this PR as widgets/messages-2fa (FR/EN strings, pnpm widget:preview, demo fake). A built-in can just show result.error, so the comparisons with the old English strings can go. Until then nothing can call the provider, and the admin would show a switch for a feature no widget uses.
  • Small ones. The permission message says "Fremkit", but macOS lists "Fremkit Helper" (or the terminal under pnpm dev); naming what the user will actually see in System Settings would help. A test that feeds defaultRun's classifier a real authorization denied stderr would cover the new branch. In the CHANGELOG, please fold the new "Fixed" entry into the single Added line: the feature hasn't shipped yet. Once the widget is here, that line can say what a user can do with it.

Still requesting changes, but the remaining work is well defined, and once the clear fix and the widget port are in, I think this is ready.

mcouzinet pushed a commit to mcouzinet/fremkit that referenced this pull request Nov 5, 2026
Every rule in isSecret started at eight characters, so a six-digit OTP
copied from an SMS or an authenticator was recorded unmasked: shown on
the Edge, kept in the 20-entry history, and sent to a paired iPad whose
screen holds the clipboard widget.

A text that is only four to eight digits, or two groups of three or four
split by one space or dash, is now masked. It is tested before the
whitespace rule so "482 913" counts. The rule is anchored on the whole
text: digits inside prose, phone numbers (more digits, more groups) and
lopsided splits are left alone. A bare year or postcode copied on its
own is masked too, which costs a preview and nothing else.

Found while reviewing PR fdussert#49.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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