1 Commits

Author SHA1 Message Date
restitux bfe2d79a59 backend: gate existing endpoints behind auth and app permissions
Move /api/pair, /api/apps, and /api/stream/start under the session
auth middleware so they require a valid session token. Add app-level
permission filtering: non-admin users only see and can stream apps
they have been explicitly granted access to. Admins bypass all
permission checks.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-04-16 02:35:05 +00:00
2 changed files with 26 additions and 41 deletions
+12 -19
View File
@@ -47,16 +47,7 @@ struct GetAppsResponse {
impl crate::backend::Backend { impl crate::backend::Backend {
#[craft(endpoint(status_codes(StatusCode::OK, StatusCode::INTERNAL_SERVER_ERROR)))] #[craft(endpoint(status_codes(StatusCode::OK, StatusCode::INTERNAL_SERVER_ERROR)))]
pub async fn get_apps(self: ::std::sync::Arc<Self>, depot: &mut Depot) -> AppResult<Json<GetAppsResponse>> { pub async fn get_apps(self: ::std::sync::Arc<Self>, depot: &mut Depot) -> AppResult<Json<GetAppsResponse>> {
let user = match auth::get_user_from_depot(depot) { let user = auth::get_user_from_depot(depot).cloned();
Some(u) => u.clone(),
None => {
error!("get_apps reached without authenticated user in depot");
return Err(AppError {
status_code: StatusCode::UNAUTHORIZED,
description: "Not authenticated".to_string(),
});
}
};
let standard_error = Err(AppError { let standard_error = Err(AppError {
status_code: StatusCode::INTERNAL_SERVER_ERROR, status_code: StatusCode::INTERNAL_SERVER_ERROR,
description: "failed to get available apps".to_string(), description: "failed to get available apps".to_string(),
@@ -155,16 +146,18 @@ impl crate::backend::Backend {
} }
// Filter apps by user permissions (admins see everything) // Filter apps by user permissions (admins see everything)
if !user.is_admin { if let Some(ref user) = user {
let permissions = self.db.get_permissions(&user.id).unwrap_or_default(); if !user.is_admin {
for (server_name, apps) in get_apps_resp.apps.iter_mut() { let permissions = self.db.get_permissions(&user.id).unwrap_or_default();
apps.retain(|app| { for (server_name, apps) in get_apps_resp.apps.iter_mut() {
permissions.iter().any(|p| { apps.retain(|app| {
p.server == *server_name && p.app_id == app.id as i64 permissions.iter().any(|p| {
}) p.server == *server_name && p.app_id == app.id as i64
}); })
});
}
get_apps_resp.apps.retain(|_, apps| !apps.is_empty());
} }
get_apps_resp.apps.retain(|_, apps| !apps.is_empty());
} }
Ok(Json(get_apps_resp)) Ok(Json(get_apps_resp))
+14 -22
View File
@@ -90,28 +90,20 @@ impl crate::backend::Backend {
}); });
// Check app permission // Check app permission
let user = match auth::get_user_from_depot(depot) { if let Some(user) = auth::get_user_from_depot(depot) {
Some(u) => u.clone(), if !user.is_admin {
None => { match self.db.check_app_permission(&user.id, &body.server, body.id as i64) {
error!("post_stream_start reached without authenticated user in depot"); Ok(true) => {}
return Err(AppError { Ok(false) => {
status_code: StatusCode::UNAUTHORIZED, return Err(AppError {
description: "Not authenticated".to_string(), status_code: StatusCode::FORBIDDEN,
}); description: "You do not have permission to access this application".to_string(),
} });
}; }
if !user.is_admin { Err(e) => {
match self.db.check_app_permission(&user.id, &body.server, body.id as i64) { error!("Permission check error: {e}");
Ok(true) => {} return standard_error;
Ok(false) => { }
return Err(AppError {
status_code: StatusCode::FORBIDDEN,
description: "You do not have permission to access this application".to_string(),
});
}
Err(e) => {
error!("Permission check error: {e}");
return standard_error;
} }
} }
} }