A fresh two-lens end-to-end review (security + correctness) of the committed
M5/M3 diff surfaced four real defects; all fixed here with regressions.
- HIGH (M5 authz bypass) — `enforce_env_approval` resolved a group node with a
bare `get_by_slug` and SKIPPED the GroupAdmin gate on miss. A node addressed
by the group's UUID (which the apply still resolves) let a group EDITOR apply
to a confirm-required env without admin. Now resolves UUID-or-slug and fails
closed, mirroring authz_tree / prepare_tree. Raw-wire regression added.
- FIX-FIRST (M3 correctness) — the legacy `project_key=None` path was not inert:
the conflict loop pushed a conflict for any owned group (`Some(oid) != None`),
so a keyless/direct-API tree apply touching an already-claimed group was
spuriously 409'd. Ownership classification is now gated on a key being present
(the discriminator is key-present, not project-persisted, so a first-ever
keyed apply still sees an already-owned group as a conflict). Regression added.
- FIX-FIRST (M3 data-loss) — the structural-prune guard checked collection
MARKERS (`group_collections`) but data outlives its marker: un-declaring a
`collections=[...]` entry drops the marker while the kv/docs/files/queue rows
survive until group delete. A dropped-then-pruned group would silently CASCADE
that orphaned data. The guard now EXISTS-checks the actual data tables
(group_kv_entries/docs/files/queue_messages) plus secrets + markers.
- MEDIUM (audit) — group ownership takeover (and claim) now emit a
tracing::info! audit line with the actor + ousted owner, matching the M5
gated-env audit. Previously only a report counter.
Also: documented WHY `pic plan --dir` mints the project key (a rare write for a
read-only command) — plan and apply must present the same key or the ownership
token folds diverge and trip a spurious StateMoved.
Re-verified: 411 manager-core lib tests; 28 CLI journeys (incl. 2 new
regressions: uuid-slug admin gate, keyless legacy apply); workspace clippy
-D warnings + fmt clean.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Ports the M3 milestone from the superseded feat/hierarchies branch onto main's
evolved apply engine. A group node is authoritatively managed by at most one
project-root; `pic apply --dir` claims, conflicts, takes over, and (with
--prune) structurally reaps owned nodes.
- Migration 0066: `projects(id, key UNIQUE)` + promote the inert
`groups.owner_project` (0047) to a real FK (ON DELETE SET NULL) + index.
- CLI mints a stable, gitignored project key in `.picloud/project.json`
(`pic init`, or lazily on first tree plan/apply) and presents it on every
tree request; `pic apply --dir --takeover` flag; `pic plan --dir` surfaces
ownership conflicts + structural-prune candidates read-only.
- Server: prepare_tree resolves ownership read-only (token folds each group's
owner key, so a claim/takeover by another repo between plan and apply trips
StateMoved). apply_tree upserts the project in-tx, claims unclaimed declared
groups, and takes over owned ones under `--takeover`. --prune reaps
owned-but-undeclared groups leaf-first (delete_group_tx RESTRICT — never
another repo's or a UI-owned node).
- Authz (§7.4, ownership ⟂ RBAC): takeover requires GroupAdmin per contested
node — enforced in authz_tree (pre-tx) AND re-verified in-tx at the ownership
decision, so --force (which waives the staleness token) can't open a
takeover-without-admin window. The attacker-supplied project key is
length/charset-validated server-side.
Adaptation to main (which lacks the superseded branch's M2 declarative group
create/reparent): groups pre-exist, so ownership stamps existing declared
nodes rather than claim-on-create; the declarative attach-point is deferred.
Review fixes (adversarial pass, 3 findings, all closed):
- HIGH: an empty `[group]` node was claimed capability-free — require a
baseline GroupScriptsWrite (editor) on every group apply node, so claiming
ownership needs write authority (Ownership ⟂ RBAC).
- MEDIUM: structural prune would silently CASCADE a group's §11.6 shared
collections + secrets (which postdate M3's original design). Guard: a prune
candidate holding shared data/secrets is KEPT with a warning (plus its
candidate ancestors, so a preserved child never aborts a parent delete).
- LOW: a corrupt `.picloud/project.json` silently re-minted a new key
(orphaning ownership) — now fails loudly if the file exists but won't parse.
Tests: tests/ownership.rs (claim → conflict → takeover → flip; non-admin
takeover → 403; prune-owned-only; prune-refuses-to-cascade-shared-data);
format_conflicts + validate_project_key unit tests; schema golden reblessed.
Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>