Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
301 changes: 159 additions & 142 deletions stdlib/src/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,165 @@ use std::collections::HashMap;
use std::process::Command;
use std::rc::Rc;
use techscript_runtime::{
context::Capability,
context::{Capability, RuntimeContext},
error::{RuntimeError, RuntimeErrorKind},
value::RuntimeValue,
};

fn process_run(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Process) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Process capability is denied".to_string(),
),
None,
None,
));
}
let cmd = args[0].try_into_string()?;
if cmd.contains("..") || cmd.contains('\0') {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Path traversal or null bytes in command are denied"
.to_string(),
),
None,
None,
));
}
let args_list = match &args[1] {
RuntimeValue::List { items, .. } => {
let mut list = Vec::new();
for item in items.borrow().iter() {
list.push(item.try_into_string()?);
}
list
}
other => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "List".to_string(),
found: other.runtime_type().to_string(),
},
None,
None,
))
}
};

let output = Command::new(cmd).args(args_list).output().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!("Failed to execute command: {}", e)),
None,
None,
)
})?;

let mut res_map = IndexMap::new();
res_map.insert(
"stdout".to_string(),
RuntimeValue::Str(String::from_utf8_lossy(&output.stdout).to_string()),
);
res_map.insert(
"stderr".to_string(),
RuntimeValue::Str(String::from_utf8_lossy(&output.stderr).to_string()),
);
res_map.insert(
"code".to_string(),
RuntimeValue::Int(output.status.code().unwrap_or(-1) as i64),
);

Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
}

fn process_spawn(
ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
if !ctx.config.capabilities.contains(&Capability::Process) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Process capability is denied".to_string(),
),
None,
None,
));
}
let cmd = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};

if cmd.contains("..") || cmd.contains('\0') {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Path traversal or null bytes in command are denied"
.to_string(),
),
None,
None,
));
}

let parsed = shlex::split(&cmd).ok_or_else(|| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation("Failed to parse command string".to_string()),
None,
None,
)
})?;

if parsed.is_empty() {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation("Empty command string".to_string()),
None,
None,
));
}

Command::new(&parsed[0])
.args(&parsed[1..])
.spawn()
.map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
})?;
Ok(RuntimeValue::Null)
}

fn process_exit(
_ctx: &mut RuntimeContext,
args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
let code = args[0].try_into_int()? as i32;
std::process::exit(code);
}

fn process_pid(
_ctx: &mut RuntimeContext,
_args: Vec<RuntimeValue>,
) -> Result<RuntimeValue, RuntimeError> {
Ok(RuntimeValue::Int(std::process::id() as i64))
}

impl StdlibRegistry {
pub fn register_process(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
Expand All @@ -20,77 +174,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "run".to_string(),
arity: 2,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Process) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Process capability is denied"
.to_string(),
),
None,
None,
));
}
let cmd = args[0].try_into_string()?;
if cmd.contains("..") || cmd.contains('\0') {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Path traversal or null bytes in command are denied".to_string(),
),
None,
None,
));
}
let args_list = match &args[1] {
RuntimeValue::List { items, .. } => {
let mut list = Vec::new();
for item in items.borrow().iter() {
list.push(item.try_into_string()?);
}
list
}
other => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "List".to_string(),
found: other.runtime_type().to_string(),
},
None,
None,
))
}
};

let output = Command::new(cmd).args(args_list).output().map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(format!(
"Failed to execute command: {}",
e
)),
None,
None,
)
})?;

let mut res_map = IndexMap::new();
res_map.insert(
"stdout".to_string(),
RuntimeValue::Str(String::from_utf8_lossy(&output.stdout).to_string()),
);
res_map.insert(
"stderr".to_string(),
RuntimeValue::Str(String::from_utf8_lossy(&output.stderr).to_string()),
);
res_map.insert(
"code".to_string(),
RuntimeValue::Int(output.status.code().unwrap_or(-1) as i64),
);

Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(res_map)),
is_const: false,
})
},
callback: process_run,
}),
);

Expand All @@ -99,71 +183,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "spawn".to_string(),
arity: 1,
callback: |ctx, args| {
if !ctx.config.capabilities.contains(&Capability::Process) {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Process capability is denied"
.to_string(),
),
None,
None,
));
}
let cmd = match &args[0] {
RuntimeValue::Str(s) => s.clone(),
_ => {
return Err(RuntimeError::new(
RuntimeErrorKind::TypeMismatch {
expected: "string".to_string(),
found: "other".to_string(),
},
None,
None,
))
}
};

if cmd.contains("..") || cmd.contains('\0') {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Security policy violation: Path traversal or null bytes in command are denied".to_string(),
),
None,
None,
));
}

let parsed = shlex::split(&cmd).ok_or_else(|| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(
"Failed to parse command string".to_string(),
),
None,
None,
)
})?;

if parsed.is_empty() {
return Err(RuntimeError::new(
RuntimeErrorKind::InvalidOperation("Empty command string".to_string()),
None,
None,
));
}

Command::new(&parsed[0])
.args(&parsed[1..])
.spawn()
.map_err(|e| {
RuntimeError::new(
RuntimeErrorKind::InvalidOperation(e.to_string()),
None,
None,
)
})?;
Ok(RuntimeValue::Null)
},
callback: process_spawn,
}),
);

Expand All @@ -172,10 +192,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "exit".to_string(),
arity: 1,
callback: |_ctx, args| {
let code = args[0].try_into_int()? as i32;
std::process::exit(code);
},
callback: process_exit,
}),
);

Expand All @@ -184,7 +201,7 @@ impl StdlibRegistry {
Rc::new(StdFunction {
name: "pid".to_string(),
arity: 0,
callback: |_ctx, _args| Ok(RuntimeValue::Int(std::process::id() as i64)),
callback: process_pid,
}),
);

Expand Down
Loading