From eb3a6ef578ca7805cb0424b26ddf15a27838c12b Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 16 Sep 2019 07:53:15 +0200 Subject: [PATCH] Truncate all files we open to fix #240 Otherwise it's possible to leave bytes related to a previous version of the file in tact, tailing the newly written content. This in turn leads to malformed json when tokens are rewritten. --- src/rust/cli/cmn.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/rust/cli/cmn.rs b/src/rust/cli/cmn.rs index 3f4407a5db..4df622d630 100644 --- a/src/rust/cli/cmn.rs +++ b/src/rust/cli/cmn.rs @@ -404,7 +404,7 @@ pub fn writer_from_opts(arg: Option<&str>) -> Result, io::Error> { let f = arg.unwrap_or("-"); match f { "-" => Ok(Box::new(stdout())), - _ => match fs::OpenOptions::new().create(true).write(true).open(f) { + _ => match fs::OpenOptions::new().create(true).truncate(true).write(true).open(f) { Ok(f) => Ok(Box::new(f)), Err(io_err) => Err(io_err), }, @@ -486,7 +486,7 @@ impl TokenStorage for JsonTokenStorage { } } Some(token) => { - match fs::OpenOptions::new().create(true).write(true).open(&self.path(scope_hash)) { + match fs::OpenOptions::new().create(true).write(true).truncate(true).open(&self.path(scope_hash)) { Ok(mut f) => { match json::to_writer_pretty(&mut f, &token) { Ok(_) => Ok(()), @@ -767,6 +767,7 @@ pub fn application_secret_from_directory(dir: &str, err = match fs::OpenOptions::new() .create(true) .write(true) + .truncate(true) .open(&secret_path) { Err(cfe) => cfe, Ok(mut f) => {