pub struct CapabilitySet(/* private fields */);Expand description
A set of capabilities held by a plugin instance.
Membership tests are O(1) on average.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
let mut caps = CapabilitySet::empty();
caps.grant(Capability::Network);
assert!(caps.contains(&Capability::Network));
caps.revoke(&Capability::Network);
assert!(!caps.contains(&Capability::Network));Implementations§
Source§impl CapabilitySet
impl CapabilitySet
Sourcepub fn empty() -> Self
pub fn empty() -> Self
Creates an empty capability set.
§Examples
use martensite_plugin::CapabilitySet;
let caps = CapabilitySet::empty();
assert!(caps.is_empty());Sourcepub fn builder() -> PluginBuilder
pub fn builder() -> PluginBuilder
Returns a builder for constructing a capability set fluently.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
let caps = CapabilitySet::builder().grant(Capability::Network).build();
assert!(caps.contains(&Capability::Network));Sourcepub fn len(&self) -> usize
pub fn len(&self) -> usize
Returns the number of distinct capabilities in the set.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
let mut caps = CapabilitySet::empty();
assert_eq!(caps.len(), 0);
caps.grant(Capability::Network);
assert_eq!(caps.len(), 1);Sourcepub fn is_empty(&self) -> bool
pub fn is_empty(&self) -> bool
Returns true if no capabilities have been granted.
§Examples
use martensite_plugin::CapabilitySet;
let caps = CapabilitySet::empty();
assert!(caps.is_empty());Sourcepub fn grant(&mut self, cap: Capability) -> bool
pub fn grant(&mut self, cap: Capability) -> bool
Grants a capability, returning true if it was newly inserted.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
let mut caps = CapabilitySet::empty();
assert!(caps.grant(Capability::Network));
assert!(!caps.grant(Capability::Network)); // already grantedSourcepub fn revoke(&mut self, cap: &Capability) -> bool
pub fn revoke(&mut self, cap: &Capability) -> bool
Revokes a capability, returning true if it was present.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
let mut caps = CapabilitySet::empty();
caps.grant(Capability::Network);
assert!(caps.revoke(&Capability::Network));
assert!(!caps.revoke(&Capability::Network)); // already revokedSourcepub fn contains(&self, cap: &Capability) -> bool
pub fn contains(&self, cap: &Capability) -> bool
Returns true if the capability is currently granted.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
let mut caps = CapabilitySet::empty();
caps.grant(Capability::Network);
assert!(caps.contains(&Capability::Network));Sourcepub fn file_read_allowed(&self, requested_path: &Path) -> bool
pub fn file_read_allowed(&self, requested_path: &Path) -> bool
Returns true if a file_read request for requested_path is
authorized by any granted Capability::FileRead entry.
Authorization is performed by canonicalizing both the granted
roots and the requested path, then requiring the requested path
to be equal to, or descend into, at least one granted root. This
defeats path-traversal attacks (/assets/../etc/passwd) that
exact-match checks would otherwise miss when a directory is
granted and a child file is requested.
When the requested file does not exist on disk (so
std::fs::canonicalize fails), the path is normalized
lexically via std::path::Path::components stripping of .
and resolving .. against the granted root, and the prefix
check is applied to the normalized form. This keeps the check
total (no filesystem dependency) while still rejecting ..
escapes.
Granting a directory (e.g. FileRead("/assets")) authorizes
reads of any file beneath it (e.g. /assets/textures/foo.png).
Granting a file authorizes only that exact file.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
use std::path::Path;
let mut caps = CapabilitySet::empty();
caps.grant(Capability::FileRead("/assets".into()));
assert!(caps.file_read_allowed(Path::new("/assets/foo.txt")));
assert!(!caps.file_read_allowed(Path::new("/etc/passwd")));Sourcepub fn file_write_allowed(&self, requested_path: &Path) -> bool
pub fn file_write_allowed(&self, requested_path: &Path) -> bool
Returns true if a file_write request for requested_path is
authorized by any granted Capability::FileWrite entry.
See Self::file_read_allowed for canonicalization semantics.
§Examples
use martensite_plugin::{Capability, CapabilitySet};
use std::path::Path;
let mut caps = CapabilitySet::empty();
caps.grant(Capability::FileWrite("/tmp/log".into()));
assert!(caps.file_write_allowed(Path::new("/tmp/log")));
assert!(!caps.file_write_allowed(Path::new("/etc/passwd")));Trait Implementations§
Source§impl Clone for CapabilitySet
impl Clone for CapabilitySet
Source§fn clone(&self) -> CapabilitySet
fn clone(&self) -> CapabilitySet
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for CapabilitySet
impl Debug for CapabilitySet
Source§impl Default for CapabilitySet
impl Default for CapabilitySet
Source§fn default() -> CapabilitySet
fn default() -> CapabilitySet
impl Eq for CapabilitySet
Source§impl PartialEq for CapabilitySet
impl PartialEq for CapabilitySet
impl StructuralPartialEq for CapabilitySet
Auto Trait Implementations§
impl Freeze for CapabilitySet
impl RefUnwindSafe for CapabilitySet
impl Send for CapabilitySet
impl Sync for CapabilitySet
impl Unpin for CapabilitySet
impl UnsafeUnpin for CapabilitySet
impl UnwindSafe for CapabilitySet
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> GetSetFdFlags for T
impl<T> GetSetFdFlags for T
Source§fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
fn get_fd_flags(&self) -> Result<FdFlags, Error>where
T: AsFilelike,
self file descriptor.Source§fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>where
T: AsFilelike,
Source§fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: Sized + AsFilelike,
fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>where
T: Sized + AsFilelike,
self file descriptor. Read moreSource§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
Source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more