pub struct SafeCommand { /* private fields */ }Expand description
Safe command builder that prevents injection attacks
Instead of executing commands through a shell (vulnerable to injection), this executes programs directly with validated arguments.
§Security Features
- No shell execution (prevents
; rm -rf /attacks) - Argument validation (prevents metacharacter injection)
- Whitelist of allowed commands
- Path validation for file operations
§Examples
use crate::security::command::SafeCommand;
// Safe: Direct program execution
let output = SafeCommand::new("git")?
.arg("init")?
.execute()?;
// Unsafe equivalent that we prevent:
// Command::new("sh").arg("-c").arg(user_input) // VULNERABLE!Implementations§
Source§impl SafeCommand
impl SafeCommand
Sourcepub fn new(program: &str) -> Result<Self>
pub fn new(program: &str) -> Result<Self>
Create a new safe command
§Security
- Validates command is in whitelist
- Prevents shell execution
- Rejects commands with dangerous characters
Sourcepub fn arg(self, arg: &str) -> Result<Self>
pub fn arg(self, arg: &str) -> Result<Self>
Add an argument to the command
§Security
- Validates argument doesn’t contain shell metacharacters
- Prevents injection via argument expansion
Sourcepub fn current_dir(self, dir: &Path) -> Result<Self>
pub fn current_dir(self, dir: &Path) -> Result<Self>
Set the working directory
§Security
- Validates path exists and is a directory
- Prevents path traversal attacks
Sourcepub fn execute(self) -> Result<Output>
pub fn execute(self) -> Result<Output>
Execute the command safely
§Security
- Uses direct program execution (no shell)
- All arguments are properly escaped
- Output is validated as UTF-8
Sourcepub fn execute_stdout(self) -> Result<String>
pub fn execute_stdout(self) -> Result<String>
Execute and return stdout as String
§Security
- Validates output is valid UTF-8
- Returns error on non-zero exit code
Trait Implementations§
Source§impl Clone for SafeCommand
impl Clone for SafeCommand
Source§fn clone(&self) -> SafeCommand
fn clone(&self) -> SafeCommand
Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source. Read moreAuto Trait Implementations§
impl Freeze for SafeCommand
impl RefUnwindSafe for SafeCommand
impl Send for SafeCommand
impl Sync for SafeCommand
impl Unpin for SafeCommand
impl UnsafeUnpin for SafeCommand
impl UnwindSafe for SafeCommand
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
Mutably borrows from an owned value. Read more
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<T> EvidenceKind for T
impl<T> EvidenceKind for T
default fn kind_label(&self) -> &'static str
Source§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> ⓘ
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 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> ⓘ
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