capsec-core 0.2.2

Core capability types for compile-time capability-based security in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Runtime-revocable and time-bounded capability tokens.
//!
//! [`RuntimeCap<P>`] wraps a static [`Cap<P>`](crate::cap::Cap) with a shared
//! revocation flag. [`TimedCap<P>`] wraps a `Cap<P>` with an expiry deadline.
//! Unlike `Cap<P>`, neither type implements [`Has<P>`](crate::has::Has) — callers
//! must use [`try_cap()`](RuntimeCap::try_cap) and handle the fallible result explicitly.
//!
//! A [`Revoker`] handle is returned alongside each `RuntimeCap` and can be used
//! to invalidate the capability at any time from any thread.

use crate::cap::Cap;
use crate::error::CapSecError;
use crate::permission::Permission;
use std::marker::PhantomData;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Duration, Instant};

/// A revocable capability token proving the holder has permission `P`.
///
/// Created via [`RuntimeCap::new`], which consumes a [`Cap<P>`] as proof of
/// possession and returns a `(RuntimeCap<P>, Revoker)` pair.
///
/// `!Send + !Sync` by default — use [`make_send`](RuntimeCap::make_send) for
/// cross-thread transfer. Cloning shares the same revocation state: revoking
/// one clone revokes all of them.
pub struct RuntimeCap<P: Permission> {
    _phantom: PhantomData<P>,
    // PhantomData<*const ()> makes RuntimeCap !Send + !Sync
    _not_send: PhantomData<*const ()>,
    active: Arc<AtomicBool>,
}

impl<P: Permission> RuntimeCap<P> {
    /// Creates a revocable capability by consuming a [`Cap<P>`] as proof of possession.
    ///
    /// Returns a `(RuntimeCap<P>, Revoker)` pair. The `Revoker` can invalidate
    /// this capability (and all its clones) from any thread.
    pub fn new(_cap: Cap<P>) -> (Self, Revoker) {
        let active = Arc::new(AtomicBool::new(true));
        let revoker = Revoker {
            active: Arc::clone(&active),
        };
        let cap = Self {
            _phantom: PhantomData,
            _not_send: PhantomData,
            active,
        };
        (cap, revoker)
    }

    /// Attempts to obtain a [`Cap<P>`] from this revocable capability.
    ///
    /// Returns `Ok(Cap<P>)` if still active, or `Err(CapSecError::Revoked)` if
    /// the associated [`Revoker`] has been invoked.
    pub fn try_cap(&self) -> Result<Cap<P>, CapSecError> {
        if self.active.load(Ordering::Acquire) {
            Ok(Cap::new())
        } else {
            Err(CapSecError::Revoked)
        }
    }

    /// Advisory check — returns `true` if the capability has not been revoked.
    ///
    /// The result is immediately stale; do not use for control flow.
    /// Always use [`try_cap`](RuntimeCap::try_cap) for actual access.
    pub fn is_active(&self) -> bool {
        self.active.load(Ordering::Acquire)
    }

    /// Converts this capability into a [`RuntimeSendCap`] that can cross thread boundaries.
    ///
    /// This is an explicit opt-in — you're acknowledging that this capability
    /// will be used in a multi-threaded context.
    pub fn make_send(self) -> RuntimeSendCap<P> {
        RuntimeSendCap {
            _phantom: PhantomData,
            active: self.active,
        }
    }
}

impl<P: Permission> Clone for RuntimeCap<P> {
    fn clone(&self) -> Self {
        Self {
            _phantom: PhantomData,
            _not_send: PhantomData,
            active: Arc::clone(&self.active),
        }
    }
}

/// A handle that can revoke its associated [`RuntimeCap`] (and all clones).
///
/// `Revoker` is `Send + Sync` and `Clone` — multiple owners can hold revokers
/// to the same capability, and any of them can revoke it from any thread.
/// Revocation is idempotent: calling [`revoke`](Revoker::revoke) multiple times
/// is safe and has no additional effect.
pub struct Revoker {
    active: Arc<AtomicBool>,
}

impl Revoker {
    /// Revokes the associated capability. All subsequent calls to
    /// [`RuntimeCap::try_cap`] (and clones) will return `Err(CapSecError::Revoked)`.
    ///
    /// Idempotent — calling multiple times is safe.
    pub fn revoke(&self) {
        self.active.store(false, Ordering::Release);
    }

    /// Returns `true` if the capability has been revoked.
    pub fn is_revoked(&self) -> bool {
        !self.active.load(Ordering::Acquire)
    }
}

impl Clone for Revoker {
    fn clone(&self) -> Self {
        Self {
            active: Arc::clone(&self.active),
        }
    }
}

