//! Authorization for drive items: one pure function that every page, form, API route and live stream //! calls before it acts. A denial is either `NotFound` (the viewer must not learn that the item exists) //! or `Forbidden` (the viewer can see the item but may not do this). //! //! Rules (architecture section 6): //! - Anonymous and service callers never reach drive items. //! - The owner may do everything on live items except `Purge` and `Restore`, which apply to trashed //! items only. On trashed items the owner may only view, download, restore, purge, and change or //! remove a grant that already exists (`Revoke`). A new grant or link on a trashed item is refused. //! - An admin may `Administer` any item but reads nobody else's files. //! - A grantee sees a live item with a grant, lists and views it, and downloads it only with a //! download grant. Nothing else. //! - A link visitor is a grantee for the link's subtree (`Target::inside_link`) with the link's download //! setting. /// Who is asking. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Actor<'a> { /// Nobody signed in. Anonymous, /// A signed-in person. Member { /// The OIDC subject of the person. sub: &'a str, /// Whether the person is an instance administrator. is_admin: bool, }, /// A visitor who opened a validated public share link. Link { /// Whether the link allows downloads. can_download: bool, }, /// A machine caller of another family app. It has no access to drive items. Service { /// The service key from the token's `svc` claim. key: &'a str, }, } /// The item being asked about. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct Target<'a> { /// The owner of the item. pub owner: &'a str, /// Whether the item or one of its ancestors is in the trash. pub trashed: bool, /// For link visitors: whether the item lies inside the linked folder (or is the linked item). pub inside_link: bool, } /// The most permissive grant that applies to the item: a direct grant on it or on any live ancestor. #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)] pub enum Grant { /// No grant applies. None, /// The grantee may see and open the item. View, /// The grantee may also download it. Download, } /// What the actor wants to do. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Action { /// See the item in a listing or search. List, /// Open the item and preview its bytes inline. View, /// Download the bytes. Download, /// Rename, move, upload into, or trash. Write, /// Restore a trashed item to its place. Restore, /// Create a grant or a link: change who can see the item. Share, /// Change the level of an existing grant, or remove it. Revoke, /// Permanently delete a trashed item. Purge, /// Administer the instance. Administer, } /// Why an action was refused. #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub enum Denied { /// The item is hidden from this actor, or the action is not something it may know about. NotFound, /// The actor can see the item but may not do this. Forbidden, } /// Decides whether `actor` may perform `action` on `target`, given the effective `grant`. pub fn authorize( actor: Actor<'_>, target: Target<'_>, grant: Grant, action: Action, ) -> Result<(), Denied> { match actor { Actor::Anonymous | Actor::Service { .. } => Err(Denied::NotFound), Actor::Link { can_download } => { if !target.inside_link || target.trashed { return Err(Denied::NotFound); } match action { Action::List | Action::View => Ok(()), Action::Download if can_download => Ok(()), Action::Download => Err(Denied::Forbidden), Action::Administer => Err(Denied::NotFound), _ => Err(Denied::Forbidden), } } Actor::Member { is_admin, .. } if is_admin && action == Action::Administer => Ok(()), Actor::Member { sub, .. } if sub == target.owner => owner_rule(target.trashed, action), Actor::Member { .. } => { if target.trashed || grant == Grant::None || action == Action::Administer { return Err(Denied::NotFound); } match action { Action::List | Action::View => Ok(()), Action::Download if grant == Grant::Download => Ok(()), _ => Err(Denied::Forbidden), } } } } /// The owner's rule: everything on live items, and only the trash actions on trashed ones. fn owner_rule(trashed: bool, action: Action) -> Result<(), Denied> { match (trashed, action) { (_, Action::Administer) => Err(Denied::NotFound), // Taking back access never shows anything new, so it is allowed on trashed items as well. (_, Action::Revoke) => Ok(()), (false, Action::Purge | Action::Restore) => Err(Denied::Forbidden), (false, _) => Ok(()), (true, Action::View | Action::Download | Action::Restore | Action::Purge) => Ok(()), (true, _) => Err(Denied::Forbidden), } } #[cfg(test)] mod tests { use super::*; const OWNER: &str = "owner"; const OTHER: &str = "other"; fn live() -> Target<'static> { Target { owner: OWNER, trashed: false, inside_link: false, } } fn trashed() -> Target<'static> { Target { trashed: true, ..live() } } fn owner() -> Actor<'static> { Actor::Member { sub: OWNER, is_admin: false, } } fn grantee() -> Actor<'static> { Actor::Member { sub: OTHER, is_admin: false, } } fn admin() -> Actor<'static> { Actor::Member { sub: "root", is_admin: true, } } /// The truth table, one row per (actor, target, grant, action) with the expected answer. #[test] fn truth_table() { use Action::*; use Denied::*; let link_dl = Actor::Link { can_download: true }; let link_view = Actor::Link { can_download: false, }; let inside = Target { inside_link: true, ..live() }; type Case<'a> = (Actor<'a>, Target<'a>, Grant, Action, Result<(), Denied>); let cases: Vec = vec![ // Anonymous and services never see drive items. ( Actor::Anonymous, live(), Grant::Download, View, Err(NotFound), ), ( Actor::Service { key: "im" }, live(), Grant::Download, List, Err(NotFound), ), // Owner, live. (owner(), live(), Grant::None, List, Ok(())), (owner(), live(), Grant::None, View, Ok(())), (owner(), live(), Grant::None, Download, Ok(())), (owner(), live(), Grant::None, Write, Ok(())), (owner(), live(), Grant::None, Share, Ok(())), (owner(), live(), Grant::None, Revoke, Ok(())), (owner(), live(), Grant::None, Purge, Err(Forbidden)), (owner(), live(), Grant::None, Restore, Err(Forbidden)), (owner(), live(), Grant::None, Administer, Err(NotFound)), // Owner, trashed. (owner(), trashed(), Grant::None, View, Ok(())), (owner(), trashed(), Grant::None, Download, Ok(())), (owner(), trashed(), Grant::None, Restore, Ok(())), (owner(), trashed(), Grant::None, Purge, Ok(())), (owner(), trashed(), Grant::None, Write, Err(Forbidden)), (owner(), trashed(), Grant::None, Share, Err(Forbidden)), (owner(), trashed(), Grant::None, Revoke, Ok(())), (owner(), trashed(), Grant::None, List, Err(Forbidden)), // Grantee with view only, live. (grantee(), live(), Grant::View, List, Ok(())), (grantee(), live(), Grant::View, View, Ok(())), (grantee(), live(), Grant::View, Download, Err(Forbidden)), (grantee(), live(), Grant::View, Write, Err(Forbidden)), // Grantee with download. (grantee(), live(), Grant::Download, Download, Ok(())), (grantee(), live(), Grant::Download, Share, Err(Forbidden)), (grantee(), live(), Grant::Download, Revoke, Err(Forbidden)), // Grantee without any grant, or on a trashed item, learns nothing. (grantee(), live(), Grant::None, View, Err(NotFound)), (grantee(), trashed(), Grant::Download, View, Err(NotFound)), // Admin: administers, but reads nobody else's files. (admin(), live(), Grant::None, Administer, Ok(())), (admin(), live(), Grant::None, View, Err(NotFound)), // An admin who holds no grant on someone else's item sees nothing of it. (admin(), live(), Grant::None, Download, Err(NotFound)), // With a grant the admin is simply a grantee. (admin(), live(), Grant::Download, Download, Ok(())), // Owner who is also admin keeps the owner rules. ( Actor::Member { sub: OWNER, is_admin: true, }, live(), Grant::None, Write, Ok(()), ), // Link visitors. (link_dl, inside, Grant::None, Download, Ok(())), (link_view, inside, Grant::None, Download, Err(Forbidden)), (link_view, inside, Grant::None, View, Ok(())), (link_dl, live(), Grant::None, View, Err(NotFound)), (link_dl, trashed(), Grant::None, View, Err(NotFound)), ]; for (actor, target, grant, action, want) in cases { assert_eq!( authorize(actor, target, grant, action), want, "actor {actor:?}, target {target:?}, grant {grant:?}, action {action:?}" ); } } }