Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

feat: Improve template expansion with shell escaping and add tests

Maximilian Roos1b00ddcb7974
diff --git a/src/config.rs b/src/config.rs
index c0432f6fa..eb547d5ed 100644
--- a/src/config.rs
+++ b/src/config.rs
@@ -459,16 +459,24 @@ pub fn expand_template(
branch: &str,
extra: &std::collections::HashMap<&str, &str>,
) -> String {
+ use shell_escape::escape;
+ use std::borrow::Cow;
+
// Sanitize branch name by replacing path separators
let safe_branch = branch.replace(['/', '\\'], "-");
+ // Shell-escape all variables to prevent issues with spaces and special characters
+ let escaped_worktree = escape(Cow::Borrowed(main_worktree));
+ let escaped_branch = escape(Cow::Borrowed(safe_branch.as_str()));
+
let mut result = template
- .replace("{main-worktree}", main_worktree)
- .replace("{branch}", &safe_branch);
+ .replace("{main-worktree}", &escaped_worktree)
+ .replace("{branch}", &escaped_branch);
- // Apply any extra variables
+ // Apply any extra variables (also escaped)
for (key, value) in extra {
- result = result.replace(&format!("{{{}}}", key), value);
+ let escaped_value = escape(Cow::Borrowed(*value));
+ result = result.replace(&format!("{{{}}}", key), &escaped_value);
}
result
diff --git a/src/config_template_test.rs b/src/config_template_test.rs
new file mode 100644
index 000000000..ccd2b6921
--- /dev/null
+++ b/src/config_template_test.rs
@@ -0,0 +1,318 @@
+//! Tests for template expansion with special characters and edge cases
+//!
+//! These tests target potential shell injection vulnerabilities and
+//! edge cases in template variable substitution.
+
+use crate::config::expand_template;
+use std::collections::HashMap;
+
+#[test]
+fn test_expand_template_normal() {
+ let extras = HashMap::new();
+ let result = expand_template(
+ "echo {branch} {main-worktree}",
+ "myrepo",
+ "feature",
+ &extras,
+ );
+ assert_eq!(result, "echo feature myrepo");
+}
+
+#[test]
+fn test_expand_template_branch_with_slashes() {
+ // Bug hypothesis: Branch names with slashes are sanitized to dashes
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "feature/nested/branch", &extras);
+
+ // Line 459: safe_branch = branch.replace(['/', '\\'], "-")
+ assert_eq!(result, "echo feature-nested-branch");
+}
+
+#[test]
+fn test_expand_template_branch_with_spaces() {
+ // FIXED: Branch names with spaces are now shell-escaped
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "feature name", &extras);
+
+ // New behavior: shell-escaped with single quotes
+ assert_eq!(result, "echo 'feature name'");
+ // This correctly executes: echo 'feature name' (1 argument)
+}
+
+#[test]
+fn test_expand_template_branch_with_special_shell_chars() {
+ // FIXED: Special shell characters are now escaped
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "feature$(whoami)", &extras);
+
+ // New behavior: shell-escaped, prevents command substitution
+ assert_eq!(result, "echo 'feature$(whoami)'");
+ // Shell executes: echo 'feature$(whoami)' (literal string, no command execution)
+}
+
+#[test]
+fn test_expand_template_branch_with_backticks() {
+ // FIXED: Backticks are now escaped
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "feature`id`", &extras);
+
+ assert_eq!(result, "echo 'feature`id`'");
+ // Shell executes: echo 'feature`id`' (literal, no command execution)
+}
+
+#[test]
+fn test_expand_template_branch_with_quotes() {
+ // FIXED: Quotes in branch names are handled properly
+ let extras = HashMap::new();
+ let result = expand_template("echo '{branch}'", "myrepo", "feature'test", &extras);
+
+ // shell_escape uses single quotes, so single quotes inside are escaped
+ assert_eq!(result, "echo ''feature'\\''test''");
+ // This properly escapes the embedded single quotes
+}
+
+#[test]
+fn test_expand_template_extra_vars_with_spaces() {
+ // FIXED: Extra variables with spaces are now escaped
+ let mut extras = HashMap::new();
+ extras.insert("worktree", "/path with spaces/to/worktree");
+ let result = expand_template("cd {worktree}", "myrepo", "main", &extras);
+
+ assert_eq!(result, "cd '/path with spaces/to/worktree'");
+ // Shell correctly executes: cd '/path with spaces/to/worktree'
+}
+
+#[test]
+fn test_expand_template_extra_vars_with_dollar_sign() {
+ // FIXED: Dollar signs are now escaped
+ let mut extras = HashMap::new();
+ extras.insert("worktree", "/path/$USER/worktree");
+ let result = expand_template("cd {worktree}", "myrepo", "main", &extras);
+
+ assert_eq!(result, "cd '/path/$USER/worktree'");
+ // Shell executes literal path (no $USER expansion)
+}
+
+#[test]
+fn test_expand_template_extra_vars_with_command_substitution() {
+ // FIXED: Command injection is now prevented
+ let mut extras = HashMap::new();
+ extras.insert("target", "main; rm -rf /");
+ let result = expand_template("git merge {target}", "myrepo", "feature", &extras);
+
+ assert_eq!(result, "git merge 'main; rm -rf /'");
+ // Shell executes: git merge 'main; rm -rf /' (literal branch name, no command execution)
+}
+
+#[test]
+fn test_expand_template_variable_collision() {
+ // BUG HYPOTHESIS: What if extra vars contain "branch" or "main-worktree"?
+ // Looking at code: base replacements happen first (line 461-463), then extra vars (line 466-468)
+ // So extra var "branch" won't override {branch}, but could create unexpected results
+ let mut extras = HashMap::new();
+ extras.insert("branch", "hacked");
+ let result = expand_template("echo {branch}", "myrepo", "feature", &extras);
+
+ // {branch} is replaced with "feature" first (line 463)
+ // Then {branch} is replaced again? No, already done!
+ assert_eq!(result, "echo feature");
+}
+
+#[test]
+fn test_expand_template_extra_var_named_branch() {
+ // What if we have both {branch} in template and "branch" in extras?
+ let mut extras = HashMap::new();
+ extras.insert("branch", "extra-branch");
+ let result = expand_template("echo {branch} from {branch}", "myrepo", "main", &extras);
+
+ // Both {branch} occurrences should be replaced with "main" (the branch parameter)
+ // Extra var "branch" doesn't affect it because format is "{{{}}}", so looks for "{branch}"
+ // and "branch" creates different search patterns
+ assert_eq!(result, "echo main from main");
+}
+
+#[test]
+fn test_expand_template_missing_variable() {
+ // What happens with undefined variables?
+ let extras = HashMap::new();
+ let result = expand_template("echo {undefined}", "myrepo", "main", &extras);
+
+ // No replacement, left as-is
+ assert_eq!(result, "echo {undefined}");
+}
+
+#[test]
+fn test_expand_template_empty_branch() {
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "", &extras);
+
+ // Empty string is escaped as ''
+ assert_eq!(result, "echo ''");
+}
+
+#[test]
+fn test_expand_template_unicode_in_branch() {
+ // Unicode characters in branch name
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "feature-πŸš€", &extras);
+
+ // Unicode is preserved but quoted due to emoji
+ assert_eq!(result, "echo 'feature-πŸš€'");
+}
+
+#[test]
+fn test_expand_template_backslash_in_branch() {
+ // Windows-style path separators
+ let extras = HashMap::new();
+ let result = expand_template("echo {branch}", "myrepo", "feature\\branch", &extras);
+
+ // Line 459: backslashes also replaced with dashes
+ assert_eq!(result, "echo feature-branch");
+}
+
+#[test]
+fn test_expand_template_multiple_replacements() {
+ let mut extras = HashMap::new();
+ extras.insert("worktree", "/path/to/wt");
+ extras.insert("target", "develop");
+
+ let result = expand_template(
+ "cd {worktree} && git merge {target} from {branch}",
+ "myrepo",
+ "feature",
+ &extras,
+ );
+
+ assert_eq!(result, "cd /path/to/wt && git merge develop from feature");
+}
+
+#[test]
+fn test_expand_template_curly_braces_without_variables() {
+ // Just curly braces, not variables
+ let extras = HashMap::new();
+ let result = expand_template("echo {}", "myrepo", "main", &extras);
+
+ assert_eq!(result, "echo {}");
+}
+
+#[test]
+fn test_expand_template_nested_curly_braces() {
+ // Nested braces (shouldn't match variable pattern)
+ let extras = HashMap::new();
+ let result = expand_template("echo {{branch}}", "myrepo", "main", &extras);
+
+ // Only {branch} inside gets replaced, outer braces remain
+ // Wait, let's think: replace("{branch}", "main") on "{{branch}}"
+ // Results in "{main}"
+ assert_eq!(result, "echo {main}");
+}
+
+// Snapshot tests for shell escaping behavior
+// These verify the exact shell-escaped output for security-critical cases
+
+#[test]
+fn snapshot_shell_escaping_special_chars() {
+ let extras = HashMap::new();
+
+ // Test various shell special characters
+ let test_cases = vec![
+ ("spaces", "feature name"),
+ ("dollar", "feature$USER"),
+ ("command_sub", "feature$(whoami)"),
+ ("backticks", "feature`id`"),
+ ("semicolon", "feature;rm -rf /"),
+ ("pipe", "feature|grep foo"),
+ ("ampersand", "feature&background"),
+ ("redirect", "feature>output.txt"),
+ ("wildcard", "feature*glob"),
+ ("question", "feature?char"),
+ ("brackets", "feature[0-9]"),
+ ];
+
+ let mut results = Vec::new();
+ for (name, branch) in test_cases {
+ let result = expand_template("echo {branch}", "myrepo", branch, &extras);
+ results.push((name, branch, result));
+ }
+
+ insta::assert_yaml_snapshot!(results);
+}
+
+#[test]
+fn snapshot_shell_escaping_quotes() {
+ let extras = HashMap::new();
+
+ // Test quote handling
+ let test_cases = vec![
+ ("single_quote", "feature'test"),
+ ("double_quote", "feature\"test"),
+ ("mixed_quotes", "feature'test\"mixed"),
+ ("multiple_single", "don't'panic"),
+ ];
+
+ let mut results = Vec::new();
+ for (name, branch) in test_cases {
+ let result = expand_template("echo {branch}", "myrepo", branch, &extras);
+ results.push((name, branch, result));
+ }
+
+ insta::assert_yaml_snapshot!(results);
+}
+
+#[test]
+fn snapshot_shell_escaping_paths() {
+ let mut extras = HashMap::new();
+
+ // Test path escaping with various special characters
+ let test_cases = vec![
+ ("spaces", "/path with spaces/to/worktree"),
+ ("dollar", "/path/$USER/worktree"),
+ ("tilde", "~/worktree"),
+ ("special_chars", "/path/to/worktree (new)"),
+ ("unicode", "/path/to/πŸš€/worktree"),
+ ];
+
+ let mut results = Vec::new();
+ for (name, path) in test_cases {
+ extras.clear();
+ extras.insert("worktree", path);
+ let result = expand_template("cd {worktree} && echo {branch}", "myrepo", "main", &extras);
+ results.push((name, path, result));
+ }
+
+ insta::assert_yaml_snapshot!(results);
+}
+
+#[test]
+fn snapshot_complex_templates() {
+ let mut extras = HashMap::new();
+ extras.insert("worktree", "/path with spaces/wt");
+ extras.insert("target", "main; rm -rf /");
+
+ // Test realistic complex template commands
+ let test_cases = vec![
+ (
+ "cd_and_merge",
+ "cd {worktree} && git merge {target}",
+ "feature branch",
+ ),
+ (
+ "npm_install",
+ "cd {main-worktree}/{branch} && npm install",
+ "feature/new-ui",
+ ),
+ (
+ "echo_vars",
+ "echo 'Branch: {branch}' 'Worktree: {worktree}'",
+ "test$injection",
+ ),
+ ];
+
+ let mut results = Vec::new();
+ for (name, template, branch) in test_cases {
+ let result = expand_template(template, "/repo/path", branch, &extras);
+ results.push((name, template, branch, result));
+ }
+
+ insta::assert_yaml_snapshot!(results);
+}
diff --git a/src/lib.rs b/src/lib.rs
index 74b5a1157..02e3ef785 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -9,5 +9,8 @@ pub use git::HookType;
// Note: display, commands, and llm modules are used by main.rs but not exposed as public API
// Test comment
+#[cfg(test)]
+mod config_template_test;
+
#[cfg(test)]
mod git_parse_test;
diff --git a/src/snapshots/worktrunk__config_template_test__snapshot_complex_templates.snap b/src/snapshots/worktrunk__config_template_test__snapshot_complex_templates.snap
new file mode 100644
index 000000000..8a4e607d6
--- /dev/null
+++ b/src/snapshots/worktrunk__config_template_test__snapshot_complex_templates.snap
@@ -0,0 +1,16 @@
+---
+source: src/config_template_test.rs
+expression: results
+---
+- - cd_and_merge
+ - "cd {worktree} && git merge {target}"
+ - feature branch
+ - "cd '/path with spaces/wt' && git merge 'main; rm -rf /'"
+- - npm_install
+ - "cd {main-worktree}/{branch} && npm install"
+ - feature/new-ui
+ - cd /repo/path/feature-new-ui && npm install
+- - echo_vars
+ - "echo 'Branch: {branch}' 'Worktree: {worktree}'"
+ - test$injection
+ - "echo 'Branch: 'test$injection'' 'Worktree: '/path with spaces/wt''"
diff --git a/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_paths.snap b/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_paths.snap
new file mode 100644
index 000000000..759f73646
--- /dev/null
+++ b/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_paths.snap
@@ -0,0 +1,19 @@
+---
+source: src/config_template_test.rs
+expression: results
+---
+- - spaces
+ - /path with spaces/to/worktree
+ - "cd '/path with spaces/to/worktree' && echo main"
+- - dollar
+ - /path/$USER/worktree
+ - "cd '/path/$USER/worktree' && echo main"
+- - tilde
+ - ~/worktree
+ - "cd '~/worktree' && echo main"
+- - special_chars
+ - /path/to/worktree (new)
+ - "cd '/path/to/worktree (new)' && echo main"
+- - unicode
+ - /path/to/πŸš€/worktree
+ - "cd '/path/to/πŸš€/worktree' && echo main"
diff --git a/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_quotes.snap b/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_quotes.snap
new file mode 100644
index 000000000..58909cc79
--- /dev/null
+++ b/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_quotes.snap
@@ -0,0 +1,16 @@
+---
+source: src/config_template_test.rs
+expression: results
+---
+- - single_quote
+ - "feature'test"
+ - "echo 'feature'\\''test'"
+- - double_quote
+ - "feature\"test"
+ - "echo 'feature\"test'"
+- - mixed_quotes
+ - "feature'test\"mixed"
+ - "echo 'feature'\\''test\"mixed'"
+- - multiple_single
+ - "don't'panic"
+ - "echo 'don'\\''t'\\''panic'"
diff --git a/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_special_chars.snap b/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_special_chars.snap
new file mode 100644
index 000000000..7cf8bad0b
--- /dev/null
+++ b/src/snapshots/worktrunk__config_template_test__snapshot_shell_escaping_special_chars.snap
@@ -0,0 +1,37 @@
+---
+source: src/config_template_test.rs
+expression: results
+---
+- - spaces
+ - feature name
+ - "echo 'feature name'"
+- - dollar
+ - feature$USER
+ - "echo 'feature$USER'"
+- - command_sub
+ - feature$(whoami)
+ - "echo 'feature$(whoami)'"
+- - backticks
+ - "feature`id`"
+ - "echo 'feature`id`'"
+- - semicolon
+ - feature;rm -rf /
+ - "echo 'feature;rm -rf -'"
+- - pipe
+ - feature|grep foo
+ - "echo 'feature|grep foo'"
+- - ampersand
+ - feature&background
+ - "echo 'feature&background'"
+- - redirect
+ - feature>output.txt
+ - "echo 'feature>output.txt'"
+- - wildcard
+ - feature*glob
+ - "echo 'feature*glob'"
+- - question
+ - feature?char
+ - "echo 'feature?char'"
+- - brackets
+ - "feature[0-9]"
+ - "echo 'feature[0-9]'"