1
Fork 0
mirror of https://github.com/thegeneralist01/archivr synced 2026-12-04 18:56:44 +01:00

Refuse visibility changes that lock a non-admin out of their own entry

A plain user who narrowed an entry to roles they do not hold could no
longer reach it by uid (entry visibility is now enforced everywhere) and so
could never undo it. Changing or removing a collection membership is now
rolled back with a 400 when it would leave the entry invisible to the
caller; admins and owners are exempt.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
TheGeneralist 2026-10-09 22:06:21 +02:00
parent 2bc656f8f1
commit 74a7703ca5
Signed by: thegeneralist01
SSH key fingerprint: SHA256:pp9qddbCNmVNoSjevdvQvM5z0DHN7LTa8qBMbcMq/R4
2 changed files with 69 additions and 3 deletions

View file

@ -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);
}

View file

@ -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<T>(
conn: &rusqlite::Connection,
auth: &AuthUser,
entry_uid: &str,
change: impl FnOnce() -> Result<T, ApiError>,
) -> Result<T, ApiError> {
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"))