/// A thread-safe revocable capability token.
///
/// Created via [`RuntimeCap::make_send`]. Unlike [`RuntimeCap`], this implements
/// `Send + Sync`, making it usable with `std::thread::spawn`, `tokio::spawn`, etc.
pub struct RuntimeSendCap<P: Permission> {
    _phantom: PhantomData<P>,
    active: Arc<AtomicBool>,
}

// SAFETY: RuntimeSendCap is explicitly opted into cross-thread transfer via make_send().
// The inner Arc<AtomicBool> is already Send+Sync; PhantomData<P> is Send+Sync when P is.
// Permission types are marker traits (ZSTs) that are always Send+Sync.
unsafe impl<P: Permission> Send for RuntimeSendCap<P> {}
unsafe impl<P: Permission> Sync for RuntimeSendCap<P> {}

impl<P: Permission> RuntimeSendCap<P> {
    /// Attempts to obtain a [`Cap<P>`] from this revocable capability.
    ///
    /// Returns `Ok(Cap<P>)` if still active, or `Err(CapSecError::Revoked)` if
    /// the associated [`Revoker`] has been invoked.
    pub fn try_cap(&self) -> Result<Cap<P>, CapSecError> {
        if self.active.load(Ordering::Acquire) {
            Ok(Cap::new())
        } else {
            Err(CapSecError::Revoked)
        }
    }

    /// Advisory check — returns `true` if the capability has not been revoked.
    ///
    /// The result is immediately stale; do not use for control flow.
    pub fn is_active(&self) -> bool {
        self.active.load(Ordering::Acquire)
    }
}

impl<P: Permission> Clone for RuntimeSendCap<P> {
    fn clone(&self) -> Self {
        Self {
            _phantom: PhantomData,
            active: Arc::clone(&self.active),
        }
    }
}

/// A time-bounded capability token proving the holder has permission `P`.
///
/// Created via [`TimedCap::new`], which consumes a [`Cap<P>`] and a TTL duration.
/// After the TTL elapses, [`try_cap()`](TimedCap::try_cap) returns
/// `Err(CapSecError::Expired)`.
///
/// `!Send + !Sync` by default — use [`make_send`](TimedCap::make_send) for
/// cross-thread transfer. Cloning copies the same expiry instant.
pub struct TimedCap<P: Permission> {
    _phantom: PhantomData<P>,
    // PhantomData<*const ()> makes TimedCap !Send + !Sync
    _not_send: PhantomData<*const ()>,
    expires_at: Instant,
}

impl<P: Permission> TimedCap<P> {
    /// Creates a time-bounded capability by consuming a [`Cap<P>`] as proof of possession.
    ///
    /// The capability expires after `ttl` has elapsed from the moment of creation.
    pub fn new(_cap: Cap<P>, ttl: Duration) -> Self {
        Self {
            _phantom: PhantomData,
            _not_send: PhantomData,
            expires_at: Instant::now() + ttl,
        }
    }

    /// Attempts to obtain a [`Cap<P>`] from this timed capability.
    ///
    /// Returns `Ok(Cap<P>)` if the TTL has not elapsed, or `Err(CapSecError::Expired)`
    /// if the capability has expired.
    pub fn try_cap(&self) -> Result<Cap<P>, CapSecError> {
        if Instant::now() < self.expires_at {
            Ok(Cap::new())
        } else {
            Err(CapSecError::Expired)
        }
    }

    /// Advisory check — returns `true` if the capability has not yet expired.
    ///
    /// The result is immediately stale; do not use for control flow.
    /// Always use [`try_cap`](TimedCap::try_cap) for actual access.
    pub fn is_active(&self) -> bool {
        Instant::now() < self.expires_at
    }

    /// Returns the remaining duration before expiry.
    ///
    /// Returns [`Duration::ZERO`] if the capability has already expired.
    pub fn remaining(&self) -> Duration {
        self.expires_at.saturating_duration_since(Instant::now())
    }

    /// Converts this capability into a [`TimedSendCap`] that can cross thread boundaries.
    ///
    /// This is an explicit opt-in — you're acknowledging that this capability
    /// will be used in a multi-threaded context.
    pub fn make_send(self) -> TimedSendCap<P> {
        TimedSendCap {
            _phantom: PhantomData,
            expires_at: self.expires_at,
        }
    }
}

impl<P: Permission> Clone for TimedCap<P> {
    fn clone(&self) -> Self {
        Self {
            _phantom: PhantomData,
            _not_send: PhantomData,
            expires_at: self.expires_at,
        }
    }
}

/// A thread-safe time-bounded capability token.
///
/// Created via [`TimedCap::make_send`]. Unlike [`TimedCap`], this implements
/// `Send + Sync`, making it usable with `std::thread::spawn`, `tokio::spawn`, etc.
pub struct TimedSendCap<P: Permission> {
    _phantom: PhantomData<P>,
    expires_at: Instant,
}

