Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
Update Rust core vault_sharing logic (#2159)
Leendert de Borst53f926df561c
diff --git a/apps/browser-extension/src/i18n/locales/en.json b/apps/browser-extension/src/i18n/locales/en.jsonindex 8a80e3ae2..9841e4c81 100644--- a/apps/browser-extension/src/i18n/locales/en.json+++ b/apps/browser-extension/src/i18n/locales/en.json@@ -225,6 +225,7 @@"deleteFolderKeepItemsDescription": "Items will be moved to the parent folder.","deleteFolderAndItems": "Delete folder and all items","deleteFolderAndItemsDescription": "{{count}} item(s) will be moved to Recently Deleted.",+ "deleteSharedFolderHint": "This folder is shared with your family and cannot be deleted here. Leave it, or change who has access to it, in Family Sharing.","filters": {"folders": "Folders","totp": "2FA Codes"@@ -530,17 +531,70 @@"enableSuccess": "PIN unlock enabled successfully!","pinLocked": "PIN unlock has been disabled. Please use your master password to unlock your vault.","pinSecurityWarning": "PIN unlock in the browser extension can be less secure than your master password, as PINs typically have lower entropy and may be brute-forced if your device is compromised. Use it only on devices you fully trust."- }+ },+ "familySharing": "Family Sharing"},"sharing": {"shareWithOthers": "Share with others","manageAccess": "Manage access","share": "Share",- "grantSuccess": "Access granted.",- "members": "Members",+ "members": "Family members","owner": "Owner","revoke": "Revoke",- "keyFingerprint": "Key fingerprint"+ "keyFingerprint": "Key fingerprint",+ "family": {+ "title": "Family Sharing",+ "beta": "Beta",+ "description": "Share folders with the people in your family.",+ "notAvailable": "Family sharing is not enabled for this account yet. Once a family has been set up for you, it appears here.",+ "invitations": "Invitations",+ "invitedBy": "Invited by {{username}}",+ "accept": "Accept",+ "decline": "Decline",+ "noSharedVaultAdmin": "This family has no shared folder yet. Creating one adds a folder to your vault that every member you invite can see.",+ "noSharedVaultMember": "Nobody has created a shared folder for this family yet.",+ "createSharedVault": "Create a shared folder",+ "sharedFolders": "Shared folders",+ "hasAccess": "Has access",+ "invited": "Invited",+ "you": "You",+ "admin": "Admin",+ "member": "Member",+ "withdraw": "Withdraw",+ "leaveVault": "Leave folder",+ "leaveVaultConfirm": "Leave “{{vault}}”? You lose access to it, but stay in the family.",+ "cannotInviteWithoutAccess": "You do not have access to this folder, so you cannot invite anyone to it. Ask a member who has access to invite you first.",+ "errors": {+ "loadFailed": "Family sharing could not be loaded.",+ "createVaultFailed": "The shared folder could not be created. Please try again.",+ "vaultUpgradeRequired": "Your vault has to finish upgrading before it can be shared.",+ "accountKeyMismatch": "Sharing is unavailable: this account's encryption key could not be verified. Sign out and back in, and if it keeps happening, do not share anything from this device.",+ "revokeAccessFailed": "The access could not be changed. Please try again.",+ "inviteFailed": "The invitation could not be sent. Please try again.",+ "userNotReady": "That account has to finish upgrading AliasVault before a folder can be shared with it.",+ "alreadyInvited": "That person has already been invited.",+ "invitationGone": "That invitation is no longer available.",+ "invitationKeyOutdated": "This invitation is no longer valid because the shared folder's key has changed. Ask for a new invitation.",+ "notFamilyMember": "That person is not in this family.",+ "alreadyHasAccess": "That person already has access to this folder.",+ "lastMemberWithAccess": "Somebody has to be able to open the shared folder. Give another member access to it first.",+ "vaultLimitReached": "Max amount of shared folders reached.",+ "deleteVaultFailed": "The shared folder could not be deleted. Please try again."+ },+ "vaultNamePlaceholder": "Folder name, for example Streaming",+ "create": "Create",+ "invite": "Invite",+ "invitationSent": "Invitation sent to {{username}}.",+ "noAccess": "No access",+ "rosterManagedElsewhere": "Who belongs to this family is set by AliasVault. Contact support to have somebody added or removed.",+ "revokeAccessConfirm": "Take {{username}} out of “{{vault}}”? They lose access to it right away, but stay in the family.",+ "revokeAccessWarning": "They keep any copy of this folder they already downloaded. Change the passwords in it that you do not want them to keep using.",+ "deleteVault": "Delete folder",+ "deleteVaultConfirm": "Permanently delete “{{vault}}”? Every member of the family loses this folder and everything in it. This cannot be undone.",+ "deleteVaultPasswordPrompt": "Enter your master password to confirm deleting “{{vault}}” for the whole family.",+ "vaultDeleted": "The shared folder has been deleted.",+ "unnamedVault": "Shared folder"+ }},"passkeys": {"passkey": "Passkey",diff --git a/core/rust/src/vault_codec/sharing.rs b/core/rust/src/vault_codec/sharing.rsindex 7d9c0803d..c3475a879 100644--- a/core/rust/src/vault_codec/sharing.rs+++ b/core/rust/src/vault_codec/sharing.rs@@ -1,211 +1,495 @@-//! Shared-folder split/combine logic for multi-manifest vaults.+//! Split/combine logic for multi-manifest vaults.//!-//! A shared folder is a folder whose entire subtree (subfolders, items, and every row that belongs-//! to those items) lives in its own manifest with its own VEK and blob salt, so it can be granted to-//! other users without exposing the rest of the vault. This module implements the two directions:+//! A manifest is a namespace: an id, a display name, its own VEK and blob salt, and the rows carrying+//! its id in their `ManifestId` column. No manifest is privileged by itself, and there is no fallback scope: every+//! row routes by its own stamp, canonicalize rejects an unstamped one, and a row stamped for a manifest this+//! vault no longer carries is dropped, since there is no namespace left to write it to. How a manifest+//! is presented (a folder in the tree or as a separate vault) is each client's runtime choice.//!-//! - **partition** (canonicalize side): pull each shared folder's subtree out of the unified materialized table-//! set into its own per-folder table set. Row routing rules, applied in order:-//! 1. `Folders`: rows whose id is in a shared subtree move to that folder's partition; the subtree-//! root's `ParentFolderId` is normalized to NULL (per-user placement of a shared folder inside-//! a personal folder is not preserved, a shared folder is top-level for every participant).-//! 2. `Items`: rows whose `FolderId` is in a shared subtree move with it.-//! 3. Any other table row carrying an `ItemId` column follows its item (FieldValues, ItemTags,-//! Attachments, TotpCodes, Passkeys, FieldHistories, etc.).-//! 4. Reference-copied tables (`Logos` via `Items.LogoId`, `Tags` via `ItemTags.TagId`,-//! `FieldDefinitions` via `FieldValues`/`FieldHistories`): a row is *copied* into every shared-//! partition that references it so each manifest stays self-contained, and stays in the root-//! when the root references it or nothing references it. A row referenced only by shared-//! partitions leaves the root entirely.-//! 5. Everything else (EncryptionKeys, bucketed tables like Settings, unknown/overflow tables) is-//! personal by design and stays in the root manifest at all times.+//! **partition** (canonicalize side) routes every row of the unified table set to its owning manifest:+//! - `Folders`, `Items`, `Tags`, `FieldDefinitions`, `Logos` and `EncryptionKeys` route by stamp and+//! are restamped with the owning manifest's id. A folder whose parent lives in another namespace+//! has its `ParentFolderId` nulled.+//! - Any other registered table row that has a parent `ItemId` follows it, so the parent manifest location is leading.+//! - Tables this build's registry does not know (a newer writer's, carried through the codec+//! overflow) route by stamp too.+//! - Bucketed tables (Settings) leave the routing set beforehand: one bucket type per manifest.//!-//! - **combine** (materialize side): append every shared manifest's rows to the root's with-//! root-wins primary-key dedup (a shared manifest must never override a root row; duplicates are-//! expected for reference-copied rows), defensively drop personal tables a shared manifest must-//! not carry, null any `ParentFolderId` that doesn't resolve inside the combined set, and run the-//! cross-manifest Logos `Source` dedup so the `UNIQUE(Logos.Source)` index can't break the insert.+//! Each manifest is then made self-contained: it clones in the rows its own rows reference but does+//! not hold (see [`clone_referenced_rows`]) and drops the reproducible logos nothing references any+//! more (see [`prune_unreferenced_logos`]).+//!+//! **combine** (materialize side) normalizes each manifest's logos to its own id, stamps its rows with+//! the manifest they arrived in, drops the `EncryptionKeys` rows it may not publish plus the bucketed,+//! personal and bookkeeping tables no manifest may carry, folds in its data buckets, then repairs the+//! references a namespace boundary leaves dangling.use std::collections::{HashMap, HashSet};-use serde_json::Value;+use serde_json::{json, Value};-use super::logos::dedupe_logos_by_source;-use super::manifest::{CodecRecord, DataBucket, Manifest, SharedFolderSpec};-use super::types::{is_personal_table, is_skip_table, primary_key_for, OVERFLOW_TABLE};+use super::manifest::{CodecRecord, Manifest, ManifestSpec};+use super::scoped_assets::{is_custom_logo, normalize_logo_scope, reconcile_logo_references};+use super::types::{+ is_bucketed_table, is_manifest_scoped, is_personal_table, is_skip_table, manifest_scoped_tables, row_identity, ENCRYPTION_KEYS_TABLE,+ MANIFEST_ID_COL, OVERFLOW_TABLE,+};use crate::error::{VaultError, VaultResult};const FOLDERS_TABLE: &str = "Folders";const ITEMS_TABLE: &str = "Items";+const LOGOS_TABLE: &str = "Logos";const PARENT_FOLDER_ID_COL: &str = "ParentFolderId";-const FOLDER_ID_COL: &str = "FolderId";const ITEM_ID_COL: &str = "ItemId";const ID_COL: &str = "Id";+const TAGS_TABLE: &str = "Tags";+const ITEM_TAGS_TABLE: &str = "ItemTags";+const TAG_ID_COL: &str = "TagId";+const FIELD_DEFINITIONS_TABLE: &str = "FieldDefinitions";+const FIELD_DEFINITION_ID_COL: &str = "FieldDefinitionId";-/// Reference-copied tables: `(target_table, [(referencing_table, referencing_column)])`. A target-/// row is copied into every partition holding a referencing row to ensure each manifest stays self-contained.-static REF_COPIED_TABLES: &[(&str, &[(&str, &str)])] = &[- ("Logos", &[("Items", "LogoId")]),+/// Rows referenced from inside a manifest: `(target_table, [(referencing_table, column)])`.+///+/// The foreign keys are composite, so a row in another namespace cannot satisfy them: a manifest that+/// references one of these gets its own stamped copy (see [`clone_referenced_rows`]). `Logos` are+/// copied through [`scoped_assets`](super::scoped_assets) instead, which re-mints ids from the natural+/// key rather than keeping them.+static REFERENCED_TABLES: &[(&str, &[(&str, &str)])] = &[("Tags", &[("ItemTags", "TagId")]),("FieldDefinitions", &[("FieldValues", "FieldDefinitionId"), ("FieldHistories", "FieldDefinitionId")]),];-/// One shared folder's partition produced by [`partition_for_sharing`].-pub(super) struct SharedPartition {- pub folder_id: String,- pub user_salt: String,- pub tables: HashMap<String, Vec<CodecRecord>>,+/// The tables a caller must snapshot before routing so [`clone_referenced_rows`] can copy from them:+/// every [`REFERENCED_TABLES`] target plus `Logos`, which the scoped-asset path pulls from.+pub(super) fn referenced_tables() -> Vec<&'static str> {+ let mut out: Vec<&'static str> = vec![LOGOS_TABLE];+ out.extend(REFERENCED_TABLES.iter().map(|(target, _)| *target));+ out+}++/// Where one row lands when [`partition_by_manifest`] routes it.+#[derive(Clone, Copy, PartialEq, Eq)]+enum Route {+ /// The manifest being written from: the rows stamped for it.+ Base,+ /// One of the other manifests in this push, by spec index.+ Partition(usize),+ /// A manifest this vault no longer carries: no namespace is left for the row, so it is dropped.+ Gone,}-/// Split each shared folder's structure (subfolders, items, and rows) out of `tables` into a new partition for that folder.-/// Returns one partition per folder (spec), in folder order. If a folder no longer exists locally, it results in-/// an empty partition (the folder was deleted; its manifest empties out).-pub(super) fn partition_for_sharing(tables: &mut HashMap<String, Vec<CodecRecord>>, specs: &[SharedFolderSpec]) -> VaultResult<Vec<SharedPartition>> {- if specs.is_empty() {- return Ok(Vec::new());+/// Where each item was routed, so the rows hanging off it can be sent the same way.+///+/// A child row names its item through the composite foreign key `(ManifestId, ItemId)` and is resolved+/// by it. Keying on the bare `ItemId` would be a leak: two manifests may hold an item with the same id,+/// and the first one found would then pull in every manifest's rows for that id, sending a personal+/// password, TOTP secret or attachment into a manifest shared with other people.+///+/// `by_id` is the fallback for the one case the composite key cannot answer: a client that moved an+/// item between manifests without re-stamping its children. It applies only while exactly one item+/// carries that id.+#[derive(Default)]+struct ItemRoutes {+ by_identity: HashMap<(String, String), Route>,+ by_id: HashMap<String, Option<Route>>,+}++impl ItemRoutes {+ /// Record where one item row was routed, under both its full identity and its bare id.+ fn record(&mut self, row: &CodecRecord, destination: Route) {+ let Some(id) = str_col(row, ID_COL) else { return };+ let scope = str_col(row, MANIFEST_ID_COL).unwrap_or_default().to_string();+ self.by_identity.insert((scope, id.to_string()), destination);+ // Two items with the same id agree only while they route to the same place.+ self.by_id+ .entry(id.to_string())+ .and_modify(|current| {+ if *current != Some(destination) {+ *current = None;+ }+ })+ .or_insert(Some(destination));}- let mut seen_spec_folders: HashSet<&str> = HashSet::new();+ /// Where `row`'s item went, or `None` when it names no item this vault holds.+ fn route_for(&self, row: &CodecRecord) -> Option<Route> {+ let item_id = str_col(row, ITEM_ID_COL)?;+ let scope = str_col(row, MANIFEST_ID_COL).unwrap_or_default();+ if let Some(destination) = self.by_identity.get(&(scope.to_string(), item_id.to_string())) {+ return Some(*destination);+ }+ self.by_id.get(item_id).copied().flatten()+ }+}++/// One manifest's partition produced by [`partition_by_manifest`].+pub(super) struct ManifestPartition {+ pub manifest_id: String,+ pub name: Option<String>,+ pub manifest_salt: String,+ pub tables: HashMap<String, Vec<CodecRecord>>,+}++/// Route every row of `tables` to the manifest that owns it, moving the rows of `specs` out into one+/// partition each (in spec order) and leaving the writing manifest's rows behind in `tables`.+///+/// Ownership is the row's `ManifestId` stamp, for every table alike, including ones this build's+/// registry does not know. Registered item-child tables (`FieldValues`, `ItemTags`, ...) are the one+/// exception: they follow the item their own `(ManifestId, ItemId)` names and are restamped to agree+/// with it, because that pair is their foreign key.+///+/// `writing_manifest_id` is the manifest the caller wrote this vault from, and is no fallback scope: it+/// keeps exactly the rows stamped for it, and a row stamped for a manifest in neither place is dropped.+///+/// `snapshots` holds the vault-wide `Logos`, `Tags` and `FieldDefinitions` sets captured before routing+/// (see [`REFERENCED_TABLES`]), so a manifest can clone in a row its own rows reference but that lives+/// in another scope.+pub(super) fn partition_by_manifest(+ tables: &mut HashMap<String, Vec<CodecRecord>>,+ specs: &[ManifestSpec],+ snapshots: &HashMap<String, Vec<CodecRecord>>,+ writing_manifest_id: &str,+) -> VaultResult<Vec<ManifestPartition>> {+ let mut seen_spec_manifests: HashSet<&str> = HashSet::new();for spec in specs {- if !seen_spec_folders.insert(spec.folder_id.as_str()) {- return Err(VaultError::General(format!("duplicate shared folder spec for folder {}", spec.folder_id)));+ if !seen_spec_manifests.insert(spec.manifest_id.as_str()) || spec.manifest_id == writing_manifest_id {+ return Err(VaultError::General(format!("duplicate manifest id in manifest spec {}", spec.manifest_id)));}}- // Folder id -> spec index, for every folder in every spec's subtree.- let folder_to_spec = compute_subtrees(tables.get(FOLDERS_TABLE).map(Vec::as_slice).unwrap_or(&[]), specs)?;+ // Manifest id -> spec index: the routing key every stamped table uses.+ let manifest_to_spec: HashMap<&str, usize> = specs.iter().enumerate().map(|(idx, spec)| (spec.manifest_id.as_str(), idx)).collect();++ // Where a row goes, read straight off its `ManifestId` stamp.+ let route = |row: &CodecRecord| -> Route {+ let scope = str_col(row, MANIFEST_ID_COL);+ if scope == Some(writing_manifest_id) {+ return Route::Base;+ }+ scope.and_then(|s| manifest_to_spec.get(s).copied()).map(Route::Partition).unwrap_or(Route::Gone)+ };++ /*+ * The `(ManifestId, Id)` of every folder, so rule 1 can tell whether a folder's parent lives in the+ * same namespace. Both halves, because `Folders.ParentFolderId` resolves through the composite key:+ * the same id in another manifest is another folder, not this one's parent.+ */+ let folder_identities: HashSet<(String, String)> = tables+ .get(FOLDERS_TABLE)+ .map(Vec::as_slice)+ .unwrap_or(&[])+ .iter()+ .filter_map(|row| Some((str_col(row, MANIFEST_ID_COL)?.to_string(), str_col(row, ID_COL)?.to_string())))+ .collect();- let mut partitions: Vec<SharedPartition> = specs+ let mut partitions: Vec<ManifestPartition> = specs.iter()- .map(|spec| SharedPartition { folder_id: spec.folder_id.clone(), user_salt: spec.user_salt.clone(), tables: HashMap::new() })+ .map(|spec| ManifestPartition {+ manifest_id: spec.manifest_id.clone(),+ name: spec.name.clone(),+ manifest_salt: spec.manifest_salt.clone(),+ tables: HashMap::new(),+ }).collect();- // 1. Folders: subtree rows move; each subtree root's ParentFolderId is normalized to NULL.- let mut item_to_spec: HashMap<String, usize> = HashMap::new();+ // 1. Folders, routed by stamp and restamped with their owning manifest's id.+ let mut item_routes: ItemRoutes = ItemRoutes::default();if let Some(folder_rows) = tables.remove(FOLDERS_TABLE) {- let mut root_rows: Vec<CodecRecord> = Vec::with_capacity(folder_rows.len());+ let mut base_rows: Vec<CodecRecord> = Vec::with_capacity(folder_rows.len());for mut row in folder_rows {- match str_col(&row, ID_COL).and_then(|id| folder_to_spec.get(id).copied()) {- Some(spec_idx) => {- if str_col(&row, ID_COL) == Some(specs[spec_idx].folder_id.as_str()) {+ match route(&row) {+ Route::Partition(spec_idx) => {+ /*+ * A folder whose parent belongs to another manifest heads this one, so the link is+ * cut. The parent is looked up under this folder's own scope, the namespace its+ * foreign key resolves in.+ */+ let scope = str_col(&row, MANIFEST_ID_COL).unwrap_or_default().to_string();+ let parent_in_other_manifest = str_col(&row, PARENT_FOLDER_ID_COL).map(|parent| !folder_identities.contains(&(scope.clone(), parent.to_string()))).unwrap_or(false);+ if parent_in_other_manifest {row.insert(PARENT_FOLDER_ID_COL.to_string(), Value::Null);}+ row.insert(MANIFEST_ID_COL.to_string(), json!(specs[spec_idx].manifest_id));partitions[spec_idx].tables.entry(FOLDERS_TABLE.to_string()).or_default().push(row);}- None => root_rows.push(row),+ Route::Base => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(writing_manifest_id));+ base_rows.push(row);+ }+ Route::Gone => {}}}- tables.insert(FOLDERS_TABLE.to_string(), root_rows);+ tables.insert(FOLDERS_TABLE.to_string(), base_rows);}- // 2. Items follow their folder; remember each shared item for rule 3.+ // 2. Items, routed like folders; remember where each one went for rule 4.if let Some(item_rows) = tables.remove(ITEMS_TABLE) {- let mut root_rows: Vec<CodecRecord> = Vec::with_capacity(item_rows.len());- for row in item_rows {- match str_col(&row, FOLDER_ID_COL).and_then(|fid| folder_to_spec.get(fid).copied()) {- Some(spec_idx) => {- if let Some(id) = str_col(&row, ID_COL) {- item_to_spec.insert(id.to_string(), spec_idx);- }+ let mut base_rows: Vec<CodecRecord> = Vec::with_capacity(item_rows.len());+ for mut row in item_rows {+ let destination = route(&row);+ item_routes.record(&row, destination);+ match destination {+ Route::Partition(spec_idx) => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(specs[spec_idx].manifest_id));partitions[spec_idx].tables.entry(ITEMS_TABLE.to_string()).or_default().push(row);}- None => root_rows.push(row),+ Route::Base => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(writing_manifest_id));+ base_rows.push(row);+ }+ // The item goes with its manifest, and rule 4 sends the rows hanging off it the same way.+ Route::Gone => {}}}- tables.insert(ITEMS_TABLE.to_string(), root_rows);+ tables.insert(ITEMS_TABLE.to_string(), base_rows);}- // 3. Generic item-scoped rule: any remaining table row carrying an ItemId follows its item.- // Reference-copied targets are handled by rule 4 below (none of them carries an ItemId column).- let item_scoped_tables: Vec<String> = tables- .iter()- .filter(|(name, rows)| !is_personal_table(name) && rows.iter().any(|r| r.contains_key(ITEM_ID_COL)))- .map(|(name, _)| name.clone())- .collect();- for name in item_scoped_tables {- let rows = tables.remove(&name).unwrap_or_default();- let mut root_rows: Vec<CodecRecord> = Vec::with_capacity(rows.len());- for row in rows {- match str_col(&row, ITEM_ID_COL).and_then(|iid| item_to_spec.get(iid).copied()) {- Some(spec_idx) => partitions[spec_idx].tables.entry(name.clone()).or_default().push(row),- None => root_rows.push(row),+ /*+ * 3. Logos are scoped, not copied: a row moves to the partition its ManifestId names. The stamp+ * value itself is re-minted to the owning manifest below.+ */+ if let Some(logo_rows) = tables.remove(LOGOS_TABLE) {+ let mut base_rows: Vec<CodecRecord> = Vec::with_capacity(logo_rows.len());+ for row in logo_rows {+ match route(&row) {+ Route::Partition(spec_idx) => partitions[spec_idx].tables.entry(LOGOS_TABLE.to_string()).or_default().push(row),+ Route::Base => base_rows.push(row),+ Route::Gone => {}}}- tables.insert(name, root_rows);+ tables.insert(LOGOS_TABLE.to_string(), base_rows);}- // 4. Reference-copied tables: copy each referenced row into the partitions that reference it;- // keep it in the root when the root references it or nothing references it.- for (target, referencing) in REF_COPIED_TABLES {- let Some(target_rows) = tables.remove(*target) else { continue };-- // Target row id -> (referenced by root?, set of referencing partitions).- let mut root_refs: HashSet<String> = HashSet::new();- let mut partition_refs: HashMap<String, HashSet<usize>> = HashMap::new();- for (ref_table, ref_column) in *referencing {- for row in tables.get(*ref_table).map(Vec::as_slice).unwrap_or(&[]) {- if let Some(id) = str_col(row, ref_column) {- root_refs.insert(id.to_string());+ /*+ * 3b. EncryptionKeys routes by stamp like every other table; it is called out only because dropping+ * a `Route::Gone` row here is load-bearing. Re-homing a dead manifest's keypair would resurrect it+ * on a future re-share (handing pre-share mail to the new members) and would let a tampered local+ * DB pass a fabricated key row off as the writing manifest's own. Mail encrypted to a dropped key+ * becomes unreadable, which is the intended outcome: the key is readable only by whoever currently+ * holds the manifest.+ */+ if let Some(rows) = tables.remove(ENCRYPTION_KEYS_TABLE) {+ let mut base_rows: Vec<CodecRecord> = Vec::with_capacity(rows.len());+ for mut row in rows {+ match route(&row) {+ Route::Base => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(writing_manifest_id));+ base_rows.push(row);}+ Route::Partition(spec_idx) => partitions[spec_idx].tables.entry(ENCRYPTION_KEYS_TABLE.to_string()).or_default().push(row),+ Route::Gone => {}}- for (spec_idx, partition) in partitions.iter().enumerate() {- for row in partition.tables.get(*ref_table).map(Vec::as_slice).unwrap_or(&[]) {- if let Some(id) = str_col(row, ref_column) {- partition_refs.entry(id.to_string()).or_default().insert(spec_idx);- }+ }+ tables.insert(ENCRYPTION_KEYS_TABLE.to_string(), base_rows);+ }++ /*+ * 4. Any remaining *registered* table row carrying an ItemId follows its item, restamped to agree+ * with it: its foreign key is `(ManifestId, ItemId)`, both halves, so following the same id in+ * another manifest would carry a password, TOTP secret or attachment out of the namespace it+ * belongs to (see [`ItemRoutes`]). A table the registry does not know is not assumed to follow+ * items and routes by its rows' own stamps in rule 5 instead.+ */+ let item_scoped_names: Vec<String> = tables+ .iter()+ .filter(|(name, rows)| is_manifest_scoped(name) && !is_bucketed_table(name) && !is_personal_table(name) && rows.iter().any(|r| r.contains_key(ITEM_ID_COL)))+ .map(|(name, _)| name.clone())+ .collect();+ for name in item_scoped_names.clone() {+ let rows = tables.remove(&name).unwrap_or_default();+ let mut base_rows: Vec<CodecRecord> = Vec::with_capacity(rows.len());+ for mut row in rows {+ // Where this row's item went, or, when it names no item at all, its own stamp.+ match item_routes.route_for(&row).unwrap_or_else(|| route(&row)) {+ Route::Partition(spec_idx) => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(specs[spec_idx].manifest_id));+ partitions[spec_idx].tables.entry(name.clone()).or_default().push(row);}+ Route::Base => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(writing_manifest_id));+ base_rows.push(row);+ }+ // The item left with a manifest this vault no longer carries; its rows go with it.+ Route::Gone => {}}}+ tables.insert(name, base_rows);+ }- let mut root_rows: Vec<CodecRecord> = Vec::with_capacity(target_rows.len());- for row in target_rows {- let id = str_col(&row, ID_COL).map(str::to_string);- let shared_refs = id.as_deref().and_then(|id| partition_refs.get(id));- if let Some(spec_idxs) = shared_refs {- for &spec_idx in spec_idxs {- partitions[spec_idx].tables.entry((*target).to_string()).or_default().push(row.clone());+ /*+ * 5. Every table left routes by its rows' own stamps: the registered independent-row tables (`Tags`,+ * `FieldDefinitions`, which exist without any item) and any table this build's registry does not+ * know. Rule 6 then gives every manifest a copy of the referenced rows its own rows point at.+ */+ let handled: HashSet<&str> = [FOLDERS_TABLE, ITEMS_TABLE, LOGOS_TABLE, ENCRYPTION_KEYS_TABLE]+ .into_iter()+ .chain(item_scoped_names.iter().map(String::as_str))+ .collect();+ let remaining_names: Vec<String> = tables.keys().filter(|name| !handled.contains(name.as_str())).cloned().collect();+ for name in remaining_names {+ let rows = tables.remove(&name).unwrap_or_default();+ let mut base_rows: Vec<CodecRecord> = Vec::with_capacity(rows.len());+ for mut row in rows {+ match route(&row) {+ Route::Partition(spec_idx) => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(specs[spec_idx].manifest_id));+ partitions[spec_idx].tables.entry(name.clone()).or_default().push(row);}- }- // Root keeps the row unless it is referenced exclusively by shared partitions.- let referenced_only_by_shared = shared_refs.is_some() && id.as_deref().map(|id| !root_refs.contains(id)).unwrap_or(false);- if !referenced_only_by_shared {- root_rows.push(row);+ Route::Base => {+ row.insert(MANIFEST_ID_COL.to_string(), json!(writing_manifest_id));+ base_rows.push(row);+ }+ Route::Gone => {}}}- tables.insert((*target).to_string(), root_rows);+ tables.insert(name, base_rows);+ }++ /*+ * 6. Each manifest ends up self-contained: it gets its own copy of every row its rows reference but+ * do not hold, taken from the vault-wide snapshot and stamped for this manifest. Logos additionally+ * re-mint their id from the scope (see `scoped_assets`); the others keep theirs, since the same id+ * in two manifests is simply two rows.+ */+ let no_logos: Vec<CodecRecord> = Vec::new();+ let all_logos = snapshots.get(LOGOS_TABLE).unwrap_or(&no_logos);+ for partition in partitions.iter_mut() {+ let scope = partition.manifest_id.clone();+ reconcile_logo_references(&mut partition.tables, &scope, all_logos);+ normalize_logo_scope(&mut partition.tables, &scope);+ prune_unreferenced_logos(&mut partition.tables);+ clone_referenced_rows(&mut partition.tables, &scope, snapshots);}Ok(partitions)}-/// Combine the root manifest's table set with shared manifests' tables into one unified set.-pub(super) fn combine_manifest_tables(mut tables: HashMap<String, Vec<CodecRecord>>, shared_manifests: Vec<Manifest>) -> HashMap<String, Vec<CodecRecord>> {- // Root-wins dedup registry: table -> set of primary-key values already present.+/// Give one manifest's table set its own copy of every row it references but does not hold.+///+/// The foreign keys are composite (`(ManifestId, TagId)`, `(ManifestId, FieldDefinitionId)`), so a tag+/// sitting in another namespace is as absent as one that was deleted. The row is copied from+/// `snapshots` (the vault-wide set captured before routing) and stamped for this manifest, keeping its+/// id.+///+/// Copying rather than moving is the point: the manifest the row came from keeps its own, so two+/// members editing "the same" tag never overwrite each other.+pub(super) fn clone_referenced_rows(tables: &mut HashMap<String, Vec<CodecRecord>>, scope: &str, snapshots: &HashMap<String, Vec<CodecRecord>>) {+ for (target, referencing) in REFERENCED_TABLES {+ let Some(source_rows) = snapshots.get(*target) else { continue };++ let present: HashSet<String> = tables+ .get(*target)+ .map(|rows| rows.iter().filter_map(|r| str_col(r, ID_COL)).map(str::to_string).collect())+ .unwrap_or_default();++ let mut missing: Vec<String> = referencing+ .iter()+ .flat_map(|(ref_table, ref_column)| tables.get(*ref_table).map(Vec::as_slice).unwrap_or(&[]).iter().filter_map(move |row| str_col(row, ref_column)))+ .filter(|id| !present.contains(*id))+ .map(str::to_string)+ .collect();+ missing.sort();+ missing.dedup();+ if missing.is_empty() {+ continue;+ }++ let scope_value = json!(scope);+ let clones: Vec<CodecRecord> = missing+ .iter()+ .filter_map(|id| source_rows.iter().find(|row| str_col(row, ID_COL) == Some(id.as_str())))+ .map(|row| {+ let mut clone = row.clone();+ clone.insert(MANIFEST_ID_COL.to_string(), scope_value.clone());+ clone+ })+ .collect();+ if !clones.is_empty() {+ tables.entry((*target).to_string()).or_default().extend(clones);+ }+ }+}++/// Drop `Logos` rows no item in this table set references.+///+/// A logo left behind by an item that was deleted or moved to another manifest would otherwise sit in+/// the manifest, and ship to every member of a shared one, forever. Losing one costs nothing: a favicon+/// is refetched from the item's domain and a built-in logo is drawn from the catalog.+///+/// Uploads ([`is_custom_logo`]) are the exception, in every manifest alike: they are the one kind no+/// client can reproduce, and they are what a logo picker offers as a library to pick from again. An+/// upload therefore outlives the item that first used it and leaves only when the user removes it.+pub(super) fn prune_unreferenced_logos(tables: &mut HashMap<String, Vec<CodecRecord>>) {+ let referenced: HashSet<String> = tables+ .get(ITEMS_TABLE)+ .map(|rows| rows.iter().filter_map(|r| str_col(r, "LogoId")).map(str::to_string).collect())+ .unwrap_or_default();+ if let Some(logos) = tables.get_mut(LOGOS_TABLE) {+ logos.retain(|row| is_custom_logo(row) || str_col(row, ID_COL).map(|id| referenced.contains(id)).unwrap_or(false));+ }+}++/// Combine the caller's own manifest tables with every other manifest's tables into one unified set.+///+/// Every manifest is held to the same rules: its rows are stamped with its own id, it may publish only+/// its own key material, and it may carry no bucketed or bookkeeping tables.+///+/// Rows are keyed by `(ManifestId, Id)`, matching the primary key the local schema declares, so two+/// manifests carrying the same `Id` produce two rows that live side by side.+///+/// Logos are normalized to their own manifest's id first: the materialized SQLite holds the union of+/// all manifests under a `UNIQUE(ManifestId, Kind, Source)` index, so a manifest written by a writer+/// that stamped the wrong scope would otherwise collide with the reader's own rows.+pub(super) fn combine_manifest_tables(+ mut tables: HashMap<String, Vec<CodecRecord>>,+ base_manifest_id: &str,+ other_manifests: Vec<Manifest>,+) -> HashMap<String, Vec<CodecRecord>> {+ // A bucketed table inside a manifest is malformed: its rows would land in the combined set twice,+ // once from the manifest and once from the bucket that legitimately carries them, colliding on the+ // primary key. The caller's own manifest is held to that rule like every other.+ tables.retain(|name, _| !is_bucketed_table(name));+ claim_manifest_scope(&mut tables, base_manifest_id);++ /*+ * First-manifest-wins registry: table -> set of row identities already present. Every row is stamped+ * by `claim_manifest_scope` above, tables this build does not know included, and the identity folds+ * the stamp in, so this can never fire across manifests; it only dedupes a genuinely duplicated row+ * inside one manifest.+ */let mut seen: HashMap<String, HashSet<String>> = HashMap::new();for (name, rows) in &tables {- let pk_column = primary_key_for(name);let keys = seen.entry(name.clone()).or_default();for row in rows {- if let Some(key) = row.get(pk_column).map(super::materialize::row_key) {- keys.insert(key);+ if let Some(identity) = row_identity(name, row) {+ keys.insert(identity);}}}- for manifest in shared_manifests {+ for mut manifest in other_manifests {+ // The manifest's own id is the scope every row inside must claim.+ let manifest_id = manifest.manifest_id.clone();+ claim_manifest_scope(&mut manifest.tables, &manifest_id);+for (name, rows) in manifest.tables {- // A shared manifest is authored by another user: never let it carry personal tables- // (key material, settings), local bookkeeping, or platform skip-tables into this vault.- if is_skip_table(&name) || name == OVERFLOW_TABLE || is_personal_table(&name) {+ /*+ * Bucketed tables (Settings) sync as their own resource beside the manifest, so a manifest+ * carrying them is malformed however it was produced; a personal-only table has no business+ * in a manifest that is not this vault's own; and local bookkeeping and platform+ * skip-tables never travel inside one. Dropping them here also means a manifest authored by+ * another user cannot inject rows into this vault's personal scope.+ */+ if is_skip_table(&name) || name == OVERFLOW_TABLE || is_bucketed_table(&name) || is_personal_table(&name) {continue;}- let pk_column = primary_key_for(&name);+let keys = seen.entry(name.clone()).or_default();let target = tables.entry(name.clone()).or_default();for row in rows {- match row.get(pk_column).map(super::materialize::row_key) {- Some(key) => {- if keys.insert(key) {+ match row_identity(&name, &row) {+ Some(identity) => {+ if keys.insert(identity) {target.push(row);}}@@ -215,76 +499,181 @@ pub(super) fn combine_manifest_tables(mut tables: HashMap<String, Vec<CodecRecor}}- // A folder's parent may live in a manifest this user has no access to (e.g. the owner nested the- // shared folder before this format normalized it, or a partial grant set): null it rather than- // fail the platform's foreign_key_check.+ /*+ * Repair what a namespace boundary can leave dangling, so the combined set satisfies the schema's+ * foreign keys: a folder's parent or an item's folder may live in a manifest this user has no access+ * to, and a manifest may carry a child row naming an item it does not hold. Each lookup is per+ * manifest, since with a composite foreign key, resolving in *some* manifest is not resolving.+ */null_dangling_parent_folders(&mut tables);-- // Two manifests can legitimately carry different Logos rows for the same Source (each side minted- // its own favicon row); collapse them so UNIQUE(Logos.Source) holds, remapping Items.LogoId.- dedupe_logos_by_source(&mut tables);+ null_dangling_item_folders(&mut tables);+ null_dangling_field_definitions(&mut tables);+ drop_orphan_item_children(&mut tables);+ drop_item_tags_without_a_tag(&mut tables);tables}-/// Null every `Folders.ParentFolderId` that doesn't resolve to a folder in the combined set.-fn null_dangling_parent_folders(tables: &mut HashMap<String, Vec<CodecRecord>>) {- let folder_ids: HashSet<String> = tables- .get(FOLDERS_TABLE)- .map(|rows| rows.iter().filter_map(|r| str_col(r, ID_COL).map(str::to_string)).collect())- .unwrap_or_default();- if let Some(rows) = tables.get_mut(FOLDERS_TABLE) {+/// Make every row of a single manifest's table set claim `manifest_id` as its scope, every table alike,+/// registered or not. Stamping a row with the manifest it arrived in is what lets the next push route+/// it home by its own stamp, instead of trusting whatever scope its author wrote into it (which would+/// let one manifest inject rows into another's namespace through a table the registry cannot police).+///+/// Order matters here. `Logos` are stamped by [`normalize_logo_scope`], which also re-mints their+/// derived ids and repoints `Items.LogoId`, so they must not be stamped twice. `EncryptionKeys` are+/// never stamped at all: there the stamp is a *claim* that [`retain_own_encryption_keys`] checks, and+/// stamping first would make every claim true.+fn claim_manifest_scope(tables: &mut HashMap<String, Vec<CodecRecord>>, manifest_id: &str) {+ normalize_logo_scope(tables, manifest_id);+ retain_own_encryption_keys(tables, manifest_id);++ let names: Vec<String> = tables.keys().filter(|name| *name != LOGOS_TABLE && *name != ENCRYPTION_KEYS_TABLE).cloned().collect();+ for table in names {+ stamp_table(tables, &table, manifest_id);+ }+}++/// Keep only the `EncryptionKeys` rows a manifest may legitimately publish: the ones stamped with its+/// own id. Applied to every manifest alike, so each namespace is protected from the others.+///+/// Unstamped rows are dropped here rather than adopted. Adoption is a *local* migration concern handled+/// on the canonicalize side, so an unstamped row in a manifest coming back through combine is not a+/// legacy row: it is a manifest asking to have key material adopted into a scope it never proved it+/// owns.+fn retain_own_encryption_keys(tables: &mut HashMap<String, Vec<CodecRecord>>, manifest_id: &str) {+ if let Some(rows) = tables.get_mut(ENCRYPTION_KEYS_TABLE) {+ rows.retain(|row| str_col(row, MANIFEST_ID_COL) == Some(manifest_id));+ }+}++/// Stamp every row of `table` with `manifest_id` in its `ManifestId` column.+fn stamp_table(tables: &mut HashMap<String, Vec<CodecRecord>>, table: &str, manifest_id: &str) {+ if let Some(rows) = tables.get_mut(table) {for row in rows {- if let Some(parent_id) = str_col(row, PARENT_FOLDER_ID_COL) {- if !folder_ids.contains(parent_id) {- row.insert(PARENT_FOLDER_ID_COL.to_string(), Value::Null);- }- }+ row.insert(MANIFEST_ID_COL.to_string(), json!(manifest_id));}}}-/// Extract the encryption-key row whose `PublicKey` matches `public_key`, straight from the decrypted-/// `EncryptionKeys` data bucket. Deleted rows are skipped. Returns `None` when no live row carries that key.-pub fn extract_encryption_key_for_public_key_from_bucket(bucket: &DataBucket, public_key: &str) -> Option<CodecRecord> {- bucket- .tables- .get("EncryptionKeys")?- .iter()- .find(|row| str_col(row, "PublicKey") == Some(public_key) && !is_truthy(row.get("IsDeleted")))- .cloned()+/// Null every `Folders.ParentFolderId` that doesn't resolve to a folder *in the same manifest*.+fn null_dangling_parent_folders(tables: &mut HashMap<String, Vec<CodecRecord>>) {+ null_dangling_reference(tables, FOLDERS_TABLE, FOLDERS_TABLE, PARENT_FOLDER_ID_COL);}-/// Map every folder inside a spec's subtree (root included) to that spec's index. Errors when one-/// spec's folder lies inside another spec's subtree (nested shares are not supported).-fn compute_subtrees(folder_rows: &[CodecRecord], specs: &[SharedFolderSpec]) -> VaultResult<HashMap<String, usize>> {- let mut children: HashMap<&str, Vec<&str>> = HashMap::new();- for row in folder_rows {- if let (Some(id), Some(parent_id)) = (str_col(row, ID_COL), str_col(row, PARENT_FOLDER_ID_COL)) {- children.entry(parent_id).or_default().push(id);- }+/// Null every `Items.FolderId` that doesn't resolve to a folder *in the same manifest*.+fn null_dangling_item_folders(tables: &mut HashMap<String, Vec<CodecRecord>>) {+ null_dangling_reference(tables, ITEMS_TABLE, FOLDERS_TABLE, "FolderId");+}++/// Null every `FieldDefinitionId` that names no definition in the referencing row's own manifest.+///+/// The column is nullable (a value with no definition is how a *system* field is stored), so nulling+/// keeps the user's value and loses only the metadata this vault does not hold anyway. Dropping the row+/// instead would throw away what the user actually typed.+fn null_dangling_field_definitions(tables: &mut HashMap<String, Vec<CodecRecord>>) {+ for table in ["FieldValues", "FieldHistories"] {+ null_dangling_reference(tables, table, FIELD_DEFINITIONS_TABLE, FIELD_DEFINITION_ID_COL);}+}- let mut folder_to_spec: HashMap<String, usize> = HashMap::new();- for (spec_idx, spec) in specs.iter().enumerate() {- let mut queue: Vec<&str> = vec![spec.folder_id.as_str()];- while let Some(folder_id) = queue.pop() {- match folder_to_spec.get(folder_id) {- // Already visited for this spec: skip (defensive against a ParentFolderId cycle).- Some(&other) if other == spec_idx => continue,- Some(&other) => {- return Err(VaultError::General(format!(- "shared folders {} and {} overlap, nested shared folders are not supported",- specs[other].folder_id, spec.folder_id- )));+/// Drop every `ItemTags` row whose `(ManifestId, TagId)` names no tag in the combined set.+///+/// Unlike a field definition, the tag reference is the entire content of the row and cannot be nulled.+/// Skipped when the combined set carries no `Tags` table at all, which means a partial table set rather+/// than a vault with no tags.+fn drop_item_tags_without_a_tag(tables: &mut HashMap<String, Vec<CodecRecord>>) {+ if !tables.contains_key(TAGS_TABLE) {+ return;+ }+ let tag_ids = scoped_ids(tables, TAGS_TABLE);+ if let Some(rows) = tables.get_mut(ITEM_TAGS_TABLE) {+ rows.retain(|row| match str_col(row, TAG_ID_COL) {+ None => true,+ Some(tag_id) => tag_ids.contains(&(str_col(row, MANIFEST_ID_COL).unwrap_or_default().to_string(), tag_id.to_string())),+ });+ }+}++/// Null every `table.column` reference that names no row of `target` inside the referencing row's own+/// manifest. Both foreign keys this serves have a composite `(ManifestId, Id)` parent, so a reference+/// that resolves only in *another* namespace is as dangling as one that resolves nowhere.+fn null_dangling_reference(tables: &mut HashMap<String, Vec<CodecRecord>>, table: &str, target: &str, column: &str) {+ let target_ids = scoped_ids(tables, target);+ if let Some(rows) = tables.get_mut(table) {+ for row in rows {+ let scope = str_col(row, MANIFEST_ID_COL).unwrap_or_default().to_string();+ if let Some(reference) = str_col(row, column) {+ if !target_ids.contains(&(scope, reference.to_string())) {+ row.insert(column.to_string(), Value::Null);}- None => {}}- folder_to_spec.insert(folder_id.to_string(), spec_idx);- queue.extend(children.get(folder_id).map(Vec::as_slice).unwrap_or(&[]));}}- Ok(folder_to_spec)+}++/// Drop every item-scoped row whose `(ManifestId, ItemId)` names no item in the combined set.+///+/// A manifest is self-contained, so a leftover child is one whose item this vault does not hold: a+/// manifest built against a stale item set, or one hand-crafted to attach rows to an item in a+/// namespace it has no access to. Either way the composite foreign key rejects it, so it is dropped+/// here rather than failing the whole insert.+///+/// Skipped when the combined set carries no `Items` table at all: that is a caller passing a partial+/// table set, and pruning against an absent parent would delete every child row.+fn drop_orphan_item_children(tables: &mut HashMap<String, Vec<CodecRecord>>) {+ if !tables.contains_key(ITEMS_TABLE) {+ return;+ }+ let item_ids = scoped_ids(tables, ITEMS_TABLE);+ for table in manifest_scoped_tables() {+ let Some(rows) = tables.get_mut(table) else { continue };+ rows.retain(|row| match str_col(row, ITEM_ID_COL) {+ None => true,+ Some(item_id) => {+ let scope = str_col(row, MANIFEST_ID_COL).unwrap_or_default().to_string();+ item_ids.contains(&(scope, item_id.to_string()))+ }+ });+ }+}++/// The `(ManifestId, Id)` identity of every row in `table`.+fn scoped_ids(tables: &HashMap<String, Vec<CodecRecord>>, table: &str) -> HashSet<(String, String)> {+ tables+ .get(table)+ .map(|rows| {+ rows.iter()+ .filter_map(|row| str_col(row, ID_COL).map(|id| (str_col(row, MANIFEST_ID_COL).unwrap_or_default().to_string(), id.to_string())))+ .collect()+ })+ .unwrap_or_default()+}++/// The encryption-key row whose `PublicKey` matches `public_key`, from a decrypted manifest. The row+/// must be stamped with the manifest's own id, so a caller that skips the combine step still cannot be+/// tricked into using another manifest's key material. Deleted rows are skipped; returns `None` when no+/// live row carries that key.+pub fn extract_encryption_key_for_public_key(manifest: &Manifest, public_key: &str) -> Option<CodecRecord> {+ let scope = Some(manifest.manifest_id.as_str());+ manifest+ .tables+ .get(ENCRYPTION_KEYS_TABLE)?+ .iter()+ .find(|row| str_col(row, "PublicKey") == Some(public_key) && str_col(row, MANIFEST_ID_COL) == scope && !is_truthy(row.get("IsDeleted")))+ .cloned()+}++/// The manifest's *active* keypair: the live `IsPrimary` row stamped with the manifest's own id, whose+/// public half is published to the server for SMTP delivery. Superseded rows stay in the manifest, so+/// mail received before a rotation remains decryptable, but are never returned here.+pub fn active_encryption_key(manifest: &Manifest) -> Option<CodecRecord> {+ let scope = Some(manifest.manifest_id.as_str());+ manifest+ .tables+ .get(ENCRYPTION_KEYS_TABLE)?+ .iter()+ .find(|row| is_truthy(row.get("IsPrimary")) && str_col(row, MANIFEST_ID_COL) == scope && !is_truthy(row.get("IsDeleted")))+ .cloned()}fn str_col<'a>(row: &'a CodecRecord, column: &str) -> Option<&'a str> {diff --git a/core/rust/src/vault_codec/sharing_tests.rs b/core/rust/src/vault_codec/sharing_tests.rsindex 476e36a99..a4e100a80 100644--- a/core/rust/src/vault_codec/sharing_tests.rs+++ b/core/rust/src/vault_codec/sharing_tests.rs@@ -1,19 +1,25 @@-//! Unit tests for the shared-folder split (canonicalize) and combine (materialize) logic.+//! Unit tests for the shared-manifest split (canonicalize) and combine (materialize) logic.//!-//! These cover the core contract of vault sharing: `canonicalize_from_sqlite` with shared-folder-//! specs pulls each shared folder's subtree into its own manifest, and `materialize_as_sqlite` with-//! shared manifests combines everything back into one unified table set — for both the owner (whose-//! local vault holds every row) and a recipient (whose root manifest knows nothing of the share).+//! These cover the core contract of vault sharing: `canonicalize_from_sqlite` with shared-manifest+//! specs pulls each anchor folder's subtree into its own manifest, and `materialize_as_sqlite` with+//! shared manifests combines everything back into one unified table set, for both the owner (whose+//! local vault holds every row) and a recipient (whose personal manifest knows nothing of the share).use super::*;+use super::tests::{fitting_schema, materialize_manifests, materialize_input, stamp_unstamped};+use super::types::manifest_scoped_tables;use base64::engine::general_purpose::STANDARD as BASE64;use base64::Engine;use serde_json::json;-use std::collections::HashMap;+use std::collections::{HashMap, HashSet};-const SALT_ROOT: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";+const SALT_PERSONAL: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff";const SALT_SHARED: &str = "ffeeddccbbaa99887766554433221100ffeeddccbbaa99887766554433221100";+/// The personal manifest id every test canonicalizes against. Personal rows are stamped with it — there is+/// no NULL-scope convention anywhere in the format.+const PERSONAL_M: &str = "m-personal";+fn b64(bytes: &[u8]) -> String {BASE64.encode(bytes)}@@ -26,17 +32,31 @@ fn table(name: &str, records: Vec<CodecRecord>) -> CodecTableData {CodecTableData { name: name.to_string(), records }}-fn spec(folder_id: &str) -> SharedFolderSpec {- SharedFolderSpec { folder_id: folder_id.to_string(), user_salt: SALT_SHARED.to_string() }+/// A spec for `folder_id`, whose manifest id is derived as `m-<folder_id>` so every test can predict+/// the scope stamp of a partition's rows.+fn spec(folder_id: &str) -> ManifestSpec {+ ManifestSpec {+ manifest_id: format!("m-{folder_id}"),+ manifest_salt: SALT_SHARED.to_string(),+ name: Some(format!("Share {folder_id}")),+ }+}++/// A canonicalize input whose rows are stamped the way a real client writes them (see+/// [`super::tests::stamp_unstamped`]): fixtures declare the stamps that matter and this fills in the rest.+fn input_with_shares(tables: Vec<CodecTableData>, shared_manifests: Vec<ManifestSpec>) -> CanonicalizeInput {+ raw_input_with_shares(stamp_unstamped(tables, PERSONAL_M), shared_manifests)}-fn input_with_shares(tables: Vec<CodecTableData>, shared_folders: Vec<SharedFolderSpec>) -> CanonicalizeInput {+/// [`input_with_shares`] without the stamping pass, for the tests that hand canonicalize rows a client+/// failed to stamp.+fn raw_input_with_shares(tables: Vec<CodecTableData>, shared_manifests: Vec<ManifestSpec>) -> CanonicalizeInput {+ let personal = ManifestSpec { manifest_id: PERSONAL_M.to_string(), manifest_salt: SALT_PERSONAL.to_string(), name: None };CanonicalizeInput {tables,- user_salt: SALT_ROOT.to_string(),- migration_id: "20250101000000_Init".to_string(),canonicalized_at: "2026-01-01T00:00:00.000Z".to_string(),- shared_folders,+ manifests: std::iter::once(personal).chain(shared_manifests).collect(),+ adopt_unstamped_into: None,}}@@ -51,9 +71,103 @@ fn ids(records: &[CodecRecord]) -> Vec<&str> {out}-/// A representative owner vault: a personal folder+item, and a shared folder "f-shared" carrying a-/// subfolder, two items (one in the subfolder), and their child rows.+/// The scoped logo id the codec derives for `(manifest id, source)`, what tests assert against, since+/// a logo's identity is a function of its manifest and domain rather than whatever id the writer minted.+fn logo_id(scope: &str, source: &str) -> String {+ scoped_assets::logo_id_for(scope, scoped_assets::KIND_FAVICON, source)+}++/// The `Logos` row for `source` in a manifest, resolved through its manifest-id-derived id.+fn logo_row<'a>(m: &'a Manifest, source: &str) -> &'a CodecRecord {+ let id = logo_id(&m.manifest_id, source);+ rows(m, "Logos")+ .iter()+ .find(|r| r.get("Id").and_then(|v| v.as_str()) == Some(id.as_str()))+ .unwrap_or_else(|| panic!("no {source} logo in manifest {:?}", m.manifest_id))+}++/// The `Source` values of a manifest's logos, sorted.+fn logo_sources(m: &Manifest) -> Vec<&str> {+ let mut out: Vec<&str> = rows(m, "Logos").iter().filter_map(|r| r.get("Source").and_then(|v| v.as_str())).collect();+ out.sort();+ out+}++/// Stamp a folder's whole subtree — its folders and the items in them — into `manifest_id`. Mirrors+/// exactly what the client's `FolderRepository.restampSubtree` writes when a folder starts being+/// shared, so fixtures carry the membership the codec now routes on.+///+/// Logos are deliberately NOT stamped here, just as the client does not stamp them: a logo is a+/// per-manifest asset the codec reconciles itself (cloning a copy into every manifest whose items+/// reference it), so moving items between manifests never has to move logo rows.+fn stamp_subtree(mut tables: Vec<CodecTableData>, folder_id: &str, manifest_id: &str) -> Vec<CodecTableData> {+ // Walk the folder tree down from `folder_id`.+ let mut subtree: HashSet<String> = HashSet::new();+ subtree.insert(folder_id.to_string());+ loop {+ let mut grew = false;+ for t in tables.iter().filter(|t| t.name == "Folders") {+ for row in &t.records {+ let (Some(id), Some(parent)) = (row.get("Id").and_then(|v| v.as_str()), row.get("ParentFolderId").and_then(|v| v.as_str())) else { continue };+ if subtree.contains(parent) && subtree.insert(id.to_string()) {+ grew = true;+ }+ }+ }+ if !grew {+ break;+ }+ }++ // Folders in the subtree join the manifest; every other folder keeps the stamp it already had.+ for t in tables.iter_mut().filter(|t| t.name == "Folders") {+ for row in t.records.iter_mut() {+ let id = row.get("Id").and_then(|v| v.as_str()).unwrap_or("").to_string();+ if subtree.contains(&id) {+ row.insert("ManifestId".to_string(), json!(manifest_id));+ } else if !row.contains_key("ManifestId") {+ row.insert("ManifestId".to_string(), json!(PERSONAL_M));+ }+ }+ }++ /*+ * Every item takes the stamp of the folder it sits in, which is what the client's INSERT/UPDATE/+ * MOVE statements resolve via `BaseQueries.MANIFEST_OF_FOLDER`. Deriving it here rather than+ * carrying it forward is what makes an item moved OUT of a shared folder leave that manifest.+ */+ let folder_scope: HashMap<String, String> = tables+ .iter()+ .filter(|t| t.name == "Folders")+ .flat_map(|t| t.records.iter())+ .filter_map(|row| {+ let id = row.get("Id").and_then(|v| v.as_str())?;+ let scope = row.get("ManifestId").and_then(|v| v.as_str()).unwrap_or(PERSONAL_M);+ Some((id.to_string(), scope.to_string()))+ })+ .collect();+ for t in tables.iter_mut().filter(|t| t.name == "Items") {+ for row in t.records.iter_mut() {+ let scope = row+ .get("FolderId")+ .and_then(|v| v.as_str())+ .and_then(|f| folder_scope.get(f).cloned())+ .unwrap_or_else(|| PERSONAL_M.to_string());+ row.insert("ManifestId".to_string(), json!(scope));+ }+ }++ tables+}++/// A representative owner vault: a personal folder+item, and a shared manifest ("m-f-shared") carrying+/// the "f-shared" subtree, two items (one in a subfolder), and their child rows.fn owner_tables() -> Vec<CodecTableData> {+ stamp_subtree(owner_tables_unstamped(), "f-shared", "m-f-shared")+}++/// The same vault with no `ManifestId` stamps at all, as a pre-2.2.0 vault holds it.+fn owner_tables_unstamped() -> Vec<CodecTableData> {vec![table("Folders", vec![row(&[("Id", json!("f-personal")), ("Name", json!("Personal")), ("ParentFolderId", serde_json::Value::Null)]),@@ -116,33 +230,38 @@ fn canonicalize_owner() -> CanonicalizedVault {#[test]fn split_moves_folder_subtree_and_items_into_shared_manifest() {let out = canonicalize_owner();- assert_eq!(out.shared_vaults.len(), 1);- let shared = &out.shared_vaults[0].manifest;+ assert_eq!(out.rest().len(), 1);+ let shared = &out.rest()[0].manifest;- assert_eq!(out.shared_vaults[0].folder_id, "f-shared");- assert_eq!(shared.shared_folder_id.as_deref(), Some("f-shared"));- assert_eq!(shared.user_salt, SALT_SHARED);+ assert_eq!(out.rest()[0].manifest.name.as_deref(), Some("Share f-shared"));+ assert_eq!(shared.manifest_id, "m-f-shared");+ assert_eq!(shared.name.as_deref(), Some("Share f-shared"), "the manifest carries its display name, not a folder anchor");+ assert_eq!(out.first().manifest.manifest_id, PERSONAL_M);+ assert_eq!(shared.manifest_salt, SALT_SHARED);- // Folder subtree (root + subfolder) moved; personal folder stayed.+ // Folder subtree (anchor + subfolder) moved; personal folder stayed.assert_eq!(ids(rows(shared, "Folders")), vec!["f-shared", "f-sub"]);- assert_eq!(ids(rows(&out.manifest, "Folders")), vec!["f-personal"]);+ assert_eq!(ids(rows(&out.first().manifest, "Folders")), vec!["f-personal"]);// Items in the subtree moved (including nested subfolder items); others stayed.assert_eq!(ids(rows(shared, "Items")), vec!["i-shared", "i-sub"]);- assert_eq!(ids(rows(&out.manifest, "Items")), vec!["i-nofolder", "i-personal"]);+ assert_eq!(ids(rows(&out.first().manifest, "Items")), vec!["i-nofolder", "i-personal"]);}#[test]-fn split_normalizes_shared_root_parent_to_null() {- // Owner had nested the shared folder under a personal folder: the shared manifest must not leak+fn split_normalizes_shared_anchor_parent_to_null() {+ // Owner had nested the anchor folder under a personal folder: the shared manifest must not leak// (or depend on) that personal placement.let mut tables = owner_tables();tables[0].records[1].insert("ParentFolderId".to_string(), json!("f-personal"));+ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();- let shared_root = rows(&out.shared_vaults[0].manifest, "Folders").iter().find(|r| r["Id"] == json!("f-shared")).unwrap().clone();- assert_eq!(shared_root["ParentFolderId"], serde_json::Value::Null);+ let shared_anchor = rows(&out.rest()[0].manifest, "Folders").iter().find(|r| r["Id"] == json!("f-shared")).unwrap().clone();+ assert_eq!(shared_anchor["ParentFolderId"], serde_json::Value::Null);// The subfolder keeps its parent (it points inside the shared manifest).- let sub = rows(&out.shared_vaults[0].manifest, "Folders").iter().find(|r| r["Id"] == json!("f-sub")).unwrap().clone();+ let sub = rows(&out.rest()[0].manifest, "Folders").iter().find(|r| r["Id"] == json!("f-sub")).unwrap().clone();assert_eq!(sub["ParentFolderId"], json!("f-shared"));}@@ -150,79 +269,452 @@ fn split_normalizes_shared_root_parent_to_null() {fn split_routes_item_scoped_tables_generically() {// FieldValues / TotpCodes / Attachments / ItemTags all follow their item with zero per-table wiring.let out = canonicalize_owner();- let shared = &out.shared_vaults[0].manifest;+ let shared = &out.rest()[0].manifest;assert_eq!(ids(rows(shared, "FieldValues")), vec!["fv-shared", "fv-sub"]);- assert_eq!(ids(rows(&out.manifest, "FieldValues")), vec!["fv-personal"]);+ assert_eq!(ids(rows(&out.first().manifest, "FieldValues")), vec!["fv-personal"]);assert_eq!(ids(rows(shared, "TotpCodes")), vec!["totp-shared"]);- assert!(rows(&out.manifest, "TotpCodes").is_empty());+ assert!(rows(&out.first().manifest, "TotpCodes").is_empty());assert_eq!(ids(rows(shared, "Attachments")), vec!["att-shared"]);- assert_eq!(ids(rows(&out.manifest, "Attachments")), vec!["att-personal"]);+ assert_eq!(ids(rows(&out.first().manifest, "Attachments")), vec!["att-personal"]);assert_eq!(ids(rows(shared, "ItemTags")), vec!["it-2", "it-3"]);- assert_eq!(ids(rows(&out.manifest, "ItemTags")), vec!["it-1"]);+ assert_eq!(ids(rows(&out.first().manifest, "ItemTags")), vec!["it-1"]);}#[test]-fn split_copies_referenced_tags_logos_and_field_definitions() {+fn split_gives_each_manifest_its_own_copy_of_the_tags_it_uses() {let out = canonicalize_owner();- let shared = &out.shared_vaults[0].manifest;-- // Tag referenced from both sides is copied to both; shared-only tag leaves the root; the- // unreferenced tag stays personal.+ let shared = &out.rest()[0].manifest;++ /*+ * A tag is manifest-scoped like everything else, so the shared manifest gets its own stamped copy+ * of the two its items carry — the composite `(ManifestId, TagId)` foreign key cannot reach into+ * another namespace. The copies are copies: the base keeps every tag it holds, including the one+ * only shared items use now and the one nothing uses at all. An unused tag is still the user's.+ */assert_eq!(ids(rows(shared, "Tags")), vec!["tag-both", "tag-shared-only"]);- assert_eq!(ids(rows(&out.manifest, "Tags")), vec!["tag-both", "tag-unused"]);-- // Same rule for logos (referenced via Items.LogoId)...- assert_eq!(ids(rows(shared, "Logos")), vec!["logo-both", "logo-shared-only"]);- assert_eq!(ids(rows(&out.manifest, "Logos")), vec!["logo-both"]);+ assert_eq!(ids(rows(&out.first().manifest, "Tags")), vec!["tag-both", "tag-shared-only", "tag-unused"]);+ for row in rows(shared, "Tags") {+ assert_eq!(row["ManifestId"], json!("m-f-shared"), "the copy claims the manifest it lives in");+ }- // ...and field definitions (referenced via FieldValues.FieldDefinitionId).+ // Field definitions follow the same rule (referenced via FieldValues.FieldDefinitionId).assert_eq!(ids(rows(shared, "FieldDefinitions")), vec!["fd-1"]);- assert_eq!(ids(rows(&out.manifest, "FieldDefinitions")), vec!["fd-1", "fd-unused"]);+ assert_eq!(ids(rows(&out.first().manifest, "FieldDefinitions")), vec!["fd-1", "fd-unused"]);+}++#[test]+fn split_clones_a_tag_into_the_manifest_an_item_moved_into() {+ /*+ * An item moves into the shared folder carrying a tag that only ever existed in the user's own+ * manifest. Its ItemTags row follows the item, and the tag it points at has to come with it: the+ * foreign key is `(ManifestId, TagId)`, so the copy sitting in another namespace cannot satisfy it.+ */+ let mut tables = owner_tables_unstamped();+ tables.iter_mut().find(|t| t.name == "Items").unwrap().records.iter_mut()+ .find(|r| r["Id"] == json!("i-personal")).unwrap()+ .insert("FolderId".to_string(), json!("f-shared"));+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0].manifest;++ // "tag-both" travelled with the item; the base manifest keeps its own copy regardless.+ assert!(ids(rows(shared, "Tags")).contains(&"tag-both"));+ assert!(ids(rows(&out.first().manifest, "Tags")).contains(&"tag-both"));+ let cloned = rows(shared, "Tags").iter().find(|r| r["Id"] == json!("tag-both")).unwrap();+ assert_eq!(cloned["ManifestId"], json!("m-f-shared"), "the copy claims the manifest it moved into");+ assert_eq!(cloned["Name"], json!("work"), "content copied verbatim, id kept");+}++#[test]+fn split_refuses_rows_that_name_no_manifest() {+ /*+ * Membership IS the stamp, so a row that names no manifest is a client that could not say where its+ * data belongs. There is no fallback scope to demote it into: the push is refused, naming the table+ * and row, rather than homing it somewhere plausible and hoping.+ */+ let mut tables = stamp_unstamped(owner_tables(), PERSONAL_M);+ for row in tables.iter_mut().find(|t| t.name == "Items").unwrap().records.iter_mut() {+ row.insert("ManifestId".to_string(), json!(""));+ }++ let err = canonicalize_from_sqlite(raw_input_with_shares(tables, vec![spec("f-shared")])).unwrap_err().to_string();+ assert!(err.contains("Items"), "the error names the offending table: {err}");+ assert!(err.contains("name no manifest"), "the error says what is wrong: {err}");+}++#[test]+fn split_reads_the_all_zero_guid_as_naming_no_manifest() {+ /*+ * The schema stamps `00000000-0000-0000-0000-000000000000` into rows whose manifest it could not+ * derive (SQLite will not add a NOT NULL column without a default). Read as a real id it would route+ * to a manifest nobody carries and be dropped as revoked; it has to mean "unstamped" instead.+ */+ let mut tables = stamp_unstamped(owner_tables(), PERSONAL_M);+ for row in tables.iter_mut().find(|t| t.name == "Tags").unwrap().records.iter_mut() {+ row.insert("ManifestId".to_string(), json!("00000000-0000-0000-0000-000000000000"));+ }++ let err = canonicalize_from_sqlite(raw_input_with_shares(tables.clone(), vec![spec("f-shared")])).unwrap_err().to_string();+ assert!(err.contains("Tags"), "the sentinel is refused like any other unstamped row: {err}");++ let mut input = raw_input_with_shares(tables, vec![spec("f-shared")]);+ input.adopt_unstamped_into = Some(PERSONAL_M.to_string());+ let out = canonicalize_from_sqlite(input).unwrap();+ assert_eq!(ids(rows(&out.first().manifest, "Tags")), vec!["tag-both", "tag-shared-only", "tag-unused"], "and adopted, never dropped");+}++#[test]+fn split_adopts_unstamped_rows_only_when_the_client_asks() {+ // The sqlite-blob conversion is the one caller allowed to hand over unstamped rows, and it has to+ // say so: `adoptUnstampedInto` names the manifest they join. Rows that already name one keep it.+ let tables = stamp_subtree(owner_tables_unstamped(), "f-shared", "m-f-shared");+ let mut input = raw_input_with_shares(tables, vec![spec("f-shared")]);+ input.adopt_unstamped_into = Some(PERSONAL_M.to_string());++ let out = canonicalize_from_sqlite(input).unwrap();+ let base = &out.first().manifest;+ assert_eq!(ids(rows(base, "Items")), vec!["i-nofolder", "i-personal"], "no row was dropped");+ assert_eq!(ids(rows(base, "Tags")), vec!["tag-both", "tag-shared-only", "tag-unused"]);+ for row in rows(base, "Tags") {+ assert_eq!(row["ManifestId"], json!(PERSONAL_M), "adopted rows carry a real manifest id on the way out");+ }+ assert_eq!(ids(rows(&out.rest()[0].manifest, "Items")), vec!["i-shared", "i-sub"], "explicit stamps still route");+}++#[test]+fn sqlite_blob_migration_stamps_every_row_of_a_vault_that_has_no_manifest_id_column() {+ /*+ * The sqlite-blob > manifest-v1 migration, end to end: a vault whose schema stops short of the+ * `ManifestId` column (so no row carries the key at all) is canonicalized and materialized straight+ * back out, with no share in sight. Nothing stamps those rows beforehand — the column they would be+ * stamped in does not exist yet — so canonicalize adopting them into the manifest being written from+ * IS the conversion. Every row must come out carrying a real manifest id: the materialized schema+ * declares the column NOT NULL, so a single unstamped row fails the whole migration.+ */+ let mut input = raw_input_with_shares(owner_tables_unstamped(), vec![]);+ input.adopt_unstamped_into = Some(PERSONAL_M.to_string());+ let out = canonicalize_from_sqlite(input).unwrap();+ assert_eq!(out.rest().len(), 0, "no shares in this push");++ let base = &out.first().manifest;+ assert_eq!(ids(rows(base, "Items")), vec!["i-nofolder", "i-personal", "i-shared", "i-sub"], "no row was dropped");+ for name in manifest_scoped_tables() {+ for record in rows(base, name) {+ assert_eq!(record["ManifestId"], json!(PERSONAL_M), "{name} row {:?} left canonicalize unstamped", record.get("Id"));+ }+ }++ // And the same holds of what actually gets written back into the SQLite database.+ let re = materialize_as_sqlite(materialize_input(base.clone(), vec![], out.data_buckets.clone())).unwrap();+ for table in re.tables.iter().filter(|t| manifest_scoped_tables().contains(&t.name.as_str())) {+ for record in &table.records {+ assert_eq!(record["ManifestId"], json!(PERSONAL_M), "{} row {:?} would violate the NOT NULL stamp", table.name, record.get("Id"));+ }+ }+}++#[test]+fn split_scopes_logos_per_manifest_instead_of_copying_them() {+ // Logos are NOT reference-copied: each manifest gets its own row per domain, identified by its+ // own scope. The owner's legacy rows carry no scope.+ let out = canonicalize_owner();+ let shared = &out.rest()[0].manifest;++ // The shared manifest holds exactly the domains its items use, under folder-scoped ids.+ assert_eq!(logo_sources(shared), vec!["github.com", "netflix.com"]);+ let mut expected_ids = vec![logo_id("m-f-shared", "github.com"), logo_id("m-f-shared", "netflix.com")];+ expected_ids.sort();+ assert_eq!(ids(rows(shared, "Logos")), expected_ids.iter().map(String::as_str).collect::<Vec<&str>>());+ for row in rows(shared, "Logos") {+ assert_eq!(row["ManifestId"], json!("m-f-shared"));+ }++ // The personal manifest keeps its own row under a personal-scoped id, but only for a domain it still uses:+ // netflix.com left with the item that referenced it, and a favicon is refetchable.+ assert_eq!(logo_sources(&out.first().manifest), vec!["github.com"]);+ assert_eq!(logo_row(&out.first().manifest, "github.com")["ManifestId"], json!(PERSONAL_M));++ // Same domain, two manifests, two distinct rows: nothing converged.+ assert_ne!(logo_row(&out.first().manifest, "github.com")["Id"], logo_row(shared, "github.com")["Id"]);++ // Items point at the row in their own manifest.+ let personal = rows(&out.first().manifest, "Items").iter().find(|r| r["Id"] == json!("i-personal")).unwrap();+ assert_eq!(personal["LogoId"], logo_row(&out.first().manifest, "github.com")["Id"]);+ let shared_item = rows(shared, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap();+ assert_eq!(shared_item["LogoId"], logo_row(shared, "github.com")["Id"]);+}++#[test]+fn manifests_drop_logos_no_item_references() {+ // An item leaves the shared manifest: its logo must not linger there (every member downloads it),+ // and the copy it now needs lands in the manifest it moved to.+ let mut tables = owner_tables();+ let items = tables.iter_mut().find(|t| t.name == "Items").unwrap();+ items.records.iter_mut().find(|r| r["Id"] == json!("i-sub")).unwrap().insert("FolderId".to_string(), json!("f-personal"));++ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ assert_eq!(logo_sources(&out.rest()[0].manifest), vec!["github.com"], "netflix logo left with its item");+ assert_eq!(logo_sources(&out.first().manifest), vec!["github.com", "netflix.com"]);+}++#[test]+fn orphan_favicons_are_pruned_everywhere_and_uploads_survive_everywhere() {+ /*+ * A favicon nothing references is refetchable, so it goes, in every manifest alike. An image the+ * user uploaded is not reproducible, so it stays — also in every manifest alike: each one carries+ * its own library, and which of them a client offers to pick from is a runtime choice, not+ * something the format decides by anointing one manifest.+ */+ let mut tables = owner_tables();+ let logos = tables.iter_mut().find(|t| t.name == "Logos").unwrap();+ logos.records.push(row(&[+ ("Id", json!("upload-personal")), ("Kind", json!("custom")), ("Source", json!("aa11")),+ ("ManifestId", json!(PERSONAL_M)), ("FileData", json!({ "__b64": b64(&[0x55]) })),+ ]));+ logos.records.push(row(&[+ ("Id", json!("upload-shared")), ("Kind", json!("custom")), ("Source", json!("bb22")),+ ("ManifestId", json!("m-f-shared")), ("FileData", json!({ "__b64": b64(&[0x66]) })),+ ]));++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();++ // netflix.com left with the item that used it; each manifest kept the upload stamped for it.+ assert_eq!(logo_sources(&out.first().manifest), vec!["aa11", "github.com"]);+ assert_eq!(logo_sources(&out.rest()[0].manifest), vec!["bb22", "github.com", "netflix.com"]);+}++#[test]+fn logo_ids_are_derived_from_scope_and_source() {+ // The identity contract every platform reproduces: same (scope, domain) -> same row, and the+ // derivation is case-insensitive on the domain.+ assert_eq!(logo_id(PERSONAL_M, "github.com"), logo_id(PERSONAL_M, "GitHub.com"));+ assert_ne!(logo_id(PERSONAL_M, "github.com"), logo_id("m-f-shared", "github.com"));+ assert_ne!(logo_id("m-f-a", "github.com"), logo_id("m-f-b", "github.com"));+ // UUID-shaped so it round-trips through Guid/UUID types on every platform.+ let id = logo_id(PERSONAL_M, "github.com");+ assert_eq!(id.len(), 36);+ assert_eq!(id.chars().filter(|c| *c == '-').count(), 4);}#[test]fn split_hashes_blobs_with_per_manifest_salts() {let out = canonicalize_owner();- let shared = &out.shared_vaults[0];+ let shared = &out.rest()[0];- // Root blob map: personal attachment + the logo copy referenced by the personal item.- let root_hashes: Vec<&String> = out.blobs.keys().collect();- assert_eq!(root_hashes.len(), 2, "root: att-personal + logo-both, got {:?}", out.blobs.values().map(|b| &b.kind).collect::<Vec<_>>());+ // Personal blob map: personal attachment + the one logo a personal item still references.+ let personal_hashes: Vec<&String> = out.first().blobs.keys().collect();+ assert_eq!(personal_hashes.len(), 2, "personal: att-personal + github logo, got {:?}", out.first().blobs.values().map(|b| &b.kind).collect::<Vec<_>>());// Shared blob map: shared attachment + both logo copies, hashed with the SHARED salt.assert_eq!(shared.blobs.len(), 3);let logo_bytes = [0xAAu8, 0xBB];- let expected_root_hash = hash::salted_blob_hash(&logo_bytes, SALT_ROOT);+ let expected_personal_hash = hash::salted_blob_hash(&logo_bytes, SALT_PERSONAL);let expected_shared_hash = hash::salted_blob_hash(&logo_bytes, SALT_SHARED);- assert!(out.blobs.contains_key(&expected_root_hash), "logo-both hashed with root salt in root manifest");- assert!(shared.blobs.contains_key(&expected_shared_hash), "logo-both hashed with shared salt in shared manifest");- assert_ne!(expected_root_hash, expected_shared_hash);+ assert!(out.first().blobs.contains_key(&expected_personal_hash), "github favicon hashed with the personal salt in personal manifest");+ assert!(shared.blobs.contains_key(&expected_shared_hash), "github favicon hashed with shared salt in shared manifest");+ assert_ne!(expected_personal_hash, expected_shared_hash);// The blob refs inside each manifest point at their own map's hashes.- let root_logo = rows(&out.manifest, "Logos").iter().find(|r| r["Id"] == json!("logo-both")).unwrap().clone();- assert_eq!(root_logo["FileData"]["__blobRef"], json!(expected_root_hash));- let shared_logo = rows(&shared.manifest, "Logos").iter().find(|r| r["Id"] == json!("logo-both")).unwrap().clone();- assert_eq!(shared_logo["FileData"]["__blobRef"], json!(expected_shared_hash));+ assert_eq!(logo_row(&out.first().manifest, "github.com")["FileData"]["__blobRef"], json!(expected_personal_hash));+ assert_eq!(logo_row(&shared.manifest, "github.com")["FileData"]["__blobRef"], json!(expected_shared_hash));}#[test]-fn split_keeps_personal_tables_out_of_shared_manifests() {+fn split_keeps_bucketed_and_foreign_key_tables_out_of_shared_manifests() {let out = canonicalize_owner();- let shared = &out.shared_vaults[0].manifest;- assert!(!shared.tables.contains_key("EncryptionKeys"), "key material must never enter a shared manifest");- assert!(!shared.tables.contains_key("Settings"), "settings are personal (and bucketed)");- // Root keeps them in their own buckets (EncryptionKeys and Settings are both bucketed now).- assert!(!out.manifest.tables.contains_key("EncryptionKeys"), "key material lives in its bucket, not the manifest");- assert!(out.data_buckets.iter().any(|b| b.category == "EncryptionKeys" && !b.tables.get("EncryptionKeys").map(Vec::is_empty).unwrap_or(true)));- assert!(out.data_buckets.iter().any(|b| b.category == "Settings" && !b.tables.is_empty()));+ let shared = &out.rest()[0].manifest;+ assert!(!shared.tables.contains_key("EncryptionKeys"), "personal (personal-stamped) key material must never enter a shared manifest");+ assert!(!shared.tables.contains_key("Settings"), "a bucketed table belongs in its manifest's data bucket, never in a manifest");+ assert!(!out.first().manifest.tables.contains_key("Settings"), "…and that holds for the personal manifest too");+ // The personal keypair set rides inside the personal manifest itself; Settings rides in its bucket.+ assert_eq!(ids(rows(&out.first().manifest, "EncryptionKeys")), vec!["ek-1"], "personal keys live in the personal manifest");+ assert!(!out.data_buckets.iter().any(|b| b.category == "EncryptionKeys"), "EncryptionKeys is no longer a bucket category");++ /*+ * Every manifest gets its own Settings bucket, holding exactly its own rows — the shared one empty+ * here, since the single Settings row belongs to the personal manifest. Leaving a manifest's bucket+ * out would read as "unchanged" rather than "emptied", so its last row could never be deleted.+ */+ let settings: Vec<&DataBucket> = out.data_buckets.iter().filter(|b| b.category == "Settings").collect();+ assert_eq!(settings.iter().map(|b| b.manifest_id.as_str()).collect::<Vec<&str>>(), vec!["m-f-shared", PERSONAL_M]);+ assert_eq!(settings[0].tables["Settings"].len(), 0, "the shared manifest holds no settings of its own, and says so");+ assert_eq!(settings[1].tables["Settings"].len(), 1);}#[test]-fn split_supports_multiple_disjoint_shared_folders() {+fn split_routes_bucket_rows_to_the_manifest_that_owns_them() {+ /*+ * The whole point of addressing a bucket by manifest: a settings row stamped for a shared manifest+ * lands in THAT manifest's bucket, not the writer's. Nothing in the vault does this yet (only the+ * personal manifest carries settings today), but the routing is the row's stamp, not a hardcoded+ * assumption about which manifest may own a bucket.+ */+ let mut tables = owner_tables();+ let settings = tables.iter_mut().find(|t| t.name == "Settings").expect("settings fixture");+ settings.records.push(row(&[("Key", json!("sort")), ("Value", json!("name")), ("ManifestId", json!("m-f-shared"))]));+ settings.records.push(row(&[("Key", json!("stale")), ("Value", json!("x")), ("ManifestId", json!("m-gone"))]));++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();++ let bucket_of = |manifest_id: &str| -> Option<&DataBucket> {+ out.data_buckets.iter().find(|b| b.category == "Settings" && b.manifest_id == manifest_id)+ };+ let keys = |b: &DataBucket| -> Vec<String> {+ let mut out: Vec<String> = b.tables["Settings"].iter().filter_map(|r| r.get("Key").and_then(|v| v.as_str()).map(str::to_string)).collect();+ out.sort();+ out+ };++ assert_eq!(keys(bucket_of(PERSONAL_M).expect("personal bucket")), vec!["theme"]);+ assert_eq!(keys(bucket_of("m-f-shared").expect("shared bucket")), vec!["sort"]);+ assert!(bucket_of("m-gone").is_none(), "a row stamped for a manifest this vault no longer carries is dropped, like every other row");++ // Each bucket is self-consistent: its rows claim its own manifest, so validation passes.+ for bucket in out.data_buckets.iter() {+ let result = validate_data_bucket(bucket);+ assert!(result.ok, "bucket {} invalid: {:?}", bucket.manifest_id, result.failed_rules);+ }+}++#[test]+fn combine_stamps_bucket_rows_with_the_manifest_that_delivered_them() {+ /*+ * The mirror rule of "the shipping manifest IS the membership": a bucket arrives under one manifest,+ * so its rows claim that manifest. A bucket whose rows name someone else cannot move them there.+ */+ let out = canonicalize_owner();+ let mut evil = out.data_buckets.iter().find(|b| b.category == "Settings" && b.manifest_id == PERSONAL_M).expect("personal settings bucket").clone();+ evil.manifest_id = "m-f-shared".to_string();++ let re = materialize_as_sqlite(materialize_input(out.first().manifest.clone(), out.rest().iter().map(|s| s.manifest.clone()).collect(), vec![evil])).unwrap();+ let map = materialized_map(&re);+ assert_eq!(map["Settings"].len(), 1);+ assert_eq!(map["Settings"][0]["ManifestId"], json!("m-f-shared"), "the delivering manifest decides the scope, not the row's own claim");+}++#[test]+fn combine_keeps_two_manifests_settings_side_by_side() {+ // Two manifests holding the same setting key are two rows: (ManifestId, Key) keeps them apart.+ let out = canonicalize_owner();+ let personal_bucket = out.data_buckets.iter().find(|b| b.category == "Settings" && b.manifest_id == PERSONAL_M).expect("personal settings bucket").clone();+ let mut shared_bucket = personal_bucket.clone();+ shared_bucket.manifest_id = "m-f-shared".to_string();++ let re = materialize_as_sqlite(materialize_input(+ out.first().manifest.clone(),+ out.rest().iter().map(|s| s.manifest.clone()).collect(),+ vec![personal_bucket, shared_bucket],+ ))+ .unwrap();+ let map = materialized_map(&re);+ let mut scopes: Vec<&str> = map["Settings"].iter().filter_map(|r| r["ManifestId"].as_str()).collect();+ scopes.sort();+ assert_eq!(scopes, vec!["m-f-shared", PERSONAL_M], "one 'theme' row per manifest, neither shadowing the other");+ assert!(map["Settings"].iter().all(|r| r["Key"] == json!("theme")));+}++#[test]+fn extract_buckets_splits_a_category_the_way_canonicalize_does() {+ /*+ * The bucket-only push path routes rows by the manifest each one names, exactly like the full push:+ * the caller hands over the whole category and gets one bucket per manifest it can write. Every+ * manifest asked for gets a bucket even when it holds nothing (otherwise deleting its last row+ * would never reach the server), a row naming a manifest this vault does not carry is dropped with+ * that manifest, and a row that names no manifest has no scope the codec may invent — it refuses.+ */+ let settings = |rows: Vec<CodecRecord>| -> HashMap<String, Vec<CodecRecord>> { [("Settings".to_string(), rows)].into_iter().collect() };++ let buckets = extract_buckets(+ "Settings".to_string(),+ vec![PERSONAL_M.to_string(), "m-f-shared".to_string()],+ settings(vec![+ row(&[("Key", json!("sort")), ("Value", json!("name")), ("ManifestId", json!("M-F-SHARED"))]),+ row(&[("Key", json!("theme")), ("Value", json!("dark")), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Key", json!("locale")), ("Value", json!("nl")), ("ManifestId", json!("m-gone"))]),+ ]),+ )+ .unwrap();++ let ids: Vec<&str> = buckets.iter().map(|b| b.manifest_id.as_str()).collect();+ assert_eq!(ids, vec!["m-f-shared", PERSONAL_M], "one bucket per manifest asked for, in a stable order");+ assert!(buckets.iter().all(|b| validate_data_bucket(b).ok));++ let shared = buckets.iter().find(|b| b.manifest_id == "m-f-shared").unwrap();+ assert_eq!(shared.tables["Settings"].len(), 1, "the shared manifest gets exactly its own row");+ assert_eq!(shared.tables["Settings"][0]["Key"], json!("sort"));+ assert_eq!(shared.tables["Settings"][0]["ManifestId"], json!("m-f-shared"), "a casing difference is normalized to the declared id");++ let personal = buckets.iter().find(|b| b.manifest_id == PERSONAL_M).unwrap();+ assert_eq!(personal.tables["Settings"].len(), 1, "the row stamped for a manifest this vault no longer carries is dropped, not re-homed");+ assert_eq!(personal.tables["Settings"][0]["Key"], json!("theme"));++ // A manifest holding nothing still gets its bucket, with the category's tables declared empty.+ let empty = extract_buckets("Settings".to_string(), vec![PERSONAL_M.to_string()], settings(vec![])).unwrap();+ assert_eq!(empty.len(), 1);+ assert_eq!(empty[0].tables["Settings"].len(), 0, "an emptied table is declared, so the delete reaches the server");++ let unstamped = extract_buckets("Settings".to_string(), vec![PERSONAL_M.to_string()], settings(vec![row(&[("Key", json!("k")), ("Value", json!("v"))])]))+ .unwrap_err()+ .to_string();+ assert!(unstamped.contains("name no manifest"), "the error says what is wrong: {unstamped}");+}++#[test]+fn split_routes_unregistered_tables_by_their_own_stamps() {+ /*+ * A table this build's registry does not know — a newer writer's table carried through the codec+ * overflow — routes by its rows' own stamps like every registered table. The writing (personal)+ * manifest is not special: it gets exactly its own rows, a shared manifest gets exactly its own,+ * and a row stamped for a manifest this vault no longer carries is dropped with it.+ */+ let mut tables = owner_tables();+ tables.push(table("Widgets", vec![+ row(&[("Id", json!("w-personal")), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("w-shared")), ("ManifestId", json!("m-f-shared"))]),+ row(&[("Id", json!("w-gone")), ("ManifestId", json!("m-revoked"))]),+ ]));++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ assert_eq!(ids(rows(&out.first().manifest, "Widgets")), vec!["w-personal"], "the writing manifest keeps only rows stamped for it");+ assert_eq!(ids(rows(&out.rest()[0].manifest, "Widgets")), vec!["w-shared"], "a shared manifest's rows come home to it");+}++#[test]+fn split_refuses_unregistered_rows_that_name_no_manifest() {+ // The no-fallback-scope rule holds for tables the registry does not know too: an unstamped row of+ // one is refused, never demoted into the writing manifest.+ let mut tables = owner_tables();+ tables.push(table("Widgets", vec![row(&[("Id", json!("w-1"))])]));++ let err = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap_err().to_string();+ assert!(err.contains("Widgets"), "the error names the offending table: {err}");+ assert!(err.contains("name no manifest"), "the error says what is wrong: {err}");+}++#[test]+fn validate_rejects_an_unaddressed_or_cross_stamped_bucket() {+ let out = canonicalize_owner();+ let bucket = out.data_buckets.iter().find(|b| b.category == "Settings" && b.manifest_id == PERSONAL_M).expect("personal settings bucket");++ let mut unaddressed = bucket.clone();+ unaddressed.manifest_id = String::new();+ assert!(validate_data_bucket(&unaddressed).failed_rules.iter().any(|r| r == "dataBucket-manifestId-missing"));++ let mut cross_stamped = bucket.clone();+ cross_stamped.manifest_id = "m-f-shared".to_string();+ assert!(validate_data_bucket(&cross_stamped).failed_rules.iter().any(|r| r == "dataBucket-scope-mismatch"));+}++#[test]+fn split_supports_multiple_disjoint_shared_manifests() {let tables = vec![table("Folders", vec![row(&[("Id", json!("f-a")), ("ParentFolderId", serde_json::Value::Null)]),@@ -234,44 +726,51 @@ fn split_supports_multiple_disjoint_shared_folders() {]),];let mut spec_b = spec("f-b");- spec_b.user_salt = SALT_ROOT.to_string();+ spec_b.manifest_salt = SALT_PERSONAL.to_string();+ let tables = stamp_subtree(stamp_subtree(tables, "f-a", "m-f-a"), "f-b", "m-f-b");let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-a"), spec_b])).unwrap();- assert_eq!(out.shared_vaults.len(), 2);- assert_eq!(out.shared_vaults[0].folder_id, "f-a");- assert_eq!(out.shared_vaults[1].folder_id, "f-b");- assert_eq!(ids(rows(&out.shared_vaults[0].manifest, "Items")), vec!["i-a"]);- assert_eq!(ids(rows(&out.shared_vaults[1].manifest, "Items")), vec!["i-b"]);- assert!(rows(&out.manifest, "Items").is_empty());+ assert_eq!(out.rest().len(), 2);+ assert_eq!(out.rest()[0].manifest.manifest_id, "m-f-a");+ assert_eq!(out.rest()[1].manifest.manifest_id, "m-f-b");+ assert_eq!(ids(rows(&out.rest()[0].manifest, "Items")), vec!["i-a"]);+ assert_eq!(ids(rows(&out.rest()[1].manifest, "Items")), vec!["i-b"]);+ assert!(rows(&out.first().manifest, "Items").is_empty());}#[test]-fn split_rejects_nested_shared_folders() {+fn nested_manifests_each_keep_their_own_subtree() {+ // Nesting is no longer a special case: a folder inside another manifest's folder simply carries a+ // different stamp, and each manifest gets exactly the rows stamped for it. The inner manifest's+ // folder loses its ParentFolderId, since that parent lives in a namespace it cannot see.let tables = vec![table("Folders", vec![row(&[("Id", json!("f-outer")), ("ParentFolderId", serde_json::Value::Null)]),row(&[("Id", json!("f-inner")), ("ParentFolderId", json!("f-outer"))]),])];- let err = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-outer"), spec("f-inner")])).unwrap_err();- assert!(err.to_string().contains("nested"), "unexpected error: {err}");+ let tables = stamp_subtree(stamp_subtree(tables, "f-outer", "m-f-outer"), "f-inner", "m-f-inner");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-outer"), spec("f-inner")])).unwrap();+ assert_eq!(ids(rows(&out.rest()[0].manifest, "Folders")), vec!["f-outer"]);+ assert_eq!(ids(rows(&out.rest()[1].manifest, "Folders")), vec!["f-inner"]);+ assert_eq!(rows(&out.rest()[1].manifest, "Folders")[0]["ParentFolderId"], serde_json::Value::Null);}#[test]-fn split_rejects_duplicate_shared_folder_specs() {+fn split_rejects_duplicate_shared_manifest_specs() {let err = canonicalize_from_sqlite(input_with_shares(owner_tables(), vec![spec("f-shared"), spec("f-shared")])).unwrap_err();assert!(err.to_string().contains("duplicate"), "unexpected error: {err}");}#[test]fn split_of_deleted_folder_yields_empty_shared_manifest() {- // The shared folder was deleted locally: its manifest empties out (and would be pushed as such).+ // The anchor folder was deleted locally: its manifest empties out (and would be pushed as such).let tables = vec![table("Folders", vec![row(&[("Id", json!("f-personal")), ("ParentFolderId", serde_json::Value::Null)])]),table("Items", vec![row(&[("Id", json!("i-1")), ("FolderId", json!("f-personal"))])]),];let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-gone")])).unwrap();- assert_eq!(out.shared_vaults.len(), 1);- assert!(rows(&out.shared_vaults[0].manifest, "Folders").is_empty());- assert!(rows(&out.shared_vaults[0].manifest, "Items").is_empty());- assert_eq!(ids(rows(&out.manifest, "Items")), vec!["i-1"]);+ assert_eq!(out.rest().len(), 1);+ assert!(rows(&out.rest()[0].manifest, "Folders").is_empty());+ assert!(rows(&out.rest()[0].manifest, "Items").is_empty());+ assert_eq!(ids(rows(&out.first().manifest, "Items")), vec!["i-1"]);}#[test]@@ -285,56 +784,91 @@ fn split_includes_tombstoned_rows_in_subtree() {]),table("Items", vec![row(&[("Id", json!("i-del")), ("FolderId", json!("f-sub")), ("IsDeleted", json!(1))])]),];- let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();- let shared = &out.shared_vaults[0].manifest;+ let out = canonicalize_from_sqlite(input_with_shares(stamp_subtree(tables, "f-shared", "m-f-shared"), vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0].manifest;assert_eq!(ids(rows(shared, "Folders")), vec!["f-shared", "f-sub"]);assert_eq!(ids(rows(shared, "Items")), vec!["i-del"]);}#[test]-fn split_with_no_specs_matches_legacy_single_manifest_output() {+fn split_with_a_single_spec_drops_rows_of_manifests_it_does_not_carry() {+ /*+ * The vault still holds rows stamped for "m-f-shared", but this push declares only one manifest:+ * whoever wrote it no longer has that one. Those rows have no namespace to go to and are left out+ * rather than re-homed into the manifest that happens to be first.+ */let out = canonicalize_from_sqlite(input_with_shares(owner_tables(), vec![])).unwrap();- assert!(out.shared_vaults.is_empty());- assert_eq!(ids(rows(&out.manifest, "Items")), vec!["i-nofolder", "i-personal", "i-shared", "i-sub"]);- // And the serialized output omits the field entirely (wire-compat with pre-sharing clients).+ assert!(out.rest().is_empty());+ assert_eq!(ids(rows(&out.first().manifest, "Items")), vec!["i-nofolder", "i-personal"]);+ assert_eq!(ids(rows(&out.first().manifest, "Folders")), vec!["f-personal"]);+ // The rows hanging off a dropped item go with it rather than dangling.+ assert_eq!(ids(rows(&out.first().manifest, "FieldValues")), vec!["fv-personal"]);+ assert!(rows(&out.first().manifest, "TotpCodes").is_empty());++ // The serialized output carries the one manifest, with nothing flagging it and no folder anchor.let json = serde_json::to_value(&out).unwrap();- assert!(json.get("sharedVaults").is_none());- assert!(json["manifest"].get("sharedFolderId").is_none());+ assert_eq!(json["manifests"].as_array().unwrap().len(), 1);+ assert!(json["manifests"][0].get("isPersonal").is_none());+ assert!(json["manifests"][0]["manifest"].get("anchorFolderId").is_none());+ assert_eq!(json["manifests"][0]["manifest"]["manifestId"], json!(PERSONAL_M));}#[test]fn shared_manifests_validate_clean_after_split() {let out = canonicalize_owner();- let root_result = validate_manifest(&out.manifest);- assert!(root_result.ok, "root manifest invalid: {:?}", root_result.failed_rules);- let shared_result = validate_manifest(&out.shared_vaults[0].manifest);+ let personal_result = validate_manifest(&out.first().manifest);+ assert!(personal_result.ok, "personal manifest invalid: {:?}", personal_result.failed_rules);+ let shared_result = validate_manifest(&out.rest()[0].manifest);assert!(shared_result.ok, "shared manifest invalid: {:?}", shared_result.failed_rules);}#[test]-fn validate_rejects_shared_manifest_carrying_personal_tables() {- let mut manifest = canonicalize_owner().shared_vaults[0].manifest.clone();+fn validate_rejects_shared_manifest_carrying_out_of_manifest_tables() {+ // A NULL-scope (personal) key row in a shared manifest trips the key-scope rule…+ let mut manifest = canonicalize_owner().rest()[0].manifest.clone();manifest.tables.insert("EncryptionKeys".to_string(), vec![row(&[("Id", json!("ek-evil")), ("PrivateKey", json!("x"))])]);let result = validate_manifest(&manifest);assert!(!result.ok);- assert!(result.failed_rules.iter().any(|r| r == "shared-manifest-carries-personal-table"));+ assert!(result.failed_rules.iter().any(|r| r == "encryption-keys-scope-mismatch"));++ /*+ * …and a bucketed table (Settings) trips the bucketed-table rule, not the personal-table one: it is+ * rejected because it belongs in the manifest's own data bucket, not because settings could never+ * belong to a shared manifest at all.+ */+ let mut manifest = canonicalize_owner().rest()[0].manifest.clone();+ manifest.tables.insert("Settings".to_string(), vec![row(&[("Key", json!("theme")), ("Value", json!("evil"))])]);+ let result = validate_manifest(&manifest);+ assert!(!result.ok);+ assert!(result.failed_rules.iter().any(|r| r == "manifest-carries-bucketed-table"));+ assert!(!result.failed_rules.iter().any(|r| r == "manifest-carries-personal-table"));}#[test]fn split_regrafts_overflow_columns_onto_shared_rows() {// A newer writer added a column to a shared item; this client's schema couldn't hold it, so it// rode in the overflow carrier. On push it must re-graft and travel with the row into the- // shared manifest, not the root.+ // shared manifest, not the personal one.+ // Recorded under the identity the row had when the overflow was written — before the folder was+ // shared, so under the personal scope. The re-graft has to survive the row changing manifest.let overflow = CodecOverflow {- columns: [("Items".to_string(), [("i-shared".to_string(), row(&[("FutureCol", json!("keep-me"))]))].into_iter().collect())].into_iter().collect(),+ columns: [(+ "Items".to_string(),+ [(format!("{}\u{1f}i-shared", PERSONAL_M), row(&[("FutureCol", json!("keep-me"))]))].into_iter().collect(),+ )]+ .into_iter()+ .collect(),..Default::default()};let mut tables = owner_tables();tables.push(table(OVERFLOW_TABLE, overflow.to_table_records()));+ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();- let shared_item = rows(&out.shared_vaults[0].manifest, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap().clone();+ let shared_item = rows(&out.rest()[0].manifest, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap().clone();assert_eq!(shared_item["FutureCol"], json!("keep-me"));- assert!(rows(&out.manifest, "Items").iter().all(|r| !r.contains_key("FutureCol")));+ assert!(rows(&out.first().manifest, "Items").iter().all(|r| !r.contains_key("FutureCol")));}// ─────────────────────────────────────────────────────────────────────────────@@ -348,16 +882,10 @@ fn materialized_map(m: &MaterializedTables) -> HashMap<String, Vec<CodecRecord>>#[test]fn owner_split_then_combine_roundtrips_to_original_tables() {- // The core round-trip contract: canonicalize with shares, then materialize root+shared —+ // The core round-trip contract: canonicalize with shares, then materialize personal+shared// semantically identical to the original unified vault.let out = canonicalize_owner();- let re = materialize_as_sqlite(MaterializeInput {- manifest: out.manifest.clone(),- data_buckets: out.data_buckets.clone(),- schema_columns: None,- shared_manifests: out.shared_vaults.iter().map(|s| s.manifest.clone()).collect(),- })- .unwrap();+ let re = materialize_as_sqlite(materialize_input(out.first().manifest.clone(), out.rest().iter().map(|s| s.manifest.clone()).collect(), out.data_buckets.clone())).unwrap();let map = materialized_map(&re);assert_eq!(ids(&map["Items"]), vec!["i-nofolder", "i-personal", "i-shared", "i-sub"]);@@ -366,10 +894,30 @@ fn owner_split_then_combine_roundtrips_to_original_tables() {assert_eq!(ids(&map["Attachments"]), vec!["att-personal", "att-shared"]);assert_eq!(ids(&map["TotpCodes"]), vec!["totp-shared"]);assert_eq!(ids(&map["ItemTags"]), vec!["it-1", "it-2", "it-3"]);- // Reference-copied rows deduped back to one copy each.- assert_eq!(ids(&map["Tags"]), vec!["tag-both", "tag-shared-only", "tag-unused"]);- assert_eq!(ids(&map["FieldDefinitions"]), vec!["fd-1", "fd-unused"]);- assert_eq!(map["Logos"].len(), 2, "logo copies deduped by Source");+ /*+ * Tags and field definitions are manifest-scoped like the rest: the two the shared items carry+ * exist once in each manifest that uses them, side by side under `(ManifestId, Id)`. The base keeps+ * its own copies, including "tag-unused", which nothing references at all.+ */+ assert_eq!(ids(&map["Tags"]), vec!["tag-both", "tag-both", "tag-shared-only", "tag-shared-only", "tag-unused"]);+ let tag_scopes: HashSet<(&str, &str)> = map["Tags"]+ .iter()+ .map(|r| (r["ManifestId"].as_str().unwrap(), r["Id"].as_str().unwrap()))+ .collect();+ assert_eq!(tag_scopes.len(), 5, "each (scope, id) appears once");+ assert_eq!(ids(&map["FieldDefinitions"]), vec!["fd-1", "fd-1", "fd-unused"]);+ /*+ * Logos do NOT collapse back to one row per domain: the personal manifest's github row and the shared+ * manifest's own github row are separate rows, which is the point of scoping them. They coexist+ * under UNIQUE(ManifestId, Kind, Source), and every scope is internally unique. Three rows, not+ * four: netflix.com exists only in the shared scope, since the personal manifest holds no item using it.+ */+ assert_eq!(map["Logos"].len(), 3);+ let scoped: HashSet<(Option<&str>, &str)> = map["Logos"]+ .iter()+ .map(|r| (r.get("ManifestId").and_then(|v| v.as_str()), r["Source"].as_str().unwrap()))+ .collect();+ assert_eq!(scoped.len(), 3, "each (scope, source) appears once");assert_eq!(map["EncryptionKeys"].len(), 1);assert_eq!(map["Settings"].len(), 1);}@@ -378,13 +926,7 @@ fn owner_split_then_combine_roundtrips_to_original_tables() {fn owner_roundtrip_is_stable_across_a_second_split() {// Split -> combine -> split again must reproduce the same manifests (no oscillation).let first = canonicalize_owner();- let re = materialize_as_sqlite(MaterializeInput {- manifest: first.manifest.clone(),- data_buckets: first.data_buckets.clone(),- schema_columns: None,- shared_manifests: first.shared_vaults.iter().map(|s| s.manifest.clone()).collect(),- })- .unwrap();+ let re = materialize_as_sqlite(materialize_input(first.first().manifest.clone(), first.rest().iter().map(|s| s.manifest.clone()).collect(), first.data_buckets.clone())).unwrap();// Simulate the platform read-back: blob refs stay as-is (the platform rebinds bytes, and a// re-canonicalize would read real bytes; for stability we compare table row id sets).@@ -395,17 +937,17 @@ fn owner_roundtrip_is_stable_across_a_second_split() {.unwrap();for name in ["Folders", "Items", "FieldValues", "Attachments", "TotpCodes", "ItemTags", "Tags", "FieldDefinitions", "Logos"] {- assert_eq!(ids(rows(&second.manifest, name)), ids(rows(&first.manifest, name)), "root {name} drifted");+ assert_eq!(ids(rows(&second.first().manifest, name)), ids(rows(&first.first().manifest, name)), "personal {name} drifted");assert_eq!(- ids(rows(&second.shared_vaults[0].manifest, name)),- ids(rows(&first.shared_vaults[0].manifest, name)),+ ids(rows(&second.rest()[0].manifest, name)),+ ids(rows(&first.rest()[0].manifest, name)),"shared {name} drifted");}}-/// A recipient's own vault (no knowledge of the share) canonicalized to a root manifest.-fn recipient_root_manifest() -> (Manifest, Vec<DataBucket>) {+/// A recipient's own vault (no knowledge of the share) canonicalized to a personal manifest.+fn recipient_personal_manifest() -> (Manifest, Vec<DataBucket>) {let out = canonicalize_from_sqlite(input_with_shares(vec![table("Folders", vec![row(&[("Id", json!("f-mine")), ("ParentFolderId", serde_json::Value::Null)])]),@@ -416,31 +958,42 @@ fn recipient_root_manifest() -> (Manifest, Vec<DataBucket>) {vec![],)).unwrap();- (out.manifest, out.data_buckets)+ (out.first().manifest.clone(), out.data_buckets.clone())}#[test]-fn recipient_combine_materializes_shared_folder_into_their_vault() {+fn recipient_combine_materializes_shared_manifest_into_their_vault() {let owner = canonicalize_owner();- let (recipient_manifest, buckets) = recipient_root_manifest();- let re = materialize_as_sqlite(MaterializeInput {- manifest: recipient_manifest,- data_buckets: buckets,- schema_columns: None,- shared_manifests: vec![owner.shared_vaults[0].manifest.clone()],- })- .unwrap();+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![owner.rest()[0].manifest.clone()], buckets)).unwrap();let map = materialized_map(&re);// Recipient sees their own rows plus the shared subtree.assert_eq!(ids(&map["Folders"]), vec!["f-mine", "f-shared", "f-sub"]);assert_eq!(ids(&map["Items"]), vec!["i-mine", "i-shared", "i-sub"]);assert_eq!(ids(&map["FieldValues"]), vec!["fv-shared", "fv-sub"]);- // Owner's netflix.com logo collides with the recipient's by Source: deduped to one row, and- // every Items.LogoId resolves to a surviving logo.- let sources: Vec<&str> = map["Logos"].iter().filter_map(|r| r.get("Source").and_then(|v| v.as_str())).collect();- assert_eq!(sources.iter().filter(|s| **s == "netflix.com").count(), 1);- let logo_ids: std::collections::HashSet<&str> = map["Logos"].iter().filter_map(|r| r.get("Id").and_then(|v| v.as_str())).collect();+ /*+ * The owner and the recipient both hold a netflix.com logo, legitimately different images (fetched+ * at different times, or one of them uploaded by hand). They must BOTH survive, in their own+ * scopes: the recipient keeps their personal logo and the shared manifest keeps the owner's. This is+ * the case the old cross-manifest Source dedup collapsed, silently repointing the recipient's own+ * item at a row authored inside someone else's manifest.+ */+ let netflix: Vec<Option<&str>> = map["Logos"]+ .iter()+ .filter(|r| r["Source"] == json!("netflix.com"))+ .map(|r| r.get("ManifestId").and_then(|v| v.as_str()))+ .collect();+ assert_eq!(netflix.len(), 2, "one netflix logo per scope");+ assert!(netflix.contains(&Some(PERSONAL_M)), "recipient's personal logo survives");+ assert!(netflix.contains(&Some("m-f-shared")), "shared manifest's logo survives");++ // The recipient's own item still points at their own row, untouched by the share.+ let mine = map["Items"].iter().find(|r| r["Id"] == json!("i-mine")).unwrap();+ assert_eq!(mine["LogoId"], json!(logo_id(PERSONAL_M, "netflix.com")));++ // ...and every reference resolves to a logo that is actually present.+ let logo_ids: HashSet<&str> = map["Logos"].iter().filter_map(|r| r.get("Id").and_then(|v| v.as_str())).collect();for item in &map["Items"] {if let Some(logo_id) = item.get("LogoId").and_then(|v| v.as_str()) {assert!(logo_ids.contains(logo_id), "Item {:?} points at missing logo {logo_id}", item.get("Id"));@@ -451,18 +1004,12 @@ fn recipient_combine_materializes_shared_folder_into_their_vault() {}#[test]-fn recipient_roundtrip_reproduces_shared_manifest_without_leaking_into_root() {+fn recipient_roundtrip_reproduces_shared_manifest_without_leaking_into_personal() {// Recipient pulls (combine), then pushes (split): the shared manifest must contain exactly the- // shared rows, and none of them may leak into the recipient's root manifest.+ // shared rows, and none of them may leak into the recipient's personal manifest.let owner = canonicalize_owner();- let (recipient_manifest, buckets) = recipient_root_manifest();- let re = materialize_as_sqlite(MaterializeInput {- manifest: recipient_manifest,- data_buckets: buckets,- schema_columns: None,- shared_manifests: vec![owner.shared_vaults[0].manifest.clone()],- })- .unwrap();+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![owner.rest()[0].manifest.clone()], buckets)).unwrap();let pushed = canonicalize_from_sqlite(input_with_shares(re.tables.iter().map(|t| table(&t.name, t.records.clone())).collect(),@@ -471,38 +1018,251 @@ fn recipient_roundtrip_reproduces_shared_manifest_without_leaking_into_root() {.unwrap();// Shared manifest reproduced.- let shared = &pushed.shared_vaults[0].manifest;+ let shared = &pushed.rest()[0].manifest;assert_eq!(ids(rows(shared, "Items")), vec!["i-shared", "i-sub"]);assert_eq!(ids(rows(shared, "Folders")), vec!["f-shared", "f-sub"]);assert_eq!(ids(rows(shared, "FieldValues")), vec!["fv-shared", "fv-sub"]);assert_eq!(ids(rows(shared, "Tags")), vec!["tag-both", "tag-shared-only"]);- // Recipient's root holds only their own rows — no shared items/folders leaked in.- assert_eq!(ids(rows(&pushed.manifest, "Items")), vec!["i-mine"]);- assert_eq!(ids(rows(&pushed.manifest, "Folders")), vec!["f-mine"]);- assert!(rows(&pushed.manifest, "FieldValues").is_empty());- // Owner's tags are referenced only by shared items, so they stay out of the recipient's root.- assert!(rows(&pushed.manifest, "Tags").is_empty());+ // Recipient's personal manifest holds only their own rows, no shared items/folders leaked in.+ assert_eq!(ids(rows(&pushed.first().manifest, "Items")), vec!["i-mine"]);+ assert_eq!(ids(rows(&pushed.first().manifest, "Folders")), vec!["f-mine"]);+ assert!(rows(&pushed.first().manifest, "FieldValues").is_empty());+ // Owner's tags are referenced only by shared items, so they stay out of the recipient's personal manifest.+ assert!(rows(&pushed.first().manifest, "Tags").is_empty());++ /*+ * The shared manifest the recipient pushes back carries the same logo rows they pulled, their own+ * netflix.com logo never bleeds into it. This is the property that keeps a member's routine push+ * from rewriting a folder they merely participate in (burning a revision, and racing the owner's+ * concurrent write on the all-or-nothing gate). Ids and scopes only: this round trip never rebinds+ * blob bytes, so FileData reads as null on the way back out.+ */+ assert_eq!(ids(rows(shared, "Logos")), ids(rows(&owner.rest()[0].manifest, "Logos")));+ assert_eq!(logo_sources(shared), logo_sources(&owner.rest()[0].manifest));+ assert_eq!(logo_row(shared, "netflix.com")["ManifestId"], json!("m-f-shared"));++ // And the recipient's personal manifest keeps its own row for that same domain.+ assert_eq!(logo_row(&pushed.first().manifest, "netflix.com")["ManifestId"], json!(PERSONAL_M));+ assert_ne!(logo_row(&pushed.first().manifest, "netflix.com")["Id"], logo_row(shared, "netflix.com")["Id"]);}#[test]-fn combine_root_wins_on_primary_key_collision() {- let mut root = canonicalize_from_sqlite(input_with_shares(- vec![table("Items", vec![row(&[("Id", json!("i-dup")), ("Name", json!("root-version")), ("FolderId", serde_json::Value::Null)])])],+fn item_moved_into_shared_manifest_brings_its_logo_along() {+ // A personal item is dragged into the shared manifest. Its logo lives in the personal scope, so the+ // partition must clone it under the folder's scope, otherwise the reference would dangle and the+ // logo would be nulled away on the way out.+ let mut tables = owner_tables();+ let items = tables.iter_mut().find(|t| t.name == "Items").unwrap();+ items.records.iter_mut().find(|r| r["Id"] == json!("i-personal")).unwrap().insert("FolderId".to_string(), json!("f-shared"));++ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0].manifest;++ let moved = rows(shared, "Items").iter().find(|r| r["Id"] == json!("i-personal")).unwrap();+ assert_eq!(moved["LogoId"], json!(logo_id("m-f-shared", "github.com")), "logo followed the item into the folder");+ assert!(logo_row(shared, "github.com")["FileData"]["__blobRef"].is_string(), "and it carries real bytes");+}++#[test]+fn item_moved_into_shared_manifest_adopts_its_existing_logo() {+ // Same move, but the folder already shows a logo for that domain. The item adopts it rather than+ // dragging a second row onto the same (scope, domain), the folder's members keep seeing one logo.+ let mut tables = owner_tables();+ let items = tables.iter_mut().find(|t| t.name == "Items").unwrap();+ // i-personal (github.com) joins the shared manifest, where i-shared already uses github.com.+ items.records.iter_mut().find(|r| r["Id"] == json!("i-personal")).unwrap().insert("FolderId".to_string(), json!("f-shared"));++ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0].manifest;++ assert_eq!(logo_sources(shared), vec!["github.com", "netflix.com"], "no duplicate github row");+ let shared_github = logo_row(shared, "github.com")["Id"].clone();+ for item_id in ["i-personal", "i-shared"] {+ let item = rows(shared, "Items").iter().find(|r| r["Id"] == json!(item_id)).unwrap();+ assert_eq!(item["LogoId"], shared_github);+ }+}++#[test]+fn combine_scopes_a_legacy_shared_manifest_before_it_can_collide() {+ // A shared manifest whose ROWS predate manifest-id scoping: its logos were minted at random and+ // carry no scope stamp. Materializing it next to the recipient's own rows must not collide on+ // UNIQUE(ManifestId, Kind, Source) — combine normalizes every row to the manifest's own id.+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let legacy_shared = Manifest {+ schema_version: SCHEMA_VERSION,+ manifest_salt: SALT_SHARED.to_string(),+ canonicalized_at: "2026-01-01T00:00:00.000Z".to_string(),+ manifest_id: "m-legacy".to_string(),+ name: Some("Legacy share".to_string()),+ tables: HashMap::from([+ ("Folders".to_string(), vec![row(&[("Id", json!("f-legacy")), ("ParentFolderId", serde_json::Value::Null)])]),+ ("Items".to_string(), vec![row(&[("Id", json!("i-legacy")), ("FolderId", json!("f-legacy")), ("LogoId", json!("their-random-id"))])]),+ ("Logos".to_string(), vec![row(&[("Id", json!("their-random-id")), ("Source", json!("netflix.com")), ("FileData", json!({ "__b64": b64(&[0x99]) }))])]),+ ]),+ extra: HashMap::new(),+ };++ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![legacy_shared], buckets)).unwrap();+ let map = materialized_map(&re);++ let scoped: Vec<(Option<&str>, &str)> = map["Logos"]+ .iter()+ .map(|r| (r.get("ManifestId").and_then(|v| v.as_str()), r["Source"].as_str().unwrap()))+ .collect();+ assert_eq!(scoped.len(), 2);+ assert!(scoped.contains(&(Some(PERSONAL_M), "netflix.com")), "recipient's own row keeps the personal scope");+ assert!(scoped.contains(&(Some("m-legacy"), "netflix.com")), "legacy shared row was pulled into its manifest's scope");++ // The legacy manifest's item follows its row into the new scope.+ let legacy_item = map["Items"].iter().find(|r| r["Id"] == json!("i-legacy")).unwrap();+ assert_eq!(legacy_item["LogoId"], json!(logo_id("m-legacy", "netflix.com")));+}++#[test]+fn combine_keeps_both_rows_when_two_manifests_share_a_primary_key() {+ // Ids are client-minted, so a shared manifest may legitimately carry a row whose Id equals one of+ // the reader's own. Because rows are keyed by (ManifestId, Id) — the primary key the local schema+ // declares — both survive in their own namespace and neither shadows the other.+ let mut personal = canonicalize_from_sqlite(input_with_shares(+ vec![table("Items", vec![row(&[("Id", json!("i-dup")), ("Name", json!("personal-version")), ("FolderId", serde_json::Value::Null)])])],vec![],)).unwrap()- .manifest;- root.tables.insert("Folders".to_string(), vec![]);-- let mut shared = root.clone();- shared.shared_folder_id = Some("f-x".to_string());+ .first()+ .manifest+ .clone();+ personal.tables.insert("Folders".to_string(), vec![]);++ let mut shared = personal.clone();+ shared.manifest_id = "m-x".to_string();+ shared.name = Some("Renamed".to_string());shared.tables.insert("Items".to_string(), vec![row(&[("Id", json!("i-dup")), ("Name", json!("shared-version")), ("FolderId", serde_json::Value::Null)])]);- let re = materialize_as_sqlite(MaterializeInput { manifest: root, data_buckets: vec![], schema_columns: None, shared_manifests: vec![shared] }).unwrap();+ let re = materialize_as_sqlite(materialize_input(personal, vec![shared], vec![])).unwrap();let map = materialized_map(&re);- assert_eq!(map["Items"].len(), 1);- assert_eq!(map["Items"][0]["Name"], json!("root-version"));++ let by_scope: Vec<(&str, &str)> = map["Items"]+ .iter()+ .map(|r| (r["ManifestId"].as_str().unwrap(), r["Name"].as_str().unwrap()))+ .collect();+ assert_eq!(by_scope.len(), 2, "one row per manifest, not one row total");+ assert!(by_scope.contains(&(PERSONAL_M, "personal-version")));+ assert!(by_scope.contains(&("m-x", "shared-version")));+}++#[test]+fn combine_keeps_each_manifests_own_copy_of_a_shared_tag() {+ /*+ * Two manifests carrying the same tag id are two rows, not one: `Tags` is keyed by+ * `(ManifestId, Id)` like everything else, so neither copy can shadow the other and a member+ * renaming their copy never overwrites anyone else's. Presenting them as one tag (by name, say)+ * is the client's call.+ */+ let mut base = canonicalize_from_sqlite(input_with_shares(+ vec![table("Tags", vec![row(&[("Id", json!("t-1")), ("Name", json!("base-copy"))])])],+ vec![],+ ))+ .unwrap()+ .first()+ .manifest+ .clone();+ base.tables.insert("Folders".to_string(), vec![]);++ let mut shared = base.clone();+ shared.manifest_id = "m-x".to_string();+ shared.tables.insert("Tags".to_string(), vec![row(&[("Id", json!("t-1")), ("Name", json!("shared-copy"))])]);++ let re = materialize_as_sqlite(materialize_input(base, vec![shared], vec![])).unwrap();+ let map = materialized_map(&re);+ assert_eq!(map["Tags"].len(), 2, "one row per manifest");+ let by_scope: HashSet<(&str, &str)> = map["Tags"]+ .iter()+ .map(|r| (r["ManifestId"].as_str().unwrap(), r["Name"].as_str().unwrap()))+ .collect();+ assert!(by_scope.contains(&(PERSONAL_M, "base-copy")));+ assert!(by_scope.contains(&("m-x", "shared-copy")));+}++#[test]+fn split_stamps_item_scoped_rows_with_the_manifest_their_item_joined() {+ // A child row's foreign key is (ManifestId, ItemId), so it has to carry the same stamp as the item+ // it followed — otherwise it points at a row in a namespace it isn't in.+ let tables = stamp_subtree(owner_tables(), "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();++ for name in ["FieldValues", "TotpCodes", "Attachments", "ItemTags"] {+ let shared_rows = rows(&out.rest()[0].manifest, name);+ assert!(!shared_rows.is_empty(), "{name} should have rows in the shared manifest");+ assert!(shared_rows.iter().all(|r| r["ManifestId"] == json!("m-f-shared")), "{name} rows carry the shared manifest's stamp");+ assert!(rows(&out.first().manifest, name).iter().all(|r| r["ManifestId"] == json!(PERSONAL_M)), "{name} rows left behind carry the personal manifest's");+ }+}++#[test]+fn combine_drops_child_rows_naming_an_item_the_manifest_does_not_hold() {+ // A manifest may only describe its own items. One carrying a FieldValue for an item in the+ // reader's personal scope is either stale or hand-crafted to graft a field onto it; the composite+ // foreign key rejects it either way, so combine drops it rather than failing the whole insert.+ let mut personal = canonicalize_from_sqlite(input_with_shares(+ vec![+ table("Items", vec![row(&[("Id", json!("i-mine")), ("Name", json!("mine")), ("FolderId", serde_json::Value::Null)])]),+ table("FieldValues", vec![row(&[("Id", json!("fv-mine")), ("ItemId", json!("i-mine")), ("FieldKey", json!("username")), ("Value", json!("me"))])]),+ ],+ vec![],+ ))+ .unwrap()+ .first()+ .manifest+ .clone();+ personal.tables.insert("Folders".to_string(), vec![]);++ let mut hostile = personal.clone();+ hostile.manifest_id = "m-x".to_string();+ hostile.tables.insert("Items".to_string(), vec![]);+ hostile.tables.insert(+ "FieldValues".to_string(),+ vec![row(&[("Id", json!("fv-injected")), ("ItemId", json!("i-mine")), ("FieldKey", json!("password")), ("Value", json!("stolen"))])],+ );++ let re = materialize_as_sqlite(materialize_input(personal, vec![hostile], vec![])).unwrap();+ let map = materialized_map(&re);+ assert_eq!(ids(&map["FieldValues"]), vec!["fv-mine"], "the foreign row never reaches the reader's item");+}++#[test]+fn combine_nulls_an_item_folder_that_only_resolves_in_another_manifest() {+ // Folder ids are per-manifest now: a folder with the right Id in the wrong namespace is not this+ // item's folder, and leaving the reference would fail the platform's foreign_key_check.+ let mut personal = canonicalize_from_sqlite(input_with_shares(+ vec![+ table("Items", vec![row(&[("Id", json!("i-orphan")), ("Name", json!("mine")), ("FolderId", json!("f-elsewhere"))])]),+ table("Folders", vec![]),+ ],+ vec![],+ ))+ .unwrap()+ .first()+ .manifest+ .clone();+ personal.tables.insert("Folders".to_string(), vec![]);++ let mut shared = personal.clone();+ shared.manifest_id = "m-x".to_string();+ shared.tables.insert("Items".to_string(), vec![]);+ shared.tables.insert("Folders".to_string(), vec![row(&[("Id", json!("f-elsewhere")), ("Name", json!("Theirs")), ("ParentFolderId", serde_json::Value::Null)])]);++ let re = materialize_as_sqlite(materialize_input(personal, vec![shared], vec![])).unwrap();+ let map = materialized_map(&re);+ let item = map["Items"].iter().find(|r| r["Id"] == json!("i-orphan")).unwrap();+ assert_eq!(item["FolderId"], serde_json::Value::Null);}#[test]@@ -510,13 +1270,13 @@ fn combine_strips_personal_and_bookkeeping_tables_from_shared_manifests() {// A malicious/buggy shared manifest tries to inject key material, settings, and an overflow// carrier into the recipient's vault: all must be dropped.let owner = canonicalize_owner();- let mut evil = owner.shared_vaults[0].manifest.clone();+ let mut evil = owner.rest()[0].manifest.clone();evil.tables.insert("EncryptionKeys".to_string(), vec![row(&[("Id", json!("ek-evil")), ("PrivateKey", json!("stolen"))])]);evil.tables.insert("Settings".to_string(), vec![row(&[("Key", json!("theme")), ("Value", json!("evil"))])]);evil.tables.insert(OVERFLOW_TABLE.to_string(), vec![row(&[("Id", json!("x")), ("Data", json!("{}"))])]);- let (recipient_manifest, buckets) = recipient_root_manifest();- let re = materialize_as_sqlite(MaterializeInput { manifest: recipient_manifest, data_buckets: buckets, schema_columns: None, shared_manifests: vec![evil] }).unwrap();+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![evil], buckets)).unwrap();let map = materialized_map(&re);assert_eq!(ids(&map["EncryptionKeys"]), vec!["ek-r"], "injected key row dropped");assert!(!map.contains_key("Settings"), "injected settings dropped");@@ -527,17 +1287,17 @@ fn combine_strips_personal_and_bookkeeping_tables_from_shared_manifests() {fn combine_nulls_dangling_parent_folder_references() {// A shared manifest written before parent normalization (or with a partial grant set) may carry// a ParentFolderId pointing outside every manifest this user holds: null it so FK checks pass.- let mut shared = canonicalize_owner().shared_vaults[0].manifest.clone();+ let mut shared = canonicalize_owner().rest()[0].manifest.clone();for folder in shared.tables.get_mut("Folders").unwrap() {if folder["Id"] == json!("f-shared") {folder.insert("ParentFolderId".to_string(), json!("f-owner-personal-folder"));}}- let (recipient_manifest, buckets) = recipient_root_manifest();- let re = materialize_as_sqlite(MaterializeInput { manifest: recipient_manifest, data_buckets: buckets, schema_columns: None, shared_manifests: vec![shared] }).unwrap();+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![shared], buckets)).unwrap();let map = materialized_map(&re);- let shared_root = map["Folders"].iter().find(|r| r["Id"] == json!("f-shared")).unwrap();- assert_eq!(shared_root["ParentFolderId"], serde_json::Value::Null);+ let shared_anchor = map["Folders"].iter().find(|r| r["Id"] == json!("f-shared")).unwrap();+ assert_eq!(shared_anchor["ParentFolderId"], serde_json::Value::Null);// Intact parents are untouched.let sub = map["Folders"].iter().find(|r| r["Id"] == json!("f-sub")).unwrap();assert_eq!(sub["ParentFolderId"], json!("f-shared"));@@ -547,7 +1307,7 @@ fn combine_nulls_dangling_parent_folder_references() {fn combine_applies_schema_overflow_to_shared_rows_too() {// An old client materializing a share written by a newer client: unknown columns on shared rows// must land in overflow, not crash the insert.- let mut shared = canonicalize_owner().shared_vaults[0].manifest.clone();+ let mut shared = canonicalize_owner().rest()[0].manifest.clone();shared.tables.get_mut("Items").unwrap()[0].insert("FutureCol".to_string(), json!("v"));let schema: HashMap<String, Vec<String>> = [@@ -558,8 +1318,8 @@ fn combine_applies_schema_overflow_to_shared_rows_too() {.into_iter().collect();- let (recipient_manifest, buckets) = recipient_root_manifest();- let re = materialize_as_sqlite(MaterializeInput { manifest: recipient_manifest, data_buckets: buckets, schema_columns: Some(schema), shared_manifests: vec![shared] }).unwrap();+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(MaterializeInput::new(recipient_manifest, vec![shared], buckets, schema)).unwrap();let map = materialized_map(&re);assert!(map["Items"].iter().all(|r| !r.contains_key("FutureCol")));let overflowed: Vec<&String> = re.overflow.columns.get("Items").map(|m| m.keys().collect()).unwrap_or_default();@@ -567,22 +1327,19 @@ fn combine_applies_schema_overflow_to_shared_rows_too() {}#[test]-fn combine_with_no_shared_manifests_is_unchanged_legacy_behavior() {+fn combine_of_a_single_manifest_carries_only_its_own_rows() {+ // Canonicalizing without listing the shared manifest leaves its rows out of the push entirely,+ // so the vault materialized back holds only what the one manifest carries.let out = canonicalize_from_sqlite(input_with_shares(owner_tables(), vec![])).unwrap();- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None, shared_manifests: vec![] }).unwrap();+ let re = materialize_as_sqlite(materialize_input(out.first().manifest.clone(), vec![], out.data_buckets.clone())).unwrap();let map = materialized_map(&re);- assert_eq!(ids(&map["Items"]), vec!["i-nofolder", "i-personal", "i-shared", "i-sub"]);+ assert_eq!(ids(&map["Items"]), vec!["i-nofolder", "i-personal"]);}// ─────────────────────────────────────────────────────────────────────────────// Primary encryption key extraction// ─────────────────────────────────────────────────────────────────────────────-/// The `EncryptionKeys` data bucket produced by a canonicalize run (panics if the codec didn't emit one).-fn encryption_keys_bucket(out: &CanonicalizedVault) -> &DataBucket {- out.data_buckets.iter().find(|b| b.category == "EncryptionKeys").expect("EncryptionKeys bucket emitted")-}-#[test]fn extract_encryption_key_for_public_key_picks_matching_row_over_primary() {// A rotated vault: the old (non-primary) key must still be selectable so a grant wrapped for it unwraps.@@ -594,12 +1351,15 @@ fn extract_encryption_key_for_public_key_picks_matching_row_over_primary() {vec![],)).unwrap();- let key = extract_encryption_key_for_public_key_from_bucket(encryption_keys_bucket(&out), "pub-old").expect("old key present");+ let key = extract_encryption_key_for_public_key(&out.first().manifest, "pub-old").expect("old key present");assert_eq!(key["Id"], json!("ek-old"));assert_eq!(key["PrivateKey"], json!("priv-old"));- let cur = extract_encryption_key_for_public_key_from_bucket(encryption_keys_bucket(&out), "pub-cur").expect("current key present");+ let cur = extract_encryption_key_for_public_key(&out.first().manifest, "pub-cur").expect("current key present");assert_eq!(cur["Id"], json!("ek-cur"));++ // The manifest's active key is the primary row of its own scope.+ assert_eq!(active_encryption_key(&out.first().manifest).expect("primary key present")["Id"], json!("ek-cur"));}#[test]@@ -612,20 +1372,20 @@ fn extract_encryption_key_for_public_key_skips_deleted_and_returns_none_on_miss()).unwrap();// A deleted row is never returned even when its public key matches.- assert!(extract_encryption_key_for_public_key_from_bucket(encryption_keys_bucket(&out), "pub-del").is_none());+ assert!(extract_encryption_key_for_public_key(&out.first().manifest, "pub-del").is_none());// An unknown public key yields nothing.- assert!(extract_encryption_key_for_public_key_from_bucket(encryption_keys_bucket(&out), "pub-unknown").is_none());+ assert!(extract_encryption_key_for_public_key(&out.first().manifest, "pub-unknown").is_none());}#[test]-fn extract_encryption_key_for_public_key_from_bucket_json_sibling_roundtrips() {+fn extract_encryption_key_for_public_key_json_sibling_roundtrips() {let out = canonicalize_from_sqlite(input_with_shares(owner_tables(), vec![])).unwrap();- let bucket_json = serde_json::to_string(encryption_keys_bucket(&out)).unwrap();- let key_json = extract_encryption_key_for_public_key_from_bucket_json(&bucket_json, "pub").unwrap();+ let manifest_json = serde_json::to_string(&out.first().manifest).unwrap();+ let key_json = extract_encryption_key_for_public_key_json(&manifest_json, "pub").unwrap();let key: serde_json::Value = serde_json::from_str(&key_json).unwrap();assert_eq!(key["PrivateKey"], json!("priv"));- let miss = extract_encryption_key_for_public_key_from_bucket_json(&bucket_json, "nope").unwrap();+ let miss = extract_encryption_key_for_public_key_json(&manifest_json, "nope").unwrap();assert_eq!(miss, "null");}@@ -634,30 +1394,43 @@ fn extract_encryption_key_for_public_key_from_bucket_json_sibling_roundtrips() {// ─────────────────────────────────────────────────────────────────────────────#[test]-fn shared_folder_specs_deserialize_from_camel_case_json() {+fn manifest_specs_deserialize_from_camel_case_json() {+ // Every manifest is described the same way on the wire — id, salt, optional name — with nothing+ // marking one of them as special. A manifest carries a name, never a folder anchor.let input_json = json!({"tables": [{ "name": "Items", "records": [] }],- "userSalt": SALT_ROOT,- "migrationId": "m","canonicalizedAt": "2026-01-01T00:00:00.000Z",- "sharedFolders": [{ "folderId": "f-1", "userSalt": SALT_SHARED }]+ "manifests": [+ { "manifestId": PERSONAL_M, "manifestSalt": SALT_PERSONAL },+ { "manifestId": "m-1", "manifestSalt": SALT_SHARED, "name": "Team", "anchorFolderId": "f-1" }+ ]}).to_string();let out_json = canonicalize_from_sqlite_json(&input_json).unwrap();let out: CanonicalizedVault = serde_json::from_str(&out_json).unwrap();- assert_eq!(out.shared_vaults.len(), 1);- assert_eq!(out.shared_vaults[0].folder_id, "f-1");+ assert_eq!(out.rest().len(), 1);+ assert_eq!(out.rest()[0].manifest.manifest_id, "m-1");+ assert_eq!(out.rest()[0].manifest.name.as_deref(), Some("Team"));++ // One entry per spec, in spec order, all shaped alike; the buckets sit beside them, not inside one.let value: serde_json::Value = serde_json::from_str(&out_json).unwrap();- assert_eq!(value["sharedVaults"][0]["manifest"]["sharedFolderId"], json!("f-1"));+ assert_eq!(value["manifests"][0]["manifest"]["manifestId"], json!(PERSONAL_M));+ assert_eq!(value["manifests"][1]["manifest"]["manifestId"], json!("m-1"));+ assert_eq!(value["manifests"][1]["manifest"]["name"], json!("Team"));+ assert!(value["manifests"].as_array().unwrap().iter().all(|m| m.get("isPersonal").is_none()), "no manifest is flagged");+ assert!(value["dataBuckets"].is_array(), "buckets belong to the vault");+ assert!(value["manifests"][0].get("dataBuckets").is_none());+ assert!(value["manifests"][1]["manifest"].get("anchorFolderId").is_none(), "folder anchoring is a client concern, never persisted");}#[test]-fn materialize_input_accepts_shared_manifests_from_json() {+fn materialize_input_accepts_every_manifest_in_one_list_from_json() {let owner = canonicalize_owner();+ let schema = fitting_schema([&owner.first().manifest, &owner.rest()[0].manifest], &owner.data_buckets.clone());let input_json = json!({- "manifest": owner.manifest,- "dataBuckets": owner.data_buckets,- "sharedManifests": [owner.shared_vaults[0].manifest]+ "manifests": [owner.first().manifest, owner.rest()[0].manifest],+ "dataBuckets": owner.data_buckets.clone(),+ "schemaColumns": schema,}).to_string();let out_json = materialize_as_sqlite_json(&input_json).unwrap();@@ -665,3 +1438,806 @@ fn materialize_input_accepts_shared_manifests_from_json() {let items = out.tables.iter().find(|t| t.name == "Items").unwrap();assert_eq!(items.records.len(), 4);}++/// A vault is made of manifests; with none there is nothing to combine into and no data-model version+/// to report, so materialize refuses rather than emitting an empty vault.+#[test]+fn materialize_rejects_an_empty_manifest_list() {+ let empty = materialize_as_sqlite(materialize_manifests(vec![], vec![]));+ assert!(empty.unwrap_err().to_string().contains("no manifests"));+}++/// Nothing marks a manifest as the caller's own: the list order says it. The first entry is the base+/// the others combine into, and the bookkeeping rows record every manifest alike — id and name, no+/// claim about which one is whose. Nothing about the base becomes a vault-wide fact either: the base+/// contributes no schema/version claim the other manifests are then held to.+#[test]+fn materialize_treats_the_first_manifest_as_the_base() {+ let owner = canonicalize_owner();+ let shared = owner.rest()[0].manifest.clone();+ let own = owner.first().manifest.clone();++ let re = materialize_as_sqlite(materialize_manifests(vec![own, shared], owner.data_buckets.clone())).unwrap();++ // Nothing version-shaped travels with the manifests; the platform's own schema decides that alone.+ let json = serde_json::to_string(&re).unwrap();+ assert!(!json.contains("migration"), "materialize reports no migration id: the schema the platform applies is the only one there is");++ let manifests = re.tables.iter().find(|t| t.name == "Manifests").unwrap();+ assert_eq!(manifests.records.len(), 2);+ assert_eq!(manifests.records[0]["Id"], json!(PERSONAL_M), "bookkeeping rows follow the input order");+ assert!(manifests.records.iter().all(|r| !r.contains_key("IsPersonal")), "no manifest is flagged as the vault's own");+}++/// Rebind every `{ "__blobRef": hash }` cell to the inline bytes the platform inserts into SQLite,+/// what a real pull does before the next read hands the rows back to canonicalize. Without this a+/// round-trip test never exercises the blob path (an unresolved ref canonicalizes to NULL).+fn rebind_blobs(tables: &mut [CodecTableData], blobs: &HashMap<String, BlobEntry>) {+ for table in tables.iter_mut() {+ for record in table.records.iter_mut() {+ let refs: Vec<(String, String)> = record+ .iter()+ .filter_map(|(col, value)| Some((col.clone(), value.get("__blobRef")?.as_str()?.to_string())))+ .collect();+ for (col, hash) in refs {+ let bytes = blobs.get(&hash).map(|b| b.bytes_base64.clone()).unwrap_or_default();+ record.insert(col, json!({ "__b64": bytes }));+ }+ }+ }+}++#[test]+fn member_push_preserves_the_shared_logo_bytes() {+ // Full member round trip WITH blob bytes bound, as a real pull does: owner shares a folder, the+ // member materializes it, then pushes back an unrelated change. The shared manifest they push must+ // still carry the logo, row, scope, and bytes.+ let owner = canonicalize_owner();+ let owner_shared = &owner.rest()[0];+ let (recipient_manifest, buckets) = recipient_personal_manifest();++ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![owner_shared.manifest.clone()], buckets)).unwrap();++ // The member's SQLite: rows with real bytes in the blob columns.+ let mut member_tables: Vec<CodecTableData> = re.tables.iter().map(|t| table(&t.name, t.records.clone())).collect();+ let mut all_blobs = owner.first().blobs.clone();+ all_blobs.extend(owner_shared.blobs.clone());+ rebind_blobs(&mut member_tables, &all_blobs);++ let pushed = canonicalize_from_sqlite(input_with_shares(member_tables, vec![spec("f-shared")])).unwrap();+ let pushed_shared = &pushed.rest()[0];+ let logo = logo_row(&pushed_shared.manifest, "github.com");++ let blob_ref = logo["FileData"]["__blobRef"].as_str().expect("member's push keeps the logo's bytes as a blob ref");+ let entry = pushed_shared.blobs.get(blob_ref).expect("and registers those bytes in the shared manifest's blob map");+ assert_eq!(entry.bytes_base64, b64(&[0xAA, 0xBB]), "the bytes are the owner's original logo");++ // The item still points at that row, so the logo survives the member's next pull.+ let item = rows(&pushed_shared.manifest, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap();+ assert_eq!(item["LogoId"], logo["Id"]);+}++/// The member's SQLite after pulling the owner's share, with blob bytes bound as the platform binds them.+fn member_tables_after_pull(owner: &CanonicalizedVault) -> Vec<CodecTableData> {+ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![owner.rest()[0].manifest.clone()], buckets)).unwrap();+ let mut tables: Vec<CodecTableData> = re.tables.iter().map(|t| table(&t.name, t.records.clone())).collect();+ let mut all_blobs = owner.first().blobs.clone();+ all_blobs.extend(owner.rest()[0].blobs.clone());+ rebind_blobs(&mut tables, &all_blobs);+ tables+}++#[test]+fn member_edit_repointing_the_item_at_a_personal_logo_keeps_the_shared_logo() {+ // What the client does when a member edits a shared item: FaviconService only ever looks up (and+ // creates) personal-scope rows, so the item ends up pointing at a fresh personal row. The push must+ // fold that back onto the folder's own row instead of losing the folder's logo.+ let owner = canonicalize_owner();+ let mut tables = member_tables_after_pull(&owner);++ let personal_id = logo_id(PERSONAL_M, "github.com");+ let logos = tables.iter_mut().find(|t| t.name == "Logos").unwrap();+ logos.records.push(row(&[+ ("Id", json!(personal_id)),+ ("Source", json!("github.com")),+ ("ManifestId", json!(PERSONAL_M)),+ ("FileData", json!({ "__b64": b64(&[0x77, 0x88]) })),+ ]));+ let items = tables.iter_mut().find(|t| t.name == "Items").unwrap();+ items.records.iter_mut().find(|r| r["Id"] == json!("i-shared")).unwrap().insert("LogoId".to_string(), json!(personal_id));++ let pushed = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &pushed.rest()[0];+ let logo = logo_row(&shared.manifest, "github.com");+ let blob_ref = logo["FileData"]["__blobRef"].as_str().expect("shared logo still carries bytes");+ assert_eq!(shared.blobs[blob_ref].bytes_base64, b64(&[0xAA, 0xBB]), "the folder keeps the logo its members agreed on");++ let item = rows(&shared.manifest, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap();+ assert_eq!(item["LogoId"], logo["Id"], "the item was folded back onto the folder's row");+}++#[test]+fn member_whose_shared_logo_blob_never_arrived_does_not_wipe_the_folders_logo() {+ // The member's pull could not resolve the shared logo's bytes (blob missing/undecryptable), so their+ // local row sits there empty while their own personal row for that domain has real bytes. Their next+ // push must NOT publish the empty row as the folder's logo: that drops the last reference to the+ // owner's blob and every member loses the image.+ let owner = canonicalize_owner();+ let mut tables = member_tables_after_pull(&owner);++ let shared_logo_id = logo_id("m-f-shared", "github.com");+ let personal_id = logo_id(PERSONAL_M, "github.com");+ let logos = tables.iter_mut().find(|t| t.name == "Logos").unwrap();+ logos.records.iter_mut().find(|r| r["Id"] == json!(shared_logo_id)).unwrap().insert("FileData".to_string(), serde_json::Value::Null);+ logos.records.push(row(&[+ ("Id", json!(personal_id)),+ ("Source", json!("github.com")),+ ("ManifestId", json!(PERSONAL_M)),+ ("FileData", json!({ "__b64": b64(&[0x77, 0x88]) })),+ ]));+ let items = tables.iter_mut().find(|t| t.name == "Items").unwrap();+ items.records.iter_mut().find(|r| r["Id"] == json!("i-shared")).unwrap().insert("LogoId".to_string(), json!(personal_id));++ let pushed = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &pushed.rest()[0];+ let logo = logo_row(&shared.manifest, "github.com");+ assert!(logo["FileData"]["__blobRef"].is_string(), "the pushed folder logo must still carry bytes, got {:?}", logo["FileData"]);+ assert!(!shared.blobs.is_empty(), "and register them so the write keeps a live blob reference");+}++// ─────────────────────────────────────────────────────────────────────────────+// Logo kinds: one row shape, one `Items.LogoId` pointer, three key spaces.+// ─────────────────────────────────────────────────────────────────────────────++/// The scoped logo id for any `(scope, kind, source)`.+fn logo_id_of_kind(scope: &str, kind: &str, source: &str) -> String {+ scoped_assets::logo_id_for(scope, kind, source)+}++#[test]+fn logo_kinds_key_independently_so_a_domain_and_a_catalog_key_never_collide() {+ // Kind is part of the natural key: the same Source string in two kinds is two different logos,+ // and neither may swallow the other when a scope is normalized.+ let tables = vec![+ table("Items", vec![+ row(&[("Id", json!("i-fav")), ("FolderId", serde_json::Value::Null), ("LogoId", json!("legacy-fav"))]),+ row(&[("Id", json!("i-builtin")), ("FolderId", serde_json::Value::Null), ("LogoId", json!("legacy-builtin"))]),+ ]),+ table("Logos", vec![+ row(&[("Id", json!("legacy-fav")), ("Kind", json!("favicon")), ("Source", json!("shopping")), ("FileData", json!({ "__b64": b64(&[1, 2]) }))]),+ row(&[("Id", json!("legacy-builtin")), ("Kind", json!("builtin")), ("Source", json!("shopping")), ("FileData", serde_json::Value::Null)]),+ ]),+ ];++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![])).unwrap();+ let logos = rows(&out.first().manifest, "Logos");+ assert_eq!(logos.len(), 2, "both kinds survive: {:?}", logos);++ let fav_id = logo_id_of_kind(PERSONAL_M, "favicon", "shopping");+ let builtin_id = logo_id_of_kind(PERSONAL_M, "builtin", "shopping");+ assert_ne!(fav_id, builtin_id, "kinds must derive into different ids");+ assert_eq!(ids(logos), { let mut e = vec![fav_id.as_str(), builtin_id.as_str()]; e.sort(); e });++ let items = rows(&out.first().manifest, "Items");+ let item = |id: &str| items.iter().find(|r| r["Id"] == json!(id)).unwrap().clone();+ assert_eq!(item("i-fav")["LogoId"], json!(fav_id));+ assert_eq!(item("i-builtin")["LogoId"], json!(builtin_id));+}++#[test]+fn icon_row_without_a_kind_is_a_favicon_and_keeps_its_legacy_id() {+ // Rows written before the Kind column exists must not be re-minted: their id has to stay exactly+ // what `logo_id_for_source` produces, or every item in every older vault loses its logo once.+ let tables = vec![+ table("Items", vec![row(&[("Id", json!("i-1")), ("FolderId", serde_json::Value::Null), ("LogoId", json!("random-guid"))])]),+ table("Logos", vec![row(&[("Id", json!("random-guid")), ("Source", json!("github.com")), ("FileData", json!({ "__b64": b64(&[7]) }))])]),+ ];++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![])).unwrap();+ let logo = &rows(&out.first().manifest, "Logos")[0];+ assert_eq!(logo["Id"], json!(logo_id(PERSONAL_M, "github.com")), "a kindless row derives as a favicon");+ assert_eq!(logo["Kind"], json!("favicon"), "and is stamped with the kind it implicitly had");+}++#[test]+fn a_custom_logo_follows_its_item_into_a_shared_manifest() {+ // An uploaded logo is scoped like a favicon: the item that uses it moves into the share, so the+ // image is cloned into that manifest under the folder-scoped id and the item repointed at it.+ let content_hash = "a".repeat(64);+ let mut tables = owner_tables();+ tables.iter_mut().find(|t| t.name == "Items").unwrap().records.iter_mut().find(|r| r["Id"] == json!("i-shared")).unwrap()+ .insert("LogoId".to_string(), json!("personal-custom"));+ tables.iter_mut().find(|t| t.name == "Logos").unwrap().records.push(row(&[+ ("Id", json!("personal-custom")),+ ("Kind", json!("custom")),+ ("Source", json!(content_hash.clone())),+ ("ManifestId", json!(PERSONAL_M)),+ ("FileData", json!({ "__b64": b64(&[0xaa, 0xbb, 0xcc]) })),+ ]));++ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0];+ let scoped_id = logo_id_of_kind("m-f-shared", "custom", &content_hash);+ let cloned = rows(&shared.manifest, "Logos").iter().find(|r| r["Id"] == json!(scoped_id))+ .expect("the uploaded logo is cloned into the folder's manifest");+ assert_eq!(cloned["Kind"], json!("custom"));+ assert_eq!(cloned["ManifestId"], json!("m-f-shared"));+ assert!(cloned["FileData"]["__blobRef"].is_string(), "with its bytes, so members can render it");++ let shared_item = rows(&shared.manifest, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap().clone();+ assert_eq!(shared_item["LogoId"], json!(scoped_id), "and the item points at the folder-scoped row");+}++#[test]+fn a_builtin_logo_survives_without_any_image_bytes() {+ // Built-in logos carry no FileData at all: every platform draws them from the shared catalog. The+ // row must still round-trip (and travel into a share) rather than being dropped as "empty".+ let mut tables = owner_tables();+ tables.iter_mut().find(|t| t.name == "Items").unwrap().records.iter_mut().find(|r| r["Id"] == json!("i-shared")).unwrap()+ .insert("LogoId".to_string(), json!("builtin-row"));+ tables.iter_mut().find(|t| t.name == "Logos").unwrap().records.push(row(&[+ ("Id", json!("builtin-row")),+ ("Kind", json!("builtin")),+ ("Source", json!("shopping")),+ ("ManifestId", json!(PERSONAL_M)),+ ("FileData", serde_json::Value::Null),+ ]));++ // Moving an item across a folder boundary moves it across a manifest boundary: the client+ // re-stamps it on the move, so the fixture does too.+ let tables = stamp_subtree(tables, "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0];+ let scoped_id = logo_id_of_kind("m-f-shared", "builtin", "shopping");+ let logo = rows(&shared.manifest, "Logos").iter().find(|r| r["Id"] == json!(scoped_id))+ .expect("a bytesless built-in logo still belongs in the manifest");+ assert_eq!(logo["Source"], json!("shopping"), "the catalog key is what identifies it");++ let shared_item = rows(&shared.manifest, "Items").iter().find(|r| r["Id"] == json!("i-shared")).unwrap().clone();+ assert_eq!(shared_item["LogoId"], json!(scoped_id));+}++// ─────────────────────────────────────────────────────────────────────────────+// Folder keypairs (manifest-stamped `EncryptionKeys` rows)+//+// One table serves every manifest: personal-stamped rows are the personal manifest's own delivery keys,+// shared-manifest-stamped rows are that folder's delivery keypair. The guards point both ways — a+// personal key must never travel INTO a shared manifest, and a folder key must never travel OUT of+// its own. Both directions are attacks a folder co-owner can attempt by writing rows into a manifest+// the victim materializes.+// ─────────────────────────────────────────────────────────────────────────────++/// A delivery keypair row stamped with `scope` (a manifest id, or a legacy folder-id stamp).+fn folder_key(id: &str, scope: &str, public_key: &str, is_primary: i32) -> CodecRecord {+ row(&[+ ("Id", json!(id)),+ ("ManifestId", json!(scope)),+ ("PublicKey", json!(public_key)),+ ("PrivateKey", json!(format!("priv-{}", public_key))),+ ("IsPrimary", json!(is_primary)),+ ])+}++/// Owner tables plus extra `EncryptionKeys` rows (canonicalize merges same-named table chunks), so the+/// keypair tests don't disturb the shared fixtures. The base fixture already carries the personal key "ek-1".+fn owner_tables_with_folder_keys(keys: Vec<CodecRecord>) -> Vec<CodecTableData> {+ let mut tables = owner_tables();+ tables.push(table("EncryptionKeys", keys));+ tables+}++#[test]+fn split_routes_folder_keypair_into_its_manifest_and_never_the_personal_one() {+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-1", "m-f-shared", "pub-folder", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap();++ assert_eq!(ids(rows(&out.rest()[0].manifest, "EncryptionKeys")), vec!["sfk-1"]);+ assert_eq!(ids(rows(&out.first().manifest, "EncryptionKeys")), vec!["ek-1"], "the personal manifest keeps exactly its personal keys");+ // The unstamped legacy personal row was adopted: stamped with the personal manifest's id.+ assert_eq!(rows(&out.first().manifest, "EncryptionKeys")[0]["ManifestId"], json!(PERSONAL_M));+}++#[test]+fn split_drops_a_keypair_stamped_for_a_manifest_that_is_not_in_this_push() {+ // A key row whose stamp names no manifest being written is dropped, never re-homed into the personal manifest:+ // a stale key falling back would resurrect the old keypair if that manifest is ever re-created.+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-gone", "m-deleted", "pub-folder", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap();+ assert!(rows(&out.rest()[0].manifest, "EncryptionKeys").is_empty());+ assert!(!ids(rows(&out.first().manifest, "EncryptionKeys")).contains(&"sfk-gone"));+}++#[test]+fn split_drops_folder_keypair_whose_scope_is_not_shared() {+ // A key row stamped for a manifest that is not part of this push (revoked, deleted, or fabricated+ // locally) has nowhere to go: it must be dropped, NOT fall back into the personal manifest — a stale+ // copy demoted into the personal manifest would resurrect the old keypair on a future re-share, and would+ // leave a private delivery key in a namespace revocation cannot reach.+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![+ folder_key("sfk-live", "m-f-shared", "pub-folder", 1),+ folder_key("sfk-orphan", "m-dead-manifest", "pub-orphan", 1),+ folder_key("sfk-orphan-legacy", "f-personal", "pub-orphan2", 1),+ ]),+ vec![spec("f-shared")],+ ))+ .unwrap();++ assert_eq!(ids(rows(&out.rest()[0].manifest, "EncryptionKeys")), vec!["sfk-live"]);+ assert_eq!(ids(rows(&out.first().manifest, "EncryptionKeys")), vec!["ek-1"], "orphaned folder keypair dropped, never demoted to the personal manifest");+}++#[test]+fn split_drops_folder_keypair_even_when_nothing_is_shared() {+ // The unshare window: sharing was just turned off (no specs this push) but the local DB still+ // holds the folder's key rows. They must be dropped, not leak into the personal manifest.+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-stale", "m-f-shared", "pub-stale", 1)]),+ vec![],+ ))+ .unwrap();+ assert!(out.rest().is_empty());+ assert_eq!(ids(rows(&out.first().manifest, "EncryptionKeys")), vec!["ek-1"], "stale folder keypair dropped from the personal manifest");+}++#[test]+fn combine_accepts_folder_keypair_from_the_manifest_that_owns_it() {+ // The whole point of folder-scoped rows: a recipient materializes the folder's keypair so they can+ // decrypt mail sent to the folder's shared aliases.+ let owner = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-1", "m-f-shared", "pub-folder", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap();++ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![owner.rest()[0].manifest.clone()], buckets)).unwrap();+ let map = materialized_map(&re);++ // The folder's keypair lands next to the recipient's own personal key, distinguished by scope.+ assert_eq!(ids(&map["EncryptionKeys"]), vec!["ek-r", "sfk-1"]);+ let folder_row = map["EncryptionKeys"].iter().find(|r| r["Id"] == json!("sfk-1")).unwrap();+ assert_eq!(folder_row["PrivateKey"], json!("priv-pub-folder"));+ assert_eq!(folder_row["ManifestId"], json!("m-f-shared"));+}++#[test]+fn combine_drops_folder_keypair_scoped_to_another_folder() {+ /*+ * The injection attack the scope filter exists to resist. Mallory co-owns folder A and writes into+ * A's manifest a keypair claiming to be folder B's delivery key. If the recipient accepted it,+ * their client would publish Mallory's public key for B and all of B's mail would be readable by her.+ *+ * The row is DROPPED rather than re-scoped to A: stamping it with A's id would merely turn the+ * attack into "displace A's real keypair", which is no better.+ */+ let owner = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-legit", "m-f-shared", "pub-folder", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap();++ let mut evil = owner.rest()[0].manifest.clone();+ evil.tables.get_mut("EncryptionKeys").unwrap().push(folder_key("sfk-evil", "m-other-manifest", "pub-mallory", 1));++ let (recipient_manifest, buckets) = recipient_personal_manifest();+ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![evil], buckets)).unwrap();+ let map = materialized_map(&re);++ assert_eq!(ids(&map["EncryptionKeys"]), vec!["ek-r", "sfk-legit"], "cross-folder key row dropped");+ assert!(+ !map["EncryptionKeys"].iter().any(|r| r["PublicKey"] == json!("pub-mallory")),+ "the injected key must not survive under any scope, including a re-stamped one"+ );+}++#[test]+fn combine_drops_folder_keypair_carried_by_a_personal_manifest() {+ // Canonicalize never emits one, so a folder-scoped key row in a personal manifest means a tampered or+ // malformed manifest. Drop it rather than materialize key material with no owning folder.+ let (mut recipient_manifest, buckets) = recipient_personal_manifest();+ recipient_manifest.tables.get_mut("EncryptionKeys").unwrap().push(folder_key("sfk-orphan", "f-whatever", "pub-x", 1));++ let re = materialize_as_sqlite(materialize_input(recipient_manifest, vec![], buckets)).unwrap();+ let map = materialized_map(&re);+ assert_eq!(ids(&map["EncryptionKeys"]), vec!["ek-r"], "personal-manifest-carried folder keypair dropped");+}++#[test]+fn encryption_keys_is_scoped_rather_than_personal_or_bucketed() {+ /*+ * Pins the routing class itself: the keypair table travels inside the manifest its scope names, so+ * it must be neither personal-only (which would pin every row to the personal manifest) nor bucketed+ * (which would move it out of the manifest into a resource of its own).+ */+ assert!(!is_personal_table("EncryptionKeys"));+ assert!(!is_bucketed_table("EncryptionKeys"));++ /*+ * And pins the two axes apart. Bucketed says where a table syncs; personal says which manifests may+ * hold it. Settings is bucketed and NOT personal — its bucket belongs to a manifest, so a shared+ * manifest carrying its own settings is a routing question, never a rule violation.+ */+ assert!(is_bucketed_table("Settings"));+ assert!(!is_personal_table("Settings"));+}++#[test]+fn folder_keypair_survives_a_full_split_combine_roundtrip() {+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-1", "m-f-shared", "pub-folder", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap();+ let re = materialize_as_sqlite(materialize_input(out.first().manifest.clone(), out.rest().iter().map(|s| s.manifest.clone()).collect(), out.data_buckets.clone())).unwrap();+ let map = materialized_map(&re);+ assert_eq!(ids(&map["EncryptionKeys"]), vec!["ek-1", "sfk-1"]);+}++#[test]+fn active_folder_key_is_the_primary_row_and_rotated_keys_stay_resolvable() {+ // After a rotation the superseded row stays in the manifest so mail received before the rotation+ // remains decryptable, but only the live primary is offered as the folder's delivery key.+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![+ folder_key("sfk-old", "m-f-shared", "pub-old", 0),+ folder_key("sfk-cur", "m-f-shared", "pub-cur", 1),+ ]),+ vec![spec("f-shared")],+ ))+ .unwrap();+ let shared = &out.rest()[0].manifest;++ assert_eq!(active_encryption_key(shared).expect("primary key present")["Id"], json!("sfk-cur"));+ assert_eq!(+ extract_encryption_key_for_public_key(shared, "pub-old").expect("rotated key still resolvable")["PrivateKey"],+ json!("priv-pub-old")+ );+ assert!(extract_encryption_key_for_public_key(shared, "pub-nonexistent").is_none());+}++#[test]+fn folder_key_lookup_ignores_deleted_and_out_of_scope_rows() {+ let mut manifest = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-cur", "m-f-shared", "pub-cur", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap()+ .rest()[0]+ .manifest+ .clone();++ // A caller that skips combine must still not be handed another folder's key material.+ manifest.tables.get_mut("EncryptionKeys").unwrap().push(folder_key("sfk-foreign", "m-other", "pub-foreign", 1));+ assert!(extract_encryption_key_for_public_key(&manifest, "pub-foreign").is_none(), "out-of-scope row ignored");++ let mut deleted = folder_key("sfk-dead", "m-f-shared", "pub-dead", 0);+ deleted.insert("IsDeleted".to_string(), json!(1));+ manifest.tables.get_mut("EncryptionKeys").unwrap().push(deleted);+ assert!(extract_encryption_key_for_public_key(&manifest, "pub-dead").is_none(), "deleted row ignored");+}++#[test]+fn validate_rejects_misplaced_folder_keypairs_before_upload() {+ // Catching this at validate time means a tampered vault fails loudly on push instead of quietly+ // losing rows at each recipient's combine step.+ let out = canonicalize_from_sqlite(input_with_shares(+ owner_tables_with_folder_keys(vec![folder_key("sfk-1", "m-f-shared", "pub-folder", 1)]),+ vec![spec("f-shared")],+ ))+ .unwrap();++ let mut personal = out.first().manifest.clone();+ personal.tables.get_mut("EncryptionKeys").unwrap().push(folder_key("sfk-orphan", "m-f-shared", "pub-x", 1));+ let result = validate_manifest(&personal);+ assert!(!result.ok);+ assert!(result.failed_rules.contains(&"encryption-keys-scope-mismatch".to_string()));++ let mut shared = out.rest()[0].manifest.clone();+ shared.tables.get_mut("EncryptionKeys").unwrap().push(folder_key("sfk-evil", "m-other", "pub-mallory", 1));+ let result = validate_manifest(&shared);+ assert!(!result.ok);+ assert!(result.failed_rules.contains(&"encryption-keys-scope-mismatch".to_string()));++ // The well-formed pair still validates.+ assert!(validate_manifest(&out.rest()[0].manifest).ok);+ assert!(validate_manifest(&out.first().manifest).ok);+}++// ─────────────────────────────────────────────────────────────────────────────+// Cross-manifest routing: the same id in two manifests+// ─────────────────────────────────────────────────────────────────────────────+//+// A manifest is a namespace, so two of them may each hold a row with the same `Id` — and they do, for+// ordinary reasons: a member moves a shared item into their own vault (the client re-stamps the row and+// keeps its id), or an item that was moved out comes back through another member's push. Every rule+// that resolves a reference therefore has to resolve it by `(ManifestId, Id)`, both halves. Anything+// that keys on the bare id sends rows across a manifest boundary — which for a shared manifest means+// handing them to other people.++/// Two manifests each holding an item with the same id, each with its own child rows. `personal_secret`+/// / `shared_secret` are what the assertions look for: the personal one must never appear in the share.+fn tables_with_a_duplicated_item_id() -> Vec<CodecTableData> {+ vec![+ table("Folders", vec![+ row(&[("Id", json!("f-personal")), ("Name", json!("Personal")), ("ParentFolderId", serde_json::Value::Null), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("f-shared")), ("Name", json!("Family")), ("ParentFolderId", serde_json::Value::Null), ("ManifestId", json!("m-f-shared"))]),+ ]),+ table("Items", vec![+ row(&[("Id", json!("i-dup")), ("Name", json!("mine")), ("FolderId", json!("f-personal")), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("i-dup")), ("Name", json!("ours")), ("FolderId", json!("f-shared")), ("ManifestId", json!("m-f-shared"))]),+ ]),+ table("FieldValues", vec![+ row(&[("Id", json!("fv-personal")), ("ItemId", json!("i-dup")), ("FieldKey", json!("password")), ("Value", json!("personal_secret")), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("fv-shared")), ("ItemId", json!("i-dup")), ("FieldKey", json!("password")), ("Value", json!("shared_secret")), ("ManifestId", json!("m-f-shared"))]),+ ]),+ table("TotpCodes", vec![+ row(&[("Id", json!("totp-personal")), ("ItemId", json!("i-dup")), ("SecretKey", json!({ "__b64": b64(b"personal_secret") })), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("totp-shared")), ("ItemId", json!("i-dup")), ("SecretKey", json!({ "__b64": b64(b"shared_secret") })), ("ManifestId", json!("m-f-shared"))]),+ ]),+ table("Attachments", vec![+ row(&[("Id", json!("att-personal")), ("ItemId", json!("i-dup")), ("Blob", json!({ "__b64": b64(b"personal_secret") })), ("ManifestId", json!(PERSONAL_M))]),+ ]),+ table("Passkeys", vec![+ row(&[("Id", json!("pk-personal")), ("ItemId", json!("i-dup")), ("PrivateKey", json!("personal_secret")), ("ManifestId", json!(PERSONAL_M))]),+ ]),+ table("FieldHistories", vec![+ row(&[("Id", json!("fh-personal")), ("ItemId", json!("i-dup")), ("Value", json!("personal_secret")), ("ManifestId", json!(PERSONAL_M))]),+ ]),+ table("ItemTags", vec![+ row(&[("Id", json!("it-personal")), ("ItemId", json!("i-dup")), ("TagId", json!("tag-personal")), ("ManifestId", json!(PERSONAL_M))]),+ ]),+ table("Tags", vec![+ row(&[("Id", json!("tag-personal")), ("Name", json!("personal_secret")), ("ManifestId", json!(PERSONAL_M))]),+ ]),+ ]+}++/// Every string a manifest carries, at any depth, so a test can ask whether a secret leaked into it+/// without having to know which column of which table it would have arrived in.+fn all_strings(m: &Manifest) -> Vec<String> {+ fn walk(value: &serde_json::Value, out: &mut Vec<String>) {+ match value {+ serde_json::Value::String(s) => out.push(s.clone()),+ serde_json::Value::Array(items) => items.iter().for_each(|v| walk(v, out)),+ serde_json::Value::Object(map) => map.values().for_each(|v| walk(v, out)),+ _ => {}+ }+ }+ let mut out = Vec::new();+ for rows in m.tables.values() {+ for row in rows {+ for value in row.values() {+ walk(value, &mut out);+ }+ }+ }+ out+}++/// True when `needle` appears anywhere in the manifest, base64-encoded blobs included.+fn manifest_mentions(m: &Manifest, needle: &str) -> bool {+ let encoded = b64(needle.as_bytes());+ all_strings(m).iter().any(|s| s == needle || s == &encoded)+}++#[test]+fn split_never_follows_a_bare_item_id_into_another_manifest() {+ /*+ * The leak this whole section exists for. Both manifests hold an item called `i-dup`; the personal+ * one's child rows carry a password, a TOTP secret, an attachment, a passkey and a field history.+ * Routing them by the bare `ItemId` would send every one of them into the manifest shared with+ * other people.+ */+ let out = canonicalize_from_sqlite(raw_input_with_shares(tables_with_a_duplicated_item_id(), vec![spec("f-shared")])).unwrap();+ let personal = &out.first().manifest;+ let shared = &out.rest()[0].manifest;++ assert!(!manifest_mentions(shared, "personal_secret"), "a personal item's child rows must never follow a same-id item into a shared manifest");+ assert!(manifest_mentions(personal, "personal_secret"), "and they must still be in the manifest they belong to");++ // Each manifest keeps exactly its own child rows, and the shared one keeps working.+ assert_eq!(ids(rows(personal, "FieldValues")), vec!["fv-personal"]);+ assert_eq!(ids(rows(shared, "FieldValues")), vec!["fv-shared"]);+ assert_eq!(ids(rows(personal, "TotpCodes")), vec!["totp-personal"]);+ assert_eq!(ids(rows(shared, "TotpCodes")), vec!["totp-shared"]);+ for name in ["Attachments", "Passkeys", "FieldHistories", "ItemTags"] {+ assert!(rows(shared, name).is_empty(), "{name} of the personal item stayed out of the share");+ assert_eq!(rows(personal, name).len(), 1, "{name} of the personal item stayed with it");+ }+}++#[test]+fn split_never_pulls_a_shared_items_child_into_the_personal_manifest() {+ // The same rule read the other way: the share's own rows must not be swept into the personal+ // manifest because a personal item happens to carry the same id.+ let out = canonicalize_from_sqlite(raw_input_with_shares(tables_with_a_duplicated_item_id(), vec![spec("f-shared")])).unwrap();+ assert!(!manifest_mentions(&out.first().manifest, "shared_secret"), "the share's rows stayed in the share");+ assert!(manifest_mentions(&out.rest()[0].manifest, "shared_secret"));+}++#[test]+fn split_keeps_child_rows_when_a_manifest_this_vault_lost_shares_their_item_id() {+ /*+ * A row stamped for a manifest this vault no longer carries is dropped — but only its own rows. An+ * item left behind by a revoked share must not take the child rows of a live item that happens to+ * carry the same id down with it.+ */+ let tables = vec![+ table("Folders", vec![row(&[("Id", json!("f-personal")), ("Name", json!("Personal")), ("ParentFolderId", serde_json::Value::Null), ("ManifestId", json!(PERSONAL_M))])]),+ table("Items", vec![+ row(&[("Id", json!("i-dup")), ("Name", json!("mine")), ("FolderId", json!("f-personal")), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("i-dup")), ("Name", json!("revoked")), ("FolderId", serde_json::Value::Null), ("ManifestId", json!("m-revoked"))]),+ ]),+ table("FieldValues", vec![+ row(&[("Id", json!("fv-personal")), ("ItemId", json!("i-dup")), ("FieldKey", json!("password")), ("Value", json!("personal_secret")), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("fv-revoked")), ("ItemId", json!("i-dup")), ("FieldKey", json!("password")), ("Value", json!("gone")), ("ManifestId", json!("m-revoked"))]),+ ]),+ ];++ let out = canonicalize_from_sqlite(raw_input_with_shares(tables, vec![])).unwrap();+ let personal = &out.first().manifest;+ assert_eq!(ids(rows(personal, "Items")), vec!["i-dup"], "the revoked manifest's item is dropped, the live one kept");+ assert_eq!(ids(rows(personal, "FieldValues")), vec!["fv-personal"], "the live item keeps its own child rows");+ assert!(!manifest_mentions(personal, "gone"), "the revoked manifest's rows are not adopted into the personal one");+}++#[test]+fn split_still_follows_an_item_whose_children_were_left_unstamped() {+ /*+ * The safety net the item-follow rule exists for, which the scope-aware lookup must not cost us: a+ * client that moved an item between manifests without re-stamping its children (no trigger, an+ * older platform) leaves them naming the manifest the item left. While only one item carries that+ * id there is no ambiguity, so the child follows it and is re-stamped to agree.+ */+ let tables = stamp_subtree(owner_tables_unstamped(), "f-shared", "m-f-shared");+ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();++ // `input_with_shares` stamps everything the fixture left unstamped as personal, child rows included.+ let shared = &out.rest()[0].manifest;+ assert_eq!(ids(rows(shared, "FieldValues")), vec!["fv-shared", "fv-sub"], "children followed their item across the boundary");+ assert!(rows(shared, "FieldValues").iter().all(|r| r["ManifestId"] == json!("m-f-shared")), "and were re-stamped to agree with it");+}++#[test]+fn split_falls_back_to_a_rows_own_stamp_when_two_items_could_claim_it() {+ /*+ * The fallback above is only safe while it is unambiguous. With two same-id items in different+ * manifests, a child naming neither (its own manifest holds no such item) has no item to follow —+ * so it routes by its own stamp rather than being handed to whichever manifest happens to be found+ * first.+ */+ let mut tables = tables_with_a_duplicated_item_id();+ // A third manifest's orphan child: its stamp names a manifest that is not in this push.+ tables.push(table("FieldValues", vec![+ row(&[("Id", json!("fv-orphan")), ("ItemId", json!("i-dup")), ("FieldKey", json!("password")), ("Value", json!("orphan_secret")), ("ManifestId", json!("m-revoked"))]),+ ]));++ let out = canonicalize_from_sqlite(raw_input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ assert!(!manifest_mentions(&out.rest()[0].manifest, "orphan_secret"), "an ambiguous child is never guessed into a shared manifest");+ assert!(!manifest_mentions(&out.first().manifest, "orphan_secret"), "its own stamp names a manifest this vault no longer carries, so it is dropped");+}++#[test]+fn split_keeps_a_parent_folder_link_that_resolves_in_the_folders_own_manifest() {+ /*+ * The same-id rule for folders. A personal folder carrying the id of the share's subfolder must not+ * make the share's own parent link look foreign — nulling it would silently flatten the shared tree.+ */+ let tables = vec![+ table("Folders", vec![+ row(&[("Id", json!("f-shared")), ("Name", json!("Family")), ("ParentFolderId", serde_json::Value::Null), ("ManifestId", json!("m-f-shared"))]),+ row(&[("Id", json!("f-sub")), ("Name", json!("Streaming")), ("ParentFolderId", json!("f-shared")), ("ManifestId", json!("m-f-shared"))]),+ // A personal folder that happens to carry the shared anchor's id.+ row(&[("Id", json!("f-shared")), ("Name", json!("Mine")), ("ParentFolderId", serde_json::Value::Null), ("ManifestId", json!(PERSONAL_M))]),+ ]),+ ];++ let out = canonicalize_from_sqlite(raw_input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let shared = &out.rest()[0].manifest;+ let sub = rows(shared, "Folders").iter().find(|r| r["Id"] == json!("f-sub")).unwrap();+ assert_eq!(sub["ParentFolderId"], json!("f-shared"), "the parent resolves inside this manifest, so the link stands");+}++#[test]+fn split_roundtrips_two_same_id_items_without_mixing_their_children() {+ // End to end: the duplicate survives a full split + combine as two independent items, each with+ // its own secrets, which is what `(ManifestId, Id)` promises everywhere else.+ let out = canonicalize_from_sqlite(raw_input_with_shares(tables_with_a_duplicated_item_id(), vec![spec("f-shared")])).unwrap();+ let re = materialize_as_sqlite(materialize_input(out.first().manifest.clone(), vec![out.rest()[0].manifest.clone()], out.data_buckets.clone())).unwrap();+ let map = materialized_map(&re);++ let field_values: Vec<(&str, &str)> = map["FieldValues"]+ .iter()+ .map(|r| (r["ManifestId"].as_str().unwrap(), r["Value"].as_str().unwrap()))+ .collect();+ assert!(field_values.contains(&(PERSONAL_M, "personal_secret")));+ assert!(field_values.contains(&("m-f-shared", "shared_secret")));+ assert_eq!(field_values.len(), 2, "neither row was dropped or duplicated into the other manifest");+}++// ─────────────────────────────────────────────────────────────────────────────+// Cross-manifest routing: the blanket invariant+// ─────────────────────────────────────────────────────────────────────────────++#[test]+fn every_row_of_a_canonicalized_manifest_carries_that_manifests_own_stamp() {+ /*+ * The invariant every routing rule has to preserve, asserted over a whole vault rather than+ * table by table: a manifest is a namespace, so a row inside one that names another is either a row+ * that leaked in or a reference that will resolve against the wrong namespace on the way back.+ * A new table added to the codec is covered by this the day it appears in a manifest.+ */+ let out = canonicalize_owner();+ for canonicalized in out.manifests.iter() {+ let manifest = &canonicalized.manifest;+ for (name, rows) in manifest.tables.iter() {+ for row in rows {+ assert_eq!(+ row.get("ManifestId").and_then(|v| v.as_str()),+ Some(manifest.manifest_id.as_str()),+ "{name} row {:?} sits in manifest {} but names another",+ row.get("Id"),+ manifest.manifest_id+ );+ }+ }+ }+ for bucket in out.data_buckets.iter() {+ for (name, rows) in bucket.tables.iter() {+ for row in rows {+ assert_eq!(+ row.get("ManifestId").and_then(|v| v.as_str()),+ Some(bucket.manifest_id.as_str()),+ "{name} row in the {} bucket of {} names another manifest",+ bucket.category,+ bucket.manifest_id+ );+ }+ }+ }+}++#[test]+fn no_key_material_crosses_a_manifest_boundary_in_a_full_roundtrip() {+ /*+ * The delivery keypair decides who can read a manifest's alias mail, so it is the one row whose+ * misrouting is unrecoverable: publishing the personal private key inside a shared manifest hands+ * every member the user's own mail. Split and combine are both checked here, on a vault where each+ * manifest holds a keypair whose private half names the manifest it belongs to.+ */+ let mut tables = owner_tables();+ tables.retain(|t| t.name != "EncryptionKeys");+ tables.push(table("EncryptionKeys", vec![+ row(&[("Id", json!("ek-personal")), ("PublicKey", json!("pub-personal")), ("PrivateKey", json!("priv_personal_only")), ("IsPrimary", json!(1)), ("IsDeleted", json!(0)), ("ManifestId", json!(PERSONAL_M))]),+ row(&[("Id", json!("ek-shared")), ("PublicKey", json!("pub-shared")), ("PrivateKey", json!("priv_shared_only")), ("IsPrimary", json!(1)), ("IsDeleted", json!(0)), ("ManifestId", json!("m-f-shared"))]),+ ]));++ let out = canonicalize_from_sqlite(input_with_shares(tables, vec![spec("f-shared")])).unwrap();+ let personal = out.first().manifest.clone();+ let shared = out.rest()[0].manifest.clone();++ assert!(!manifest_mentions(&shared, "priv_personal_only"), "the personal private key must never be inside a manifest other people hold");+ assert!(!manifest_mentions(&personal, "priv_shared_only"), "nor the share's key inside the personal manifest");+ assert!(validate_manifest(&shared).ok && validate_manifest(&personal).ok);++ // And the same after combine, where the rule is enforced against the manifest's own claim.+ let re = materialize_as_sqlite(materialize_input(personal, vec![shared], out.data_buckets.clone())).unwrap();+ let map = materialized_map(&re);+ for key_row in map["EncryptionKeys"].iter() {+ let expected = if key_row["PrivateKey"] == json!("priv_personal_only") { PERSONAL_M } else { "m-f-shared" };+ assert_eq!(key_row["ManifestId"], json!(expected), "a key row materialized into the wrong manifest");+ }+ assert_eq!(map["EncryptionKeys"].len(), 2);+}+diff --git a/core/rust/src/vault_merge/mod.rs b/core/rust/src/vault_merge/mod.rsindex bfec2b5e4..35b87bfbf 100644--- a/core/rust/src/vault_merge/mod.rs+++ b/core/rust/src/vault_merge/mod.rs@@ -128,26 +128,24 @@ pub fn merge_vaults(input: MergeInput) -> VaultResult<MergeOutput> {(None, None) => continue,};- // Merge the table and generate SQL statements- let table_statements = if table_config.uses_composite_key() {- merge_table_by_composite_key(- table_name,- base_records,- incoming_records,- table_config.composite_key_columns,- table_config.primary_key,- &mut total_stats,- )+ // A row is addressed by its identity columns, and matched to its counterpart by those same+ // columns unless the table declares a semantic match key of its own (FieldValues).+ let identity_columns = table_config.identity_columns();+ let match_columns: &[&str] = if table_config.uses_composite_key() {+ table_config.composite_key_columns} else {- merge_table_by_id(- table_name,- base_records,- incoming_records,- table_config.primary_key,- &mut total_stats,- )+ &identity_columns};+ let table_statements = merge_table(+ table_name,+ base_records,+ incoming_records,+ match_columns,+ &identity_columns,+ &mut total_stats,+ );+statements.extend(table_statements);total_stats.tables_processed += 1;}@@ -168,45 +166,57 @@ pub fn merge_vaults_json(input_json: &str) -> VaultResult<String> {Ok(output_json)}-/// Merge table records by their primary-key column (standard merge).-/// `primary_key` is the column that identifies a row (usually "Id"; "Key" for Settings).-/// `base_records` are the merge base (kept on tie / when the base wins); `incoming_records` are the-/// changes merged onto it. Returns SQL statements to apply to the base database.-fn merge_table_by_id(+/// Merge one table's records with last-write-wins.+///+/// `match_columns` decide which base row an incoming row *is* (its identity columns, or the table's own+/// semantic key where it declares one); `identity_columns` are what a winning incoming row is written+/// back through, so the base row keeps its own identity on conflict. On a manifest-scoped table that+/// identity is `(ManifestId, Id)`, which keeps two manifests holding the same `Id` apart.+///+/// `base_records` are the merge base (kept on tie); `incoming_records` are the changes merged onto it.+/// Returns SQL statements to apply to the base database.+fn merge_table(table_name: &str,base_records: &[Record],incoming_records: &[Record],- primary_key: &str,+ match_columns: &[&str],+ identity_columns: &[&str],stats: &mut MergeStats,) -> Vec<SqlStatement> {let mut statements: Vec<SqlStatement> = Vec::new();- // Create map of incoming records by primary key+ // Map of incoming records by match key; on duplicates the latest UpdatedAt wins.let mut incoming_map: HashMap<String, &Record> = HashMap::new();for record in incoming_records {- if let Some(id) = get_record_pk(record, primary_key) {- incoming_map.insert(id, record);+ let key = get_key(record, match_columns);+ match incoming_map.get(&key) {+ Some(existing) if get_updated_at(record) <= get_updated_at(existing) => {}+ _ => {+ incoming_map.insert(key, record);+ }}}// Process base recordsfor base_record in base_records {- let base_id = match get_record_pk(base_record, primary_key) {- Some(id) => id,+ let match_key = get_key(base_record, match_columns);++ let base_identity = match get_identity(base_record, identity_columns) {+ Some(identity) => identity,None => continue,};- if let Some(incoming_record) = incoming_map.get(&base_id) {+ if let Some(incoming_record) = incoming_map.get(&match_key) {// Record exists in both - compare UpdatedAt for LWWlet incoming_ts = get_updated_at(incoming_record);let base_ts = get_updated_at(base_record);match (incoming_ts, base_ts) {(Some(i_ts), Some(b_ts)) if i_ts > b_ts => {- // Incoming wins - generate UPDATE on the base row+ // Incoming wins - update the base row in place, keeping its own identitystats.conflicts += 1;stats.records_from_server += 1;- if let Some(stmt) = generate_update_sql(table_name, incoming_record, &base_id, primary_key) {+ if let Some(stmt) = generate_update_sql(table_name, incoming_record, &base_identity) {statements.push(stmt);}}@@ -215,7 +225,7 @@ fn merge_table_by_id(stats.records_from_local += 1;}}- incoming_map.remove(&base_id);+ incoming_map.remove(&match_key);} else {// Only in base - no action neededstats.records_created_locally += 1;@@ -233,83 +243,40 @@ fn merge_table_by_id(statements}-/// Merge table by composite key.-/// `base_records` are the merge base (kept on tie / when the base wins, and their primary key is-/// preserved on conflict); `incoming_records` are merged onto the base by composite key.-/// Returns SQL statements to apply to the base database.-fn merge_table_by_composite_key(- table_name: &str,- base_records: &[Record],- incoming_records: &[Record],- key_columns: &[&str],- primary_key: &str,- stats: &mut MergeStats,-) -> Vec<SqlStatement> {- let mut statements: Vec<SqlStatement> = Vec::new();-- // Create map of incoming records by composite key- let mut incoming_map: HashMap<String, &Record> = HashMap::new();- for record in incoming_records {- let key = get_composite_key(record, key_columns);- // Keep the one with latest UpdatedAt if duplicate keys- if let Some(existing) = incoming_map.get(&key) {- if get_updated_at(record) > get_updated_at(existing) {- incoming_map.insert(key, record);- }- } else {- incoming_map.insert(key, record);- }- }-- // Process base records- for base_record in base_records {- let composite_key = get_composite_key(base_record, key_columns);-- let base_id = match get_record_pk(base_record, primary_key) {- Some(id) => id,- None => continue,- };-- if let Some(incoming_record) = incoming_map.get(&composite_key) {- // Record exists in both - compare UpdatedAt- let incoming_ts = get_updated_at(incoming_record);- let base_ts = get_updated_at(base_record);-- match (incoming_ts, base_ts) {- (Some(i_ts), Some(b_ts)) if i_ts > b_ts => {- // Incoming wins - update with incoming data but keep the base Id- stats.conflicts += 1;- stats.records_from_server += 1;- if let Some(stmt) = generate_update_sql(table_name, incoming_record, &base_id, primary_key) {- statements.push(stmt);- }- }- _ => {- // Base wins - no action needed- stats.records_from_local += 1;- }- }- incoming_map.remove(&composite_key);- } else {- // Only in base - no action needed- stats.records_created_locally += 1;- }- }-- // Incoming-only records (by composite key) - generate INSERTs into the base- for (_key, incoming_record) in &incoming_map {- stats.records_inserted += 1;- if let Some(stmt) = generate_insert_sql(table_name, incoming_record) {- statements.push(stmt);- }- }+/// The `(column, value)` pairs that address `record`, or `None` when it carries no primary key.+///+/// `columns` always ends with the table's primary key (see `TableConfig::identity_columns`). A row+/// missing an earlier column (a manifest-scoped row with no scope, which the schema no longer permits+/// but an older vault can still carry) is addressed by the columns it does carry, rather than emitting+/// a `WHERE ManifestId = NULL` that silently matches nothing.+fn get_identity<'a>(record: &Record, columns: &[&'a str]) -> Option<Vec<(&'a str, serde_json::Value)>> {+ let (primary_key, scope_columns) = columns.split_last()?;+ let primary_value = record.get(*primary_key).filter(|v| !v.is_null())?.clone();++ let mut identity: Vec<(&'a str, serde_json::Value)> = scope_columns+ .iter()+ .filter_map(|column| Some((*column, record.get(*column).filter(|v| !v.is_null())?.clone())))+ .collect();+ identity.push((*primary_key, primary_value));+ Some(identity)+}- statements+/// Stable string key over `columns`. A column the record does not carry contributes an empty part,+/// so a row missing one still matches its counterpart rather than dropping out of the merge.+fn get_key(record: &Record, columns: &[&str]) -> String {+ columns+ .iter()+ .map(|column| record.get(*column).filter(|v| !v.is_null()).map(key_part).unwrap_or_default())+ .collect::<Vec<_>>()+ .join(":")}-/// Get the primary-key field (by column name) from a record.-fn get_record_pk(record: &Record, primary_key: &str) -> Option<String> {- record.get(primary_key).and_then(|v| v.as_str()).map(String::from)+/// One key component rendered as a string (strings unquoted, everything else canonical JSON).+fn key_part(value: &serde_json::Value) -> String {+ match value.as_str() {+ Some(s) => s.to_string(),+ None => value.to_string(),+ }}/// Get the UpdatedAt timestamp from a record.@@ -333,21 +300,6 @@ fn get_updated_at(record: &Record) -> Option<DateTime<Utc>> {})}-/// Generate composite key from specified columns.-/// Concatenates column values with ":" separator.-fn get_composite_key(record: &Record, key_columns: &[&str]) -> String {- key_columns- .iter()- .map(|col| {- record- .get(*col)- .and_then(|v| v.as_str())- .unwrap_or("")- })- .collect::<Vec<_>>()- .join(":")-}-/// Generate an INSERT SQL statement for a record./// Uses INSERT OR REPLACE to handle potential conflicts.fn generate_insert_sql(table_name: &str, record: &Record) -> Option<SqlStatement> {@@ -371,31 +323,30 @@ fn generate_insert_sql(table_name: &str, record: &Record) -> Option<SqlStatementSome(SqlStatement { sql, params })}-/// Generate an UPDATE SQL statement for a record.-/// Updates all columns except the primary-key column, which is used in the WHERE clause.-fn generate_update_sql(table_name: &str, record: &Record, id: &str, primary_key: &str) -> Option<SqlStatement> {+/// Generate an UPDATE SQL statement for a record, addressing the base row by `identity`: the *base*+/// row's own `(column, value)` pairs, so it keeps its identity when a semantically-matched incoming row+/// wins. Those columns are excluded from the SET clause for the same reason, since writing the incoming+/// `ManifestId`/`Id` would move the row out of the namespace it was matched in.+fn generate_update_sql(table_name: &str, record: &Record, identity: &[(&str, serde_json::Value)]) -> Option<SqlStatement> {if record.is_empty() {return None;}- // Sort column names for consistent ordering, excluding the primary-key column- let mut columns: Vec<&String> = record.keys().filter(|c| c.as_str() != primary_key).collect();+ // Sort column names for consistent ordering, excluding the identity columns+ let mut columns: Vec<&String> = record.keys().filter(|c| !identity.iter().any(|(id_col, _)| *id_col == c.as_str())).collect();columns.sort();if columns.is_empty() {return None;}- let set_clause = columns- .iter()- .map(|c| format!("{} = ?", c))- .collect::<Vec<_>>()- .join(", ");+ let set_clause = columns.iter().map(|c| format!("{} = ?", c)).collect::<Vec<_>>().join(", ");+ let where_clause = identity.iter().map(|(column, _)| format!("{} = ?", column)).collect::<Vec<_>>().join(" AND ");let mut params: Vec<serde_json::Value> = columns.iter().map(|c| record[*c].clone()).collect();- params.push(serde_json::json!(id)); // Add primary key for WHERE clause+ params.extend(identity.iter().map(|(_, value)| value.clone()));- let sql = format!("UPDATE {} SET {} WHERE {} = ?", table_name, set_clause, primary_key);+ let sql = format!("UPDATE {} SET {} WHERE {}", table_name, set_clause, where_clause);Some(SqlStatement { sql, params })}@@ -418,7 +369,7 @@ mod tests {let server = vec![make_record("1", "2024-01-01T00:00:00Z")];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Test", &local, &server, "Id", &mut stats);+ let statements = merge_table("Test", &local, &server, &["Id"], &["Id"], &mut stats);assert_eq!(stats.records_from_local, 1);assert_eq!(stats.records_from_server, 0);@@ -431,7 +382,7 @@ mod tests {let server = vec![make_record("1", "2024-01-02T00:00:00Z")];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Test", &local, &server, "Id", &mut stats);+ let statements = merge_table("Test", &local, &server, &["Id"], &["Id"], &mut stats);assert_eq!(stats.records_from_server, 1);assert_eq!(stats.conflicts, 1);@@ -445,7 +396,7 @@ mod tests {let server = vec![make_record("1", "2024-01-01T00:00:00Z")];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Test", &local, &server, "Id", &mut stats);+ let statements = merge_table("Test", &local, &server, &["Id"], &["Id"], &mut stats);assert_eq!(stats.records_inserted, 1);assert_eq!(statements.len(), 1);@@ -458,7 +409,7 @@ mod tests {let server: Vec<Record> = vec![];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Test", &local, &server, "Id", &mut stats);+ let statements = merge_table("Test", &local, &server, &["Id"], &["Id"], &mut stats);assert_eq!(stats.records_created_locally, 1);assert!(statements.is_empty()); // No SQL needed@@ -526,8 +477,8 @@ mod tests {fn logos_merge_by_id_so_same_domain_in_two_scopes_survives() {// A personal logo and a shared manifest's logo for the same domain are different rows by design// (see vault_codec::logos). Merging Logos by Id keeps both.- let mut personal = logo(&crate::vault_codec::logo_id_for_source("m-root", "github.com"), "github.com", "2024-01-01T00:00:00Z");- personal.insert("ManifestId".to_string(), serde_json::json!("m-root"));+ let mut personal = logo(&crate::vault_codec::logo_id_for_source("m-personal", "github.com"), "github.com", "2024-01-01T00:00:00Z");+ personal.insert("ManifestId".to_string(), serde_json::json!("m-personal"));let mut shared = logo(&crate::vault_codec::logo_id_for_source("m-1", "github.com"), "github.com", "2024-01-02T00:00:00Z");shared.insert("ManifestId".to_string(), serde_json::json!("m-1"));@@ -546,24 +497,84 @@ mod tests {#[test]fn logos_merge_resolves_same_scope_rows_by_lww() {- // Within one scope, two writers produce the SAME derived Id, so an ordinary by-Id LWW settles- // whose favicon bytes win.- let id = crate::vault_codec::logo_id_for_source("m-root", "github.com");- let base = vec![logo(&id, "github.com", "2024-01-01T00:00:00Z")];- let incoming = vec![logo(&id, "github.com", "2024-01-02T00:00:00Z")];+ // Within one scope, two writers produce the SAME derived Id, so an ordinary LWW settles whose+ // favicon bytes win, and the UPDATE addresses the row by its full (ManifestId, Id) identity.+ let id = crate::vault_codec::logo_id_for_source("m-personal", "github.com");+ let scoped = |updated_at: &str| {+ let mut row = logo(&id, "github.com", updated_at);+ row.insert("ManifestId".to_string(), serde_json::json!("m-personal"));+ row+ };+ let base = vec![scoped("2024-01-01T00:00:00Z")];+ let incoming = vec![scoped("2024-01-02T00:00:00Z")];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Logos", &base, &incoming, "Id", &mut stats);+ let statements = merge_table("Logos", &base, &incoming, &["ManifestId", "Id"], &["ManifestId", "Id"], &mut stats);assert_eq!(stats.conflicts, 1);assert_eq!(stats.records_inserted, 0, "no second row for the same scope+domain");assert_eq!(statements.len(), 1);assert!(statements[0].sql.starts_with("UPDATE Logos SET"));+ assert!(statements[0].sql.ends_with("WHERE ManifestId = ? AND Id = ?"));assert_eq!(statements[0].params.last().unwrap(), &serde_json::json!(id));}+ #[test]+ fn same_id_in_two_manifests_never_merges() {+ // The point of the composite key: two manifests may each hold a row with the same Id. Neither+ // may update the other, however much newer it is: they are different rows.+ let scoped = |manifest_id: &str, updated_at: &str| {+ let mut row = make_record("shared-id", updated_at);+ row.insert("ManifestId".to_string(), serde_json::json!(manifest_id));+ row+ };++ let output = merge_vaults(MergeInput {+ local_tables: vec![TableData { name: "Items".to_string(), records: vec![scoped("m-1", "2024-01-09T00:00:00Z")] }],+ server_tables: vec![TableData { name: "Items".to_string(), records: vec![scoped("m-personal", "2024-01-01T00:00:00Z")] }],+ })+ .unwrap();++ assert_eq!(output.stats.conflicts, 0, "rows in different manifests are never in conflict");+ assert_eq!(output.statements.len(), 1);+ assert!(output.statements[0].sql.starts_with("INSERT OR REPLACE INTO Items"), "the other manifest's row is inserted alongside");+ }++ #[test]+ fn scoped_update_never_rewrites_the_rows_own_scope() {+ // FieldValues match on (ManifestId, ItemId, FieldKey), so a winning incoming row may carry a+ // different Id than the base row it updates. Neither half of the identity may leak into SET:+ // writing them would move the base row out of the namespace it was matched in.+ let field_value = |id: &str, updated_at: &str| {+ let mut row: Record = HashMap::new();+ row.insert("Id".to_string(), serde_json::json!(id));+ row.insert("ManifestId".to_string(), serde_json::json!("m-personal"));+ row.insert("ItemId".to_string(), serde_json::json!("item-1"));+ row.insert("FieldKey".to_string(), serde_json::json!("username"));+ row.insert("Value".to_string(), serde_json::json!(id));+ row.insert("UpdatedAt".to_string(), serde_json::json!(updated_at));+ row+ };++ let output = merge_vaults(MergeInput {+ local_tables: vec![TableData { name: "FieldValues".to_string(), records: vec![field_value("fv-incoming", "2024-01-09T00:00:00Z")] }],+ server_tables: vec![TableData { name: "FieldValues".to_string(), records: vec![field_value("fv-base", "2024-01-01T00:00:00Z")] }],+ })+ .unwrap();++ assert_eq!(output.statements.len(), 1);+ let stmt = &output.statements[0];+ assert!(stmt.sql.ends_with("WHERE ManifestId = ? AND Id = ?"));++ let assignments: Vec<&str> = stmt.sql.trim_start_matches("UPDATE FieldValues SET ").split(" WHERE ").next().unwrap().split(", ").collect();+ assert!(!assignments.contains(&"ManifestId = ?"), "scope must not be in the SET clause");+ assert!(!assignments.contains(&"Id = ?"), "identity must not be in the SET clause");+ assert_eq!(stmt.params.last().unwrap(), &serde_json::json!("fv-base"), "the base row keeps its own Id");+ }+fn setting(key: &str, value: &str, updated_at: &str) -> Record {let mut r = HashMap::new();+ r.insert("ManifestId".to_string(), serde_json::json!("m-personal"));r.insert("Key".to_string(), serde_json::json!(key));r.insert("Value".to_string(), serde_json::json!(value));r.insert("UpdatedAt".to_string(), serde_json::json!(updated_at));@@ -571,32 +582,52 @@ mod tests {}#[test]- fn settings_merge_by_key_server_wins_updates_by_key() {- // Settings has no "Id" column, its primary key is "Key". A newer server value must UPDATE the- // local row addressed by Key, not be skipped (which is what would happen if merge assumed "Id").- let local = vec![setting("theme", "light", "2024-01-01T00:00:00Z")];- let server = vec![setting("theme", "dark", "2024-01-02T00:00:00Z")];+ fn settings_merge_by_key_within_their_manifest() {+ // A setting is identified by (ManifestId, Key), with no generated id: a newer incoming value+ // updates the base row addressed by that pair instead of becoming a second row.+ let base = vec![setting("theme", "light", "2024-01-01T00:00:00Z")];+ let incoming = vec![setting("theme", "dark", "2024-01-02T00:00:00Z")];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Settings", &local, &server, "Key", &mut stats);+ let statements = merge_table("Settings", &base, &incoming, &["ManifestId", "Key"], &["ManifestId", "Key"], &mut stats);assert_eq!(stats.conflicts, 1);- assert_eq!(stats.records_from_server, 1);+ assert_eq!(stats.records_inserted, 0, "the same key must not produce a second row");assert_eq!(statements.len(), 1);assert!(statements[0].sql.starts_with("UPDATE Settings SET"));- assert!(statements[0].sql.ends_with("WHERE Key = ?"));- // The Key column is used in WHERE, not in the SET clause.+ assert!(statements[0].sql.ends_with("WHERE ManifestId = ? AND Key = ?"));+ // The identity columns address the row; they must never be written back through the SET clause.assert!(!statements[0].sql.contains("Key = ?,"));assert_eq!(statements[0].params.last().unwrap(), &serde_json::json!("theme"));}#[test]- fn settings_merge_by_key_local_and_server_only_rows() {- let local = vec![setting("theme", "dark", "2024-01-02T00:00:00Z")];- let server = vec![setting("language", "en", "2024-01-01T00:00:00Z")];+ fn settings_of_two_manifests_never_merge() {+ // The same key may exist in two manifests; they are different settings, as everywhere else.+ let scoped = |manifest_id: &str, value: &str| {+ let mut row = setting("theme", value, "2024-01-01T00:00:00Z");+ row.insert("ManifestId".to_string(), serde_json::json!(manifest_id));+ row+ };++ let output = merge_vaults(MergeInput {+ local_tables: vec![TableData { name: "Settings".to_string(), records: vec![scoped("m-shared", "dark")] }],+ server_tables: vec![TableData { name: "Settings".to_string(), records: vec![scoped("m-personal", "light")] }],+ })+ .unwrap();++ assert_eq!(output.stats.conflicts, 0, "the same key in two manifests is not a conflict");+ assert_eq!(output.statements.len(), 1);+ assert!(output.statements[0].sql.starts_with("INSERT OR REPLACE INTO Settings"));+ }++ #[test]+ fn settings_merge_local_and_server_only_rows() {+ let base = vec![setting("theme", "dark", "2024-01-02T00:00:00Z")];+ let incoming = vec![setting("language", "en", "2024-01-01T00:00:00Z")];let mut stats = MergeStats::default();- let statements = merge_table_by_id("Settings", &local, &server, "Key", &mut stats);+ let statements = merge_table("Settings", &base, &incoming, &["ManifestId", "Key"], &["ManifestId", "Key"], &mut stats);assert_eq!(stats.records_created_locally, 1);assert_eq!(stats.records_inserted, 1);@@ -608,7 +639,7 @@ mod tests {fn settings_and_encryption_keys_are_syncable() {let settings = SYNCABLE_TABLES.iter().find(|t| t.name == "Settings").unwrap();assert_eq!(settings.primary_key, "Key");- assert!(!settings.uses_composite_key());+ assert!(!settings.uses_composite_key(), "a key already names one setting per manifest; no semantic match key is needed");let keys = SYNCABLE_TABLES.iter().find(|t| t.name == "EncryptionKeys").unwrap();assert_eq!(keys.primary_key, "Id");@@ -617,6 +648,54 @@ mod tests {assert!(SYNCABLE_TABLE_NAMES.contains(&"EncryptionKeys"));}+ #[test]+ fn item_stats_merge_is_last_use_wins_per_item() {+ // ItemStats.Id *is* the item's id, so two devices recording a use of the same item produce the+ // same row and LWW settles it: recording a use is a plain upsert, no semantic match key needed.+ let stat = |manifest_id: &str, item_id: &str, last_used_at: &str| -> Record {+ let mut r = HashMap::new();+ r.insert("ManifestId".to_string(), serde_json::json!(manifest_id));+ r.insert("Id".to_string(), serde_json::json!(item_id));+ r.insert("LastUsedAt".to_string(), serde_json::json!(last_used_at));+ r.insert("UpdatedAt".to_string(), serde_json::json!(last_used_at));+ r+ };++ let output = merge_vaults(MergeInput {+ local_tables: vec![TableData { name: "ItemStats".to_string(), records: vec![stat("m-personal", "i-1", "2024-01-09T00:00:00Z")] }],+ server_tables: vec![TableData { name: "ItemStats".to_string(), records: vec![stat("m-personal", "i-1", "2024-01-01T00:00:00Z")] }],+ })+ .unwrap();++ assert_eq!(output.stats.conflicts, 1);+ assert_eq!(output.stats.records_inserted, 0);+ assert_eq!(output.statements.len(), 1);+ assert!(output.statements[0].sql.starts_with("UPDATE ItemStats SET"));+ assert!(output.statements[0].sql.ends_with("WHERE ManifestId = ? AND Id = ?"));+ }++ #[test]+ fn item_stats_of_two_manifests_never_merge() {+ // The same item id may exist in two manifests; their stats are different rows, as everywhere else.+ let stat = |manifest_id: &str| -> Record {+ let mut r = HashMap::new();+ r.insert("ManifestId".to_string(), serde_json::json!(manifest_id));+ r.insert("Id".to_string(), serde_json::json!("i-1"));+ r.insert("UpdatedAt".to_string(), serde_json::json!("2024-01-01T00:00:00Z"));+ r+ };++ let output = merge_vaults(MergeInput {+ local_tables: vec![TableData { name: "ItemStats".to_string(), records: vec![stat("m-shared")] }],+ server_tables: vec![TableData { name: "ItemStats".to_string(), records: vec![stat("m-personal")] }],+ })+ .unwrap();++ assert_eq!(output.stats.conflicts, 0);+ assert_eq!(output.statements.len(), 1);+ assert!(output.statements[0].sql.starts_with("INSERT OR REPLACE INTO ItemStats"));+ }+#[test]fn logos_config_merges_by_id_not_source() {// Guard on the decision itself: Logos identity is the codec-derived Id, so this table must@@ -628,9 +707,9 @@ mod tests {#[test]fn merge_never_touches_codec_overflow_carrier() {- // The merge base is the server vault, so the codec overflow carrier rides along on the server- // base untouched (implicit server-wins) and needs no special case. Even if a platform were to- // pass the carrier in the merge input, merge must emit NO statement referencing it, it is not+ // The merge base is the server vault, so the carrier rides along on it untouched (implicit+ // server-wins). Even if a platform passes it in the merge input, merge must emit no statement+ // referencing it: it is not a syncable table.// a syncable table.let overflow_table = crate::vault_codec::OVERFLOW_TABLE;let overflow_row = |data: &str| -> Record {@@ -652,6 +731,110 @@ mod tests {assert!(!names.contains(&crate::vault_codec::OVERFLOW_TABLE), "carrier is not part of the merge input");}+ // Cross-manifest guards. The local vault holds every manifest's rows in one SQLite file, kept apart+ // only by the `(ManifestId, Id)` identity, so a merge can resolve a row against one in its own+ // manifest and nowhere else. Asserted for every syncable table at once, so a table added later is+ // covered the day it is registered.++ /// A row of `config` addressed by `manifest_id`, carrying whatever columns the table's own key+ /// needs plus a payload to tell two versions apart.+ fn scoped_row(config: &TableConfig, manifest_id: &str, updated_at: &str, payload: &str) -> Record {+ let mut record: Record = HashMap::new();+ record.insert(crate::vault_codec::MANIFEST_ID_COL.to_string(), serde_json::json!(manifest_id));+ record.insert(config.primary_key.to_string(), serde_json::json!("row-1"));+ for column in config.composite_key_columns {+ record.entry((*column).to_string()).or_insert_with(|| serde_json::json!("k"));+ }+ record.insert("UpdatedAt".to_string(), serde_json::json!(updated_at));+ record.insert("Payload".to_string(), serde_json::json!(payload));+ record+ }++ fn merge_one_table(config: &TableConfig, base: Record, incoming: Record) -> MergeOutput {+ merge_vaults(MergeInput {+ local_tables: vec![TableData { name: config.name.to_string(), records: vec![incoming] }],+ server_tables: vec![TableData { name: config.name.to_string(), records: vec![base] }],+ })+ .unwrap()+ }++ #[test]+ fn every_syncable_table_is_manifest_scoped() {+ // A table registered without `manifest_scoped()` is matched on its primary key alone, so the+ // same id in two manifests becomes one row and the later write silently overwrites the other+ // manifest's. There is no table for which that is right, so the invariant holds for all of them.+ for config in SYNCABLE_TABLES {+ assert!(config.manifest_scoped, "{} must be manifest_scoped or it merges across manifests", config.name);+ assert_eq!(config.identity_columns().first(), Some(&crate::vault_codec::MANIFEST_ID_COL), "{} must be addressed by its manifest first", config.name);+ }+ }++ #[test]+ fn no_syncable_table_merges_a_row_into_another_manifest() {+ // The same id in two manifests is two rows, in every table alike: neither may update the+ // other, however much newer it is.+ for config in SYNCABLE_TABLES {+ let base = scoped_row(config, "m-personal", "2024-01-01T00:00:00Z", "personal");+ let incoming = scoped_row(config, "m-shared", "2024-01-09T00:00:00Z", "shared");+ let output = merge_one_table(config, base, incoming);++ assert_eq!(output.stats.conflicts, 0, "{}: rows in different manifests are not in conflict", config.name);+ assert_eq!(output.statements.len(), 1, "{}", config.name);+ assert!(output.statements[0].sql.starts_with(&format!("INSERT OR REPLACE INTO {}", config.name)), "{}: the other manifest's row is inserted alongside, never merged in", config.name);+ }+ }++ #[test]+ fn no_merge_update_ever_rewrites_a_rows_manifest() {+ // The other half: a winning incoming row is written into the base row it matched, which keeps+ // its own address. `ManifestId` in the SET clause would move a row out of its manifest on an+ // ordinary edit, handing a personal row to whoever holds the manifest it landed in.+ for config in SYNCABLE_TABLES {+ let base = scoped_row(config, "m-personal", "2024-01-01T00:00:00Z", "old");+ let incoming = scoped_row(config, "m-personal", "2024-01-09T00:00:00Z", "new");+ let output = merge_one_table(config, base, incoming);++ assert_eq!(output.statements.len(), 1, "{}", config.name);+ let sql = &output.statements[0].sql;+ let prefix = format!("UPDATE {} SET ", config.name);+ assert!(sql.starts_with(&prefix), "{}: expected an in-place update, got {}", config.name, sql);++ let set_clause = sql.trim_start_matches(&prefix).split(" WHERE ").next().unwrap();+ assert!(!set_clause.contains(crate::vault_codec::MANIFEST_ID_COL), "{}: an update must never rewrite the row's manifest ({})", config.name, sql);+ assert!(sql.ends_with(&format!("WHERE ManifestId = ? AND {} = ?", config.primary_key)), "{}: the base row is addressed by its full identity ({})", config.name, sql);+ }+ }++ #[test]+ fn encryption_keys_of_two_manifests_never_merge() {+ // Spelled out for the one table where a mis-merge is unrecoverable rather than merely wrong: a+ // manifest's keypair decrypts the mail sent to its aliases, so letting another manifest's row+ // overwrite it makes every message already delivered under it unreadable.+ let key = |manifest_id: &str, private_key: &str, updated_at: &str| -> Record {+ [+ ("ManifestId".to_string(), serde_json::json!(manifest_id)),+ ("Id".to_string(), serde_json::json!("ek-1")),+ ("PublicKey".to_string(), serde_json::json!(format!("pub-{manifest_id}"))),+ ("PrivateKey".to_string(), serde_json::json!(private_key)),+ ("IsPrimary".to_string(), serde_json::json!(1)),+ ("UpdatedAt".to_string(), serde_json::json!(updated_at)),+ ]+ .into_iter()+ .collect()+ };++ let output = merge_vaults(MergeInput {+ local_tables: vec![TableData { name: "EncryptionKeys".to_string(), records: vec![key("m-shared", "priv-shared", "2024-01-09T00:00:00Z")] }],+ server_tables: vec![TableData { name: "EncryptionKeys".to_string(), records: vec![key("m-personal", "priv-personal", "2024-01-01T00:00:00Z")] }],+ })+ .unwrap();++ assert_eq!(output.stats.conflicts, 0);+ assert_eq!(output.statements.len(), 1);+ assert!(output.statements[0].sql.starts_with("INSERT OR REPLACE INTO EncryptionKeys"));+ assert!(!output.statements[0].sql.contains("UPDATE"), "the personal manifest's private key is never overwritten by another manifest's");+ }+#[test]fn test_generate_insert_sql() {let record = make_record("test-id", "2024-01-01T00:00:00Z");@@ -667,7 +850,7 @@ mod tests {#[test]fn test_generate_update_sql() {let record = make_record("test-id", "2024-01-01T00:00:00Z");- let stmt = generate_update_sql("Items", &record, "test-id", "Id").unwrap();+ let stmt = generate_update_sql("Items", &record, &[("Id", serde_json::json!("test-id"))]).unwrap();assert!(stmt.sql.starts_with("UPDATE Items SET"));assert!(stmt.sql.contains("WHERE Id = ?"));