mirror of
https://github.com/dani-garcia/vaultwarden.git
synced 2026-05-24 21:10:21 +03:00
Replace organization_uuid unwrap with proper error handling (#6936)
The collection update endpoints (post_collections_update and post_collections_admin) call .unwrap() on cipher.organization_uuid in four places. If a user-owned cipher without an organization somehow reaches these code paths, the server would panic. Extract the organization UUID early with a descriptive error message instead of relying on .unwrap(), preventing potential panics and providing a clear API error response. Co-authored-by: Daniel García <dani-garcia@users.noreply.github.com>
This commit is contained in:
+12
-4
@@ -814,12 +814,16 @@ async fn post_collections_update(
|
||||
err!("Collection cannot be changed")
|
||||
}
|
||||
|
||||
let Some(ref org_uuid) = cipher.organization_uuid else {
|
||||
err!("Cipher is not owned by an organization")
|
||||
};
|
||||
|
||||
let posted_collections = HashSet::<CollectionId>::from_iter(data.collection_ids);
|
||||
let current_collections =
|
||||
HashSet::<CollectionId>::from_iter(cipher.get_collections(headers.user.uuid.clone(), &conn).await);
|
||||
|
||||
for collection in posted_collections.symmetric_difference(¤t_collections) {
|
||||
match Collection::find_by_uuid_and_org(collection, cipher.organization_uuid.as_ref().unwrap(), &conn).await {
|
||||
match Collection::find_by_uuid_and_org(collection, org_uuid, &conn).await {
|
||||
None => err!("Invalid collection ID provided"),
|
||||
Some(collection) => {
|
||||
if collection.is_writable_by_user(&headers.user.uuid, &conn).await {
|
||||
@@ -850,7 +854,7 @@ async fn post_collections_update(
|
||||
log_event(
|
||||
EventType::CipherUpdatedCollections as i32,
|
||||
&cipher.uuid,
|
||||
&cipher.organization_uuid.clone().unwrap(),
|
||||
org_uuid,
|
||||
&headers.user.uuid,
|
||||
headers.device.atype,
|
||||
&headers.ip.ip,
|
||||
@@ -890,12 +894,16 @@ async fn post_collections_admin(
|
||||
err!("Collection cannot be changed")
|
||||
}
|
||||
|
||||
let Some(ref org_uuid) = cipher.organization_uuid else {
|
||||
err!("Cipher is not owned by an organization")
|
||||
};
|
||||
|
||||
let posted_collections = HashSet::<CollectionId>::from_iter(data.collection_ids);
|
||||
let current_collections =
|
||||
HashSet::<CollectionId>::from_iter(cipher.get_admin_collections(headers.user.uuid.clone(), &conn).await);
|
||||
|
||||
for collection in posted_collections.symmetric_difference(¤t_collections) {
|
||||
match Collection::find_by_uuid_and_org(collection, cipher.organization_uuid.as_ref().unwrap(), &conn).await {
|
||||
match Collection::find_by_uuid_and_org(collection, org_uuid, &conn).await {
|
||||
None => err!("Invalid collection ID provided"),
|
||||
Some(collection) => {
|
||||
if collection.is_writable_by_user(&headers.user.uuid, &conn).await {
|
||||
@@ -926,7 +934,7 @@ async fn post_collections_admin(
|
||||
log_event(
|
||||
EventType::CipherUpdatedCollections as i32,
|
||||
&cipher.uuid,
|
||||
&cipher.organization_uuid.unwrap(),
|
||||
org_uuid,
|
||||
&headers.user.uuid,
|
||||
headers.device.atype,
|
||||
&headers.ip.ip,
|
||||
|
||||
Reference in New Issue
Block a user