// SAFETY: TimedSendCap is explicitly opted into cross-thread transfer via make_send().
// Instant is Send+Sync; PhantomData<P> is Send+Sync when P is.
// Permission types are marker traits (ZSTs) that are always Send+Sync.
unsafe impl<P: Permission> Send for TimedSendCap<P> {}
unsafe impl<P: Permission> Sync for TimedSendCap<P> {}

impl<P: Permission> TimedSendCap<P> {
    /// Attempts to obtain a [`Cap<P>`] from this timed capability.
    ///
    /// Returns `Ok(Cap<P>)` if the TTL has not elapsed, or `Err(CapSecError::Expired)`
    /// if the capability has expired.
    pub fn try_cap(&self) -> Result<Cap<P>, CapSecError> {
        if Instant::now() < self.expires_at {
            Ok(Cap::new())
        } else {
            Err(CapSecError::Expired)
        }
    }

    /// Advisory check — returns `true` if the capability has not yet expired.
    ///
    /// The result is immediately stale; do not use for control flow.
    pub fn is_active(&self) -> bool {
        Instant::now() < self.expires_at
    }

    /// Returns the remaining duration before expiry.
    ///
    /// Returns [`Duration::ZERO`] if the capability has already expired.
    pub fn remaining(&self) -> Duration {
        self.expires_at.saturating_duration_since(Instant::now())
    }
}

impl<P: Permission> Clone for TimedSendCap<P> {
    fn clone(&self) -> Self {
        Self {
            _phantom: PhantomData,
            expires_at: self.expires_at,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::permission::FsRead;
    use std::mem::size_of;

    #[test]
    fn runtime_cap_try_cap_succeeds_when_active() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let (rcap, _revoker) = RuntimeCap::new(cap);
        assert!(rcap.try_cap().is_ok());
    }

    #[test]
    fn runtime_cap_try_cap_fails_after_revocation() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let (rcap, revoker) = RuntimeCap::new(cap);
        revoker.revoke();
        assert!(matches!(rcap.try_cap(), Err(CapSecError::Revoked)));
    }

    #[test]
    fn revoker_is_idempotent() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let (_rcap, revoker) = RuntimeCap::new(cap);
        revoker.revoke();
        revoker.revoke(); // should not panic
        assert!(revoker.is_revoked());
    }

    #[test]
    fn revoker_is_send_and_sync() {
        fn assert_send_sync<T: Send + Sync>() {}
        assert_send_sync::<Revoker>();
    }

    #[test]
    fn runtime_send_cap_crosses_threads() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let (rcap, _revoker) = RuntimeCap::new(cap);
        let send_cap = rcap.make_send();

        std::thread::spawn(move || {
            assert!(send_cap.try_cap().is_ok());
        })
        .join()
        .unwrap();
    }

    #[test]
    fn runtime_send_cap_revocation_crosses_threads() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let (rcap, revoker) = RuntimeCap::new(cap);
        let send_cap = rcap.make_send();

        revoker.revoke();

        std::thread::spawn(move || {
            assert!(matches!(send_cap.try_cap(), Err(CapSecError::Revoked)));
        })
        .join()
        .unwrap();
    }

    #[test]
    fn cloned_runtime_cap_shares_revocation() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let (rcap, revoker) = RuntimeCap::new(cap);
        let rcap2 = rcap.clone();

        revoker.revoke();

        assert!(matches!(rcap.try_cap(), Err(CapSecError::Revoked)));
        assert!(matches!(rcap2.try_cap(), Err(CapSecError::Revoked)));
    }

    #[test]
    fn runtime_cap_is_small() {
        assert!(size_of::<RuntimeCap<FsRead>>() <= 2 * size_of::<usize>());
    }

    #[test]
    fn timed_cap_succeeds_before_expiry() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let tcap = TimedCap::new(cap, Duration::from_secs(60));
        assert!(tcap.try_cap().is_ok());
    }

    #[test]
    fn timed_cap_fails_after_expiry() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let tcap = TimedCap::new(cap, Duration::from_millis(5));
        std::thread::sleep(Duration::from_millis(50));
        assert!(matches!(tcap.try_cap(), Err(CapSecError::Expired)));
    }

    #[test]
    fn timed_cap_remaining_decreases() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let tcap = TimedCap::new(cap, Duration::from_secs(60));
        let r1 = tcap.remaining();
        std::thread::sleep(Duration::from_millis(10));
        let r2 = tcap.remaining();
        assert!(r2 < r1);
    }

    #[test]
    fn timed_cap_remaining_is_zero_after_expiry() {
        let root = crate::root::test_root();
        let cap = root.grant::<FsRead>();
        let tcap = TimedCap::new(cap, Duration::from_millis(5));
        std::thread::sleep(Duration::from_millis(50));
        assert_eq!(tcap.remaining(), Duration::ZERO);
    }
}