Skip to content

Attach to the WeChat copy that owns the selected data folder - #46

Merged
bojieli merged 13 commits into
mainfrom
fix/issue-34-select-wechat-copy
Nov 23, 2026
Merged

bojieli merged 13 commits into
mainfrom
fix/issue-34-select-wechat-copy

Conversation

@bojieli

@bojieli bojieli commented Nov 23, 2026

Copy link
Copy Markdown
Owner

Fixes #34, together with #45 (discovery of the second copy's container). The two PRs are independent and merge cleanly in either order. Merging #45 first lets this one close the issue fully.

What

greenbubbles-acquire now picks the WeChat process from the app that owns the selected db_storage root. Before, it took the lowest PID (later changed by #43 to refuse when several were running).

  • Selection. A root under ~/Library/Containers/<bundle id>/Data/… belongs to <bundle id>. The tool reads each running WeChat process's app bundle and uses the one that matches. If no process matches, or several do, preflight blocks; it never guesses.
  • Single running copy. One running copy is now also checked against the root (issue item 1b). If only the other copy is running, preflight says so instead of failing after a full logout and login.
  • --pid <n>. Chooses the process explicitly. It must belong to the invoking user and be a WeChat main executable (<X>.app/Contents/MacOS/WeChat, with a matching CFBundleExecutable). Where the owning app can be told from the root's path, the process must belong to that app. --pid is required for roots outside an app container, such as group containers, backups, or other locations. There is no override flag.
  • Re-attach. After WeChat restarts during logout, the capture follows the same app path and bundle ID for the same user. It never switches to the other copy, and it no longer fails if the other copy is briefly running too (item 1c).
  • Preflight. Version, re-signed state and the Hardened Runtime probe all come from the selected process and its own app bundle, not from /Applications/WeChat.app (item 3). The re-sign hint names that copy's path. The process line shows the PID and bundle ID. The JSON report gains selectedProcessID, selectedApplicationBundleIdentifier and databaseRootApplicationBundleIdentifier.
  • Other users. Only the invoking user's processes count: pgrep -x -U <uid> WeChat, with SUDO_UID under sudo.

Behaviour with only the standard copy and the standard container is unchanged: zero processes means "not running", and one process is selected.

How

  • WeChatProcessSelection.swift (new) holds the pure rules: DatabaseRootOwner.resolve(databaseRoot:homeDirectory:), WeChatProcessSelector.select, .validate (for --pid), .reselect (for re-attach) and .applicationPath(forExecutablePath:). They work on WeChatProcessCandidate values (pid, uid, exe path, app path, bundle ID), so they are tested without real processes.
  • WeChatProcessLocator builds the candidates: proc_pidinfo for the real UID, proc_pidpath for the executable, then the enclosing .app and its Info.plist.
  • LLDBPassphraseCapture gains var target: WeChatProcessTarget?. The only change to the capture loop is the one-line process lookup, to keep conflicts with fix(acquire): wait for the SQLCipher PBKDF call during capture #31 small. With target == nil it behaves as before.
  • main.swift: --pid, a selectProcess step before the install/version check, and passing the target to capture.

Tests

New WeChatProcessSelectionTests (16 tests), all on synthetic candidate lists and paths:

  • mapping roots to an owner: standard and duplicate containers, ./.. normalisation, group container, container root only, a non-Data subfolder, a backup path, another user's home
  • auto-selection: zero and one process; both copies in both PID orders, for each owner; owner not running; unreadable bundle; two matching processes in both orders; another user's processes ignored; an unknown owner requiring --pid
  • --pid: accepted for the owning copy and for an unknown owner; rejected for the wrong copy, a missing PID, another user's process, a non-WeChat process, or an executable outside an app
  • re-attach: follows the app across a PID change with the other copy present in both orders; waits while only the other copy runs; ignores the same bundle ID at another path and other users; errors on two instances
  • .app path extraction, SUDO_UID handling, and reading the Info.plist bundle ID from a temp .app

All tests were run with Command Line Tools only, in a scratch copy of the package with the SwiftUI GreenBubblesHistoryApp target removed (Package.swift is unchanged here). Results:

  • this branch: 190 tests pass (Acquire: 41, up from 25)
  • this branch merged with Discover WeChat data in duplicated app containers #45: 194 tests pass
  • swift build --product greenbubbles-acquire succeeds in the worktree
  • greenbubbles-acquire help and argument validation for --pid 0 work

Not tested live

No real WeChat, no debugger attach, no sudo, and no real ~/Library data were used. These parts are untested:

  • pgrep -U, proc_pidpath and proc_pidinfo against a real duplicated WeChat. This includes whether a duplicate's main executable is still Contents/MacOS/WeChat with a matching CFBundleExecutable, which the issue reporter's setup suggests.
  • Whether a real duplicate's container directory name equals its bundle ID, which is the macOS convention.
  • Re-attach across a real logout/login with two copies running.
  • The Hardened Runtime probe on the selected PID with both copies running (item 3).

A data folder outside ~/Library/Containers now needs --pid even when a single WeChat is running. Before, the tool attached to that single process.

🤖 Generated with Claude Code

bojieli and others added 12 commits November 23, 2026 18:26
With two WeChat app copies running, preflight and capture could not pick
between them (#43 made that a refusal), a single running copy was never
checked against the data folder, and the re-attach after logout looked
the process up by name again, so it could switch copies.

The locator now lists only the invoking user's WeChat processes
(pgrep -U, SUDO_UID under sudo) and describes each one by its main
executable (proc_pidpath), enclosing .app bundle and CFBundleIdentifier.
A data folder under ~/Library/Containers/<bundle id>/Data belongs to that
bundle ID, and pure selection rules choose the one process running that
app; no match or several matches block preflight. Data outside an app
container needs the new --pid option, which is checked for the same user,
a WeChat main executable and, where it can be told, the owning app. The
capture re-attaches only to the same app path and bundle ID. Preflight
reads version and signing from the selected process's app and prints the
re-sign command for that copy.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Re-selection keeps the selected process while it still runs the same
  app (path, bundle ID, user), so --pid disambiguates two instances of
  one app on the first attach. Only after it exits must exactly one
  replacement run that app. The capture now follows each replacement.
- Preflight resolves the database root once and uses that result for
  process selection, the database inventory and the capture, so the
  three can no longer diverge (and a failed first discovery can no
  longer yield ready without a target).
- The re-sign command for a non-default app copy shows a placeholder
  instead of the app's path, in both the remediation and the blocker
  text, unless --include-paths is given.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- When --pid picks one of several instances of the same app, the other
  instances running at that moment are recorded on the target. After the
  chosen process exits, only a process that was not already running can
  replace it; while none qualifies the capture keeps waiting until its
  timeout, and two new ones are ambiguous. The exclusion carries over to
  each replacement.
- After a successful preflight, the suggested capture command repeats
  the explicit --pid and --db-root, with a placeholder for the database
  path unless --include-paths is given.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Candidates and targets carry the process start time (proc_pidinfo
  PROC_PIDTBSDINFO pbi_start_tvsec/usec). The selected process is kept
  only while the same PID with the same start time runs, which also
  guards against PID reuse.
- When the selected process is gone, the moment its exit was observed is
  recorded (by the capture when lldb reports the exit, otherwise on the
  first lookup that misses it). A replacement must run the same app and
  must have started strictly after that moment; an instance that started
  while the selected one was still running, or one whose start time is
  unknown, is never attached. While none qualifies the capture keeps
  waiting and fails closed at the timeout. The rule stays a pure
  function with injected times.
- Database-root ownership canonicalizes the root and home first
  (F_GETPATH on an O_EVTONLY descriptor), so a root typed in another
  letter case or reached through a link maps to its container's on-disk
  bundle ID instead of becoming unknown.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolves the capture-loop conflict with #31: the loop keeps #31's
rejected-candidate count and deadline error, and still follows the
selected app copy and records when its process was seen to exit.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- The capture records the exit time at the moment it reads lldb's exit
  report and carries it out through lldb cleanup, which can take up to
  3 seconds. A replacement that starts during cleanup is no longer
  rejected.
- If the selected process (same PID and start time) is still listed but
  its app metadata is momentarily unreadable, the capture skips that
  attach without recording an exit.
- Siblings excluded at selection are stored as PID plus start time, so a
  later process that reuses one of their PIDs can still be the
  replacement.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in #45 (duplicate-container discovery).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- WeChatProcessLocator no longer calls the static bundleIdentifier(...)
  from inside the initializer of a local of the same name; older Swift
  (macos-15 CI) resolved the call to the local and failed to compile.
- Automatic selection refuses when any live WeChat process of the user
  can't be classified (unknown user, app bundle or bundle ID), since it
  might own the database root; preflight asks for --pid instead.
- Re-selection skips a poll, without recording an exit, while the
  selected PID is listed and nothing read proves it is a different
  process (for example when proc_pidinfo fails for one poll). It picks a
  replacement only from a conclusive list: with any unclassifiable live
  process of the user, it keeps waiting.
- The capture takes the exit timestamp as soon as lldb's exit report is
  parsed, before verifying any candidate from the same batch.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Every PID pgrep lists stays a candidate. When neither proc_pidinfo nor
  proc_pidpath describes it, it is kept with no details (unclassified)
  unless kill(pid, 0) reports ESRCH. Selection then treats it as
  unresolved: preflight asks for --pid, re-selection keeps waiting, and
  a selected PID that is listed but unreadable skips the poll without
  recording an exit.
- Container bundle IDs are compared with CFBundleIdentifier
  case-insensitively in automatic and --pid selection; several matches
  are still rejected.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Brings in #33 (query profiles, invoking-user file access). main.swift is
rebuilt on #33's version: runPreflight resolves the database root once
through #33's userDatabaseRoot (as the invoking user, realpath-checked)
and uses that root for the owner lookup (also as the invoking user),
process selection, the inventory and the capture; the locator counts
only invokingUser.uid's processes. --pid is parsed beside --profile and
added to #33's capture-command argument list, replacing the separate
hint. #33's privilege switch, writer and profile code are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- A process's Info.plist is read only when the kernel reports that it
  belongs to the invoking user, and then with that user's permissions
  (invokingUser.perform). The plist is opened without following a link
  at the final component and accepted only when the opened file is a
  regular file at <resolved app>/Contents/Info.plist (at most 1 MiB).
  Other users' and unverifiable processes stay unclassified. The
  selected app's build inspection also runs as the invoking user.
- A failure to look up the database root's owner is now a preflight
  blocker, distinct from a successful lookup that places the root
  outside any app container.
- pgrep statuses other than 0 and 1 are listing errors, not "no
  process". During capture a failed listing skips that poll without
  changing the recorded exit state.

Refs #34.

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

- Info.plist is opened with O_NONBLOCK as well, so a FIFO with no writer
  cannot block before the regular-file check rejects it.
- WeChatProcessTarget and ProcessIdentity require a known start time; two
  missing start times are never the same process. A candidate without
  one is never turned into a target: automatic selection rejects it as
  unclassified (asks for --pid) and --pid reports that the process's
  identity can't be read.

Refs #34.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@bojieli
bojieli merged commit 8f83472 into main Nov 23, 2026
4 checks passed
@bojieli
bojieli deleted the fix/issue-34-select-wechat-copy branch November 23, 2026 11:47
@bojieli bojieli mentioned this pull request Nov 23, 2026
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.

acquire: two WeChat app copies — capture attaches to the lowest PID, and the second copy's data isn't discovered

1 participant