diff --git a/crates/archivr-server/src/entry_access.rs b/crates/archivr-server/src/entry_access.rs index 6f6c40b..4a8e3f0 100644 --- a/crates/archivr-server/src/entry_access.rs +++ b/crates/archivr-server/src/entry_access.rs @@ -384,6 +384,27 @@ async fn media_token_stops_working_when_its_issuer_is_deleted() { assert_eq!(status, StatusCode::UNAUTHORIZED); } +#[tokio::test] +async fn media_token_does_not_pass_to_a_new_user_that_reuses_the_issuers_row_id() { + let w = world().await; + // The newest user gets the highest row id; SQLite hands that id out again after a delete. + let zed = make_role_session(&w.f.auth_path, "zed", &["user"]); + let url = issue_media_token(&w, &zed, &w.open).await; + let (status, _) = send(&w.f.router, "GET", &url, None, None).await; + assert_eq!(status, StatusCode::OK); + + let zed_uid = user_uid(&w.f.auth_path, "zed"); + let (status, _) = send( + &w.f.router, "DELETE", &format!("/api/admin/users/{zed_uid}"), Some(&w.admin), None, + ) + .await; + assert_eq!(status, StatusCode::NO_CONTENT); + let _yan = make_role_session(&w.f.auth_path, "yan", &["user"]); + + let (status, _) = send(&w.f.router, "GET", &url, None, None).await; + assert_eq!(status, StatusCode::UNAUTHORIZED, "the token belonged to a deleted account"); +} + #[tokio::test] async fn an_admin_issued_media_token_for_a_hidden_entry_keeps_working() { let w = world().await; diff --git a/crates/archivr-server/src/routes.rs b/crates/archivr-server/src/routes.rs index b00c215..0b7483f 100644 --- a/crates/archivr-server/src/routes.rs +++ b/crates/archivr-server/src/routes.rs @@ -66,8 +66,9 @@ pub(crate) struct MediaToken { archive_id: String, entry_uid: String, artifact_index: usize, - /// The account that issued the token. Its status and access are re-checked on every use. - user_id: i64, + /// The `user_uid` of the account that issued the token (not the integer id: SQLite reuses the + /// rowid of the newest user once deleted). Its status and access are re-checked on every use. + user_uid: String, expires_at: std::time::Instant, } @@ -938,13 +939,13 @@ async fn serve_artifact( && t.artifact_index == artifact_index && t.expires_at > std::time::Instant::now() }) - .map(|t| t.user_id) + .map(|t| t.user_uid.clone()) }); let mounted = mounted_archive(&state, &archive_id)?; let paths = archive::read_archive_paths(&mounted.archive_path)?; let conn = database::open_or_initialize(&mounted.archive_path)?; let token_valid = match token_issuer { - Some(issuer_id) => media_token_issuer_can_view(&state, &conn, issuer_id, &entry_uid)?, + Some(issuer_uid) => media_token_issuer_can_view(&state, &conn, &issuer_uid, &entry_uid)?, None => false, }; if !token_valid { @@ -983,6 +984,8 @@ async fn issue_media_token( Path((archive_id, entry_uid, artifact_index)): Path<(String, String, usize)>, ) -> Result, ApiError> { let (user_id, _) = auth_user.require_auth()?; + let user_uid = database::get_user_uid(&database::open_auth_db(&state.auth_db_path)?, user_id)? + .ok_or_else(|| ApiError::unauthorized("login required"))?; // Verify the artifact actually exists before issuing a token. let mounted = mounted_archive(&state, &archive_id)?; let conn = database::open_or_initialize(&mounted.archive_path)?; @@ -1004,7 +1007,7 @@ async fn issue_media_token( archive_id: archive_id.clone(), entry_uid: entry_uid.clone(), artifact_index, - user_id, + user_uid, expires_at: now + MEDIA_TOKEN_TTL, }, ); @@ -3325,10 +3328,13 @@ pub(crate) fn ensure_entry_visible( fn media_token_issuer_can_view( state: &AppState, conn: &rusqlite::Connection, - issuer_id: i64, + issuer_uid: &str, entry_uid: &str, ) -> Result { let auth_conn = database::open_auth_db(&state.auth_db_path)?; + let Some(issuer_id) = database::get_user_id_by_uid(&auth_conn, issuer_uid)? else { + return Ok(false); + }; if !database::user_is_active(&auth_conn, issuer_id)? { return Ok(false); } diff --git a/frontend/src/api.js b/frontend/src/api.js index c26115a..2efe474 100644 --- a/frontend/src/api.js +++ b/frontend/src/api.js @@ -417,7 +417,7 @@ export async function createToken(name, { expiresInDays = null, scope = 'full' } headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ name, expires_in_days: expiresInDays, scope }), }); - if (!res.ok) throw new Error(await res.text()); + if (!res.ok) { const b = await res.json().catch(() => ({})); throw new Error(b.error || `HTTP ${res.status}`); } return res.json(); }