Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 48 additions & 51 deletions team-server/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ use crate::model::{
};
use crate::store::{
personal_space, BlobMeta, BodyRef, Folder, FolderDelete, FolderRename, Item, Lease, Listed,
ProfileFields, ProxyFields, Refusal, Restored, SessionHead, Standing, Store, StoredItem,
ProfileFields, ProxyFields, Reader, Refusal, Restored, SessionHead, Standing, Store, StoredItem,
TeamProxy, TrashItem, TrashKind, Upload, Writer, CHUNK, META_TEAM_NAME, TEAM,
};
use hyper::body::{Frame, SizeHint};
Expand Down Expand Up @@ -463,7 +463,7 @@ impl From<anyhow::Error> for ApiError {
}
}

/// A write the store would not make. Living in another space reads exactly
/// A guarded operation the store refused. Living in another space reads exactly
/// like not being there: where an id lives is not this caller's business.
impl From<Refusal> for ApiError {
fn from(r: Refusal) -> Self {
Expand All @@ -486,6 +486,7 @@ impl From<Refusal> for ApiError {
Refusal::Forbidden => ApiError::forbidden(),
Refusal::SwitchedOff => ApiError::switched_off(),
Refusal::Removed => ApiError::unauthorized(),
Refusal::TokenReplaced => ApiError::token_replaced(),
// Said only where the caller may see it there: see `refused_team_write`.
Refusal::OtherSpace | Refusal::Missing | Refusal::InTrash(_) => ApiError::missing(),
}
Expand Down Expand Up @@ -598,8 +599,7 @@ fn allows(grants: &[Grant], action: Action, target: &Target<'_>) -> bool {
decide(grants, action, target).is_allowed()
}

/// The rules a guarded write is judged by: as the store read them under the
/// write's own lock, not the copy taken when the request came in.
/// The rules of a guarded operation, read by the store under its own lock.
fn live(s: &Standing) -> Result<&[Grant], Refusal> {
if !s.known {
Err(Refusal::Removed)
Expand All @@ -624,6 +624,10 @@ impl Caller {
Writer { member_id: &self.member.id, device_id: self.device.id.as_deref() }
}

fn reader(&self) -> Reader<'_> {
Reader { member_id: &self.member.id, token_hash: &self.token_hash }
}

/// Their own space. Grants are about the team and say nothing here.
fn mine(&self) -> String {
personal_space(&self.member.id)
Expand Down Expand Up @@ -1324,12 +1328,9 @@ fn with_open_on(st: &AppState, listed: Vec<Listed>) -> Result<Vec<ProfileRow>, A
.collect())
}

/// One profile's row as the lists have it; missing unless it is in `space`.
fn one_row(st: &AppState, space: &str, id: &str) -> Result<ProfileRow, ApiError> {
match st.store.listed_one(id)? {
Some((l, sp)) if sp == space => with_open_on(st, vec![l])?.pop().ok_or_else(ApiError::missing),
_ => Err(ApiError::missing()),
}
fn profile_detail_reply(st: &AppState, listed: Listed, data: Vec<u8>) -> Result<Json<ProfileWithData>, ApiError> {
let row = with_open_on(st, vec![listed])?.pop().ok_or_else(ApiError::missing)?;
Ok(Json(ProfileWithData { row, data: STANDARD.encode(data) }))
}

/// `?ids=a,b,c`: only these, at most [`MAX_IDS`]. Absent: the whole list.
Expand Down Expand Up @@ -1369,14 +1370,18 @@ async fn profile_get(
Path(id): Path<String>,
) -> Result<Json<ProfileWithData>, ApiError> {
let c = require_member(&st, &headers)?;
let profile = visible_profile(&st, &c, &id)?;
let t = Target::profile(&id, &profile.folder);
if !USES_A_PROFILE.iter().any(|a| c.may(*a, &t)) {
return Err(ApiError::forbidden());
}
let row = one_row(&st, TEAM, &id)?;
let data = st.store.profile_data(&id)?.ok_or_else(ApiError::missing)?;
Ok(Json(ProfileWithData { row, data: STANDARD.encode(data) }))
let (row, data) = st.store.profile_detail(TEAM, &id, c.reader(), |s, p| {
let grants = live(s)?;
let target = Target::profile(&p.id, &p.folder);
if !allows(grants, Action::See, &target) {
return Err(Refusal::Missing);
}
if !USES_A_PROFILE.iter().any(|a| allows(grants, *a, &target)) {
return Err(Refusal::Forbidden);
}
Ok(())
})??;
profile_detail_reply(&st, row, data)
}

async fn profile_put(
Expand Down Expand Up @@ -2189,14 +2194,6 @@ fn proxy_args(body: &ProxyWrite) -> Result<(&str, &str, &str, Vec<u8>), ApiError
Ok((name, kind, label(&body.country)?, data_arg(&body.data)?))
}

/// A team proxy the member may know about; anything else reads as missing.
fn visible_proxy(st: &AppState, c: &Caller, id: &str) -> Result<TeamProxy, ApiError> {
match st.store.proxy_meta(id)? {
Some((x, space)) if space == TEAM && c.may(Action::See, &proxy_target(id)) => Ok(x),
_ => Err(ApiError::missing()),
}
}

async fn proxies_list(
State(st): State<AppState>,
headers: HeaderMap,
Expand All @@ -2218,11 +2215,16 @@ async fn proxy_get(
Path(id): Path<String>,
) -> Result<Json<ProxyWithData>, ApiError> {
let c = require_member(&st, &headers)?;
let proxy = visible_proxy(&st, &c, &id)?;
if !may_use_proxy(&c.grants, &id) {
return Err(ApiError::forbidden());
}
let data = st.store.proxy_data(&id)?.ok_or_else(ApiError::missing)?;
let (proxy, data) = st.store.proxy_detail(TEAM, &id, c.reader(), |s, proxy| {
let grants = live(s)?;
if !sees_proxy(grants, &proxy.id) {
return Err(Refusal::Missing);
}
if !may_use_proxy(grants, &proxy.id) {
return Err(Refusal::Forbidden);
}
Ok(())
})??;
Ok(Json(ProxyWithData {
proxy: proxy.into(),
data: STANDARD.encode(data),
Expand Down Expand Up @@ -2539,13 +2541,6 @@ fn own_profile(st: &AppState, c: &Caller, id: &str) -> Result<TeamProfile, ApiEr
}
}

fn own_proxy(st: &AppState, c: &Caller, id: &str) -> Result<TeamProxy, ApiError> {
match st.store.proxy_meta(id)? {
Some((x, space)) if space == c.mine() => Ok(x),
_ => Err(ApiError::missing()),
}
}

async fn my_profiles_list(
State(st): State<AppState>,
headers: HeaderMap,
Expand All @@ -2561,9 +2556,8 @@ async fn my_profile_get(
Path(id): Path<String>,
) -> Result<Json<ProfileWithData>, ApiError> {
let c = require_member(&st, &headers)?;
let row = one_row(&st, &c.mine(), &id)?;
let data = st.store.profile_data(&id)?.ok_or_else(ApiError::missing)?;
Ok(Json(ProfileWithData { row, data: STANDARD.encode(data) }))
let (row, data) = st.store.profile_detail(&c.mine(), &id, c.reader(), |s, _| still_on(s))??;
profile_detail_reply(&st, row, data)
}

async fn my_profile_put(
Expand Down Expand Up @@ -2618,8 +2612,7 @@ async fn my_proxy_get(
Path(id): Path<String>,
) -> Result<Json<ProxyWithData>, ApiError> {
let c = require_member(&st, &headers)?;
let proxy = own_proxy(&st, &c, &id)?;
let data = st.store.proxy_data(&id)?.ok_or_else(ApiError::missing)?;
let (proxy, data) = st.store.proxy_detail(&c.mine(), &id, c.reader(), |s, _| still_on(s))??;
Ok(Json(ProxyWithData {
proxy: proxy.into(),
data: STANDARD.encode(data),
Expand Down Expand Up @@ -2797,20 +2790,20 @@ struct Brought {

impl Brought {
fn of(st: &AppState, c: &Caller, id: &str) -> Result<Option<Brought>, ApiError> {
let p = match st.store.profile_meta(id)? {
Some((p, space)) if space == TEAM => p,
_ => return Ok(None),
let found = st.store.profile_settings(TEAM, id, c.reader(), |s, p| {
see_then(live(s)?, Action::Start, &Target::profile(&p.id, &p.folder))
})?;
let (row, data) = match found {
Ok(detail) => detail,
Err(Refusal::Missing | Refusal::Forbidden) => return Ok(None),
Err(refusal) => return Err(refusal.into()),
};
if see_then(&c.grants, Action::Start, &Target::profile(id, &p.folder)).is_err() {
return Ok(None);
}
let data = st.store.profile_data(id)?.unwrap_or_default();
let named = serde_json::from_slice::<serde_json::Value>(&data)
.ok()
.and_then(|v| v.get("_meta")?.get("extensions")?.as_array().cloned())
.unwrap_or_default();
let extensions = named.iter().filter_map(|e| e.as_str().map(str::to_owned)).collect();
Ok(Some(Brought { id: id.to_owned(), folder: p.folder, extensions }))
Ok(Some(Brought { id: row.id, folder: row.folder, extensions }))
}

fn wants(&self, kind: &str, item: &StoredItem) -> bool {
Expand Down Expand Up @@ -3153,6 +3146,10 @@ async fn my_package_put(

#[cfg(test)]
mod tests {
mod detail_tests {
include!("api/detail_tests.rs");
}

use super::*;
use crate::auth::verify_permission;
use crate::model::Scope;
Expand Down
Loading