mirror of
https://github.com/thegeneralist01/archivr
synced 2026-12-04 18:56:44 +01:00
fix: bind media tokens to the issuer's user_uid; show token-create errors as text
SQLite reuses the row id of the newest user once deleted, so a media token keyed by the integer id could pass to a later account. Tokens now store the user_uid and the check fails when that account no longer exists. The token creation form also shows the server's error message instead of raw JSON. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
0db95f06d8
commit
e300ebf1a0
3 changed files with 34 additions and 7 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -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<Json<MediaTokenResponse>, 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<bool, ApiError> {
|
||||
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);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue