Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

Add manifest sharing Rust scaffolding (#2159)

Leendert de Borstc46d30aa7ff6
diff --git a/core/rust/src/uniffi_api.rs b/core/rust/src/uniffi_api.rs
index 7b5841757..20178c037 100644
--- a/core/rust/src/uniffi_api.rs
+++ b/core/rust/src/uniffi_api.rs
@@ -210,6 +210,13 @@ pub fn vault_codec_compute_ciphertext_hash(base64_ciphertext: String) -> String
crate::vault_codec::compute_ciphertext_hash(&base64_ciphertext)
}
+/// Extract the primary encryption-key row from the decrypted `EncryptionKeys` data bucket.
+/// Input: `DataBucket` JSON. Output: the `EncryptionKeys` row JSON object, or `null` when absent.
+#[uniffi::export]
+pub fn vault_codec_extract_primary_encryption_key_from_bucket(bucket_json: String) -> Result<String, VaultError> {
+ crate::vault_codec::extract_primary_encryption_key_from_bucket_json(&bucket_json)
+}
+
// ═══════════════════════════════════════════════════════════════════════════════
// SRP (Secure Remote Password) Functions
// ═══════════════════════════════════════════════════════════════════════════════
diff --git a/core/rust/src/vault_codec/canonicalize.rs b/core/rust/src/vault_codec/canonicalize.rs
index 9172ffc4a..8b04da131 100644
--- a/core/rust/src/vault_codec/canonicalize.rs
+++ b/core/rust/src/vault_codec/canonicalize.rs
@@ -18,8 +18,9 @@ use serde_json::json;
use super::hash::salted_blob_hash;
use super::logos::dedupe_logos_by_source;
-use super::manifest::{BlobEntry, CanonicalizeInput, CanonicalizedVault, CodecOverflow, DataBucket, Manifest, CodecRecord};
+use super::manifest::{BlobEntry, CanonicalizeInput, CanonicalizedVault, CodecOverflow, DataBucket, Manifest, CodecRecord, SharedVault};
use super::materialize::row_key;
+use super::sharing::partition_for_sharing;
use super::types::{blob_spec_for, bucket_categories, bucket_category_for, is_skip_table, primary_key_for, OVERFLOW_TABLE, SCHEMA_VERSION};
use crate::error::VaultResult;
@@ -56,6 +57,9 @@ pub fn canonicalize_from_sqlite(input: CanonicalizeInput) -> VaultResult<Canonic
// the manifest we write is free of any UNIQUE(Source) collision that would break materialize.
dedupe_logos_by_source(&mut all_tables);
+ // Split each shared folder's subtree into its own partition; `all_tables` keeps the root's rows.
+ let shared_partitions = partition_for_sharing(&mut all_tables, &input.shared_folders)?;
+
let mut blobs: HashMap<String, BlobEntry> = HashMap::new();
let mut manifest_tables: HashMap<String, Vec<CodecRecord>> = HashMap::new();
@@ -77,15 +81,7 @@ pub fn canonicalize_from_sqlite(input: CanonicalizeInput) -> VaultResult<Canonic
}
// Manifest table: extract any blob column into the content-addressed map.
- let blob_spec = blob_spec_for(&name);
- let mut out_rows: Vec<CodecRecord> = Vec::with_capacity(records.len());
- for mut row in records {
- if let Some((_, blob_col, kind)) = blob_spec {
- let extracted = extract_blob_cell(row.get(*blob_col), &input.user_salt, kind, &mut blobs);
- row.insert((*blob_col).to_string(), extracted);
- }
- out_rows.push(row);
- }
+ let out_rows = extract_table_blobs(&name, records, &input.user_salt, &mut blobs);
manifest_tables.insert(name, out_rows);
}
@@ -101,12 +97,41 @@ pub fn canonicalize_from_sqlite(input: CanonicalizeInput) -> VaultResult<Canonic
}
}
+ // Each shared partition becomes its own manifest, its blobs hashed with its own per-manifest salt.
+ let mut shared_vaults: Vec<SharedVault> = Vec::with_capacity(shared_partitions.len());
+ for partition in shared_partitions {
+ let mut shared_blobs: HashMap<String, BlobEntry> = HashMap::new();
+ let shared_tables: HashMap<String, Vec<CodecRecord>> = partition
+ .tables
+ .into_iter()
+ .map(|(name, records)| {
+ let out_rows = extract_table_blobs(&name, records, &partition.user_salt, &mut shared_blobs);
+ (name, out_rows)
+ })
+ .collect();
+ shared_vaults.push(SharedVault {
+ folder_id: partition.folder_id.clone(),
+ manifest: Manifest {
+ schema_version: SCHEMA_VERSION,
+ migration_id: input.migration_id.clone(),
+ version: input.version.clone(),
+ user_salt: partition.user_salt,
+ canonicalized_at: input.canonicalized_at.clone(),
+ shared_folder_id: Some(partition.folder_id),
+ tables: shared_tables,
+ extra: HashMap::new(),
+ },
+ blobs: shared_blobs,
+ });
+ }
+
let manifest = Manifest {
schema_version: SCHEMA_VERSION,
migration_id: input.migration_id,
version: input.version,
user_salt: input.user_salt,
canonicalized_at: input.canonicalized_at,
+ shared_folder_id: None,
tables: manifest_tables,
extra: HashMap::new(),
};
@@ -122,9 +147,24 @@ pub fn canonicalize_from_sqlite(input: CanonicalizeInput) -> VaultResult<Canonic
manifest,
data_buckets,
blobs,
+ shared_vaults,
})
}
+/// Extract `table`'s blob column (if it owns one) into `blobs`, returning the rewritten rows.
+fn extract_table_blobs(table: &str, records: Vec<CodecRecord>, user_salt: &str, blobs: &mut HashMap<String, BlobEntry>) -> Vec<CodecRecord> {
+ let blob_spec = blob_spec_for(table);
+ let mut out_rows: Vec<CodecRecord> = Vec::with_capacity(records.len());
+ for mut row in records {
+ if let Some((_, blob_col, kind)) = blob_spec {
+ let extracted = extract_blob_cell(row.get(*blob_col), user_salt, kind, blobs);
+ row.insert((*blob_col).to_string(), extracted);
+ }
+ out_rows.push(row);
+ }
+ out_rows
+}
+
/// Extract a blob column cell: if it holds non-empty `{ "__b64" }` bytes, hash + register them and
/// return a blob-ref; otherwise return JSON null.
fn extract_blob_cell(
diff --git a/core/rust/src/vault_codec/manifest.rs b/core/rust/src/vault_codec/manifest.rs
index 45c26a2d6..f5aac5d8d 100644
--- a/core/rust/src/vault_codec/manifest.rs
+++ b/core/rust/src/vault_codec/manifest.rs
@@ -18,10 +18,16 @@ pub struct Manifest {
pub migration_id: String,
/// Human-readable data-model version label (e.g. "2.0.0").
pub version: String,
- /// Per-user salt for blob hashing (hex).
+ /// Per-manifest salt for blob hashing (hex). For a shared-folder manifest this salt is shared by
+ /// every participant (it lives inside the encrypted manifest itself) so all of them compute the
+ /// same content-addressed blob hashes.
pub user_salt: String,
/// Timestamp when this canonical snapshot was produced (ISO-8601).
pub canonicalized_at: String,
+ /// Set on a shared-folder manifest: the `Folders.Id` of the folder this manifest carries. `None`
+ /// for a root (personal) manifest.
+ #[serde(default, skip_serializing_if = "Option::is_none")]
+ pub shared_folder_id: Option<String>,
/// Tables mapped to arrays of row objects. Blob columns replaced with `{ "__blobRef", "__blobKind" }`.
pub tables: HashMap<String, Vec<CodecRecord>>,
/// Forward-compat: unknown top-level keys preserved on round-trip.
@@ -71,8 +77,29 @@ pub struct BlobEntry {
pub bytes_base64: String,
}
-/// Result of canonicalizing a vault: the manifest, its data buckets (one per category, e.g. Settings),
-/// and the content-addressed blob map.
+/// Identifies one shared folder for the canonicalize split: the folder whose subtree is written into
+/// its own manifest, and the per-manifest salt used for that manifest's blob hashing (the salt is
+/// shared by every participant of the folder and travels inside the encrypted shared manifest).
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SharedFolderSpec {
+ pub folder_id: String,
+ pub user_salt: String,
+}
+
+/// One shared-folder manifest produced by the canonicalize split: the folder it represents, the
+/// manifest carrying its subtree, and the blob map hashed with that manifest's own salt.
+#[derive(Debug, Clone, Serialize, Deserialize)]
+#[serde(rename_all = "camelCase")]
+pub struct SharedVault {
+ pub folder_id: String,
+ pub manifest: Manifest,
+ /// hash > blob plaintext (base64), hashed with this manifest's salt.
+ pub blobs: HashMap<String, BlobEntry>,
+}
+
+/// Result of canonicalizing a vault: the root manifest, its data buckets (one per category, e.g.
+/// Settings), the root content-addressed blob map, and one [`SharedVault`] per requested shared folder.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CanonicalizedVault {
@@ -80,6 +107,9 @@ pub struct CanonicalizedVault {
pub data_buckets: Vec<DataBucket>,
/// hash > blob plaintext (base64).
pub blobs: HashMap<String, BlobEntry>,
+ /// One entry per [`CanonicalizeInput::shared_folders`] spec, in spec order.
+ #[serde(default, skip_serializing_if = "Vec::is_empty")]
+ pub shared_vaults: Vec<SharedVault>,
}
/// A single table's rows for reassembly.
@@ -124,7 +154,7 @@ impl CodecOverflow {
vec![row]
}
- /// Parse an `OVERFLOW_TABLE` row set read back from the vault DB. Tolerant: no rows, a missing
+ /// Parse an `OVERFLOW_TABLE` row set read back from the vault DB. Tolerant to: no rows, a missing
/// `Data` column, or unparseable JSON all yield an empty overflow (better to push what we have
/// than refuse to push at all).
pub fn from_table_records(records: &[CodecRecord]) -> Self {
@@ -139,7 +169,7 @@ impl CodecOverflow {
/// Materialized tables the platform inserts into a fresh schema DB. Blob columns carry
/// `{ "__blobRef": hash }`; inline byte columns carry `{ "__b64": ... }`. Any overflow (see
-/// [`CodecOverflow`]) is already included in `tables` as the `OVERFLOW_TABLE` row — the platform
+/// [`CodecOverflow`]) is already included in `tables` as the `OVERFLOW_TABLE` row, the platform
/// inserts it like any other table and needs no separate persistence. The `overflow` field is a
/// diagnostics copy of the same data (for logging), not something the platform must store.
#[derive(Debug, Clone, Serialize, Deserialize)]
@@ -155,15 +185,17 @@ pub struct MaterializedTables {
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CanonicalizeInput {
- /// All local vault tables (a plain `SELECT *` read). When the read includes the
- /// [`OVERFLOW_TABLE`](super::types::OVERFLOW_TABLE) row written by the last materialize, its
- /// overflow (a newer writer's tables/columns this schema can't hold) is re-merged automatically.
+ /// All local vault tables (a plain `SELECT *` read from the vault DB). When the read includes the
+ /// [`OVERFLOW_TABLE`](super::types::OVERFLOW_TABLE) row written by the last materialize, its overflow
+ /// (a newer writer's tables/columns this schema can't hold) is re-merged automatically.
pub tables: Vec<CodecTableData>,
pub user_salt: String,
pub migration_id: String,
#[serde(default = "default_version")]
pub version: String,
pub canonicalized_at: String,
+ #[serde(default)]
+ pub shared_folders: Vec<SharedFolderSpec>,
}
fn default_version() -> String {
@@ -177,6 +209,8 @@ pub struct MaterializeInput {
pub manifest: Manifest,
#[serde(default)]
pub data_buckets: Vec<DataBucket>,
+ #[serde(default)]
+ pub shared_manifests: Vec<Manifest>,
/// The caller's local SQLite schema: table > column names. When present, rows are filtered down
/// to what the schema can hold and the remainder lands in [`MaterializedTables::overflow`];
/// when absent, rows pass through verbatim (legacy behavior — unknown columns crash the insert).
diff --git a/core/rust/src/vault_codec/materialize.rs b/core/rust/src/vault_codec/materialize.rs
index ce680856a..36b83f1be 100644
--- a/core/rust/src/vault_codec/materialize.rs
+++ b/core/rust/src/vault_codec/materialize.rs
@@ -8,11 +8,11 @@
//! to the caller and let the caller handle the actual SQLite database creation and data insertion.
//!
//! Forward compatibility: when the caller supplies its local schema (`schema_columns`), anything a
-//! newer writer put in the manifest that this schema cannot hold — whole unknown tables or unknown
-//! columns on known tables — is split into [`CodecOverflow`] instead of being emitted (which would
+//! newer writer put in the manifest that this schema cannot hold (whole unknown tables or unknown
+//! columns on known tables is split into [`CodecOverflow`] instead of being emitted (which would
//! crash the platform insert). The overflow is emitted as a regular table row (`OVERFLOW_TABLE`),
//! so it lives inside the vault DB itself and `canonicalize_from_sqlite` re-merges it from the
-//! ordinary table read — this client's next push never drops the data, and no platform has to wire
+//! ordinary table read, this client's next push never drops the data, and no platform has to wire
//! (or remember) a separate persistence channel.
use std::collections::{HashMap, HashSet};
@@ -21,14 +21,22 @@ use super::manifest::{CodecOverflow, CodecRecord, CodecTableData, MaterializeInp
use super::types::{is_skip_table, primary_key_for, OVERFLOW_TABLE};
use crate::error::VaultResult;
-/// Materialize the manifest + its data buckets into the table set the platform inserts.
+/// Materialize the manifest + its data buckets into the table set the platform inserts. Shared-folder
+/// manifests (if any) are combined into the root's table set first, root-wins primary-key dedup,
+/// personal-table stripping, dangling-parent repair, and cross-manifest logo dedup.
pub fn materialize_as_sqlite(input: MaterializeInput) -> VaultResult<MaterializedTables> {
- let MaterializeInput { manifest, data_buckets, schema_columns } = input;
+ let MaterializeInput { manifest, data_buckets, schema_columns, shared_manifests } = input;
+
+ let combined = if shared_manifests.is_empty() {
+ manifest.tables
+ } else {
+ super::sharing::combine_manifest_tables(manifest.tables, shared_manifests)
+ };
let mut overflow = CodecOverflow::default();
- let mut tables: Vec<CodecTableData> = Vec::with_capacity(manifest.tables.len() + data_buckets.len());
+ let mut tables: Vec<CodecTableData> = Vec::with_capacity(combined.len() + data_buckets.len());
- for (name, records) in manifest.tables {
+ for (name, records) in combined {
// OVERFLOW_TABLE is local-only bookkeeping: it must never occur in a manifest, and passing
// one through would collide with the row this function emits below.
if is_skip_table(&name) || name == OVERFLOW_TABLE {
diff --git a/core/rust/src/vault_codec/mod.rs b/core/rust/src/vault_codec/mod.rs
index b7cb5dcd3..cbd5d97f8 100644
--- a/core/rust/src/vault_codec/mod.rs
+++ b/core/rust/src/vault_codec/mod.rs
@@ -13,6 +13,7 @@ mod hash;
mod logos;
mod manifest;
mod materialize;
+mod sharing;
mod types;
mod validate;
@@ -25,11 +26,12 @@ use crate::error::{VaultError, VaultResult};
pub use hash::{canonical_json, content_hash};
pub use manifest::{
BlobEntry, BucketLayoutEntry, CanonicalizeInput, CanonicalizedVault, CodecOverflow, DataBucket,
- Manifest, MaterializeInput, MaterializedTables, CodecRecord, CodecTableData,
+ Manifest, MaterializeInput, MaterializedTables, CodecRecord, CodecTableData, SharedFolderSpec, SharedVault,
};
+pub use sharing::extract_primary_encryption_key_from_bucket;
pub use types::{
- bucket_categories, bucket_category_for, tables_for_category, BLOB_COLUMNS, BUCKET_TABLES,
- OVERFLOW_ROW_ID, OVERFLOW_TABLE, SCHEMA_VERSION, SKIP_TABLES,
+ bucket_categories, bucket_category_for, is_personal_table, tables_for_category, BLOB_COLUMNS, BUCKET_TABLES,
+ OVERFLOW_ROW_ID, OVERFLOW_TABLE, PERSONAL_TABLES, SCHEMA_VERSION, SKIP_TABLES,
};
pub use validate::ValidationResult;
@@ -200,3 +202,6 @@ pub fn validate_data_bucket_json(data_bucket_json: &str) -> VaultResult<String>
#[cfg(test)]
mod tests;
+
+#[cfg(test)]
+mod sharing_tests;
diff --git a/core/rust/src/vault_codec/sharing.rs b/core/rust/src/vault_codec/sharing.rs
new file mode 100644
index 000000000..7616c6642
--- /dev/null
+++ b/core/rust/src/vault_codec/sharing.rs
@@ -0,0 +1,306 @@
+//! Shared-folder 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:
+//!
+//! - **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.
+//!
+//! - **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.
+
+use std::collections::{HashMap, HashSet};
+
+use serde_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 crate::error::{VaultError, VaultResult};
+
+const FOLDERS_TABLE: &str = "Folders";
+const ITEMS_TABLE: &str = "Items";
+const PARENT_FOLDER_ID_COL: &str = "ParentFolderId";
+const FOLDER_ID_COL: &str = "FolderId";
+const ITEM_ID_COL: &str = "ItemId";
+const ID_COL: &str = "Id";
+
+/// 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")]),
+ ("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>>,
+}
+
+/// 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());
+ }
+
+ let mut seen_spec_folders: 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)));
+ }
+ }
+
+ // 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)?;
+
+ let mut partitions: Vec<SharedPartition> = specs
+ .iter()
+ .map(|spec| SharedPartition { folder_id: spec.folder_id.clone(), user_salt: spec.user_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();
+ if let Some(folder_rows) = tables.remove(FOLDERS_TABLE) {
+ let mut root_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()) {
+ row.insert(PARENT_FOLDER_ID_COL.to_string(), Value::Null);
+ }
+ partitions[spec_idx].tables.entry(FOLDERS_TABLE.to_string()).or_default().push(row);
+ }
+ None => root_rows.push(row),
+ }
+ }
+ tables.insert(FOLDERS_TABLE.to_string(), root_rows);
+ }
+
+ // 2. Items follow their folder; remember each shared item for rule 3.
+ 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);
+ }
+ partitions[spec_idx].tables.entry(ITEMS_TABLE.to_string()).or_default().push(row);
+ }
+ None => root_rows.push(row),
+ }
+ }
+ tables.insert(ITEMS_TABLE.to_string(), root_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),
+ }
+ }
+ tables.insert(name, root_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());
+ }
+ }
+ 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);
+ }
+ }
+ }
+ }
+
+ 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());
+ }
+ }
+ // 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);
+ }
+ }
+ tables.insert((*target).to_string(), root_rows);
+ }
+
+ 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.
+ 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);
+ }
+ }
+ }
+
+ for manifest in shared_manifests {
+ 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) {
+ 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) {
+ target.push(row);
+ }
+ }
+ None => target.push(row),
+ }
+ }
+ }
+ }
+
+ // 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.
+ 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);
+
+ 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) {
+ 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);
+ }
+ }
+ }
+ }
+}
+
+/// Pick the current primary key row out of an `EncryptionKeys` table: the first non-deleted primary row.
+fn primary_key_row(tables: &HashMap<String, Vec<CodecRecord>>) -> Option<CodecRecord> {
+ tables
+ .get("EncryptionKeys")?
+ .iter()
+ .find(|row| is_truthy(row.get("IsPrimary")) && !is_truthy(row.get("IsDeleted")))
+ .cloned()
+}
+
+/// Extract the primary encryption-key row (the user's asymmetric keypair, e.g. for unwrapping shared-folder VEKs)
+/// straight from the decrypted `EncryptionKeys` data bucket.
+pub fn extract_primary_encryption_key_from_bucket(bucket: &DataBucket) -> Option<CodecRecord> {
+ primary_key_row(&bucket.tables)
+}
+
+/// 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);
+ }
+ }
+
+ 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
+ )));
+ }
+ 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)
+}
+
+fn str_col<'a>(row: &'a CodecRecord, column: &str) -> Option<&'a str> {
+ row.get(column).and_then(|v| v.as_str())
+}
+
+/// SQLite-tolerant truthiness: boolean true, non-zero number, or "1"/"true" strings.
+fn is_truthy(value: Option<&Value>) -> bool {
+ match value {
+ Some(Value::Bool(b)) => *b,
+ Some(Value::Number(n)) => n.as_f64().map(|f| f != 0.0).unwrap_or(false),
+ Some(Value::String(s)) => s == "1" || s.eq_ignore_ascii_case("true"),
+ _ => false,
+ }
+}
diff --git a/core/rust/src/vault_codec/tests.rs b/core/rust/src/vault_codec/tests.rs
index 91c9797e4..e2bd43b63 100644
--- a/core/rust/src/vault_codec/tests.rs
+++ b/core/rust/src/vault_codec/tests.rs
@@ -29,6 +29,7 @@ fn basic_input(tables: Vec<CodecTableData>) -> CanonicalizeInput {
migration_id: "20250101000000_Init".to_string(),
version: "2.0.0".to_string(),
canonicalized_at: "2026-01-01T00:00:00.000Z".to_string(),
+ shared_folders: Vec::new(),
}
}
@@ -56,7 +57,7 @@ fn bucket_layout_matches_bucket_tables_source_of_truth() {
assert_eq!(bucket_category_for(table), Some(entry.category.as_str()));
}
}
- assert_eq!(layout.iter().map(|e| e.category.as_str()).collect::<Vec<_>>(), vec!["Settings"]);
+ assert_eq!(layout.iter().map(|e| e.category.as_str()).collect::<Vec<_>>(), vec!["Settings", "EncryptionKeys"]);
assert_eq!(bucket_layout_json().unwrap(), serde_json::to_string(&layout).unwrap());
}
@@ -122,7 +123,7 @@ fn inline_b64_columns_survive_roundtrip() {
let cell = &out.manifest.tables["Items"][0]["Secret"];
assert_eq!(cell["__b64"], json!(b64(&secret)));
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None, shared_manifests: vec![] }).unwrap();
let items = re.tables.iter().find(|t| t.name == "Items").unwrap();
assert_eq!(items.records[0]["Secret"]["__b64"], json!(b64(&secret)));
}
@@ -135,7 +136,7 @@ fn materialize_as_sqlite_emits_settings_table_and_migration_id() {
CodecTableData { name: "Settings".to_string(), records: vec![row(&[("Key", json!("k"))])] },
]);
let out = canonicalize_from_sqlite(input).unwrap();
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None, shared_manifests: vec![] }).unwrap();
assert_eq!(re.migration_id, "20250101000000_Init");
assert!(re.tables.iter().any(|t| t.name == "Settings" && t.records.len() == 1));
assert!(re.tables.iter().any(|t| t.name == "Items"));
@@ -150,7 +151,7 @@ fn materialize_as_sqlite_drops_skip_tables() {
manifest
.tables
.insert("android_metadata".to_string(), vec![row(&[("locale", json!("en_US"))])]);
- let re = materialize_as_sqlite(MaterializeInput { manifest, data_buckets: vec![], schema_columns: None }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest, data_buckets: vec![], schema_columns: None, shared_manifests: vec![] }).unwrap();
assert!(!re.tables.iter().any(|t| t.name == "android_metadata"));
}
@@ -167,7 +168,7 @@ fn full_roundtrip_with_blobs_is_semantically_equal() {
let out = canonicalize_from_sqlite(basic_input(tables)).unwrap();
assert_eq!(out.blobs.len(), 2);
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest.clone(), data_buckets: out.data_buckets.clone(), schema_columns: None }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest.clone(), data_buckets: out.data_buckets.clone(), schema_columns: None, shared_manifests: vec![] }).unwrap();
// Items/Logos/Attachments/Settings all present (skip tables aside).
for name in ["Items", "Logos", "Attachments", "Settings"] {
assert!(re.tables.iter().any(|t| t.name == name), "missing table {name}");
@@ -410,7 +411,7 @@ fn materialize_splits_unknown_columns_into_overflow_table_and_canonicalize_remer
}]))
.unwrap();
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: Some(old_client_schema()) }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: Some(old_client_schema()), shared_manifests: vec![] }).unwrap();
let items = re.tables.iter().find(|t| t.name == "Items").unwrap();
assert!(!items.records[0].contains_key("AliasEnabled"), "unknown column filtered out of the insert set");
assert_eq!(items.records[0]["Name"], json!("GitHub"));
@@ -454,9 +455,10 @@ fn materialize_splits_unknown_tables_into_overflow_and_canonicalize_reemits() {
// in this client's schema; both must round-trip through the overflow row back to their original place.
let mut out = canonicalize_from_sqlite(basic_input(vec![CodecTableData { name: "Items".to_string(), records: vec![row(&[("Id", json!("i1"))])] }])).unwrap();
out.manifest.tables.insert("NewTable".to_string(), vec![row(&[("Id", json!("n1")), ("Data", json!("x"))])]);
- out.data_buckets[0].tables.insert("Preferences".to_string(), vec![row(&[("Key", json!("p1"))])]);
+ let settings_bucket = out.data_buckets.iter_mut().find(|b| b.category == "Settings").expect("Settings bucket");
+ settings_bucket.tables.insert("Preferences".to_string(), vec![row(&[("Key", json!("p1"))])]);
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: Some(old_client_schema()) }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: Some(old_client_schema()), shared_manifests: vec![] }).unwrap();
assert!(!re.tables.iter().any(|t| t.name == "NewTable" || t.name == "Preferences"), "unknown tables never reach the insert set");
assert_eq!(re.overflow.tables["NewTable"].len(), 1);
assert_eq!(re.overflow.bucket_tables["Settings"]["Preferences"].len(), 1);
@@ -513,7 +515,7 @@ fn materialize_without_schema_columns_passes_rows_through_verbatim() {
records: vec![row(&[("Id", json!("i1")), ("AliasEnabled", json!(true))])],
}]))
.unwrap();
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: None, shared_manifests: vec![] }).unwrap();
let items = re.tables.iter().find(|t| t.name == "Items").unwrap();
assert_eq!(items.records[0]["AliasEnabled"], json!(true));
assert!(re.overflow.is_empty());
@@ -527,7 +529,7 @@ fn materialize_drops_overflow_table_smuggled_into_a_manifest() {
let mut out = canonicalize_from_sqlite(basic_input(vec![CodecTableData { name: "Items".to_string(), records: vec![row(&[("Id", json!("i1")), ("AliasEnabled", json!(true))])] }])).unwrap();
out.manifest.tables.insert(OVERFLOW_TABLE.to_string(), vec![row(&[("Id", json!("smuggled")), ("Data", json!("{}"))])]);
- let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: Some(old_client_schema()) }).unwrap();
+ let re = materialize_as_sqlite(MaterializeInput { manifest: out.manifest, data_buckets: out.data_buckets, schema_columns: Some(old_client_schema()), shared_manifests: vec![] }).unwrap();
let overflow_table = overflow_table_of(&re).expect("legitimate overflow row still emitted");
assert_eq!(overflow_table.records.len(), 1);
assert_eq!(overflow_table.records[0]["Id"], json!(OVERFLOW_ROW_ID), "smuggled row dropped, only the codec's own row remains");
diff --git a/core/rust/src/vault_codec/validate.rs b/core/rust/src/vault_codec/validate.rs
index e7263e378..8c4759e91 100644
--- a/core/rust/src/vault_codec/validate.rs
+++ b/core/rust/src/vault_codec/validate.rs
@@ -52,6 +52,18 @@ pub fn validate_manifest(manifest: &Manifest) -> ValidationResult {
};
}
+ // A shared-folder manifest is readable by other users: it must never carry personal tables
+ // (key material, bucketed settings). See `types::is_personal_table`.
+ if manifest.shared_folder_id.is_some() {
+ for name in manifest.tables.keys() {
+ if super::types::is_personal_table(name) && !manifest.tables[name].is_empty() {
+ failed.push("shared-manifest-carries-personal-table".to_string());
+ explain.push(format!("Shared manifest carries personal table {}", name));
+ break;
+ }
+ }
+ }
+
let items = table(manifest, "Items");
let folders = table(manifest, "Folders");
let tags = table(manifest, "Tags");