mirror of
https://github.com/thegeneralist01/archivr
synced 2026-12-04 18:56:44 +01:00
fix: deleting a collection cannot lock a non-admin out of its entries
Deleting a collection cascades its memberships, which could hide an entry from every role a plain user holds, with no way for them to undo it. The delete now runs under the same self-lockout check as the other membership changes and is refused with a 400 when it would hide an entry. Co-Authored-By: Claude Haiku 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
6e5bd070ef
commit
65aeb82728
3 changed files with 175 additions and 12 deletions
|
|
@ -3459,6 +3459,20 @@ pub fn update_collection(
|
|||
Ok(true)
|
||||
}
|
||||
|
||||
/// Returns the entry_uid of every entry that is a member of the collection.
|
||||
pub fn collection_entry_uids(conn: &Connection, collection_id: i64) -> Result<Vec<String>> {
|
||||
let mut stmt = conn.prepare(
|
||||
"SELECT e.entry_uid FROM collection_entries ce \
|
||||
JOIN archived_entries e ON e.id = ce.entry_id \
|
||||
WHERE ce.collection_id = ?1 \
|
||||
ORDER BY e.id",
|
||||
)?;
|
||||
let uids = stmt
|
||||
.query_map([collection_id], |row| row.get::<_, String>(0))?
|
||||
.collect::<rusqlite::Result<Vec<_>>>()?;
|
||||
Ok(uids)
|
||||
}
|
||||
|
||||
/// Deletes a collection and cascades to collection_entries.
|
||||
/// Returns true if deleted, false if not found.
|
||||
/// Refuses to delete the '_default_' collection.
|
||||
|
|
@ -3923,6 +3937,27 @@ mod tests {
|
|||
assert_eq!(public_index_entry_count(&conn).unwrap(), 1);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn collection_entry_uids_lists_members_only() {
|
||||
let conn = conn();
|
||||
let a = create_entry_fixture(&conn, "private", None, None);
|
||||
let b = create_entry_fixture(&conn, "private", None, None);
|
||||
let outside = create_entry_fixture(&conn, "private", None, None);
|
||||
let coll = create_collection(&conn, "Mine", "mine", 2, false).unwrap();
|
||||
add_entry_to_collection(&conn, coll.id, a.id, 2).unwrap();
|
||||
add_entry_to_collection(&conn, coll.id, b.id, 4).unwrap();
|
||||
|
||||
let mut uids = collection_entry_uids(&conn, coll.id).unwrap();
|
||||
uids.sort();
|
||||
let mut expected = vec![a.entry_uid.clone(), b.entry_uid.clone()];
|
||||
expected.sort();
|
||||
assert_eq!(uids, expected);
|
||||
assert!(!uids.contains(&outside.entry_uid));
|
||||
|
||||
let empty = create_collection(&conn, "Empty", "empty", 2, false).unwrap();
|
||||
assert!(collection_entry_uids(&conn, empty.id).unwrap().is_empty());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn default_collection_allows_visibility_change_but_not_rename() {
|
||||
let conn = conn();
|
||||
|
|
|
|||
|
|
@ -437,6 +437,105 @@ async fn a_plain_user_cannot_hide_an_entry_from_all_of_their_own_roles() {
|
|||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
}
|
||||
|
||||
/// alice builds a collection holding `open` at her own role and drops the default-collection
|
||||
/// membership to admin-only, so the custom collection is the only thing letting her see it.
|
||||
/// Returns the custom collection's uid.
|
||||
async fn alice_collection_is_only_access(w: &World) -> String {
|
||||
let (status, coll) = send_json(
|
||||
&w.f.router,
|
||||
"POST",
|
||||
"/api/archives/test/collections",
|
||||
Some(&w.alice),
|
||||
Some(&json!({"name": "Private", "slug": "private"})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::CREATED, "{coll}");
|
||||
let coll_uid = coll["collection_uid"].as_str().unwrap().to_string();
|
||||
let (status, _) = send_json(
|
||||
&w.f.router,
|
||||
"POST",
|
||||
&format!("/api/archives/test/collections/{coll_uid}/entries"),
|
||||
Some(&w.alice),
|
||||
Some(&json!({"entry_uid": w.open, "visibility_bits": 2})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
{
|
||||
let conn = database::open_or_initialize(&w.f.archive_path).unwrap();
|
||||
conn.execute(
|
||||
"UPDATE collection_entries SET visibility_bits = 4 \
|
||||
WHERE entry_id = (SELECT id FROM archived_entries WHERE entry_uid = ?1) \
|
||||
AND collection_id = (SELECT id FROM collections WHERE slug = '_default_')",
|
||||
[&w.open],
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
coll_uid
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_plain_user_cannot_delete_the_collection_that_is_their_only_access_to_an_entry() {
|
||||
let w = world().await;
|
||||
let coll_uid = alice_collection_is_only_access(&w).await;
|
||||
let coll_uri = format!("/api/archives/test/collections/{coll_uid}");
|
||||
let (status, _) = send_json(&w.f.router, "GET", &entry_path(&w.open, ""), Some(&w.alice), None).await;
|
||||
assert_eq!(status, StatusCode::OK, "alice can see the entry before the delete");
|
||||
|
||||
let (status, body) = send_json(&w.f.router, "DELETE", &coll_uri, Some(&w.alice), None).await;
|
||||
assert_eq!(status, StatusCode::BAD_REQUEST, "{body}");
|
||||
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", &coll_uri, Some(&w.admin), None).await;
|
||||
assert_eq!(status, StatusCode::OK, "the rejected delete must be rolled back");
|
||||
let (status, _) = send_json(&w.f.router, "GET", &entry_path(&w.open, ""), Some(&w.alice), None).await;
|
||||
assert_eq!(status, StatusCode::OK, "the entry must stay visible to alice");
|
||||
|
||||
// An admin may still delete it, and then the entry really is gone from alice's view.
|
||||
let (status, _) = send_json(&w.f.router, "DELETE", &coll_uri, Some(&w.admin), None).await;
|
||||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
let (status, _) = send_json(&w.f.router, "GET", &entry_path(&w.open, ""), Some(&w.alice), None).await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_plain_user_can_delete_a_collection_whose_entries_stay_visible_elsewhere() {
|
||||
let w = world().await;
|
||||
let (status, coll) = send_json(
|
||||
&w.f.router,
|
||||
"POST",
|
||||
"/api/archives/test/collections",
|
||||
Some(&w.alice),
|
||||
Some(&json!({"name": "Reading", "slug": "reading"})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::CREATED, "{coll}");
|
||||
let coll_uri = format!("/api/archives/test/collections/{}", coll["collection_uid"].as_str().unwrap());
|
||||
// `open` stays in the default collection at bits 2 (alice's role), so nothing is lost.
|
||||
let (status, _) = send_json(
|
||||
&w.f.router,
|
||||
"POST",
|
||||
&format!("{coll_uri}/entries"),
|
||||
Some(&w.alice),
|
||||
Some(&json!({"entry_uid": w.open, "visibility_bits": 2})),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::NO_CONTENT);
|
||||
|
||||
let (status, body) = send_json(&w.f.router, "DELETE", &coll_uri, Some(&w.alice), None).await;
|
||||
assert_eq!(status, StatusCode::NO_CONTENT, "{body}");
|
||||
let (status, _) = send_json(&w.f.router, "GET", &entry_path(&w.open, ""), Some(&w.alice), None).await;
|
||||
assert_eq!(status, StatusCode::OK);
|
||||
|
||||
let (status, _) = send_json(
|
||||
&w.f.router,
|
||||
"DELETE",
|
||||
"/api/archives/test/collections/no_such_collection",
|
||||
Some(&w.alice),
|
||||
None,
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::NOT_FOUND);
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_role_created_after_deleting_one_never_inherits_its_hidden_entries() {
|
||||
let w = world().await;
|
||||
|
|
|
|||
|
|
@ -3313,14 +3313,14 @@ 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).
|
||||
/// Runs `change` (a mutation of collection memberships or their visibility touching `entry_uids`)
|
||||
/// and, for callers who are not admins, rolls it back with a 400 when it would leave any of those
|
||||
/// entries 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<T>(
|
||||
conn: &rusqlite::Connection,
|
||||
auth: &AuthUser,
|
||||
entry_uid: &str,
|
||||
entry_uids: &[String],
|
||||
change: impl FnOnce() -> Result<T, ApiError>,
|
||||
) -> Result<T, ApiError> {
|
||||
let AuthUser::Authenticated { role_bits, .. } = auth else {
|
||||
|
|
@ -3329,12 +3329,33 @@ fn apply_without_self_lockout<T>(
|
|||
if role_bits & (ROLE_ADMIN | ROLE_OWNER) != 0 {
|
||||
return change();
|
||||
}
|
||||
// Only entries the caller can see now can be locked out by the change; entries they could
|
||||
// not see before are not theirs to lose.
|
||||
let mut visible_before = Vec::new();
|
||||
for uid in entry_uids {
|
||||
if database::caller_can_access_entry(conn, uid, *role_bits)? {
|
||||
visible_before.push(uid);
|
||||
}
|
||||
}
|
||||
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",
|
||||
));
|
||||
let mut hidden = 0usize;
|
||||
for uid in &visible_before {
|
||||
if !database::caller_can_access_entry(conn, uid, *role_bits)? {
|
||||
hidden += 1;
|
||||
}
|
||||
}
|
||||
if hidden > 0 {
|
||||
// Returning before `commit` drops `tx`, which rolls the change back.
|
||||
let message = if hidden == 1 {
|
||||
"this change would hide the entry from all of your own roles; ask an admin to do it"
|
||||
.to_string()
|
||||
} else {
|
||||
format!(
|
||||
"this change would hide {hidden} entries from all of your own roles; ask an admin to do it"
|
||||
)
|
||||
};
|
||||
return Err(ApiError::bad_request(&message));
|
||||
}
|
||||
tx.commit()?;
|
||||
Ok(out)
|
||||
|
|
@ -3587,7 +3608,7 @@ async fn remove_entry_from_collection_handler(
|
|||
)
|
||||
.optional()?
|
||||
.ok_or(ApiError::not_found("entry not found"))?;
|
||||
let removed = apply_without_self_lockout(&conn, &auth, &entry_uid, || {
|
||||
let removed = apply_without_self_lockout(&conn, &auth, std::slice::from_ref(&entry_uid), || {
|
||||
Ok(database::remove_entry_from_collection(&conn, coll.id, entry_id)?)
|
||||
})?;
|
||||
if removed {
|
||||
|
|
@ -3617,7 +3638,7 @@ async fn update_entry_visibility_handler(
|
|||
)
|
||||
.optional()?
|
||||
.ok_or(ApiError::not_found("entry not found"))?;
|
||||
let updated = apply_without_self_lockout(&conn, &auth, &entry_uid, || {
|
||||
let updated = apply_without_self_lockout(&conn, &auth, std::slice::from_ref(&entry_uid), || {
|
||||
Ok(database::update_collection_entry_visibility(
|
||||
&conn,
|
||||
coll.id,
|
||||
|
|
@ -3683,7 +3704,15 @@ async fn delete_collection_handler(
|
|||
"cannot delete the default collection",
|
||||
));
|
||||
}
|
||||
let deleted = database::delete_collection(&conn, &coll_uid)?;
|
||||
// Deleting a collection drops its memberships. Refuse (like the other membership changes) when
|
||||
// that would hide an entry from a non-admin caller's own roles.
|
||||
let Some(coll) = database::get_collection_by_uid(&conn, &coll_uid)? else {
|
||||
return Err(ApiError::not_found("collection not found"));
|
||||
};
|
||||
let member_uids = database::collection_entry_uids(&conn, coll.id)?;
|
||||
let deleted = apply_without_self_lockout(&conn, &auth, &member_uids, || {
|
||||
Ok(database::delete_collection(&conn, &coll_uid)?)
|
||||
})?;
|
||||
if deleted {
|
||||
Ok(StatusCode::NO_CONTENT)
|
||||
} else {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue