diff --git a/crates/archivr-server/src/entry_access.rs b/crates/archivr-server/src/entry_access.rs index 083285a..d490139 100644 --- a/crates/archivr-server/src/entry_access.rs +++ b/crates/archivr-server/src/entry_access.rs @@ -407,3 +407,32 @@ async fn collection_membership_endpoints_are_gated_by_entry_visibility() { let (status, _) = send_json(&w.f.router, "DELETE", &uri, Some(&w.carol), None).await; assert_eq!(status, StatusCode::NO_CONTENT); } + +#[tokio::test] +async fn a_plain_user_cannot_hide_an_entry_from_all_of_their_own_roles() { + let w = world().await; + let default_coll = { + let conn = database::open_or_initialize(&w.f.archive_path).unwrap(); + database::get_collection_by_slug(&conn, "_default_").unwrap().unwrap().collection_uid + }; + let uri = format!("/api/archives/test/collections/{default_coll}/entries/{}", w.open); + + // alice may keep the entry for herself, but not narrow it to admins only (she could never undo it). + let (status, body) = + send_json(&w.f.router, "PATCH", &uri, Some(&w.alice), Some(&json!({"visibility_bits": 4}))).await; + assert_eq!(status, StatusCode::BAD_REQUEST); + assert!(body["error"].as_str().unwrap().contains("hide the entry from all of your own roles"), "{body}"); + let (status, _) = send_json( + &w.f.router, "GET", &format!("/api/archives/test/entries/{}", w.open), Some(&w.alice), None, + ) + .await; + assert_eq!(status, StatusCode::OK, "the rejected change must be rolled back"); + + // A change that still includes one of her roles is fine, and so is the same change by an admin. + let (status, _) = + send_json(&w.f.router, "PATCH", &uri, Some(&w.alice), Some(&json!({"visibility_bits": 6}))).await; + assert_eq!(status, StatusCode::NO_CONTENT); + let (status, _) = + send_json(&w.f.router, "PATCH", &uri, Some(&w.admin), Some(&json!({"visibility_bits": 4}))).await; + assert_eq!(status, StatusCode::NO_CONTENT); +} diff --git a/crates/archivr-server/src/routes.rs b/crates/archivr-server/src/routes.rs index f0a5559..2af079c 100644 --- a/crates/archivr-server/src/routes.rs +++ b/crates/archivr-server/src/routes.rs @@ -3303,6 +3303,33 @@ pub(crate) fn ensure_entry_visible( Ok(()) } +/// Runs `change` (a mutation of the entry's collection memberships or their visibility) and, for +/// callers who are not admins, rolls it back with a 400 when it would leave the entry hidden from +/// the caller's own roles. Without this a plain user could hide an entry from every role they hold +/// and could never reach it again to undo it (only an admin could). +fn apply_without_self_lockout( + conn: &rusqlite::Connection, + auth: &AuthUser, + entry_uid: &str, + change: impl FnOnce() -> Result, +) -> Result { + let AuthUser::Authenticated { role_bits, .. } = auth else { + return change(); + }; + if role_bits & (ROLE_ADMIN | ROLE_OWNER) != 0 { + return change(); + } + let tx = conn.unchecked_transaction()?; + let out = change()?; + if !database::caller_can_access_entry(conn, entry_uid, *role_bits)? { + return Err(ApiError::bad_request( + "this change would hide the entry from all of your own roles; ask an admin to do it", + )); + } + tx.commit()?; + Ok(out) +} + pub(crate) fn mounted_archive<'a>( state: &'a AppState, archive_id: &str, @@ -3550,7 +3577,10 @@ async fn remove_entry_from_collection_handler( ) .optional()? .ok_or(ApiError::not_found("entry not found"))?; - if database::remove_entry_from_collection(&conn, coll.id, entry_id)? { + let removed = apply_without_self_lockout(&conn, &auth, &entry_uid, || { + Ok(database::remove_entry_from_collection(&conn, coll.id, entry_id)?) + })?; + if removed { Ok(StatusCode::NO_CONTENT) } else { Err(ApiError::not_found("entry not in collection")) @@ -3577,8 +3607,15 @@ async fn update_entry_visibility_handler( ) .optional()? .ok_or(ApiError::not_found("entry not found"))?; - if database::update_collection_entry_visibility(&conn, coll.id, entry_id, body.visibility_bits)? - { + let updated = apply_without_self_lockout(&conn, &auth, &entry_uid, || { + Ok(database::update_collection_entry_visibility( + &conn, + coll.id, + entry_id, + body.visibility_bits, + )?) + })?; + if updated { Ok(StatusCode::NO_CONTENT) } else { Err(ApiError::not_found("entry not in collection"))