sandboxing goes after variables now

This commit is contained in:
2024-12-29 03:09:13 -05:00
parent c08d0a20e2
commit 37662691e2
4 changed files with 112 additions and 180 deletions

View File

@ -1,3 +1,4 @@
use crate::VERSION;
use mlua::{
Lua as Luau,
Result as lResult,
@ -15,7 +16,7 @@ where
}
fn luau_error<T>(err: mlua::Error) -> Option<T> {
cprintln!("<bold>====</>\n<r>{err}</>\n<bold>====</>");
cprintln!("<bold>====</>\n<r><bold>[!]</> {err}</>\n<bold>====</>");
None
}
@ -36,6 +37,7 @@ fn luau_out(luau_args: MultiValue) -> String {
trait Globals {
fn print(&self) -> lResult<()>;
fn printraw(&self) -> lResult<()>;
fn version(&self) -> lResult<()>;
}
impl Globals for Vm {
fn print(&self) -> lResult<()> {
@ -50,24 +52,25 @@ impl Globals for Vm {
Ok(())
})?)
}
fn version(&self) -> lResult<()> {
let luau_info = self.0.globals().get::<String>("_VERSION")?;
self.0.globals().set("_VERSION", format!("{}, liblambdashell {}", luau_info, VERSION))
}
}
pub struct Vm(Luau);
impl Vm {
pub fn new() -> Option<Self> {
let spawn_luau = || -> lResult<Luau> {
let instance = Luau::new();
instance.sandbox(true)?;
instance.globals().set("getfenv", mlua::Nil)?;
instance.globals().set("setfenv", mlua::Nil)?;
Ok(instance)
};
spawn_luau().map_or_else(|e| display_none(e), |l| Some(Self(l)))
pub fn new() -> Self {
Self(Luau::new())
}
fn set_shell_globals(&self) -> mlua::Result<()> {
self.print()?;
self.printraw()?;
self.version()?;
self.0.globals().set("getfenv", mlua::Nil)?;
self.0.globals().set("setfenv", mlua::Nil)?;
self.0.sandbox(true)?;
Ok(())
}

View File

@ -15,7 +15,7 @@ trait ShellLuauVm {
}
impl ShellLuauVm for LambdaShell {
fn shell_vm_exec(&self, source: String) {
vm::Vm::new().map(|vm| vm.exec(source));
vm::Vm::new().exec(source);
}
}
@ -36,21 +36,17 @@ impl LambdaShell {
}
}
fn input(&mut self) {
let mut input = String::new();
io::stdin().read_line(&mut input).map_or_else(|read_error| println!("{read_error}"), |_size| {
let trimmed_input = input.trim();
match trimmed_input {
//special casey
"exit" => self.terminating = true,
_ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_owned()).exec()
};
})
}
pub fn wait(&mut self) -> Result<(), io::Error> {
io::Write::flush(&mut io::stdout()).map_or_else(|flush_error| Err(flush_error), |()| {
self.input();
let mut input = String::new();
io::stdin().read_line(&mut input).map_or_else(|read_error| println!("{read_error}"), |_size| {
let trimmed_input = input.trim();
match trimmed_input {
//special casey
"exit" => self.terminating = true,
_ => self.storage.command_exit_status = commands::Command::new(trimmed_input.to_owned()).exec()
};
});
Ok(())
})
}