Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(automation): sanitize window titles in save-to-location filenames

Richie McIlroy146c89a0d0c5
diff --git a/apps/cli/src/automation.rs b/apps/cli/src/automation.rs
index 90a682b8d..73df2cab1 100644
--- a/apps/cli/src/automation.rs
+++ b/apps/cli/src/automation.rs
@@ -9,7 +9,7 @@ use std::path::{Path, PathBuf};
use cap_automation::{
AutomationExportCompression, AutomationHost, AutomationRecordingMode, AutomationsStore,
Capability, ClipboardSource, ExportDestination, ExportFormat, ExportProfile, Trigger,
- TriggerContext,
+ TriggerContext, sanitize_filename_component,
};
use cap_recording::screen_capture::ScreenCaptureTarget;
use serde_json::Value;
@@ -419,7 +419,7 @@ fn apply_filename_template(template: &str, ctx: &TriggerContext) -> String {
result = result.replace("{time}", &now.format("%H-%M-%S").to_string());
result = result.replace("{datetime}", &now.format("%Y-%m-%d_%H-%M-%S").to_string());
if let Some(title) = &ctx.window_title {
- result = result.replace("{window}", title);
+ result = result.replace("{window}", &sanitize_filename_component(title));
}
result
}
diff --git a/apps/desktop/src-tauri/src/automation.rs b/apps/desktop/src-tauri/src/automation.rs
index 7d238e257..4f7915e22 100644
--- a/apps/desktop/src-tauri/src/automation.rs
+++ b/apps/desktop/src-tauri/src/automation.rs
@@ -1,7 +1,7 @@
use cap_automation::{
AutomationExportCompression, AutomationHost, AutomationRecordingMode, AutomationsStore,
Capability, CaptureTargetKind, ClipboardSource, ExportDestination, ExportFormat, ExportProfile,
- Trigger, TriggerContext,
+ Trigger, TriggerContext, sanitize_filename_component,
};
use cap_recording::sources::screen_capture::ScreenCaptureTarget;
use clipboard_rs::Clipboard;
@@ -576,7 +576,7 @@ fn apply_filename_template(template: &str, ctx: &TriggerContext) -> String {
result = result.replace("{time}", &now.format("%H-%M-%S").to_string());
result = result.replace("{datetime}", &now.format("%Y-%m-%d_%H-%M-%S").to_string());
if let Some(ref title) = ctx.window_title {
- result = result.replace("{window}", title);
+ result = result.replace("{window}", &sanitize_filename_component(title));
}
result
}
diff --git a/crates/automation/src/lib.rs b/crates/automation/src/lib.rs
index f1b9fc39f..614f20649 100644
--- a/crates/automation/src/lib.rs
+++ b/crates/automation/src/lib.rs
@@ -422,6 +422,22 @@ pub fn load_store_from_json(value: &serde_json::Value) -> Option<AutomationsStor
.and_then(|v| serde_json::from_value(v.clone()).ok())
}
+/// Sanitize attacker-influenced text (e.g. an active window title) into a single, safe filename
+/// component before it is substituted into a `SaveToLocation` filename template. Window titles can
+/// contain path separators or reserved characters; substituting them raw would let a write escape the
+/// 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
+ .chars()
+ .map(|c| match c {
+ '/' | '\\' | ':' | '*' | '?' | '"' | '<' | '>' | '|' => '_',
+ other if other.is_control() => '_',
+ other => other,
+ })
+ .collect()
+}
+
/// Build a single shell command line from a program and its arguments, quoting each token so that
/// arguments containing spaces or shell metacharacters survive as written instead of being re-split
/// by the shell. Used by hosts running `RunCommand { use_shell: true }`.
diff --git a/crates/automation/src/tests.rs b/crates/automation/src/tests.rs
index 95f8e2d8e..be89562c5 100644
--- a/crates/automation/src/tests.rs
+++ b/crates/automation/src/tests.rs
@@ -765,6 +765,20 @@ fn shell_command_line_escapes_embedded_quotes() {
}
}
+#[test]
+fn sanitize_filename_component_neutralizes_traversal_and_reserved_chars() {
+ assert_eq!(
+ sanitize_filename_component("../../etc/passwd"),
+ ".._.._etc_passwd"
+ );
+ assert_eq!(sanitize_filename_component("C:\\Users\\me"), "C__Users_me");
+ assert_eq!(
+ sanitize_filename_component("Slack | #general"),
+ "Slack _ #general"
+ );
+ assert_eq!(sanitize_filename_component("plain title"), "plain title");
+}
+
#[test]
fn multiple_rules_same_trigger() {
let rule1 = AutomationRule {