Skip to main content

CapabilitySet

Struct CapabilitySet 

Source
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

Source

pub fn empty() -> Self

Creates an empty capability set.

§Examples
use martensite_plugin::CapabilitySet;

let caps = CapabilitySet::empty();
assert!(caps.is_empty());
Source

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));
Source

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);
Source

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());
Source

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 granted
Source

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 revoked
Source

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));
Source

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")));
Source

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

Source§

fn clone(&self) -> CapabilitySet

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for CapabilitySet

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CapabilitySet

Source§

fn default() -> CapabilitySet

Returns the “default value” for a type. Read more
Source§

impl Eq for CapabilitySet

Source§

impl PartialEq for CapabilitySet

Source§

fn eq(&self, other: &CapabilitySet) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for CapabilitySet

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> GetSetFdFlags for T

Source§

fn get_fd_flags(&self) -> Result<FdFlags, Error>
where T: AsFilelike,

Query the “status” flags for the self file descriptor.
Source§

fn new_set_fd_flags(&self, fd_flags: FdFlags) -> Result<SetFdFlags<T>, Error>
where T: AsFilelike,

Create a new SetFdFlags value for use with set_fd_flags. Read more
Source§

fn set_fd_flags(&mut self, set_fd_flags: SetFdFlags<T>) -> Result<(), Error>
where T: Sized + AsFilelike,

Set the “status” flags for the self file descriptor. Read more
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Pointee for T

Source§

type Pointer = u32

Source§

fn debug( pointer: <T as Pointee>::Pointer, f: &mut Formatter<'_>, ) -> Result<(), Error>

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more