From f41f09e37d81bb2fe4e64c929e52c2babb379fe0 Mon Sep 17 00:00:00 2001 From: tom27052006 <83423411+tom27052006@users.noreply.github.com> Date: Thu, 29 Oct 2026 23:54:42 +0100 Subject: [PATCH] Add My Items (default user collections) --- .env.template | 1 + .../down.sql | 0 .../up.sql | 6 + .../down.sql | 0 .../up.sql | 6 + .../down.sql | 0 .../up.sql | 6 + src/api/core/ciphers.rs | 157 +++++-- src/api/core/events.rs | 4 +- src/api/core/organizations.rs | 378 +++++++++++++--- src/config.rs | 1 + src/db/models/cipher.rs | 131 +++++- src/db/models/collection.rs | 427 +++++++++++++++++- src/db/models/event.rs | 2 +- src/db/models/org_policy.rs | 10 + src/db/models/organization.rs | 77 +++- src/db/models/user.rs | 27 +- src/db/schema.rs | 2 + src/util.rs | 42 ++ 19 files changed, 1135 insertions(+), 142 deletions(-) create mode 100644 migrations/mysql/2026-09-23-120000_add_default_user_collection/down.sql create mode 100644 migrations/mysql/2026-09-23-120000_add_default_user_collection/up.sql create mode 100644 migrations/postgresql/2026-09-23-120000_add_default_user_collection/down.sql create mode 100644 migrations/postgresql/2026-09-23-120000_add_default_user_collection/up.sql create mode 100644 migrations/sqlite/2026-09-23-120000_add_default_user_collection/down.sql create mode 100644 migrations/sqlite/2026-09-23-120000_add_default_user_collection/up.sql diff --git a/.env.template b/.env.template index cc195375..e9876602 100644 --- a/.env.template +++ b/.env.template @@ -406,6 +406,7 @@ ## - "pm-30529-webauthn-related-origins": Allow passkeys stored in the vault to be used on related origins listed in a site's /.well-known/webauthn file (Browser >= 2026.2.0) ## - "pm-32009-new-item-types": Enable new item types: Bank Account, Driver's License, and Passport (Clients >= 2026.4.0) ## - "pm-34171-card-scanner": Enable the new card scanner feature on mobile (Android >= 2026.4.1, iOS >= 2026.4.1) +## - "pm-20558-migrate-myvault-to-myitems": On mobile, ask members that the organization data ownership policy applies to, and that still have personal items, to transfer them to their My Items or to leave the organization (Android, iOS) ## - "enable-basic-auth-response": Enable HTTP Basic Auth autofill in the browser extension (Browser >= 2026.9.0) ## - "undetermined-cipher-scenario-logic": Enable the rewritten add/update login notification triggering logic in the browser extension (Browser >= 2026.2.0) ## - "biometrics-sdk-ipc": Enable biometric unlock over the SDK IPC framework (Desktop >= 2026.9.0, Browser >= 2026.9.0) diff --git a/migrations/mysql/2026-09-23-120000_add_default_user_collection/down.sql b/migrations/mysql/2026-09-23-120000_add_default_user_collection/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/mysql/2026-09-23-120000_add_default_user_collection/up.sql b/migrations/mysql/2026-09-23-120000_add_default_user_collection/up.sql new file mode 100644 index 00000000..4184ea04 --- /dev/null +++ b/migrations/mysql/2026-09-23-120000_add_default_user_collection/up.sql @@ -0,0 +1,6 @@ +-- Older releases ignore both columns after a downgrade: a My Items collection becomes a regular one there, which +-- Owners, Admins and members with access to all collections see with its items. +ALTER TABLE collections ADD COLUMN default_user_uuid CHAR(36); +ALTER TABLE collections ADD COLUMN default_user_collection_email TEXT; +-- A member has at most one My Items collection per organization. NULL (a shared collection) never conflicts. +CREATE UNIQUE INDEX collections_default_user_uuid ON collections (org_uuid, default_user_uuid); diff --git a/migrations/postgresql/2026-09-23-120000_add_default_user_collection/down.sql b/migrations/postgresql/2026-09-23-120000_add_default_user_collection/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/postgresql/2026-09-23-120000_add_default_user_collection/up.sql b/migrations/postgresql/2026-09-23-120000_add_default_user_collection/up.sql new file mode 100644 index 00000000..7c80ffe1 --- /dev/null +++ b/migrations/postgresql/2026-09-23-120000_add_default_user_collection/up.sql @@ -0,0 +1,6 @@ +-- Older releases ignore both columns after a downgrade: a My Items collection becomes a regular one there, which +-- Owners, Admins and members with access to all collections see with its items. +ALTER TABLE collections ADD COLUMN default_user_uuid TEXT; +ALTER TABLE collections ADD COLUMN default_user_collection_email TEXT; +-- A member has at most one My Items collection per organization. NULL (a shared collection) never conflicts. +CREATE UNIQUE INDEX collections_default_user_uuid ON collections (org_uuid, default_user_uuid); diff --git a/migrations/sqlite/2026-09-23-120000_add_default_user_collection/down.sql b/migrations/sqlite/2026-09-23-120000_add_default_user_collection/down.sql new file mode 100644 index 00000000..e69de29b diff --git a/migrations/sqlite/2026-09-23-120000_add_default_user_collection/up.sql b/migrations/sqlite/2026-09-23-120000_add_default_user_collection/up.sql new file mode 100644 index 00000000..7c80ffe1 --- /dev/null +++ b/migrations/sqlite/2026-09-23-120000_add_default_user_collection/up.sql @@ -0,0 +1,6 @@ +-- Older releases ignore both columns after a downgrade: a My Items collection becomes a regular one there, which +-- Owners, Admins and members with access to all collections see with its items. +ALTER TABLE collections ADD COLUMN default_user_uuid TEXT; +ALTER TABLE collections ADD COLUMN default_user_collection_email TEXT; +-- A member has at most one My Items collection per organization. NULL (a shared collection) never conflicts. +CREATE UNIQUE INDEX collections_default_user_uuid ON collections (org_uuid, default_user_uuid); diff --git a/src/api/core/ciphers.rs b/src/api/core/ciphers.rs index 78c27b39..4fb2579b 100644 --- a/src/api/core/ciphers.rs +++ b/src/api/core/ciphers.rs @@ -283,7 +283,31 @@ async fn cipher_json_for_scope( CipherAccessScope::User => { cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, conn).await } - CipherAccessScope::OrganizationAdmin => cipher.to_json_org_admin(&headers.host, &headers.user.uuid, conn).await, + CipherAccessScope::OrganizationAdmin => { + let mut response = cipher.to_json_org_admin(&headers.host, &headers.user.uuid, conn).await?; + add_my_items_collection_ids(&mut response, cipher, headers, conn).await; + Ok(response) + } + } +} + +/// Like upstream's administrative cipher responses, those to members with organization-wide cipher authority also +/// list the members' My Items collections the cipher is in, which they don't see otherwise. +async fn add_my_items_collection_ids(response: &mut Value, cipher: &Cipher, headers: &Headers, conn: &DbConn) { + let Some(org_uuid) = &cipher.organization_uuid else { + return; + }; + let member = Membership::find_confirmed_by_user_and_org(&headers.user.uuid, org_uuid, conn).await; + if CipherAccessScope::for_member(member.as_ref()) != CipherAccessScope::OrganizationAdmin { + return; + } + if let Some(collection_ids) = response["collectionIds"].as_array_mut() { + for my_items in CollectionCipher::find_my_items_of_cipher(&cipher.uuid, conn).await { + let my_items = json!(my_items); + if !collection_ids.contains(&my_items) { + collection_ids.push(my_items); + } + } } } @@ -929,33 +953,12 @@ async fn post_ciphers_import(data: Json, headers: Headers, conn: DbC // TODO: See if we can optimize the whole cipher adding/importing and prevent duplicate code and checks. Cipher::validate_cipher_data(&data.ciphers)?; - // Read and create the folders - let existing_folders: HashSet> = - Folder::find_by_user(&headers.user.uuid, &conn).await.into_iter().map(|f| Some(f.uuid)).collect(); - let mut folders: Vec = Vec::with_capacity(data.folders.len()); - for folder in data.folders { - let folder_id = if existing_folders.contains(&folder.id) { - folder.id.unwrap() - } else { - let mut new_folder = Folder::new(headers.user.uuid.clone(), folder.name); - new_folder.save(&conn).await?; - new_folder.uuid - }; - - folders.push(folder_id); - } - - // Read the relations between folders and ciphers - // Ciphers can only be in one folder at the same time - let mut relations_map = HashMap::with_capacity(data.folder_relationships.len()); - for relation in data.folder_relationships { - relations_map.insert(relation.key, relation.value); - } + let relations = data.folder_relationships.into_iter().map(|r| (r.key, r.value)); + let cipher_folders = import_folders(data.folders, relations, &headers.user.uuid, &conn).await?; // Read and create the ciphers for (index, mut cipher_data) in data.ciphers.into_iter().enumerate() { - let folder_id = relations_map.get(&index).and_then(|i| folders.get(*i).cloned()); - cipher_data.folder_id = folder_id; + cipher_data.folder_id = cipher_folders.get(&index).cloned(); // A personal import always creates personal ciphers, the organization ID is ignored instead of rejected // Upstream: https://github.com/bitwarden/server/blob/b06b81c1fb33ce9c9872b96397ba2b4d879b66af/src/Api/Tools/Controllers/ImportCiphersController.cs#L58 cipher_data.organization_id = None; @@ -972,6 +975,32 @@ async fn post_ciphers_import(data: Json, headers: Headers, conn: DbC Ok(()) } +/// Creates the imported folders the user doesn't have yet and returns the folder of each imported cipher, by index. +/// `relations` are the (cipher index, folder index) pairs of the import, a cipher can only be in one folder. +pub async fn import_folders( + folders: Vec, + relations: impl Iterator, + user_id: &UserId, + conn: &DbConn, +) -> Result, crate::Error> { + let existing_folders: HashSet> = + Folder::find_by_user(user_id, conn).await.into_iter().map(|f| Some(f.uuid)).collect(); + let mut folder_ids: Vec = Vec::with_capacity(folders.len()); + for folder in folders { + let folder_id = if existing_folders.contains(&folder.id) { + folder.id.unwrap() + } else { + let mut new_folder = Folder::new(user_id.clone(), folder.name); + new_folder.save(conn).await?; + new_folder.uuid + }; + + folder_ids.push(folder_id); + } + + Ok(relations.filter_map(|(cipher, folder)| Some((cipher, folder_ids.get(folder)?.clone()))).collect()) +} + /// Called when an org admin modifies an existing org cipher. #[put("/ciphers//admin", data = "")] async fn put_cipher_admin( @@ -1217,15 +1246,41 @@ async fn update_collections_by_scope( err!("Invalid collection ID provided") } } + // The My Items rule looks at every collection the user sees the cipher in, not only the writable ones + let visible = HashSet::::from_iter( + cipher.get_accessible_collections(headers.user.uuid.clone(), &conn).await, + ); + // Only looked up when the cipher is to be added to a My Items collection + let mut cipher_my_items = None; let mut changes = Vec::new(); + let mut adds_my_items = false; for id in posted.symmetric_difference(¤t) { let Some(collection) = Collection::find_by_uuid_and_org(id, &org_uuid, &conn).await else { err!("Invalid collection ID provided") }; + // Like upstream, the administrative route never adds to or removes from a My Items collection. The + // admin console doesn't list them, so it never sends the one of the acting member either. + if scope == CipherAccessScope::OrganizationAdmin && collection.is_default_user_collection() { + continue; + } if !admin_edit_any && !collection.is_writable_by_user(&headers.user.uuid, &conn).await { err!("No rights to modify the collection") } - changes.push((collection.uuid, posted.contains(id))); + let add = posted.contains(id); + if add && collection.is_default_user_collection() { + if cipher_my_items.is_none() { + cipher_my_items = Some(CollectionCipher::find_my_items_of_cipher(&cipher.uuid, &conn).await); + } + collection + .check_cipher_assignment(&visible, cipher_my_items.as_ref().unwrap_or(&HashSet::new()))?; + adds_my_items = true; + } + changes.push((collection.uuid, add)); + } + // Moved into a My Items collection, an unassigned cipher is taken away from the members with + // organization-wide access, which the revisions of the changed collections don't cover, so they sync too + if adds_my_items { + cipher.update_users_revision(&conn).await; } // Every ID and permission has been checked. A database error rolls back links and revisions. for (id, add) in changes { @@ -1264,7 +1319,9 @@ async fn update_collections_by_scope( cipher.to_json(&headers.host, &headers.user.uuid, None, CipherSyncType::User, &conn).await? } CipherAccessScope::OrganizationAdmin => { - cipher.to_json_org_admin_collections(&headers.host, &headers.user.uuid, &conn).await? + let mut response = cipher.to_json_org_admin_collections(&headers.host, &headers.user.uuid, &conn).await?; + add_my_items_collection_ids(&mut response, &cipher, &headers, &conn).await; + response } }; Ok(Json(response)) @@ -1494,6 +1551,14 @@ async fn share_cipher_by_uuid( if response_scope == CipherAccessScope::OrganizationAdmin && !admin_edit_any { err!("Cipher is not write accessible") } + let (current_collections, cipher_my_items) = if cipher.organization_uuid.is_some() { + ( + HashSet::from_iter(cipher.get_accessible_collections(headers.user.uuid.clone(), conn).await), + CollectionCipher::find_my_items_of_cipher(&cipher.uuid, conn).await, + ) + } else { + (HashSet::new(), HashSet::new()) + }; for col_id in HashSet::::from_iter(data.collection_ids) { let Some(collection) = Collection::find_by_uuid_and_org(&col_id, organization_id, conn).await else { @@ -1502,6 +1567,7 @@ async fn share_cipher_by_uuid( if !admin_edit_any && !collection.is_writable_by_user(&headers.user.uuid, conn).await { err!("No rights to modify the collection") } + collection.check_cipher_assignment(¤t_collections, &cipher_my_items)?; shared_to_collections.push(collection.uuid); } } @@ -2303,7 +2369,7 @@ async fn purge_org_vault( match Membership::find_confirmed_by_user_and_org(&user.uuid, &organization.org_id, &conn).await { Some(member) if member.atype == MembershipType::Owner => { - Cipher::delete_all_by_organization(&organization.org_id, &conn).await?; + Cipher::purge_organization(&organization.org_id, &conn).await?; nt.send_user_update(UpdateType::SyncVault, &user, headers.device.push_uuid.as_ref(), &conn).await; log_event( @@ -2417,19 +2483,14 @@ async fn delete_cipher_by_uuid( ) .await; } + } else if *delete_options == CipherDeleteOptions::HardSingle { + // Before the deletion removes the collections that decide who has to sync: without them, the cipher would + // count as unassigned and reach everybody with organization-wide access, also for My Items. + let user_ids = cipher.update_users_revision(conn).await; + cipher.delete(conn).await?; + nt.send_cipher_update(UpdateType::SyncLoginDelete, &cipher, &user_ids, &headers.device, None, conn).await; } else { cipher.delete(conn).await?; - if *delete_options == CipherDeleteOptions::HardSingle { - nt.send_cipher_update( - UpdateType::SyncLoginDelete, - &cipher, - &cipher.update_users_revision(conn).await, - &headers.device, - None, - conn, - ) - .await; - } } if let Some(org_id) = cipher.organization_uuid { @@ -2732,6 +2793,7 @@ pub struct CipherSyncData { pub user_collections: HashMap, pub user_collections_groups: HashMap, pub user_group_full_access_for_organizations: HashSet, + pub my_items_only_ciphers: HashSet, } #[derive(Eq, PartialEq)] @@ -2782,7 +2844,7 @@ impl CipherSyncData { } // Generate a HashMap with the Cipher UUID as key and one or more Collection UUID's - let user_cipher_collections = Cipher::get_collections_with_cipher_by_user(user_id.clone(), conn).await; + let user_cipher_collections = Cipher::get_collections_with_cipher_by_user(user_id.clone(), None, conn).await; let mut cipher_collections: HashMap> = HashMap::with_capacity(user_cipher_collections.len()); for (cipher, collection) in user_cipher_collections { @@ -2824,6 +2886,20 @@ impl CipherSyncData { HashSet::new() }; + // Only organization-wide access has to leave out the items stored only in a member's My Items, and only + // the user's vault checks the access restrictions at all + let my_items_only_ciphers = match sync_type { + CipherSyncType::User => { + let full_access_orgs = members + .values() + .filter(|m| m.has_full_access() || user_group_full_access_for_organizations.contains(&m.org_uuid)) + .map(|m| m.org_uuid.clone()) + .collect(); + CollectionCipher::find_my_items_only_by_orgs(full_access_orgs, conn).await + } + CipherSyncType::Organization => HashSet::new(), + }; + Self { cipher_attachments, cipher_folders, @@ -2834,6 +2910,7 @@ impl CipherSyncData { user_collections, user_collections_groups, user_group_full_access_for_organizations, + my_items_only_ciphers, } } } diff --git a/src/api/core/events.rs b/src/api/core/events.rs index 49678c68..68b934dc 100644 --- a/src/api/core/events.rs +++ b/src/api/core/events.rs @@ -237,7 +237,9 @@ async fn post_events_collect(data: Json>, headers: Headers, // user can actually access it instead of trusting the provided cipher uuid. if let Some(cipher_uuid) = &event.cipher_id && let Some(cipher) = Cipher::find_by_uuid(cipher_uuid, &conn).await - && cipher.is_accessible_to_user(&headers.user.uuid, CipherAccessScope::User, &conn).await + // Events may originate from either the user's vault or an authorized admin report. + && (cipher.is_accessible_to_user(&headers.user.uuid, CipherAccessScope::User, &conn).await + || cipher.is_accessible_to_user(&headers.user.uuid, CipherAccessScope::OrganizationAdmin, &conn).await) && let Some(org_id) = cipher.organization_uuid { log_event_impl( diff --git a/src/api/core/organizations.rs b/src/api/core/organizations.rs index ee2165a0..a2f7ab0c 100644 --- a/src/api/core/organizations.rs +++ b/src/api/core/organizations.rs @@ -29,7 +29,7 @@ use crate::{ }, mail, sso::FAKE_SSO_IDENTIFIER, - util::{NumberOrString, convert_json_key_lcase_first}, + util::{NumberOrString, convert_json_key_lcase_first, is_valid_enc_string}, }; use super::accounts::{AuthenticationData, UnlockData, V2UpgradeTokenData}; @@ -80,6 +80,7 @@ pub fn routes() -> Vec { get_organization_public_key, bulk_public_keys, revoke_member, + revoke_self, bulk_revoke_members, restore_member, restore_member_vnext, @@ -130,7 +131,8 @@ struct OrganizationUpdateData { #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct FullCollectionData { - name: String, + // Optional on updates only: the clients send none for a former My Items collection, like upstream + name: Option, groups: Vec, users: Vec, external_id: Option, @@ -443,7 +445,10 @@ async fn get_org_collections_details(org_id: OrganizationId, headers: ManagerHea .collect(); let mut data = Vec::new(); - for col in Collection::find_by_organization(&org_id, &conn).await { + // Like upstream, the members' My Items collections are not managed here + for col in + Collection::find_by_organization(&org_id, &conn).await.into_iter().filter(|c| !c.is_default_user_collection()) + { // check whether the current user has access to the given collection let assigned = has_full_access_to_org || CollectionUser::has_access_to_collection_by_user(&col.uuid, &member.user_uuid, &conn).await @@ -499,8 +504,14 @@ async fn get_org_collections_details(org_id: OrganizationId, headers: ManagerHea }))) } +// The shared collections only, like upstream async fn get_org_collections_impl(org_id: &OrganizationId, conn: &DbConn) -> Value { - Collection::find_by_organization(org_id, conn).await.iter().map(Collection::to_json).collect::() + Collection::find_by_organization(org_id, conn) + .await + .iter() + .filter(|c| !c.is_default_user_collection()) + .map(Collection::to_json) + .collect::() } #[post("/organizations//collections", data = "")] @@ -523,7 +534,10 @@ async fn post_organization_collections( err!("You don't have permission to create collections") } - let collection = Collection::new(org_id.clone(), data.name, data.external_id); + let Some(name) = data.name else { + err!("The Name field is required.") + }; + let collection = Collection::new(org_id.clone(), name, data.external_id); collection.save(&conn).await?; log_event( @@ -619,6 +633,10 @@ async fn post_bulk_access_collections( ) } + if collection.is_default_user_collection() { + err!("You cannot add access to collections with the type as DefaultUserCollection.") + } + collections.push(collection); } @@ -693,7 +711,16 @@ async fn put_organization_collection_update( err!("Collection not found") }; - collection.name = data.name; + if collection.is_default_user_collection() { + err!("You cannot edit a collection with the type as DefaultUserCollection.") + } + + // A former My Items collection is shown by its former owner's email address, its name stays as it is + if collection.default_user_collection_email.is_none() + && let Some(name) = data.name.filter(|n| !n.trim().is_empty()) + { + collection.name = name; + } collection.external_id = match data.external_id { Some(external_id) if !external_id.trim().is_empty() => Some(external_id), _ => None, @@ -750,6 +777,9 @@ async fn delete_organization_collection_impl( let Some(collection) = Collection::find_by_uuid_and_org(col_id, org_id, conn).await else { err!("Collection not found", "Collection does not exist or does not belong to this organization") }; + if collection.is_default_user_collection() { + err!("You cannot delete a collection with the type as DefaultUserCollection.") + } log_event( EventType::CollectionDeleted, &collection.uuid, @@ -795,6 +825,16 @@ async fn bulk_delete_organization_collections( let headers = CollectionDeleteHeaders::from_loose(headers, &collections, &conn).await?; + // Like upstream, reject them all before deleting any + for col_id in &collections { + if Collection::find_by_uuid_and_org(col_id, &org_id, &conn) + .await + .is_some_and(|c| c.is_default_user_collection()) + { + err!("You cannot delete collections with the type as DefaultUserCollection.") + } + } + for col_id in collections { delete_organization_collection_impl(&org_id, &col_id, &headers, &conn).await?; } @@ -905,18 +945,29 @@ struct OrgIdData { organization_id: OrganizationId, } +#[derive(FromForm)] +struct OrgDetailsData { + #[field(name = "organizationId")] + organization_id: OrganizationId, + // Set by the organization reports, which also cover the members' My Items + #[field(name = "includeMemberItems", default = false)] + include_member_items: bool, +} + #[get("/ciphers/organization-details?")] -async fn get_org_details(data: OrgIdData, headers: ManagerHeadersLoose, conn: DbConn) -> JsonResult { +async fn get_org_details(data: OrgDetailsData, headers: ManagerHeadersLoose, conn: DbConn) -> JsonResult { if data.organization_id != headers.membership.org_uuid { err_code!("Resource not found.", "Organization id's do not match", Status::NotFound.code); } + // The organization-wide cipher authority upstream requires, which also lets the reports include the members' My + // Items if !headers.membership.can_list_all_ciphers() { err_code!("Resource not found.", "User does not have full access", Status::NotFound.code); } Ok(Json(json!({ - "data": get_org_details_impl(&data.organization_id, &headers.host, &headers.user.uuid, &conn).await?, + "data": get_org_details_impl(&data.organization_id, &headers.host, &headers.user.uuid, data.include_member_items, &conn).await?, "object": "list", "continuationToken": null, }))) @@ -926,11 +977,19 @@ async fn get_org_details_impl( org_id: &OrganizationId, host: &str, user_id: &UserId, + include_member_items: bool, conn: &DbConn, ) -> Result { - let ciphers = Cipher::find_by_org(org_id, conn).await; + let mut ciphers = Cipher::find_by_org(org_id, conn).await; + if !include_member_items { + // Like upstream, leave out the items that are only in the members' My Items collections + let my_items_only = CollectionCipher::find_my_items_only_by_orgs(vec![org_id.clone()], conn).await; + ciphers.retain(|c| !my_items_only.contains(&c.uuid)); + } + let my_items = CollectionCipher::find_my_items_by_org(org_id, conn).await; let mut cipher_sync_data = CipherSyncData::new(user_id, CipherSyncType::Organization, conn).await; - // Every cipher of the organization is listed, so list every collection of them too + // Every cipher of the organization is listed, so list every collection of them too. The My Items ones are + // sorted out below. cipher_sync_data.cipher_collections = HashMap::new(); for (cipher_id, collection_id) in Cipher::get_collections_with_cipher_by_organization(org_id, conn).await { cipher_sync_data.cipher_collections.entry(cipher_id).or_default().push(collection_id); @@ -938,7 +997,18 @@ async fn get_org_details_impl( let mut ciphers_json = Vec::with_capacity(ciphers.len()); for c in ciphers { - ciphers_json.push(c.to_json(host, user_id, Some(&cipher_sync_data), CipherSyncType::Organization, conn).await?); + let mut details = c.to_json(host, user_id, Some(&cipher_sync_data), CipherSyncType::Organization, conn).await?; + // Like upstream, the members' items list all their collections, including the My Items ones, and the + // organization's items none of those + if let Some(cipher_my_items) = my_items.get(&c.uuid) + && let Some(collection_ids) = details["collectionIds"].as_array_mut() + { + collection_ids.retain(|id| id.as_str().is_none_or(|id| !cipher_my_items.iter().any(|c| c.as_ref() == id))); + if include_member_items { + collection_ids.extend(cipher_my_items.iter().map(|c| json!(c))); + } + } + ciphers_json.push(details); } Ok(json!(ciphers_json)) } @@ -1089,8 +1159,10 @@ struct InviteData { impl InviteData { async fn validate(&self, org_id: &OrganizationId, conn: &DbConn) -> EmptyResult { + // A My Items collection can't be assigned, it fails like a collection of another organization, like upstream let org_collections = Collection::find_by_organization(org_id, conn).await; - let org_collection_ids: HashSet<&CollectionId> = org_collections.iter().map(|c| &c.uuid).collect(); + let org_collection_ids: HashSet<&CollectionId> = + org_collections.iter().filter(|c| !c.is_default_user_collection()).map(|c| &c.uuid).collect(); if let Some(e) = self.collections.iter().flatten().find(|c| !org_collection_ids.contains(&c.id)) { err!( "Invalid collection", @@ -1486,12 +1558,30 @@ async fn accept_invite( struct ConfirmData { id: Option, key: Option, + // The encrypted name of the member's My Items collection, sent by the clients whenever they confirm or restore + // a member, and when they demote one from Owner or Admin + default_user_collection_name: Option, } #[derive(Deserialize)] #[serde(rename_all = "camelCase")] struct BulkConfirmData { keys: Option>, + default_user_collection_name: Option, +} + +/// Like upstream's `[EncryptedString]` and `[EncryptedStringLength(1000)]`, the name of a My Items collection has to +/// be an encrypted string: the collection can't be renamed afterwards. +fn check_default_user_collection_name(name: Option<&str>) -> EmptyResult { + if let Some(name) = name { + if name.len() > 1000 { + err!("The field DefaultUserCollectionName exceeds the maximum encrypted value length of 1000 characters.") + } + if !is_valid_enc_string(name) { + err!("DefaultUserCollectionName is not a valid encrypted string.") + } + } + Ok(()) } #[post("/organizations//users/confirm", data = "")] @@ -1506,6 +1596,7 @@ async fn bulk_confirm_invite( err!("Organization not found", "Organization id's do not match"); } let data = data.into_inner(); + check_default_user_collection_name(data.default_user_collection_name.as_deref())?; let mut bulk_response = Vec::new(); match data.keys { @@ -1515,13 +1606,17 @@ async fn bulk_confirm_invite( err!("Member id is required") }; let user_key = invite.key.unwrap_or_default(); - let err_msg = match confirm_invite_impl(&org_id, &member_id, &user_key, &headers, &conn, &nt).await { - Ok(()) => String::new(), - Err(e) => { - warn!("Unable to confirm member {}: {e:?}", member_id.escape_debug()); - e.message().to_owned() - } - }; + let collection_name = data.default_user_collection_name.as_deref(); + let err_msg = + match confirm_invite_impl(&org_id, &member_id, &user_key, collection_name, &headers, &conn, &nt) + .await + { + Ok(()) => String::new(), + Err(e) => { + warn!("Unable to confirm member {}: {e:?}", member_id.escape_debug()); + e.message().to_owned() + } + }; bulk_response.push(json!( { @@ -1552,14 +1647,25 @@ async fn confirm_invite( nt: Notify<'_>, ) -> EmptyResult { let data = data.into_inner(); + check_default_user_collection_name(data.default_user_collection_name.as_deref())?; let user_key = data.key.unwrap_or_default(); - confirm_invite_impl(&org_id, &member_id, &user_key, &headers, &conn, &nt).await + confirm_invite_impl( + &org_id, + &member_id, + &user_key, + data.default_user_collection_name.as_deref(), + &headers, + &conn, + &nt, + ) + .await } async fn confirm_invite_impl( org_id: &OrganizationId, member_id: &MembershipId, key: &str, + default_collection_name: Option<&str>, headers: &ManageUsersHeaders, conn: &DbConn, nt: &Notify<'_>, @@ -1612,13 +1718,18 @@ async fn confirm_invite_impl( mail::send_invite_confirmed(&address, &org_name).await?; } - let save_result = member_to_confirm.save(conn).await; + let mut result = member_to_confirm.save(conn).await; + // Only once the membership is stored as confirmed, like upstream: a concurrent policy update then either + // finds this member confirmed or this check finds the policy enabled, so the member always gets one. + if result.is_ok() { + result = Collection::create_default_user_collection(&member_to_confirm, default_collection_name, conn).await; + } if let Some(user) = User::find_by_uuid(&member_to_confirm.user_uuid, conn).await { nt.send_user_update(UpdateType::SyncOrgKeys, &user, headers.device.push_uuid.as_ref(), conn).await; } - save_result + result } #[get("/organizations//users/mini-details", rank = 1)] @@ -1677,6 +1788,7 @@ struct EditUserData { groups: Option>, #[serde(default)] permissions: HashMap, + default_user_collection_name: Option, } #[put("/organizations//users/", data = "", rank = 1)] @@ -1691,6 +1803,7 @@ async fn edit_member( err!("Organization not found", "Organization id's do not match"); } let data: EditUserData = data.into_inner(); + check_default_user_collection_name(data.default_user_collection_name.as_deref())?; // HACK: We need the raw user-type to be sure custom role is selected to determine the access_all permission // Unless Custom roles are enabled, a custom role (4) is converted to a manager role (3) @@ -1756,6 +1869,8 @@ async fn edit_member( .await?; } + // Upstream creates the My Items collection of a member demoted from Owner or Admin + let demoted = member_to_edit.atype >= MembershipType::Admin && new_type < MembershipType::Admin; member_to_edit.set_type(new_type); member_to_edit.access_all = access_all; if let Some(permissions) = custom_permissions.as_ref().filter(|_| new_type == MembershipType::Custom) { @@ -1766,15 +1881,26 @@ async fn edit_member( // We need to perform the check after changing the type since `admin` is exempt. OrgPolicy::check_user_allowed(&member_to_edit, "modify", &conn).await?; - // Replace the member's direct collection assignments with those in the request. - for c in CollectionUser::find_by_organization_and_user_uuid(&org_id, &member_to_edit.user_uuid, &conn).await { - c.delete(&conn).await?; + // Like upstream, reject a My Items collection before changing anything + let org_collections: HashMap = + Collection::find_by_organization(&org_id, &conn).await.into_iter().map(|c| (c.uuid.clone(), c)).collect(); + if !access_all + && data + .collections + .iter() + .flatten() + .any(|col| org_collections.get(&col.id).is_some_and(Collection::is_default_user_collection)) + { + err!("Default collections cannot be assigned to a member.") } + // Delete all the odd collections, but keep the member's My Items + CollectionUser::delete_all_but_my_items_by_user_and_org(&member_to_edit.user_uuid, &org_id, &conn).await?; + // If no accessAll, add the collections received if !access_all { for col in data.collections.iter().flatten() { - match Collection::find_by_uuid_and_org(&col.id, &org_id, &conn).await { + match org_collections.get(&col.id) { None => err!("Collection not found in Organization"), Some(collection) => { CollectionUser::save( @@ -1812,7 +1938,17 @@ async fn edit_member( ) .await; - member_to_edit.save(&conn).await + member_to_edit.save(&conn).await?; + // Only once the demotion is stored, like upstream, see `confirm_invite_impl()` + if demoted { + Collection::create_default_user_collection( + &member_to_edit, + data.default_user_collection_name.as_deref(), + &conn, + ) + .await?; + } + Ok(()) } #[delete("/organizations//users", data = "")] @@ -1961,7 +2097,8 @@ async fn bulk_public_keys( } use super::ciphers::CipherData; -use super::ciphers::update_cipher_from_data; +use super::ciphers::{import_folders, update_cipher_from_data}; +use super::folders::FolderData; // The import endpoint only ever uses the name/id/external_id of a collection. // Bitwarden's own server ignores `groups`/`users` here too, so do not make them @@ -1980,6 +2117,11 @@ struct ImportData { ciphers: Vec, collections: Vec, collection_relationships: Vec, + // The clients keep the folders of an import into My Items + #[serde(default)] + folders: Vec, + #[serde(default)] + folder_relationships: Vec, } #[derive(Deserialize)] @@ -2029,7 +2171,12 @@ async fn validate_org_import_collections( }; for col in imported { if let Some(collection) = col.id.as_ref().and_then(|id| existing.get(id)) { - if membership.atype < MembershipType::Admin && !membership.can_edit_any_collection() { + if collection.is_default_user_collection() { + // Only its owner can import into a My Items collection, whatever else the member may do + if collection.default_user_uuid.as_ref() != Some(&membership.user_uuid) { + err!(Compact, "The current user isn't allowed to manage this collection") + } + } else if membership.atype < MembershipType::Admin && !membership.can_edit_any_collection() { let can_import = if let Some(manageable) = &manageable { manageable.contains(&collection.uuid) } else { @@ -2081,6 +2228,8 @@ async fn post_org_import( let ImportData { ciphers: imported_ciphers, collections: imported_collections, + folders: imported_folders, + folder_relationships, .. } = data; let grant_new_collection_manage = headers.membership.has_type(MembershipType::Custom); @@ -2119,12 +2268,17 @@ async fn post_org_import( collections.push(collection_uuid); } + // The clients keep the folders of an import into My Items + let folder_relations = folder_relationships.into_iter().map(|r| (r.key, r.value)); + let cipher_folders = import_folders(imported_folders, folder_relations, &headers.user.uuid, &conn).await?; + let mut assigned = vec![Vec::::new(); imported_ciphers.len()]; for (cipher_index, collection_index) in relations { assigned[cipher_index].push(collections[collection_index].clone()); } - for (mut cipher_data, collection_ids) in imported_ciphers.into_iter().zip(assigned) { - cipher_data.folder_id = None; + for (index, (mut cipher_data, collection_ids)) in imported_ciphers.into_iter().zip(assigned).enumerate() { + // Only the folder relationships of the import set a folder, never the client-provided folderId + cipher_data.folder_id = cipher_folders.get(&index).cloned(); // The authenticated membership, rather than the imported file, selects the organization. cipher_data.organization_id = Some(org_id.clone()); let mut cipher = Cipher::new(cipher_data.r#type); @@ -2279,6 +2433,8 @@ async fn post_bulk_collections(data: Json, headers: Headers }; let user_collections: HashMap = collections .into_iter() + // Like upstream, never another member's My Items collection, not even for those who may edit any collection + .filter(|c| c.default_user_uuid.as_ref().is_none_or(|owner| owner == &headers.user.uuid)) .filter_map(|c| { if data.collection_ids.contains(&c.uuid) { Some((c.uuid.clone(), c)) @@ -2297,22 +2453,53 @@ async fn post_bulk_collections(data: Json, headers: Headers } } + let mut ciphers = Vec::with_capacity(data.cipher_ids.len()); for cipher_id in &data.cipher_ids { // Only act on existing cipher uuid's // Do not abort the operation just ignore it, it could be a cipher was just deleted for example if let Some(cipher) = Cipher::find_by_uuid_and_org(cipher_id, &data.organization_id, &conn).await && cipher.is_write_accessible_to_user(&headers.user.uuid, CipherAccessScope::OrganizationAdmin, &conn).await { - // When selecting a specific collection from the left filter list, and use the bulk option, you can remove an item from that collection - // In these cases the client will call this endpoint twice, once for adding the new collections and a second for deleting. - if data.remove_collections { - for collection in &data.collection_ids { - CollectionCipher::delete(&cipher.uuid, collection, &conn).await?; - } - } else { - for collection in &data.collection_ids { - CollectionCipher::save(&cipher.uuid, collection, &conn).await?; - } + // Upstream rejects sharing an item held only in somebody else's My Items, + // even when all destination collections are ordinary shared collections. + if !data.remove_collections + && CollectionCipher::find_my_items_owners_if_only(&cipher.uuid, &conn) + .await + .is_some_and(|owners| owners.iter().any(|owner| owner != &headers.user.uuid)) + { + err_code!("Resource not found", "Cannot share another member's My Items", 404) + } + ciphers.push(cipher); + } + } + + // Only the user's own My Items collection can be among them, check all ciphers before changing any + let my_items = + user_collections.values().find(|c| c.is_default_user_collection()).filter(|_| !data.remove_collections); + if let Some(my_items) = my_items { + for cipher in &ciphers { + my_items.check_cipher_assignment( + &HashSet::from_iter(cipher.get_accessible_collections(headers.user.uuid.clone(), &conn).await), + &CollectionCipher::find_my_items_of_cipher(&cipher.uuid, &conn).await, + )?; + } + } + + for cipher in ciphers { + // When selecting a specific collection from the left filter list, and use the bulk option, you can remove an item from that collection + // In these cases the client will call this endpoint twice, once for adding the new collections and a second for deleting. + if data.remove_collections { + for collection in &data.collection_ids { + CollectionCipher::delete(&cipher.uuid, collection, &conn).await?; + } + } else { + // Moved into My Items, an unassigned cipher is taken away from the members with organization-wide access, + // so they sync as well + if my_items.is_some() { + cipher.update_users_revision(&conn).await; + } + for collection in &data.collection_ids { + CollectionCipher::save(&cipher.uuid, collection, &conn).await?; } } } @@ -2413,10 +2600,14 @@ struct PolicyData { #[derive(Deserialize)] struct PutPolicy { policy: PolicyData, - // Ignore metadata for now as we do not yet support this - // "metadata": { - // "defaultUserCollectionName": "2.xx|xx==|xx=" - // } + metadata: Option, +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct PolicyMetadata { + // Sent with the organization data ownership policy, see `ConfirmData` + default_user_collection_name: Option, } #[put("/organizations//policies/", data = "")] @@ -2430,11 +2621,17 @@ async fn put_policy( if org_id != headers.org_id { err!("Organization not found", "Organization id's do not match"); } - let data: PolicyData = data.into_inner().policy; + let PutPolicy { + policy: data, + metadata, + } = data.into_inner(); let Some(pol_type_enum) = OrgPolicyType::from_i32(pol_type) else { err!("Invalid or unsupported policy type") }; + if pol_type_enum == OrgPolicyType::PersonalOwnership { + check_default_user_collection_name(metadata.as_ref().and_then(|m| m.default_user_collection_name.as_deref()))?; + } let current_policy = OrgPolicy::find_by_org_and_type(&org_id, pol_type_enum, &conn).await; let is_currently_enabled = current_policy.as_ref().is_some_and(|policy| policy.enabled); @@ -2548,6 +2745,8 @@ async fn put_policy( None => OrgPolicy::new(org_id.clone(), pol_type_enum, false, "{}".to_owned()), }; + // Bitwarden provisions default collections on first enable or re-enable, not on ordinary updates. + let policy_becomes_enabled = data.enabled && !policy.enabled; policy.enabled = data.enabled; policy.data = serde_json::to_string(&data.data)?; policy.save(&conn).await?; @@ -2563,6 +2762,15 @@ async fn put_policy( ) .await; + // Like upstream, provision on first enable or re-enable, after the policy has been saved. + // A subsequent ordinary policy update must not create new My Items collections. + if pol_type_enum == OrgPolicyType::PersonalOwnership && policy_becomes_enabled { + let collection_name = metadata.as_ref().and_then(|m| m.default_user_collection_name.as_deref()); + // `create_default_user_collections()` only provisions the confirmed members the policy applies to + let members = Membership::find_by_org(&org_id, &conn).await; + Collection::create_default_user_collections(&members, collection_name, &conn).await?; + } + Ok(Json(policy.to_json())) } @@ -2669,6 +2877,39 @@ async fn revoke_member( revoke_member_impl(&org_id, &member_id, &headers, &conn).await } +// Called by the clients when a member declines to transfer their personal items into their My Items collection, +// as the organization data ownership policy asks of them. +#[put("/organizations//users/revoke-self")] +async fn revoke_self(org_id: OrganizationId, headers: OrgMemberHeaders, conn: DbConn, nt: Notify<'_>) -> EmptyResult { + if org_id != headers.membership.org_uuid { + err!("Organization not found", "Organization id's do not match"); + } + let mut member = headers.membership; + + if !OrgPolicy::is_personal_ownership_enforced_for(&member, &conn).await { + err!( + "User is not eligible for self-revocation. The organization data ownership policy must be enabled and the user must be a confirmed member." + ) + } + + member.revoke(); + member.save(&conn).await?; + + log_event( + EventType::OrganizationUserSelfRevoked, + &member.uuid, + &org_id, + &headers.user.uuid, + headers.device.atype, + &headers.ip.ip, + &conn, + ) + .await; + + nt.send_user_update(UpdateType::SyncOrgKeys, &headers.user, headers.device.push_uuid.as_ref(), &conn).await; + Ok(()) +} + #[put("/organizations//users/revoke", data = "")] async fn bulk_revoke_members( org_id: OrganizationId, @@ -2758,16 +2999,23 @@ async fn revoke_member_impl( Ok(()) } -#[put("/organizations//users//restore/vnext")] +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct RestoreMemberData { + default_user_collection_name: Option, +} + +#[put("/organizations//users//restore/vnext", data = "")] async fn restore_member_vnext( org_id: OrganizationId, member_id: MembershipId, + data: Json, headers: ManageUsersHeaders, conn: DbConn, ) -> EmptyResult { - // Vaultwarden does not (yet) support the per User Collection linked to the `Enforce organization data ownership` policy. - // Therefor we ignore the `defaultUserCollectionName` data sent and just call restore_member - restore_member_impl(&org_id, &member_id, &headers, &conn).await + let collection_name = data.into_inner().default_user_collection_name; + check_default_user_collection_name(collection_name.as_deref())?; + restore_member_impl(&org_id, &member_id, collection_name.as_deref(), &headers, &conn).await } #[put("/organizations//users//restore")] @@ -2777,13 +3025,20 @@ async fn restore_member( headers: ManageUsersHeaders, conn: DbConn, ) -> EmptyResult { - restore_member_impl(&org_id, &member_id, &headers, &conn).await + restore_member_impl(&org_id, &member_id, None, &headers, &conn).await +} + +#[derive(Deserialize)] +#[serde(rename_all = "camelCase")] +struct BulkRestoreMemberData { + ids: Vec, + default_user_collection_name: Option, } #[put("/organizations//users/restore", data = "")] async fn bulk_restore_members( org_id: OrganizationId, - data: Json, + data: Json, headers: ManageUsersHeaders, conn: DbConn, ) -> JsonResult { @@ -2791,10 +3046,12 @@ async fn bulk_restore_members( err!("Organization not found", "Organization id's do not match"); } let data = data.into_inner(); + let collection_name = data.default_user_collection_name.as_deref(); + check_default_user_collection_name(collection_name)?; let mut bulk_response = Vec::new(); for member_id in data.ids { - let err_msg = match restore_member_impl(&org_id, &member_id, &headers, &conn).await { + let err_msg = match restore_member_impl(&org_id, &member_id, collection_name, &headers, &conn).await { Ok(()) => String::new(), Err(e) => { warn!("Unable to restore member {}: {e:?}", member_id.escape_debug()); @@ -2821,6 +3078,7 @@ async fn bulk_restore_members( async fn restore_member_impl( org_id: &OrganizationId, member_id: &MembershipId, + default_collection_name: Option<&str>, headers: &ManageUsersHeaders, conn: &DbConn, ) -> EmptyResult { @@ -2855,6 +3113,9 @@ async fn restore_member_impl( conn, ) .await; + + // Only once the restore is stored, like upstream, see `confirm_invite_impl()` + Collection::create_default_user_collection(&member, default_collection_name, conn).await?; } Some(_) => err!("User is already active"), None => err!("User not found in organization"), @@ -2986,6 +3247,12 @@ impl GroupRequest { format!("Collection {} does not belong to organization {}!", e.id.escape_debug(), org_id) ) } + if org_collections + .iter() + .any(|c| c.is_default_user_collection() && self.collections.iter().any(|g| g.id == c.uuid)) + { + err!("You cannot modify group access for collections with the type as DefaultUserCollection.") + } let org_memberships = Membership::find_by_org(org_id, conn).await; let org_membership_ids: HashSet<&MembershipId> = org_memberships.iter().map(|m| &m.uuid).collect(); @@ -3782,7 +4049,7 @@ async fn get_org_export(org_id: OrganizationId, headers: AccessImportExportHeade Ok(Json(json!({ "collections": convert_json_key_lcase_first(get_org_collections_impl(&org_id, &conn).await), - "ciphers": convert_json_key_lcase_first(get_org_details_impl(&org_id, &headers.host, &headers.user.uuid, &conn).await?), + "ciphers": convert_json_key_lcase_first(get_org_details_impl(&org_id, &headers.host, &headers.user.uuid, false, &conn).await?), }))) } @@ -3935,7 +4202,7 @@ mod tests { ); let rocket = rocket::build().manage(Arc::clone(&WS_USERS)); let notify = rocket.state::>().unwrap(); - confirm_invite_impl(&org.uuid, &target.uuid, "encrypted-key", &headers, &conn, ¬ify.into()) + confirm_invite_impl(&org.uuid, &target.uuid, "encrypted-key", None, &headers, &conn, ¬ify.into()) .await .unwrap(); let after = Membership::find_by_uuid(&target.uuid, &conn).await.unwrap(); @@ -4004,6 +4271,7 @@ mod tests { enabled: true, data: None, }, + metadata: None, }), headers, db.conn(), diff --git a/src/config.rs b/src/config.rs index 165ead41..6f0c192a 100644 --- a/src/config.rs +++ b/src/config.rs @@ -1525,6 +1525,7 @@ pub const SUPPORTED_FEATURE_FLAGS: &[&str] = &[ "pm-30529-webauthn-related-origins", // Vault Team "pm-32009-new-item-types", + "pm-20558-migrate-myvault-to-myitems", ]; #[derive(Clone, Copy, Serialize)] diff --git a/src/db/models/cipher.rs b/src/db/models/cipher.rs index 97807279..ffdfbcf2 100644 --- a/src/db/models/cipher.rs +++ b/src/db/models/cipher.rs @@ -87,6 +87,9 @@ pub enum RepromptType { /// regular vault routes, so the handlers state which one applies. They only differ for a Custom member with Edit /// any collection, who reaches every cipher of the organization on the administrative routes, but keeps their /// own vault limited to the collections they are assigned to. +/// +/// They also differ for the items stored only in a member's My Items collection: like upstream, members with +/// organization-wide cipher authority reach those through the administrative routes, never in their vault. #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum CipherAccessScope { User, @@ -546,6 +549,18 @@ impl Cipher { None => { // Belongs to Organization, need to update affected users if let Some(ref org_uuid) = self.organization_uuid { + // My Items-only ciphers belong in the owner's vault, not in the vault/revision stream of + // members that merely have organization- or group-wide access. + if let Some(owners) = CollectionCipher::find_my_items_owners_if_only(&self.uuid, conn).await { + for owner in owners { + if Membership::find_confirmed_by_user_and_org(&owner, org_uuid, conn).await.is_some() { + User::update_uuid_revision(&owner, conn).await; + user_uuids.push(owner); + } + } + return user_uuids; + } + // users having access to the collection let mut collection_users = Membership::find_by_cipher_and_org(&self.uuid, org_uuid, conn).await; if CONFIG.org_groups_enabled() { @@ -614,6 +629,24 @@ impl Cipher { Ok(()) } + /// Purges the organization vault like upstream's `Cipher_DeleteByOrganizationId`: the items stored in a + /// member's My Items collection are kept, they are only removed from the shared collections. + pub async fn purge_organization(org_uuid: &OrganizationId, conn: &DbConn) -> EmptyResult { + let my_items = CollectionCipher::find_my_items_by_org(org_uuid, conn).await; + for cipher in Self::find_by_org(org_uuid, conn).await { + if !my_items.contains_key(&cipher.uuid) { + cipher.delete(conn).await?; + } + } + + if !my_items.is_empty() { + CollectionCipher::delete_all_shared_by_organization(org_uuid, conn).await?; + let members = Membership::find_by_org(org_uuid, conn).await; + User::update_uuid_revisions(members.into_iter().map(|m| m.user_uuid).collect(), conn).await; + } + Ok(()) + } + pub async fn delete_all_by_user(user_uuid: &UserId, conn: &DbConn) -> EmptyResult { for cipher in Self::find_owned_by_user(user_uuid, conn).await { cipher.delete(conn).await?; @@ -711,6 +744,15 @@ impl Cipher { false } + /// Whether the cipher is assigned to My Items collections only. + async fn is_my_items_only(&self, cipher_sync_data: Option<&CipherSyncData>, conn: &DbConn) -> bool { + if let Some(cipher_sync_data) = cipher_sync_data { + cipher_sync_data.my_items_only_ciphers.contains(&self.uuid) + } else { + CollectionCipher::find_my_items_owners_if_only(&self.uuid, conn).await.is_some() + } + } + /// Returns whether this cipher is owned by an group in which the user has full access. async fn is_in_full_access_group( &self, @@ -742,12 +784,28 @@ impl Cipher { cipher_sync_data: Option<&CipherSyncData>, conn: &DbConn, ) -> Option<(bool, bool, bool)> { + if let Some(org_uuid) = self.organization_uuid.as_ref() + && let Some(cipher_sync_data) = cipher_sync_data + && !cipher_sync_data.members.contains_key(org_uuid) + { + // Cached collection rows can outlive a revoked membership. They are not effective without a confirmed + // membership in the same organization. + return None; + } + // Check whether this cipher is directly owned by the user, or is in // a collection that the user has full access to. If so, there are no // access restrictions. - if self.is_owned_by_user(user_uuid) - || self.is_in_full_access_org(user_uuid, scope, cipher_sync_data, conn).await - || self.is_in_full_access_group(user_uuid, cipher_sync_data, conn).await + if self.is_owned_by_user(user_uuid) { + return Some((false, false, true)); + } + // Organization-wide access doesn't reach the items stored only in a member's My Items collection. Like + // upstream, only the administrative routes open those, for members with organization-wide cipher authority, + // which is what reaching every cipher with `CipherAccessScope::OrganizationAdmin` means. + let reaches_org = self.is_in_full_access_org(user_uuid, scope, cipher_sync_data, conn).await; + if (reaches_org && scope == CipherAccessScope::OrganizationAdmin) + || ((reaches_org || self.is_in_full_access_group(user_uuid, cipher_sync_data, conn).await) + && !self.is_my_items_only(cipher_sync_data, conn).await) { return Some((false, false, true)); } @@ -974,10 +1032,12 @@ impl Cipher { cipher_uuids: &Vec, conn: &DbConn, ) -> Vec { + // access_all and admin rights don't reach the items stored only in another member's My Items collection if CONFIG.org_groups_enabled() { conn.run(move |conn| { let mut query = ciphers::table .left_join(ciphers_collections::table.on(ciphers::uuid.eq(ciphers_collections::cipher_uuid))) + .left_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) .left_join( users_organizations::table.on(ciphers::organization_uuid .eq(users_organizations::org_uuid.nullable()) @@ -1005,15 +1065,17 @@ impl Cipher { .and(collections_groups::groups_uuid.eq(groups::uuid))), ) .filter(ciphers::user_uuid.eq(user_uuid)) // Cipher owner - .or_filter(users_organizations::access_all.eq(true)) // access_all in org + .or_filter(users_organizations::access_all.eq(true).and(collections::default_user_uuid.is_null())) // access_all in org .or_filter(users_collections::user_uuid.eq(user_uuid)) // Access to collection - .or_filter(groups::access_all.eq(true)) // Access via groups + .or_filter(groups::access_all.eq(true).and(collections::default_user_uuid.is_null())) // Access via groups .or_filter(collections_groups::collections_uuid.is_not_null()) // Access via groups .into_boxed(); if !visible_only { query = query.or_filter( - users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin/owner + users_organizations::atype + .eq_any(ORG_ADMIN_ATYPES) // Org admin/owner + .and(collections::default_user_uuid.is_null()), ); } @@ -1029,6 +1091,7 @@ impl Cipher { conn.run(move |conn| { let mut query = ciphers::table .left_join(ciphers_collections::table.on(ciphers::uuid.eq(ciphers_collections::cipher_uuid))) + .left_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) .left_join( users_organizations::table.on(ciphers::organization_uuid .eq(users_organizations::org_uuid.nullable()) @@ -1042,13 +1105,15 @@ impl Cipher { .and(users_organizations::user_uuid.eq(users_collections::user_uuid))), ) .filter(ciphers::user_uuid.eq(user_uuid)) // Cipher owner - .or_filter(users_organizations::access_all.eq(true)) // access_all in org + .or_filter(users_organizations::access_all.eq(true).and(collections::default_user_uuid.is_null())) // access_all in org .or_filter(users_collections::user_uuid.eq(user_uuid)) // Access to collection .into_boxed(); if !visible_only { query = query.or_filter( - users_organizations::atype.eq_any(ORG_ADMIN_ATYPES), // Org admin/owner + users_organizations::atype + .eq_any(ORG_ADMIN_ATYPES) // Org admin/owner + .and(collections::default_user_uuid.is_null()), ); } @@ -1168,10 +1233,11 @@ impl Cipher { .filter( users_organizations::access_all .eq(true) // User has access all + .and(collections::default_user_uuid.is_null()) .or(users_collections::user_uuid .eq(user_uuid) // User has access to collection .and(users_collections::read_only.eq(false))) - .or(groups::access_all.eq(true)) // Access via groups + .or(groups::access_all.eq(true).and(collections::default_user_uuid.is_null())) // Access via groups .or(collections_groups::collections_uuid .is_not_null() // Access via groups .and(collections_groups::read_only.eq(false))), @@ -1200,6 +1266,7 @@ impl Cipher { .filter( users_organizations::access_all .eq(true) // User has access all + .and(collections::default_user_uuid.is_null()) .or(users_collections::user_uuid .eq(user_uuid) // User has access to collection .and(users_collections::read_only.eq(false))), @@ -1212,6 +1279,17 @@ impl Cipher { } } + /// The cipher's collections the user sees it in, whatever they may do there: its `collectionIds` in the user's + /// sync, and upstream's `GetManyByUserIdCipherIdAsync`. Unlike `get_collections()`, whose callers need the + /// writable assignments. + pub async fn get_accessible_collections(&self, user_uuid: UserId, conn: &DbConn) -> Vec { + Self::get_collections_with_cipher_by_user(user_uuid, Some(self.uuid.clone()), conn) + .await + .into_iter() + .map(|(_, collection_uuid)| collection_uuid) + .collect() + } + pub async fn get_admin_collections( &self, user_uuid: UserId, @@ -1250,15 +1328,19 @@ impl Cipher { .filter( users_organizations::access_all .eq(true) // User has access all + .and(collections::default_user_uuid.is_null()) .or(users_collections::user_uuid .eq(user_uuid) // User has access to collection .and(users_collections::read_only.eq(false))) - .or(groups::access_all.eq(true)) // Access via groups + .or(groups::access_all.eq(true).and(collections::default_user_uuid.is_null())) // Access via groups .or(collections_groups::collections_uuid .is_not_null() // Access via groups .and(collections_groups::read_only.eq(false))) - .or(users_organizations::atype.eq_any(ORG_ADMIN_ATYPES)) // User is admin or owner + .or(users_organizations::atype + .eq_any(ORG_ADMIN_ATYPES) // User is admin or owner + .and(collections::default_user_uuid.is_null())) .or(custom_edit_any_collection() + .and(collections::default_user_uuid.is_null()) .and::<_, diesel::sql_types::Bool>(scope == CipherAccessScope::OrganizationAdmin)), ) .select(ciphers_collections::collection_uuid) @@ -1285,11 +1367,15 @@ impl Cipher { .filter( users_organizations::access_all .eq(true) // User has access all + .and(collections::default_user_uuid.is_null()) .or(users_collections::user_uuid .eq(user_uuid) // User has access to collection .and(users_collections::read_only.eq(false))) - .or(users_organizations::atype.eq_any(ORG_ADMIN_ATYPES)) // User is admin or owner + .or(users_organizations::atype + .eq_any(ORG_ADMIN_ATYPES) // User is admin or owner + .and(collections::default_user_uuid.is_null())) .or(custom_edit_any_collection() + .and(collections::default_user_uuid.is_null()) .and::<_, diesel::sql_types::Bool>(scope == CipherAccessScope::OrganizationAdmin)), ) .select(ciphers_collections::collection_uuid) @@ -1302,12 +1388,14 @@ impl Cipher { /// Return a Vec with (cipher_uuid, collection_uuid) /// This is used during a full sync so we only need one query for all collections accessible. + /// With `cipher_uuid`, only the ones of that cipher. pub async fn get_collections_with_cipher_by_user( user_uuid: UserId, + cipher_uuid: Option, conn: &DbConn, ) -> Vec<(CipherId, CollectionId)> { conn.run(move |conn| { - ciphers_collections::table + let mut query = ciphers_collections::table .inner_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) .inner_join( users_organizations::table.on(users_organizations::org_uuid @@ -1330,12 +1418,23 @@ impl Cipher { .eq(ciphers_collections::collection_uuid) .and(collections_groups::groups_uuid.eq(groups::uuid))), ) + // access_all and admin rights don't reach another member's My Items collection .or_filter(users_collections::user_uuid.eq(user_uuid)) // User has access to collection - .or_filter(users_organizations::access_all.eq(true)) // User has access all - .or_filter(users_organizations::atype.eq_any(ORG_ADMIN_ATYPES)) // User is admin or owner - .or_filter(groups::access_all.eq(true)) //Access via group + .or_filter(users_organizations::access_all.eq(true).and(collections::default_user_uuid.is_null())) // User has access all + .or_filter( + users_organizations::atype + .eq_any(ORG_ADMIN_ATYPES) // User is admin or owner + .and(collections::default_user_uuid.is_null()), + ) + .or_filter(groups::access_all.eq(true).and(collections::default_user_uuid.is_null())) //Access via group .or_filter(collections_groups::collections_uuid.is_not_null()) //Access via group .filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) + .into_boxed(); + // After the alternatives above, so it narrows all of them + if let Some(cipher_uuid) = cipher_uuid { + query = query.filter(ciphers_collections::cipher_uuid.eq(cipher_uuid)); + } + query .select(ciphers_collections::all_columns) .distinct() .load::<(CipherId, CollectionId)>(conn) diff --git a/src/db/models/collection.rs b/src/db/models/collection.rs index 4f1533e3..f8b98b29 100644 --- a/src/db/models/collection.rs +++ b/src/db/models/collection.rs @@ -1,6 +1,13 @@ +use std::collections::{HashMap, HashSet}; + use derive_more::{AsRef, Deref, Display, From}; -use diesel::prelude::*; +use diesel::{ + dsl::{count, count_star}, + prelude::*, + result::{DatabaseErrorKind, Error as DieselError}, +}; use serde_json::Value; +use tokio::time::{Duration, sleep}; use crate::{ CONFIG, @@ -17,8 +24,8 @@ use crate::{ use macros::UuidFromParam; use super::{ - CipherId, CollectionGroup, GroupUser, Membership, MembershipId, MembershipStatus, MembershipType, OrganizationId, - User, UserId, custom_roles_enabled, organization::ORG_ADMIN_ATYPES, + CipherId, CollectionGroup, GroupUser, Membership, MembershipId, MembershipStatus, MembershipType, OrgPolicy, + OrganizationId, User, UserId, custom_roles_enabled, organization::ORG_ADMIN_ATYPES, }; // See (v2026.7.0): https://github.com/bitwarden/server/blob/5d4461aa42cadbacfef8fe2166c5453a5c52773a/src/Core/AdminConsole/Entities/Collection.cs @@ -31,6 +38,10 @@ pub struct Collection { pub org_uuid: OrganizationId, pub name: String, pub external_id: Option, + /// The member whose "My Items" collection (upstream's `DefaultUserCollection`) this is, `None` for a shared one. + pub default_user_uuid: Option, + /// The email address of the former owner of a My Items collection that was turned into a shared one. + pub default_user_collection_email: Option, } #[derive(Identifiable, Queryable, Insertable)] @@ -60,23 +71,54 @@ impl Collection { org_uuid, name, external_id: None, + default_user_uuid: None, + default_user_collection_email: None, }; new_model.set_external_id(external_id); new_model } + pub fn is_default_user_collection(&self) -> bool { + self.default_user_uuid.is_some() + } + + /// Like upstream, an item that is only in shared collections can't be put (back) into a My Items collection. + /// `current_collections` are the item's collections the acting user sees, before the change. Neither can an + /// item of another member's My Items, which the acting user may administer but doesn't see: + /// `cipher_my_items` are all the My Items collections the item is in. + pub fn check_cipher_assignment( + &self, + current_collections: &HashSet, + cipher_my_items: &HashSet, + ) -> EmptyResult { + if !self.is_default_user_collection() { + return Ok(()); + } + if !current_collections.is_empty() && !current_collections.contains(&self.uuid) { + err!( + "The cipher(s) cannot be assigned to a default collection when only assigned to non-default collections." + ) + } + if cipher_my_items.iter().any(|c| *c != self.uuid) { + err!( + "The cipher(s) cannot be assigned to a default collection when in another member's default collection." + ) + } + Ok(()) + } + pub fn to_json(&self) -> Value { json!({ "externalId": self.external_id, "id": self.uuid, "organizationId": self.org_uuid, "name": self.name, - // Collection types are either 0: SharedCollection or 1: DefaultUserCollection, of which we do not yet support DefaultUserCollection. + // Collection types are either 0: SharedCollection or 1: DefaultUserCollection ("My Items"). // See (v2026.7.0): https://github.com/bitwarden/server/blob/5d4461aa42cadbacfef8fe2166c5453a5c52773a/src/Core/AdminConsole/Enums/CollectionType.cs - "type": 0, - // This is only used together with MyItems/DefaultUserCollection, which we do not yet support. - "defaultUserCollectionEmail": null, + "type": i32::from(self.is_default_user_collection()), + // Set when a My Items collection outlived its owner's membership, the clients show it as the name. + "defaultUserCollectionEmail": self.default_user_collection_email, "object": "collection", }) } @@ -108,9 +150,10 @@ impl Collection { // Owners and Admins always have true. Users are not able to have full access Some(m) if m.has_full_access() => (false, false, m.atype >= MembershipType::Manager), Some(m) => { - // Only let a manager manage collections when the have full read/write access - // A Custom member only manages a collection with the manage option - let is_manager = m.atype == MembershipType::Manager; + // Only let a manager manage collections when the have full read/write access. + // A Custom member only manages a collection with the manage option. + // The owner of a My Items collection, its only member, manages it whatever their type, like upstream. + let is_manager = m.atype == MembershipType::Manager || self.is_default_user_collection(); let is_custom = m.has_type(MembershipType::Custom) && custom_roles_enabled(); if let Some(cu) = cipher_sync_data.user_collections.get(&self.uuid) { ( @@ -150,7 +193,7 @@ impl Collection { } } Some(m) => { - let is_manager = m.atype == MembershipType::Manager; + let is_manager = m.atype == MembershipType::Manager || self.is_default_user_collection(); let read_only = !self.is_writable_by_user(user_uuid, conn).await; let hide_passwords = self.hide_passwords_for_user(user_uuid, conn).await; (read_only, hide_passwords, is_manager && !read_only && !hide_passwords) @@ -182,6 +225,20 @@ impl Collection { pub async fn save(&self, conn: &DbConn) -> EmptyResult { self.update_users_revision(conn).await; + if self.is_default_user_collection() { + // Only ever updated here. They are created by `create_default_user_collections()`: on MySQL, the upsert + // below also fires on the unique `(org_uuid, default_user_uuid)` index and would overwrite the member's + // existing My Items collection. + return conn + .run(move |conn| { + diesel::update(collections::table.filter(collections::uuid.eq(&self.uuid))) + .set(self) + .execute(conn) + .map_res("Error saving collection") + }) + .await; + } + db_run! { conn: mysql { diesel::insert_into(collections::table) @@ -226,6 +283,12 @@ impl Collection { } pub async fn update_users_revision(&self, conn: &DbConn) { + if let Some(owner) = &self.default_user_uuid { + if Membership::find_confirmed_by_user_and_org(owner, &self.org_uuid, conn).await.is_some() { + User::update_uuid_revision(owner, conn).await; + } + return; + } for member in &Membership::find_by_collection_and_org(&self.uuid, &self.org_uuid, conn).await { User::update_uuid_revision(&member.user_uuid, conn).await; } @@ -266,12 +329,13 @@ impl Collection { .filter( users_collections::user_uuid .eq(user_uuid) + // access_all never reaches another member's My Items collection .or( // Directly accessed collection - users_organizations::access_all.eq(true), // access_all in Organization + users_organizations::access_all.eq(true).and(collections::default_user_uuid.is_null()), // access_all in Organization ) .or( - groups::access_all.eq(true), // access_all in groups + groups::access_all.eq(true).and(collections::default_user_uuid.is_null()), // access_all in groups ) .or( // access via groups @@ -302,7 +366,8 @@ impl Collection { .filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) .filter(users_collections::user_uuid.eq(user_uuid).or( // Directly accessed collection - users_organizations::access_all.eq(true), // access_all in Organization + // access_all in Organization, which never reaches another member's My Items collection + users_organizations::access_all.eq(true).and(collections::default_user_uuid.is_null()), )) .select(collections::all_columns) .distinct() @@ -354,6 +419,202 @@ impl Collection { .await } + pub async fn find_default_by_user_and_org( + user_uuid: &UserId, + org_uuid: &OrganizationId, + conn: &DbConn, + ) -> Option { + conn.run(move |conn| { + collections::table + .filter(collections::org_uuid.eq(org_uuid)) + .filter(collections::default_user_uuid.eq(user_uuid)) + .first::(conn) + .ok() + }) + .await + } + + /// Creates the member's My Items collection, with the name the client encrypted for it, when upstream does: + /// the member is confirmed, not an Owner or Admin, and the organization data ownership policy is enabled. + /// Without a name nothing is created, like upstream. + pub async fn create_default_user_collection(member: &Membership, name: Option<&str>, conn: &DbConn) -> EmptyResult { + if !OrgPolicy::is_personal_ownership_enforced_for(member, conn).await { + return Ok(()); + } + Self::create_default_user_collections(std::slice::from_ref(member), name, conn).await + } + + /// Creates all missing My Items collections for an enabled organization data ownership policy in one + /// transaction, and gives the members a missing access to their existing one back, which a member update racing + /// the creation could remove in earlier versions. The caller is responsible for checking that the policy is + /// enabled. Re-running this is safe and repairs incomplete provisioning from an earlier request. The unique index + /// on `(org_uuid, default_user_uuid)` makes sure a member never gets a second one, also when requests race. + pub async fn create_default_user_collections( + members: &[Membership], + name: Option<&str>, + conn: &DbConn, + ) -> EmptyResult { + let Some(name) = name.filter(|n| !n.trim().is_empty()) else { + return Ok(()); + }; + + let eligible: Vec<(OrganizationId, UserId)> = members + .iter() + .filter(|member| member.has_status(MembershipStatus::Confirmed) && member.atype < MembershipType::Admin) + .map(|member| (member.org_uuid.clone(), member.user_uuid.clone())) + .collect(); + let Some((org_uuid, _)) = eligible.first() else { + return Ok(()); + }; + if eligible.iter().any(|(member_org_uuid, _)| member_org_uuid != org_uuid) { + err!("Cannot create My Items collections for members of different organizations") + } + let org_uuid = org_uuid.clone(); + let user_uuids: Vec = eligible.into_iter().map(|(_, user_uuid)| user_uuid).collect(); + + // The body of the transaction below, for the members it found still eligible: gives them back a missing + // access to their existing My Items collection and creates the missing ones. Returns the members whose + // access changed. + macro_rules! provision { + ($conn:ident, $org_uuid:ident, $members:ident, $name:ident) => {{ + let missing_access = collections::table + .left_join( + users_collections::table.on(users_collections::collection_uuid + .eq(collections::uuid) + .and(users_collections::user_uuid.nullable().eq(collections::default_user_uuid))), + ) + .filter(collections::org_uuid.eq(&$org_uuid)) + .filter(collections::default_user_uuid.eq_any(&$members)) + .filter(users_collections::user_uuid.is_null()) + .select(( + collections::default_user_uuid.assume_not_null(), + collections::uuid, + false.into_sql::(), + false.into_sql::(), + true.into_sql::(), + )); + let restored = diesel::insert_into(users_collections::table) + .values(missing_access) + .into_columns(( + users_collections::user_uuid, + users_collections::collection_uuid, + users_collections::read_only, + users_collections::hide_passwords, + users_collections::manage, + )) + .execute($conn)?; + let existing: HashSet = collections::table + .filter(collections::org_uuid.eq(&$org_uuid)) + .filter(collections::default_user_uuid.eq_any(&$members)) + .select(collections::default_user_uuid) + .load::>($conn)? + .into_iter() + .flatten() + .collect(); + + let mut created = Vec::new(); + for user_uuid in $members.iter().filter(|user_uuid| !existing.contains(*user_uuid)) { + let mut collection = Self::new($org_uuid.clone(), $name.clone(), None); + collection.default_user_uuid = Some(user_uuid.clone()); + let owner_access = CollectionUser { + user_uuid: user_uuid.clone(), + collection_uuid: collection.uuid.clone(), + read_only: false, + hide_passwords: false, + manage: true, + }; + diesel::insert_into(collections::table).values(&collection).execute($conn)?; + diesel::insert_into(users_collections::table).values(&owner_access).execute($conn)?; + created.push(user_uuid.clone()); + } + Ok::<_, DieselError>(if restored > 0 { + $members + } else { + created + }) + }}; + } + + // A concurrent request can create one of these between the read and insert. Retry when the unique index + // rejects the insert, which MySQL and MariaDB often report as a deadlock instead of a unique violation; the + // next read omits the collections created by the racer. + for attempt in 0..3 { + if attempt > 0 { + // Give the concurrent request time to commit, so the next read sees what it created + sleep(Duration::from_millis(100)).await; + } + let (org_uuid, name) = (org_uuid.clone(), name.to_owned()); + // Only the members that are still confirmed, and neither an Owner nor an Admin, when the transaction runs + // get one. The members were loaded before, and a removal converting their My Items collection could have + // run meanwhile or run concurrently: see `Membership::delete()`, which removes the membership first. + let eligible = users_organizations::table + .filter(users_organizations::org_uuid.eq(org_uuid.clone())) + .filter(users_organizations::user_uuid.eq_any(user_uuids.clone())) + .filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) + // Every type below Admin, like `member.atype < MembershipType::Admin` above: Custom is stored as such + .filter(users_organizations::atype.eq_any([ + MembershipType::User as i32, + MembershipType::Custom as i32, + MembershipType::Manager as i32, + ])) + .select(users_organizations::user_uuid); + let result = db_run! { conn: + mysql, postgresql { + // Locking the memberships makes a concurrent removal wait until the collections are created, so + // it converts them, or makes this wait until the removal is done, so it doesn't find the member. + conn.transaction(|conn| { + let members = eligible.for_update().load::(conn)?; + provision!(conn, org_uuid, members, name) + }) + } + sqlite { + // No row locks: taking the database write lock before reading serializes this with the removal. + conn.immediate_transaction(|conn| { + let members = eligible.load::(conn)?; + provision!(conn, org_uuid, members, name) + }) + } + }; + + match result { + Ok(changed) => { + User::update_uuid_revisions(changed, conn).await; + return Ok(()); + } + Err(DieselError::DatabaseError( + DatabaseErrorKind::UniqueViolation | DatabaseErrorKind::SerializationFailure, + _, + )) => {} + Err(e) => return Err::<(), _>(e).map_res("Error creating My Items collections"), + } + } + + err!("A concurrent request kept changing the My Items collections; please retry") + } + + /// Offboards what remains of a deleted account in the organizations, once its memberships are removed, like + /// upstream's `User_DeleteById`: turns the My Items collections still attributed to the user into shared ones + /// named by their email address, and removes their collection access, which would otherwise block the deletion. + /// A member removal racing the creation of a My Items collection could leave both behind in earlier versions. + pub async fn release_all_by_user(user_uuid: &UserId, email: &str, conn: &DbConn) -> EmptyResult { + let email = email.to_owned(); + conn.run(move |conn| { + conn.transaction::<_, DieselError, _>(|conn| { + diesel::update(collections::table.filter(collections::default_user_uuid.eq(user_uuid))) + .set(( + collections::default_user_uuid.eq(None::), + collections::default_user_collection_email.eq(email), + )) + .execute(conn)?; + diesel::delete(users_collections::table.filter(users_collections::user_uuid.eq(user_uuid))) + .execute(conn)?; + Ok(()) + }) + .map_res("Error removing the user's collection access") + }) + .await + } + pub async fn find_by_uuid_and_user(uuid: &CollectionId, user_uuid: UserId, conn: &DbConn) -> Option { if CONFIG.org_groups_enabled() { conn.run(move |conn| { @@ -439,6 +700,12 @@ impl Collection { } pub async fn is_writable_by_user(&self, user_uuid: &UserId, conn: &DbConn) -> bool { + // Only its owner can put items into a My Items collection, admin rights and access_all don't reach into it. + // Like any collection access upstream, only while the owner is a confirmed member of the organization. + if let Some(owner) = &self.default_user_uuid { + return owner == user_uuid + && Membership::find_confirmed_by_user_and_org(user_uuid, &self.org_uuid, conn).await.is_some(); + } let user_uuid = user_uuid.to_string(); if CONFIG.org_groups_enabled() { conn.run(move |conn| { @@ -686,6 +953,8 @@ impl Collection { .and(collections_groups::collections_uuid.eq(collections::uuid))), ) .filter(collections::org_uuid.eq(&org_uuid)) + // Like upstream, only shared collections count: every member manages their own My Items collection + .filter(collections::default_user_uuid.is_null()) .filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) .filter( users_collections::manage.eq(true).or(users_collections::user_uuid @@ -914,6 +1183,38 @@ impl CollectionUser { .await } + /// Removes the user from all the organization's collections except their own My Items collection. In one + /// statement, like upstream's `OrganizationUser_UpdateWithCollections`, so it also keeps a My Items + /// collection another request creates meanwhile. + pub async fn delete_all_but_my_items_by_user_and_org( + user_uuid: &UserId, + org_uuid: &OrganizationId, + conn: &DbConn, + ) -> EmptyResult { + User::update_uuid_revision(user_uuid, conn).await; + + conn.run(move |conn| { + diesel::delete( + users_collections::table.filter(users_collections::user_uuid.eq(user_uuid)).filter( + users_collections::collection_uuid.eq_any( + collections::table + .filter(collections::org_uuid.eq(org_uuid)) + .filter( + collections::default_user_uuid + .is_null() + .or(collections::default_user_uuid.ne(user_uuid)), + ) + .select(collections::uuid), + ), + ), + ) + .execute(conn) + .map(|_| ()) + .map_res("Error removing user from collections") + }) + .await + } + pub async fn has_access_to_collection_by_user(col_id: &CollectionId, user_uuid: &UserId, conn: &DbConn) -> bool { Self::find_by_collection_and_user(col_id, user_uuid, conn).await.is_some() } @@ -988,6 +1289,104 @@ impl CollectionCipher { collection.update_users_revision(conn).await; } } + + /// The organizations' ciphers that are in My Items collections only. + pub async fn find_my_items_only_by_orgs(org_uuids: Vec, conn: &DbConn) -> HashSet { + if org_uuids.is_empty() { + return HashSet::new(); + } + conn.run(move |conn| { + ciphers_collections::table + .inner_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) + .filter(collections::org_uuid.eq_any(org_uuids)) + .group_by(ciphers_collections::cipher_uuid) + .select((ciphers_collections::cipher_uuid, count(collections::default_user_uuid), count_star())) + .load::<(CipherId, i64, i64)>(conn) + .expect("Error loading My Items ciphers") + .into_iter() + .filter(|(_, in_my_items, in_total)| in_my_items == in_total) + .map(|(cipher_uuid, _, _)| cipher_uuid) + .collect() + }) + .await + } + + /// The organization's ciphers that are in a My Items collection, each with those My Items collections. + pub async fn find_my_items_by_org( + org_uuid: &OrganizationId, + conn: &DbConn, + ) -> HashMap> { + conn.run(move |conn| { + ciphers_collections::table + .inner_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) + .filter(collections::org_uuid.eq(org_uuid)) + .filter(collections::default_user_uuid.is_not_null()) + .select(ciphers_collections::all_columns) + .load::<(CipherId, CollectionId)>(conn) + .expect("Error loading My Items ciphers") + .into_iter() + .fold(HashMap::new(), |mut map: HashMap>, (cipher, collection)| { + map.entry(cipher).or_default().push(collection); + map + }) + }) + .await + } + + /// The My Items collections the cipher is in, whoever's they are. + pub async fn find_my_items_of_cipher(cipher_uuid: &CipherId, conn: &DbConn) -> HashSet { + conn.run(move |conn| { + ciphers_collections::table + .inner_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) + .filter(ciphers_collections::cipher_uuid.eq(cipher_uuid)) + .filter(collections::default_user_uuid.is_not_null()) + .select(ciphers_collections::collection_uuid) + .load::(conn) + .expect("Error loading the cipher's My Items collections") + .into_iter() + .collect() + }) + .await + } + + /// Returns the owners when the cipher is assigned exclusively to My Items collections. `None` means that the + /// cipher is unassigned or has at least one shared collection assignment. + pub async fn find_my_items_owners_if_only(cipher_uuid: &CipherId, conn: &DbConn) -> Option> { + let owners = conn + .run(move |conn| { + ciphers_collections::table + .filter(ciphers_collections::cipher_uuid.eq(cipher_uuid)) + .inner_join(collections::table.on(collections::uuid.eq(ciphers_collections::collection_uuid))) + .select(collections::default_user_uuid) + .load::>(conn) + .expect("Error loading My Items owners") + }) + .await; + + if owners.is_empty() || owners.iter().any(Option::is_none) { + None + } else { + Some(owners.into_iter().flatten().collect()) + } + } + + pub async fn delete_all_shared_by_organization(org_uuid: &OrganizationId, conn: &DbConn) -> EmptyResult { + conn.run(move |conn| { + diesel::delete( + ciphers_collections::table.filter( + ciphers_collections::collection_uuid.eq_any( + collections::table + .filter(collections::org_uuid.eq(org_uuid)) + .filter(collections::default_user_uuid.is_null()) + .select(collections::uuid), + ), + ), + ) + .execute(conn) + .map_res("Error removing ciphers from the shared collections") + }) + .await + } } // Added in case we need the membership_uuid instead of the user_uuid diff --git a/src/db/models/event.rs b/src/db/models/event.rs index 84a73a0d..8ec3dc3a 100644 --- a/src/db/models/event.rs +++ b/src/db/models/event.rs @@ -110,7 +110,7 @@ pub enum EventType { OrganizationUserDeleted = 1515, // Both user and organization user data were deleted OrganizationUserLeft = 1516, // User voluntarily left the organization // OrganizationUserAutomaticallyConfirmed = 1517, - // OrganizationUserSelfRevoked = 1518, // User self-revoked due to declining organization data ownership policy + OrganizationUserSelfRevoked = 1518, // User self-revoked due to declining organization data ownership policy OrganizationUserAdminResetTwoFactor = 1519, OrganizationUserRevokedTwoFactorNonCompliance = 1520, OrganizationUserRevokedSingleOrganizationNonCompliance = 1521, diff --git a/src/db/models/org_policy.rs b/src/db/models/org_policy.rs index ccb738b2..2c30deed 100644 --- a/src/db/models/org_policy.rs +++ b/src/db/models/org_policy.rs @@ -405,6 +405,16 @@ impl OrgPolicy { false } + /// Whether the personal ownership (upstream: organization data ownership) policy of the member's organization + /// applies to them. Like upstream, it exempts Owners, Admins and members that are not confirmed. + pub async fn is_personal_ownership_enforced_for(member: &Membership, conn: &DbConn) -> bool { + member.has_status(MembershipStatus::Confirmed) + && member.atype < MembershipType::Admin + && Self::find_by_org_and_type(&member.org_uuid, OrgPolicyType::PersonalOwnership, conn) + .await + .is_some_and(|p| p.enabled) + } + pub async fn is_enabled_for_member(member_uuid: &MembershipId, policy_type: OrgPolicyType, conn: &DbConn) -> bool { if let Some(member) = Membership::find_by_uuid(member_uuid, conn).await && let Some(policy) = OrgPolicy::find_by_org_and_type(&member.org_uuid, policy_type, conn).await diff --git a/src/db/models/organization.rs b/src/db/models/organization.rs index 4b859406..9da3a427 100644 --- a/src/db/models/organization.rs +++ b/src/db/models/organization.rs @@ -15,8 +15,8 @@ use crate::{ db::{ DbConn, schema::{ - ciphers_collections, collections_groups, groups, groups_users, org_policies, organization_api_key, - organizations, users, users_collections, users_organizations, + ciphers_collections, collections, collections_groups, groups, groups_users, org_policies, + organization_api_key, organizations, users, users_collections, users_organizations, }, }, error::{Error, MapResult}, @@ -24,8 +24,8 @@ use crate::{ use macros::UuidFromParam; use super::{ - Cipher, CipherId, Collection, CollectionId, CollectionUser, Group, GroupId, GroupUser, KeyId, OrgPolicy, - OrgPolicyType, TwoFactor, User, UserId, + Cipher, CipherId, Collection, CollectionCipher, CollectionId, CollectionUser, Group, GroupId, GroupUser, KeyId, + OrgPolicy, OrgPolicyType, TwoFactor, User, UserId, }; /// A membership whose account recovery key still wraps the member's V1 user key: an upgrade to V2 encryption can't @@ -408,7 +408,7 @@ impl Organization { "useApi": true, "useDisableSMAdsForUsers": true, // Hide Secrets Manager ads "useInviteLinks": false, // Not (yet) supported - "useMyItems": false, // Not (yet) supported + "useMyItems": true, "useOrganizationDomains": false, // Not supported (Linked to SSO) "usePam": false, // Not supported "usePhishingBlocker": false, @@ -821,7 +821,7 @@ impl Membership { "useRiskInsights": false, // Not supported (Not AGPLv3 Licensed) "useDisableSMAdsForUsers": true, // Hide Secrets Manager ads "useInviteLinks": false, // Not (yet) supported - "useMyItems": false, // Not (yet) supported + "useMyItems": true, "useOrganizationDomains": false, // Not supported (Linked to SSO) "usePam": false, // Not supported "usePhishingBlocker": false, @@ -898,9 +898,12 @@ impl Membership { // If collections are to be included, only include them if the user does not have full access via a group or defined to the user it self // Only the collections assigned directly are returned, the ones assigned via a group are returned via a special group endpoint let collections: Vec = if include_collections && !(full_access_group || self.access_all) { + // Like upstream, a member's My Items collection is not part of their collection access + let my_items = Collection::find_default_by_user_and_org(&self.user_uuid, &self.org_uuid, conn).await; CollectionUser::find_by_organization_and_user_uuid(&self.org_uuid, &self.user_uuid, conn) .await .into_iter() + .filter(|cu| my_items.as_ref().is_none_or(|c| c.uuid != cu.collection_uuid)) .map(|cu| { json!({ "id": cu.collection_uuid, @@ -1056,15 +1059,60 @@ impl Membership { pub async fn delete(self, conn: &DbConn) -> EmptyResult { User::update_uuid_revision(&self.user_uuid, conn).await; - CollectionUser::delete_all_by_user_and_org(&self.user_uuid, &self.org_uuid, conn).await?; - GroupUser::delete_all_by_member(&self.uuid, conn).await?; - - conn.run(move |conn| { - diesel::delete(users_organizations::table.filter(users_organizations::uuid.eq(self.uuid))) - .execute(conn) + // Offboarding, like upstream's `OrganizationUser_DeleteById`: the member's My Items collection and its items + // stay in the organization as a shared collection, named by the former member's email address. Converted in + // the same transaction as the removal, so a failure can't convert it while the member stays, and by the + // member rather than by a collection looked up beforehand. + let (member_uuid, user_uuid, org_uuid) = (self.uuid, self.user_uuid, self.org_uuid); + let converted = conn + .run(move |conn| { + conn.transaction::<_, diesel::result::Error, _>(|conn| { + // Writes first: SQLite can't turn a transaction that started with a read into a writing one + // once another connection wrote meanwhile, and fails without waiting ("database is locked"). + diesel::delete(groups_users::table.filter(groups_users::users_organizations_uuid.eq(&member_uuid))) + .execute(conn)?; + // The membership goes before the conversion: its row lock (the write lock on SQLite) makes a + // concurrent creation of the member's My Items collection either finish first, so the conversion + // below finds it, or wait and then find the member gone. See + // `Collection::create_default_user_collections()`. + diesel::delete(users_organizations::table.filter(users_organizations::uuid.eq(&member_uuid))) + .execute(conn)?; + let my_items = collections::table + .filter(collections::org_uuid.eq(&org_uuid)) + .filter(collections::default_user_uuid.eq(&user_uuid)); + let converted = my_items.select(collections::uuid).load::(conn)?; + let email = users::table + .filter(users::uuid.eq(&user_uuid)) + .select(users::email) + .first::(conn) + .optional()?; + diesel::update(my_items) + .set(( + collections::default_user_uuid.eq(None::), + collections::default_user_collection_email.eq(email), + )) + .execute(conn)?; + diesel::delete( + users_collections::table.filter(users_collections::user_uuid.eq(&user_uuid)).filter( + users_collections::collection_uuid.eq_any( + collections::table + .filter(collections::org_uuid.eq(&org_uuid)) + .select(collections::uuid), + ), + ), + ) + .execute(conn)?; + Ok(converted) + }) .map_res("Error removing user from organization") - }) - .await + }) + .await?; + + // The members that now reach the former My Items collection sync it + for collection_uuid in &converted { + CollectionCipher::update_users_revision(collection_uuid, conn).await; + } + Ok(()) } pub async fn delete_all_by_organization(org_uuid: &OrganizationId, conn: &DbConn) -> EmptyResult { @@ -1547,6 +1595,7 @@ impl Membership { conn.run(move |conn| { users_organizations::table .filter(users_organizations::org_uuid.eq(org_uuid)) + .filter(users_organizations::status.eq(MembershipStatus::Confirmed as i32)) .left_join(users_collections::table.on(users_collections::user_uuid.eq(users_organizations::user_uuid))) .filter(users_organizations::access_all.eq(true).or( // AccessAll.. diff --git a/src/db/models/user.rs b/src/db/models/user.rs index 49ea67b7..7e0b5117 100644 --- a/src/db/models/user.rs +++ b/src/db/models/user.rs @@ -20,7 +20,7 @@ use crate::{ use macros::UuidFromParam; use super::{ - AuthRequest, Cipher, Device, EmergencyAccess, Favorite, Folder, Membership, MembershipType, TwoFactor, + AuthRequest, Cipher, Collection, Device, EmergencyAccess, Favorite, Folder, Membership, MembershipType, TwoFactor, TwoFactorIncomplete, UserSignatureKeyPair, }; @@ -494,6 +494,7 @@ impl User { EmergencyAccess::delete_all_by_user(&self.uuid, conn).await?; EmergencyAccess::delete_all_by_grantee_email(&self.email, conn).await?; Membership::delete_all_by_user(&self.uuid, conn).await?; + Collection::release_all_by_user(&self.uuid, &self.email, conn).await?; Cipher::delete_all_by_user(&self.uuid, conn).await?; Favorite::delete_all_by_user(&self.uuid, conn).await?; Folder::delete_all_by_user(&self.uuid, conn).await?; @@ -516,6 +517,30 @@ impl User { } } + pub async fn update_uuid_revisions(uuids: Vec, conn: &DbConn) { + if uuids.is_empty() { + return; + } + let updated_at = Utc::now().naive_utc(); + if let Err(e) = conn + .run(move |conn| { + retry( + || { + diesel::update(users::table.filter(users::uuid.eq_any(&uuids))) + .set(users::updated_at.eq(updated_at)) + .execute(conn) + }, + 10, + ) + .map(|_| ()) + .map_res("Error updating user revisions") + }) + .await + { + warn!("Failed to update user revisions: {e:#?}"); + } + } + pub async fn update_all_revisions(conn: &DbConn) -> EmptyResult { let updated_at = Utc::now().naive_utc(); diff --git a/src/db/schema.rs b/src/db/schema.rs index 112e14e6..003b625e 100644 --- a/src/db/schema.rs +++ b/src/db/schema.rs @@ -40,6 +40,8 @@ table! { org_uuid -> Text, name -> Text, external_id -> Nullable, + default_user_uuid -> Nullable, + default_user_collection_email -> Nullable, } } diff --git a/src/util.rs b/src/util.rs index 409867b3..a88082c5 100644 --- a/src/util.rs +++ b/src/util.rs @@ -600,6 +600,48 @@ pub fn is_valid_email(email: &str) -> bool { email_url.domain().is_some() && email_url.path() == "/" && email_url.query().is_none() } +/// Returns whether the value has the form of an encrypted string, like upstream's `EncryptedStringAttribute`: +/// `.|...` with a known encryption type, its number of base64 pieces, and the fixed decoded +/// lengths of the IV (16 bytes) and the MAC (32 bytes). Without a type it is the legacy `iv|ct[|mac]` format. +pub fn is_valid_enc_string(value: &str) -> bool { + const IV: Option = Some(16); + const MAC: Option = Some(32); + const ANY: Option = None; + + let (pieces, rest): (&[Option], &str) = match value.split_once('.') { + Some((header, rest)) => match header.parse::() { + // AesCbc256_B64 + Ok(0) => (&[IV, ANY], rest), + // AesCbc256_HmacSha256_B64 + Ok(2) => (&[IV, ANY, MAC], rest), + // Rsa2048_OaepSha256_B64, Rsa2048_OaepSha1_B64, and type 7: a CBOR encoded Encrypt0 message + Ok(3 | 4 | 7) => (&[ANY], rest), + // Rsa2048_OaepSha256_HmacSha256_B64, Rsa2048_OaepSha1_HmacSha256_B64 + Ok(5 | 6) => (&[ANY, ANY], rest), + _ => return false, + }, + None if value.matches('|').count() == 2 => (&[IV, ANY, MAC], value), + None => (&[IV, ANY], value), + }; + + let parts: Vec<&str> = rest.split('|').collect(); + parts.len() == pieces.len() && parts.iter().zip(pieces).all(|(part, len)| is_valid_base64_piece(part, *len)) +} + +/// Base64 with padding and, like upstream, any value for the unused bits of the last character. +fn is_valid_base64_piece(piece: &str, decoded_len: Option) -> bool { + let bytes = piece.as_bytes(); + if bytes.is_empty() || !bytes.len().is_multiple_of(4) { + return false; + } + let padding = bytes.iter().rev().take_while(|&&b| b == b'=').count(); + if padding > 2 { + return false; + } + bytes[..bytes.len() - padding].iter().all(|b| b.is_ascii_alphanumeric() || *b == b'+' || *b == b'/') + && decoded_len.is_none_or(|len| bytes.len() / 4 * 3 - padding == len) +} + // // Deployment environment methods //