Skip to content

Commit f78f462

Browse files
committed
Enforce sandbox for environment
1 parent b40ae5f commit f78f462

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/libsyntax_ext/env.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ use syntax::symbol::{keywords, Symbol};
2121
use syntax_pos::Span;
2222
use syntax::tokenstream;
2323

24-
use std::env;
25-
2624
pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt,
2725
sp: Span,
2826
tts: &[tokenstream::TokenTree])
@@ -33,8 +31,10 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt,
3331
};
3432

3533
let sp = sp.apply_mark(cx.current_expansion.mark);
36-
let e = match env::var(&*var.as_str()) {
37-
Err(..) => {
34+
let env_sb = cx.parse_sess().env_sandbox();
35+
36+
let e = match env_sb.env_get(&*var.as_str()) {
37+
None => {
3838
let lt = cx.lifetime(sp, keywords::StaticLifetime.ident());
3939
cx.expr_path(cx.path_all(sp,
4040
true,
@@ -46,7 +46,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt,
4646
ast::Mutability::Immutable)],
4747
Vec::new()))
4848
}
49-
Ok(s) => {
49+
Some(s) => {
5050
cx.expr_call_global(sp,
5151
cx.std_path(&["option", "Option", "Some"]),
5252
vec![cx.expr_str(sp, Symbol::intern(&s))])
@@ -68,6 +68,8 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt,
6868
Some(exprs) => exprs.into_iter(),
6969
};
7070

71+
let env_sb = cx.parse_sess().env_sandbox();
72+
7173
let var = match expr_to_string(cx, exprs.next().unwrap(), "expected string literal") {
7274
None => return DummyResult::expr(sp),
7375
Some((v, _style)) => v,
@@ -87,12 +89,12 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt,
8789
return DummyResult::expr(sp);
8890
}
8991

90-
let e = match env::var(&*var.as_str()) {
91-
Err(_) => {
92+
let e = match env_sb.env_get(&*var.as_str()) {
93+
None => {
9294
cx.span_err(sp, &msg.as_str());
9395
cx.expr_usize(sp, 0)
9496
}
95-
Ok(s) => cx.expr_str(sp, Symbol::intern(&s)),
97+
Some(s) => cx.expr_str(sp, Symbol::intern(&s)),
9698
};
9799
MacEager::expr(e)
98100
}

src/test/run-pass/sb-env.rs

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test to see how environment sandboxing is working
12+
// rustc-env:BLOBBIE=FOO
13+
// rustc-env:FOOBIE=BAR
14+
// rustc-env:THINGY=OLD
15+
// compile-flags:--env-allow BLOBBIE --env-define ZIPPIE=BLARG --env-define THINGY=NEW
16+
17+
fn main() {
18+
assert_eq!(option_env!("BLOBBIE"), Some("FOO")); // actual environment, allowed to be seen
19+
assert_eq!(option_env!("ZIPPIE"), Some("BLARG")); // defined on command-line
20+
assert_eq!(option_env!("THINGY"), Some("NEW")); // overridden on command-line
21+
assert_eq!(option_env!("FOOBIE"), None); // actual environment, but not allowed to be seen
22+
}

0 commit comments

Comments
 (0)