Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(automation): close brace injection and harden filename sanitizer

Richie McIlroya423e519c95a
diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs
index 614f20649..c9e31114c 100644
--- a/crates/automation/src/lib.rs
+++ b/crates/automation/src/lib.rs
@@ -428,14 +428,24 @@ pub fn load_store_from_json(value: &serde_json::Value) -> Option<AutomationsStor
/// target directory (path traversal) or produce an invalid name. Replacing separators keeps the value
/// a single component, so directories in the resolved path can only come from the user-authored template.
pub fn sanitize_filename_component(value: &str) -> String {
- value
+ let sanitized: String = value
.chars()
.map(|c| match c {
'/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
+ // Braces are template markers; replacing them stops a window title like `{image_path}`
+ // from acting as a live variable when the substituted value is run through a second
+ // template pass (e.g. notifications compose the filename and body templates).
+ '{' | '}' => '_',
other if other.is_control() => '_',
other => other,
})
- .collect()
+ .take(128)
+ .collect();
+
+ // Windows rejects filenames ending in a space or '.', so trim those after clamping the length.
+ sanitized
+ .trim_end_matches(|c: char| c == ' ' || c == '.')
+ .to_string()
}
/// Build a single shell command line from a program and its arguments, quoting each token so that
diff --git a/crates/automation/src/tests.rs b/crates/automation/src/tests.rs
index be89562c5..22830f5c9 100644
--- a/crates/automation/src/tests.rs
+++ b/crates/automation/src/tests.rs
@@ -777,6 +777,18 @@ fn sanitize_filename_component_neutralizes_traversal_and_reserved_chars() {
"Slack _ #general"
);
assert_eq!(sanitize_filename_component("plain title"), "plain title");
+ assert_eq!(sanitize_filename_component("{image_path}"), "_image_path_");
+ assert_eq!(sanitize_filename_component("report. "), "report");
+ assert_eq!(
+ sanitize_filename_component("trailing dots..."),
+ "trailing dots"
+ );
+ assert_eq!(
+ sanitize_filename_component(&"a".repeat(200))
+ .chars()
+ .count(),
+ 128
+ );
}
#[test]