Skip to content

Commit aab3973

Browse files
sandbox: Add support for passing arbitrary environment variables
1 parent 5ba1d61 commit aab3973

File tree

6 files changed

+33
-6
lines changed

6 files changed

+33
-6
lines changed

CONFIG.md

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,23 @@ test.sandbox.make_writable = [
159159
]
160160
```
161161

162+
If you need to pass particular environment variables into a sandboxed process, you can list them as
163+
follows:
164+
165+
```toml
166+
[pkg.foo.test.sandbox]
167+
pass_env = [
168+
"VAR1",
169+
"VAR2",
170+
]
171+
```
172+
173+
This will cause the variables "VAR1" and "VAR2", if set, to be passed to the sandboxed process - in
174+
this case the tests for the package `foo`.
175+
162176
### Sandboxing rustc
163177

164-
If you have a sandbox configuation, then from config version 2 onwards, rustc will be run in a
178+
If you have a sandbox configuration, then from config version 2 onwards, rustc will be run in a
165179
sandbox. This means that all proc macros get sandboxed. Controlling the sandbox on a per-proc-macro
166180
basis unfortunately isn't supported yet, but hopefully will in future. This means that if you have
167181
for example one proc macro that needs network access, you'd need to enable network access for the
@@ -241,7 +255,7 @@ file. e.g.
241255
profile = "cackle-release"
242256
```
243257

244-
You can also override with the `--profile` flag, which takes precidence over the config file.
258+
You can also override with the `--profile` flag, which takes precedence over the config file.
245259

246260
Cackle supports analysing references even when inlining occurs, so it can work to some extent even
247261
with optimisations enabled, however it's more likely that you'll run into false attribution bugs,

src/config.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ pub(crate) struct SandboxConfig {
9494

9595
#[serde(default)]
9696
pub(crate) make_writable: Vec<PathBuf>,
97+
98+
#[serde(default)]
99+
pub(crate) pass_env: Vec<String>,
97100
}
98101

99102
#[derive(Deserialize, Serialize, Debug, Default, Clone, PartialEq, Eq, Hash)]

src/config_editor.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1345,10 +1345,7 @@ mod tests {
13451345
crate_sel: crate_sel.clone(),
13461346
sandbox_config: SandboxConfig {
13471347
kind: Some(crate::config::SandboxKind::Bubblewrap),
1348-
extra_args: vec![],
1349-
allow_network: None,
1350-
bind_writable: vec![],
1351-
make_writable: vec![],
1348+
..Default::default()
13521349
},
13531350
binary_path: PathBuf::new(),
13541351
sandbox_config_display: None,

src/sandbox.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ pub(crate) fn from_config(config: &SandboxConfig) -> Result<Option<Box<dyn Sandb
8282
sandbox.set_env(OsStr::new("USER"), OsStr::new("user"));
8383
sandbox.pass_env("PATH");
8484
sandbox.pass_env("HOME");
85+
for env in &config.pass_env {
86+
sandbox.pass_env(env);
87+
}
8588

8689
// Allow read access to the crate's root source directory.
8790
sandbox.ro_bind(Path::new(&get_env("CARGO_MANIFEST_DIR")?));

test_crates/cackle.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,9 @@ allow_apis = [
139139
test.sandbox.bind_writable = [
140140
"crab-9/scratch"
141141
]
142+
test.sandbox.pass_env = [
143+
"CRAB_9_CRASH_TEST",
144+
]
142145
test.allow_apis = [
143146
"env",
144147
"fs",

test_crates/crab-9/src/lib.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,11 @@ mod tests {
4646
fn it_works() {
4747
access_files();
4848
}
49+
50+
#[test]
51+
fn conditional_crash() {
52+
if std::env::var("CRAB_9_CRASH_TEST").is_ok() {
53+
panic!("Deliberate crash");
54+
}
55+
}
4956
}

0 commit comments

Comments
 (0)