Skip to content

Commit

Permalink
Disable default WASI inheritance for every env
Browse files Browse the repository at this point in the history
  • Loading branch information
bkolobara committed Aug 25, 2021
1 parent c688184 commit 0c69398
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/config.rs
Expand Up @@ -11,6 +11,8 @@ pub struct EnvConfig {
max_fuel: Option<u64>,
allowed_namespaces: Vec<String>,
plugins: Vec<Plugin>,
wasi_args: Option<Vec<String>>,
wasi_envs: Option<Vec<(String, String)>>,
}

impl EnvConfig {
Expand All @@ -21,6 +23,8 @@ impl EnvConfig {
max_fuel,
allowed_namespaces: Vec::new(),
plugins: Vec::new(),
wasi_args: None,
wasi_envs: None,
}
}

Expand Down Expand Up @@ -55,6 +59,22 @@ impl EnvConfig {
self.plugins.push(plugin);
Ok(())
}

pub fn set_wasi_args(&mut self, args: Vec<String>) {
self.wasi_args = Some(args);
}

pub fn wasi_args(&self) -> &Option<Vec<String>> {
&self.wasi_args
}

pub fn set_wasi_envs(&mut self, envs: Vec<(String, String)>) {
self.wasi_envs = Some(envs);
}

pub fn wasi_envs(&self) -> &Option<Vec<(String, String)>> {
&self.wasi_envs
}
}

impl Default for EnvConfig {
Expand All @@ -67,6 +87,8 @@ impl Default for EnvConfig {
String::from("wasi_snapshot_preview1::"),
],
plugins: vec![],
wasi_args: None,
wasi_envs: None,
}
}
}
25 changes: 23 additions & 2 deletions src/main.rs
@@ -1,4 +1,4 @@
use std::{fs, path::Path};
use std::{env, fs, path::Path};

use clap::{crate_version, App, Arg, ArgSettings};

Expand All @@ -24,11 +24,32 @@ fn main() -> Result<()> {
Arg::new("wasm")
.value_name("WASM")
.about("Entry .wasm file")
.required(true),
.required(true)
.index(1),
)
.arg(
Arg::new("wasm_args")
.value_name("WASM_ARGS")
.about("Arguments passed to the guest")
.required(false)
.multiple_values(true)
.index(2),
)
.get_matches();

let mut config = EnvConfig::default();

// Set correct command line arguments for the guest
let wasi_args = args
.values_of("wasm_args")
.unwrap_or_default()
.map(|arg| arg.to_string())
.collect();
config.set_wasi_args(wasi_args);

// Inherit environment variables
config.set_wasi_envs(env::vars().collect());

// Add plugins passed through the --plugin or -P flags to the environment
if let Some(plugins) = args.values_of("plugin") {
for plugin in plugins {
Expand Down
1 change: 1 addition & 0 deletions src/module.rs
Expand Up @@ -66,6 +66,7 @@ impl Module {
self.clone(),
signal_mailbox.0.clone(),
message_mailbox.clone(),
self.environment().config(),
)?;

let mut store = Store::new(self.environment().engine(), state);
Expand Down
15 changes: 9 additions & 6 deletions src/state.rs
Expand Up @@ -72,15 +72,18 @@ impl ProcessState {
module: Module,
signal_mailbox: UnboundedSender<Signal>,
message_mailbox: MessageMailbox,
config: &EnvConfig,
) -> Result<Self> {
let wasi = WasiCtxBuilder::new();
let wasi = wasi.inherit_stdio();
let wasi = wasi.inherit_env()?;
// Skip the first argument (`lunatic`) from the args list. When compiling existing
// applications to Wasm, they assume that the first argument is going to be the binary
// name and all other arguments follow.
let args: Vec<String> = std::env::args().skip(1).collect();
let wasi = wasi.args(&args)?;
let wasi = match config.wasi_envs() {
Some(envs) => wasi.envs(envs)?,
None => wasi,
};
let wasi = match config.wasi_args() {
Some(args) => wasi.args(args)?,
None => wasi,
};
let state = Self {
id,
module,
Expand Down

0 comments on commit 0c69398

Please sign in to comment.