Skip to content

Commit

Permalink
fix(dot): fix traversal bug with non utf8 path
Browse files Browse the repository at this point in the history
  • Loading branch information
oknozor committed Dec 1, 2022
1 parent 6570168 commit 5627fe4
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 18 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
website/** linguist-documentation
docs/** linguist-documentation
bats-tests/** linguist-documentation
ci/**linguist-documentation
config_examplesi/** linguist-documentation
Expand Down
3 changes: 2 additions & 1 deletion config_examples/vars.example.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# - A var file MUST contains valid toml with only string key/value pair.
# - A var file MUST contains valid toml.
# - The name of the file can be anything (ex: color-scheme.toml).
# - A bombadil config can have many var files.
[theme]
terminal = "alacritty"
background = "#292C3E"
foreground = "#EBEBEB"
Expand Down
6 changes: 3 additions & 3 deletions src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod links {
for result in results {
write!(out, "{}", result)?;
}
write!(out, "\n")?;
writeln!(out)?;
}
Ok(())
}
Expand All @@ -40,7 +40,7 @@ pub mod links {
for error in errored {
writeln!(out, "\t{error:?}")?;
}
write!(out, "\n")?;
writeln!(out)?;
}
Ok(())
}
Expand All @@ -51,7 +51,7 @@ pub mod links {
for deleted in deleted {
writeln!(out, "\t{deleted:?}")?;
}
write!(out, "\n")?;
writeln!(out)?;
}
Ok(())
}
Expand Down
17 changes: 10 additions & 7 deletions src/dots.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ impl Dot {
profiles: &[String],
) -> Result<LinkResult> {
let source = &self.source()?;
let target = &self.build_copy_path();
let target = &self.copy_path_unchecked();
let source_str = source.to_str().unwrap_or_default();

let ignored_paths = if self.ignore.is_empty() {
Expand Down Expand Up @@ -91,9 +91,12 @@ impl Dot {
Ok(content) => self.create(source, target, content),
Err(e) if target.exists() => {
match e.kind {
ErrorKind::Utf8Conversion { .. } => {}
ErrorKind::Io(_) => {}
ErrorKind::Msg(message) => println!("\t{}", message.to_string().red()),
ErrorKind::Utf8Conversion { .. } | ErrorKind::Io(..) => {
// Skip non utf8 files like binaries, images etc.
// Those should be symlinked directly once this is implemented
// https://github.com/oknozor/toml-bombadil/issues/138
}
ErrorKind::Msg(message) => println!("\t{}", message.red()),
_ => {
if let Some(source) = e.source() {
println!("\t{}", source);
Expand All @@ -103,10 +106,10 @@ impl Dot {
self.update_raw(source, target)
}
Err(_) => {
fs::copy(&source, &target)?;
fs::copy(source, target)?;
Ok(LinkResult::Created {
target: target.clone(),
copy: self.copy_path()?,
target: self.target.clone(),
copy: self.copy_path_unchecked(),
})
}
}
Expand Down
7 changes: 3 additions & 4 deletions src/paths/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ pub trait DotPaths {
fn copy_path(&self) -> Result<PathBuf>;

/// Build the rendered template path, use this to create the rendered file
fn build_copy_path(&self) -> PathBuf;
fn copy_path_unchecked(&self) -> PathBuf;

/// Remove the dotfile target symlink
fn unlink(&self) -> Result<()>;
Expand Down Expand Up @@ -52,13 +52,12 @@ impl DotPaths for Dot {
}

fn copy_path(&self) -> Result<PathBuf> {
let path = self.build_copy_path();

let path = self.copy_path_unchecked();
path.canonicalize()
.map_err(|error| TemplateNotFound { path, error })
}

fn build_copy_path(&self) -> PathBuf {
fn copy_path_unchecked(&self) -> PathBuf {
dotfile_dir().join(".dots").join(&self.source)
}

Expand Down
6 changes: 3 additions & 3 deletions src/templating.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ impl Variables {
let mut decrypted_secrets = serde_json::Map::new();
for (key, value) in secrets {
let decrypted = gpg.decrypt_secret(
&value
value
.as_str()
.expect("Secret value mut be a gpg encrypted string"),
)?;
Expand Down Expand Up @@ -112,7 +112,7 @@ impl Variables {

// Replace secrets with their decrypted values
if let Some(secrets) = secrets {
let secrets = Variables::decrypt_values(&secrets, gpg)?;
let secrets = Variables::decrypt_values(secrets, gpg)?;
variables.get("secrets").replace(&secrets);
};

Expand Down Expand Up @@ -157,7 +157,7 @@ impl Variables {
let value = value
.as_str()
.expect("'[secrets]' values must be a encrypted type string");
let value = gpg.decrypt_secret(&value)?;
let value = gpg.decrypt_secret(value)?;
decrypted.insert(key.clone(), tera::Value::String(value));
}

Expand Down

0 comments on commit 5627fe4

Please sign in to comment.