From 30ce60196f87b36fc18d9e434fca5e6b020591f3 Mon Sep 17 00:00:00 2001 From: itowlson Date: Fri, 20 Nov 2026 15:45:23 +1300 Subject: [PATCH 1/2] Template snippet paths must be within template Signed-off-by: itowlson --- crates/templates/src/run.rs | 10 ++++- crates/templates/src/template.rs | 44 +++++++++++++++++++ .../evil-absolute/metadata/spin-template.toml | 9 ++++ .../metadata/spin-template.toml | 8 ++++ 4 files changed, 69 insertions(+), 2 deletions(-) create mode 100644 crates/templates/tests/templates/evil-absolute/metadata/spin-template.toml create mode 100644 crates/templates/tests/templates/evil-traverses/metadata/spin-template.toml diff --git a/crates/templates/src/run.rs b/crates/templates/src/run.rs index cca27ed63f..ee6f2a58ad 100644 --- a/crates/templates/src/run.rs +++ b/crates/templates/src/run.rs @@ -97,8 +97,7 @@ impl Run { &self, interaction: impl InteractionStrategy, ) -> anyhow::Result> { - self.validate_version()?; - self.validate_add_component_manifest_version()?; + self.validate_template()?; // TODO: rationalise `path` and `dir` let to = self.generation_target_dir(); @@ -248,6 +247,13 @@ impl Run { } } + fn validate_template(&self) -> anyhow::Result<()> { + self.template.validate_safe_to_run()?; + self.validate_version()?; + self.validate_add_component_manifest_version()?; + Ok(()) + } + fn validate_add_component_manifest_version(&self) -> anyhow::Result<()> { match &self.options.variant { TemplateVariantInfo::NewApplication => Ok(()), diff --git a/crates/templates/src/template.rs b/crates/templates/src/template.rs index a60e76d22d..353d74c6a5 100644 --- a/crates/templates/src/template.rs +++ b/crates/templates/src/template.rs @@ -285,6 +285,26 @@ impl Template { } } + pub(crate) fn validate_safe_to_run(&self) -> anyhow::Result<()> { + for variant in self.variants.values() { + for snippet_path in variant.snippets.values() { + let snippet_path = PathBuf::from(snippet_path); + if snippet_path.is_absolute() { + anyhow::bail!("invalid template: snippet paths must be relative"); + } + for component in snippet_path.components() { + if !matches!(component, std::path::Component::Normal(_)) { + anyhow::bail!( + "invalid template: snippet path attempts directory traversal" + ); + } + } + } + } + + Ok(()) + } + // TODO: we should resolve this once at the start of Run and then use that forever fn variant(&self, variant_info: &TemplateVariantInfo) -> Option { let kind = variant_info.kind(); @@ -770,4 +790,28 @@ mod test { assert!(!add_variant.snippets.contains_key("s1")); assert!(add_variant.snippets.contains_key("s2")); } + + fn test_data_root() -> PathBuf { + let crate_dir = env!("CARGO_MANIFEST_DIR"); + PathBuf::from(crate_dir).join("tests") + } + + #[test] + fn template_is_unsafe_if_it_attempts_directory_traversal() { + let test_dir = test_data_root().join("templates"); + for malign_dir in &["evil-absolute", "evil-traverses"] { + let template_dir = test_dir.join(malign_dir); + let template = Template::load_from(&TemplateLayout::new(template_dir)).unwrap(); + template.validate_safe_to_run().expect_err(&format!( + "'{malign_dir}' should have been judged unsafe but was not" + )); + } + for non_malign_dir in &["add-only-redirect", "partials"] { + let template_dir = test_dir.join(non_malign_dir); + let template = Template::load_from(&TemplateLayout::new(template_dir)).unwrap(); + template.validate_safe_to_run().unwrap_or_else(|_| { + panic!("'{non_malign_dir}' should have been judged safe but was not") + }); // I know but clippy gets mad if I use expect + } + } } diff --git a/crates/templates/tests/templates/evil-absolute/metadata/spin-template.toml b/crates/templates/tests/templates/evil-absolute/metadata/spin-template.toml new file mode 100644 index 0000000000..2c5f8fec91 --- /dev/null +++ b/crates/templates/tests/templates/evil-absolute/metadata/spin-template.toml @@ -0,0 +1,9 @@ +manifest_version = "1" +id = "evil-absolute" +description = "Tests absolute path in snippets is caught" +trigger_type = "http" + +[add_component] +[add_component.snippets] +component = "/home/victim/secrets/secret.txt" +variables = "variables.txt" diff --git a/crates/templates/tests/templates/evil-traverses/metadata/spin-template.toml b/crates/templates/tests/templates/evil-traverses/metadata/spin-template.toml new file mode 100644 index 0000000000..fdbb39e778 --- /dev/null +++ b/crates/templates/tests/templates/evil-traverses/metadata/spin-template.toml @@ -0,0 +1,8 @@ +manifest_version = "1" +id = "evil-traverses" +description = "Tests path traversal in snippets is caught" +trigger_type = "http" + +[add_component] +[add_component.snippets] +component = "wibble/../../../secrets/secret.txt" From 746f98df21cbf3acd449190e34e147ce7bab32e9 Mon Sep 17 00:00:00 2001 From: itowlson Date: Wed, 25 Nov 2026 11:58:52 +1300 Subject: [PATCH 2/2] Symlinks in templates, no thanks Signed-off-by: itowlson --- crates/templates/src/manager.rs | 32 +++++++++++++++++++++++++++++--- src/commands/templates.rs | 1 + 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/crates/templates/src/manager.rs b/crates/templates/src/manager.rs index 3fba30cbf6..079de4ebd1 100644 --- a/crates/templates/src/manager.rs +++ b/crates/templates/src/manager.rs @@ -81,6 +81,8 @@ pub enum SkippedReason { AlreadyExists, /// The template was skipped because its manifest was missing or invalid. InvalidManifest(String), + /// The template was skipped because it contained suspicious content. + SafetyValidation(String), /// The template was removed from the source but could not be removed locally. CouldNotRemove, } @@ -256,6 +258,13 @@ impl TemplateManager { }; let id = template.id(); + if let Err(e) = validate_safe_to_install(source_dir, &template) { + return Ok(InstallationResult::Skipped( + id.to_string(), + SkippedReason::SafetyValidation(e.to_string()), + )); + } + let message = format!("Installing template {id}..."); reporter.report(&message); @@ -477,6 +486,20 @@ async fn copy_template_into( load_template_from(id, dest_dir) } +fn validate_safe_to_install(source_dir: &Path, template: &Template) -> anyhow::Result<()> { + let walk = walkdir::WalkDir::new(source_dir).follow_links(false); + + for entry in walk { + if entry.is_ok_and(|e| e.path_is_symlink()) { + anyhow::bail!("template contains a symlink"); + } + } + + template.validate_safe_to_run()?; + + Ok(()) +} + fn write_install_record(dest_dir: &Path, source: &TemplateSource) { let layout = TemplateLayout::new(dest_dir); let install_record_path = layout.installation_record_file(); @@ -902,10 +925,13 @@ mod tests { let install_results = manager.install_test_data_templates().await; - assert_eq!(1, install_results.skipped.len()); + assert!(!install_results.skipped.is_empty()); - let (id, reason) = &install_results.skipped[0]; - assert_eq!("testing-custom-filter", id); + let (_, reason) = &install_results + .skipped + .iter() + .find(|(id, _)| id == "testing-custom-filter") + .expect("'testing-custom-filter' should have been skipped'"); let SkippedReason::InvalidManifest(message) = reason else { panic!("skip reason should be InvalidManifest"); // clippy dislikes assert!(false...) }; diff --git a/src/commands/templates.rs b/src/commands/templates.rs index 051ebf7fc6..61940beab0 100644 --- a/src/commands/templates.rs +++ b/src/commands/templates.rs @@ -655,6 +655,7 @@ fn skipped_reason_text(reason: &SkippedReason) -> String { match reason { SkippedReason::AlreadyExists => "Already exists".to_owned(), SkippedReason::InvalidManifest(msg) => format!("Template load error: {msg}"), + SkippedReason::SafetyValidation(msg) => format!("Invalid template: {msg}"), SkippedReason::CouldNotRemove => { "No longer exists in source, but could not uninstall".to_owned() }