forge
Explore
Log in

Don't you dare go Hollow

in

Clone
★ 0

0 watching0 forksRustAGPL-3.0528.7 KiB9215377Tahinli27 days ago37 commits

authz.rs280 lines, 10.2 KiBHistoryRawDownload
1//! Authorization for drive items: one pure function that every page, form, API route and live stream
2//! calls before it acts. A denial is either `NotFound` (the viewer must not learn that the item exists)
3//! or `Forbidden` (the viewer can see the item but may not do this).
4//!
5//! Rules (architecture section 6):
6//! - Anonymous and service callers never reach drive items.
7//! - The owner may do everything on live items except `Purge` and `Restore`, which apply to trashed
8//! items only. On trashed items the owner may only view, download, restore, purge, and change or
9//! remove a grant that already exists (`Revoke`). A new grant or link on a trashed item is refused.
10//! - An admin may `Administer` any item but reads nobody else's files.
11//! - A grantee sees a live item with a grant, lists and views it, and downloads it only with a
12//! download grant. Nothing else.
13//! - A link visitor is a grantee for the link's subtree (`Target::inside_link`) with the link's download
14//! setting.
15
16/// Who is asking.
17#[derive(Debug, Clone, Copy, PartialEq, Eq)]
18pub enum Actor<'a> {
19 /// Nobody signed in.
20 Anonymous,
21 /// A signed-in person.
22 Member {
23 /// The OIDC subject of the person.
24 sub: &'a str,
25 /// Whether the person is an instance administrator.
26 is_admin: bool,
27 },
28 /// A visitor who opened a validated public share link.
29 Link {
30 /// Whether the link allows downloads.
31 can_download: bool,
32 },
33 /// A machine caller of another family app. It has no access to drive items.
34 Service {
35 /// The service key from the token's `svc` claim.
36 key: &'a str,
37 },
38}
39
40/// The item being asked about.
41#[derive(Debug, Clone, Copy, PartialEq, Eq)]
42pub struct Target<'a> {
43 /// The owner of the item.
44 pub owner: &'a str,
45 /// Whether the item or one of its ancestors is in the trash.
46 pub trashed: bool,
47 /// For link visitors: whether the item lies inside the linked folder (or is the linked item).
48 pub inside_link: bool,
49}
50
51/// The most permissive grant that applies to the item: a direct grant on it or on any live ancestor.
52#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
53pub enum Grant {
54 /// No grant applies.
55 None,
56 /// The grantee may see and open the item.
57 View,
58 /// The grantee may also download it.
59 Download,
60}
61
62/// What the actor wants to do.
63#[derive(Debug, Clone, Copy, PartialEq, Eq)]
64pub enum Action {
65 /// See the item in a listing or search.
66 List,
67 /// Open the item and preview its bytes inline.
68 View,
69 /// Download the bytes.
70 Download,
71 /// Rename, move, upload into, or trash.
72 Write,
73 /// Restore a trashed item to its place.
74 Restore,
75 /// Create a grant or a link: change who can see the item.
76 Share,
77 /// Change the level of an existing grant, or remove it.
78 Revoke,
79 /// Permanently delete a trashed item.
80 Purge,
81 /// Administer the instance.
82 Administer,
83}
84
85/// Why an action was refused.
86#[derive(Debug, Clone, Copy, PartialEq, Eq)]
87pub enum Denied {
88 /// The item is hidden from this actor, or the action is not something it may know about.
89 NotFound,
90 /// The actor can see the item but may not do this.
91 Forbidden,
92}
93
94/// Decides whether `actor` may perform `action` on `target`, given the effective `grant`.
95pub fn authorize(
96 actor: Actor<'_>,
97 target: Target<'_>,
98 grant: Grant,
99 action: Action,
100) -> Result<(), Denied> {
101 match actor {
102 Actor::Anonymous | Actor::Service { .. } => Err(Denied::NotFound),
103 Actor::Link { can_download } => {
104 if !target.inside_link || target.trashed {
105 return Err(Denied::NotFound);
106 }
107 match action {
108 Action::List | Action::View => Ok(()),
109 Action::Download if can_download => Ok(()),
110 Action::Download => Err(Denied::Forbidden),
111 Action::Administer => Err(Denied::NotFound),
112 _ => Err(Denied::Forbidden),
113 }
114 }
115 Actor::Member { is_admin, .. } if is_admin && action == Action::Administer => Ok(()),
116 Actor::Member { sub, .. } if sub == target.owner => owner_rule(target.trashed, action),
117 Actor::Member { .. } => {
118 if target.trashed || grant == Grant::None || action == Action::Administer {
119 return Err(Denied::NotFound);
120 }
121 match action {
122 Action::List | Action::View => Ok(()),
123 Action::Download if grant == Grant::Download => Ok(()),
124 _ => Err(Denied::Forbidden),
125 }
126 }
127 }
128}
129
130/// The owner's rule: everything on live items, and only the trash actions on trashed ones.
131fn owner_rule(trashed: bool, action: Action) -> Result<(), Denied> {
132 match (trashed, action) {
133 (_, Action::Administer) => Err(Denied::NotFound),
134 // Taking back access never shows anything new, so it is allowed on trashed items as well.
135 (_, Action::Revoke) => Ok(()),
136 (false, Action::Purge | Action::Restore) => Err(Denied::Forbidden),
137 (false, _) => Ok(()),
138 (true, Action::View | Action::Download | Action::Restore | Action::Purge) => Ok(()),
139 (true, _) => Err(Denied::Forbidden),
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use super::*;
146
147 const OWNER: &str = "owner";
148 const OTHER: &str = "other";
149
150 fn live() -> Target<'static> {
151 Target {
152 owner: OWNER,
153 trashed: false,
154 inside_link: false,
155 }
156 }
157
158 fn trashed() -> Target<'static> {
159 Target {
160 trashed: true,
161 ..live()
162 }
163 }
164
165 fn owner() -> Actor<'static> {
166 Actor::Member {
167 sub: OWNER,
168 is_admin: false,
169 }
170 }
171
172 fn grantee() -> Actor<'static> {
173 Actor::Member {
174 sub: OTHER,
175 is_admin: false,
176 }
177 }
178
179 fn admin() -> Actor<'static> {
180 Actor::Member {
181 sub: "root",
182 is_admin: true,
183 }
184 }
185
186 /// The truth table, one row per (actor, target, grant, action) with the expected answer.
187 #[test]
188 fn truth_table() {
189 use Action::*;
190 use Denied::*;
191 let link_dl = Actor::Link { can_download: true };
192 let link_view = Actor::Link {
193 can_download: false,
194 };
195 let inside = Target {
196 inside_link: true,
197 ..live()
198 };
199 type Case<'a> = (Actor<'a>, Target<'a>, Grant, Action, Result<(), Denied>);
200 let cases: Vec<Case> = vec![
201 // Anonymous and services never see drive items.
202 (
203 Actor::Anonymous,
204 live(),
205 Grant::Download,
206 View,
207 Err(NotFound),
208 ),
209 (
210 Actor::Service { key: "im" },
211 live(),
212 Grant::Download,
213 List,
214 Err(NotFound),
215 ),
216 // Owner, live.
217 (owner(), live(), Grant::None, List, Ok(())),
218 (owner(), live(), Grant::None, View, Ok(())),
219 (owner(), live(), Grant::None, Download, Ok(())),
220 (owner(), live(), Grant::None, Write, Ok(())),
221 (owner(), live(), Grant::None, Share, Ok(())),
222 (owner(), live(), Grant::None, Revoke, Ok(())),
223 (owner(), live(), Grant::None, Purge, Err(Forbidden)),
224 (owner(), live(), Grant::None, Restore, Err(Forbidden)),
225 (owner(), live(), Grant::None, Administer, Err(NotFound)),
226 // Owner, trashed.
227 (owner(), trashed(), Grant::None, View, Ok(())),
228 (owner(), trashed(), Grant::None, Download, Ok(())),
229 (owner(), trashed(), Grant::None, Restore, Ok(())),
230 (owner(), trashed(), Grant::None, Purge, Ok(())),
231 (owner(), trashed(), Grant::None, Write, Err(Forbidden)),
232 (owner(), trashed(), Grant::None, Share, Err(Forbidden)),
233 (owner(), trashed(), Grant::None, Revoke, Ok(())),
234 (owner(), trashed(), Grant::None, List, Err(Forbidden)),
235 // Grantee with view only, live.
236 (grantee(), live(), Grant::View, List, Ok(())),
237 (grantee(), live(), Grant::View, View, Ok(())),
238 (grantee(), live(), Grant::View, Download, Err(Forbidden)),
239 (grantee(), live(), Grant::View, Write, Err(Forbidden)),
240 // Grantee with download.
241 (grantee(), live(), Grant::Download, Download, Ok(())),
242 (grantee(), live(), Grant::Download, Share, Err(Forbidden)),
243 (grantee(), live(), Grant::Download, Revoke, Err(Forbidden)),
244 // Grantee without any grant, or on a trashed item, learns nothing.
245 (grantee(), live(), Grant::None, View, Err(NotFound)),
246 (grantee(), trashed(), Grant::Download, View, Err(NotFound)),
247 // Admin: administers, but reads nobody else's files.
248 (admin(), live(), Grant::None, Administer, Ok(())),
249 (admin(), live(), Grant::None, View, Err(NotFound)),
250 // An admin who holds no grant on someone else's item sees nothing of it.
251 (admin(), live(), Grant::None, Download, Err(NotFound)),
252 // With a grant the admin is simply a grantee.
253 (admin(), live(), Grant::Download, Download, Ok(())),
254 // Owner who is also admin keeps the owner rules.
255 (
256 Actor::Member {
257 sub: OWNER,
258 is_admin: true,
259 },
260 live(),
261 Grant::None,
262 Write,
263 Ok(()),
264 ),
265 // Link visitors.
266 (link_dl, inside, Grant::None, Download, Ok(())),
267 (link_view, inside, Grant::None, Download, Err(Forbidden)),
268 (link_view, inside, Grant::None, View, Ok(())),
269 (link_dl, live(), Grant::None, View, Err(NotFound)),
270 (link_dl, trashed(), Grant::None, View, Err(NotFound)),
271 ];
272 for (actor, target, grant, action, want) in cases {
273 assert_eq!(
274 authorize(actor, target, grant, action),
275 want,
276 "actor {actor:?}, target {target:?}, grant {grant:?}, action {action:?}"
277 );
278 }
279 }
280}