Skip to content

Commit e54b01b

Browse files
authored
Merge pull request #101 from Shopify/fix-75
Fix #75 Untrusted directory error message should mention the directory
2 parents 01628eb + 205ba1c commit e54b01b

File tree

6 files changed

+79
-14
lines changed

6 files changed

+79
-14
lines changed

Cargo.lock

Lines changed: 45 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ rand = "0.7"
3030
[dev-dependencies]
3131
quickcheck = "1.0.3"
3232
quickcheck_macros = "1.0.0"
33+
tempfile = "3.3.0"

src/hook.rs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,12 @@ pub fn load_env(
114114

115115
/// Load a Source from the current dir, ensuring that it is trusted.
116116
fn load_trusted_source(pathbuf: PathBuf) -> Result<Option<Source>, Error> {
117-
if let Some(root) = loader::find_root(pathbuf, loader::DEFAULT_RELATIVE_COMPONENT)? {
117+
if let Some(root) = loader::find_root(&pathbuf, loader::DEFAULT_RELATIVE_COMPONENT)? {
118118
if !trust::is_dir_trusted(&root)? {
119-
return Err(trust::NotTrusted {}.into());
119+
return Err(trust::NotTrusted {
120+
not_trusted_dir_path: pathbuf.to_string_lossy().to_string(),
121+
}
122+
.into());
120123
}
121124
return Ok(loader::load(root)?);
122125
}
@@ -196,3 +199,19 @@ pub fn apply_env(
196199
fn shell_escape(s: &str) -> String {
197200
shell::escape(Cow::from(s)).to_string()
198201
}
202+
203+
#[cfg(test)]
204+
mod tests {
205+
use super::*;
206+
use std::fs;
207+
use tempfile::tempdir;
208+
#[test]
209+
fn load_trusted_source_returns_an_error_for_untrusted_folders() {
210+
let temp_dir = tempdir().unwrap().into_path();
211+
let path = temp_dir.to_string_lossy().to_string();
212+
fs::create_dir(temp_dir.join(".shadowenv.d")).unwrap();
213+
let result = load_trusted_source(temp_dir);
214+
assert!(result.is_err());
215+
assert_eq!(format!("directory: '{}' contains untrusted shadowenv program: `shadowenv help trust` to learn more.", path), result.err().unwrap().to_string())
216+
}
217+
}

src/loader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,12 @@ pub const DEFAULT_RELATIVE_COMPONENT: &'static str = ".shadowenv.d";
1111

1212
/// Search upwards the filesystem branch starting with `at` and then its ancestors looking
1313
/// for a file or directory named `relative_component`.
14-
pub fn find_root(at: PathBuf, relative_component: &str) -> Result<Option<PathBuf>, Error> {
14+
pub fn find_root(at: &PathBuf, relative_component: &str) -> Result<Option<PathBuf>, Error> {
1515
for curr in at.ancestors() {
1616
let dirpath = curr.join(relative_component);
1717

1818
match fs::read_dir(&dirpath) {
19-
Ok(_) => return Ok(Some(std::fs::canonicalize(dirpath)?)),
19+
Ok(_) => return Ok(Some(fs::canonicalize(dirpath)?)),
2020
Err(ref e) if e.kind() == ErrorKind::NotFound => (),
2121
Err(e) => return Err(e.into()),
2222
}

src/output.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ fn backticks_to_bright_green(err: Error) -> String {
7272

7373
fn check_and_trigger_cooldown(err: &Error, shellpid: u32) -> Result<bool, Error> {
7474
// if no .shadowenv.d, then Err(_) just means no cooldown: always display error.
75-
let root = loader::find_root(env::current_dir()?, loader::DEFAULT_RELATIVE_COMPONENT)?
75+
let root = loader::find_root(&env::current_dir()?, loader::DEFAULT_RELATIVE_COMPONENT)?
7676
.ok_or_else(|| format_err!("no .shadowenv.d"))?;
7777

7878
let _ = clean_up_stale_errors(&root, Duration::new(300, 0));

src/trust.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@ pub struct NoShadowenv;
1717

1818
#[derive(Fail, Debug)]
1919
#[fail(
20-
display = "directory contains untrusted shadowenv program: `shadowenv help trust` to learn more."
20+
display = "directory: '{}' contains untrusted shadowenv program: `shadowenv help trust` to learn more.",
21+
not_trusted_dir_path
2122
)]
22-
pub struct NotTrusted;
23+
pub struct NotTrusted {
24+
pub not_trusted_dir_path: String,
25+
}
2326

2427
pub fn is_dir_trusted(dir: &PathBuf) -> Result<bool, Error> {
2528
let signer = load_or_generate_signer().unwrap();
2629

27-
let root = match loader::find_root(dir.to_path_buf(), loader::DEFAULT_RELATIVE_COMPONENT)? {
30+
let root = match loader::find_root(&dir.to_path_buf(), loader::DEFAULT_RELATIVE_COMPONENT)? {
2831
None => return Err(NoShadowenv {}.into()),
2932
Some(r) => r,
3033
};
@@ -52,7 +55,7 @@ pub fn is_dir_trusted(dir: &PathBuf) -> Result<bool, Error> {
5255
}
5356

5457
fn load_or_generate_signer() -> Result<Keypair, Error> {
55-
let path = format!("{}/.config/shadowenv/trust-key-v2", std::env::var("HOME")?);
58+
let path = format!("{}/.config/shadowenv/trust-key-v2", env::var("HOME")?);
5659

5760
let r_o_bytes: Result<Option<Vec<u8>>, Error> = match fs::read(Path::new(&path)) {
5861
Ok(bytes) => Ok(Some(bytes)),
@@ -67,7 +70,7 @@ fn load_or_generate_signer() -> Result<Keypair, Error> {
6770
None => {
6871
let mut csprng = OsRng {};
6972
let seed = Keypair::generate(&mut csprng);
70-
std::fs::create_dir_all(Path::new(&path).to_path_buf().parent().unwrap())?;
73+
fs::create_dir_all(Path::new(&path).to_path_buf().parent().unwrap())?;
7174
let mut file = match File::create(OsString::from(&path)) {
7275
// TODO: error type
7376
Err(why) => panic!("couldn''t write to {}: {}", path, why),
@@ -85,7 +88,7 @@ fn load_or_generate_signer() -> Result<Keypair, Error> {
8588
pub fn run() -> Result<(), Error> {
8689
let signer = load_or_generate_signer().unwrap();
8790

88-
let root = match loader::find_root(env::current_dir()?, loader::DEFAULT_RELATIVE_COMPONENT)? {
91+
let root = match loader::find_root(&env::current_dir()?, loader::DEFAULT_RELATIVE_COMPONENT)? {
8992
None => return Err(NoShadowenv {}.into()),
9093
Some(r) => r,
9194
};

0 commit comments

Comments
 (0)