Skip to content

armgit / jan

Public Imported from GitHub
0

Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.

fix(agent-tools,vector-db): refuse ':' in store and collection names on Windows only

James Dame2d4928f50a0
diff --git a/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs b/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs
index b7d5384dc..5fdfdaa68 100644
--- a/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs
+++ b/src-tauri/plugins/tauri-plugin-agent-tools/src/workspace.rs
@@ -774,10 +774,16 @@ pub fn thread_segment(thread_id: &str) -> Result<String, String> {
/// Sanitize a caller-supplied entry name into a safe `<stem>.md` filename.
/// Rejects path separators and `..` so the result can never escape the
-/// store directory, and `:` (on Windows `C:x` is drive-relative and replaces
-/// the base it is joined to), NUL and Windows device names on every OS so a
-/// name means the same file everywhere. `.md` is appended if absent.
+/// store directory, NUL and Windows device names on every OS, and on Windows a
+/// `:` (`C:x` is drive-relative and replaces the base it is joined to, `a:b`
+/// names a data stream). `.md` is appended if absent.
pub fn workspace_filename(name: &str) -> Result<String, String> {
+ workspace_filename_on(name, cfg!(windows))
+}
+
+/// [`workspace_filename`] with the Windows-only `:` rule as a flag, so both
+/// platforms' behaviour can be tested on any host.
+fn workspace_filename_on(name: &str, reject_colon: bool) -> Result<String, String> {
let trimmed = name.trim();
let stem = trimmed.strip_suffix(".md").unwrap_or(trimmed);
// Checked on the stem, not the input: otherwise a bare "." passes and the
@@ -786,7 +792,8 @@ pub fn workspace_filename(name: &str) -> Result<String, String> {
|| stem.contains('/')
|| stem.contains('\\')
|| stem.contains("..")
- || stem.contains([':', '\0'])
+ || stem.contains('\0')
+ || (reject_colon && stem.contains(':'))
|| stem.chars().all(|c| c == '.')
|| is_windows_device_name(stem)
{
@@ -1066,14 +1073,20 @@ mod tests {
}
#[test]
- fn filename_rejects_drive_prefixes_nul_and_device_names() {
- for bad in ["C:x", "c:", "a:b", "C:x.md", "a\0b", "nul", "CON.md", "com1", "aux.txt"] {
- assert!(
- workspace_filename(bad).is_err(),
- "expected {bad:?} to be rejected"
- );
+ fn filename_rejects_nul_device_names_and_windows_drive_prefixes() {
+ for bad in ["a\0b", "nul", "CON.md", "com1", "aux.txt"] {
+ for reject_colon in [true, false] {
+ assert!(workspace_filename_on(bad, reject_colon).is_err(), "{bad:?}");
+ }
assert!(crate::skills::safe_stem(bad).is_err(), "{bad:?}");
}
+ // `:` is refused on Windows only: elsewhere it is an ordinary character
+ // that existing names may already use.
+ for colon in ["C:x", "c:", "a:b", "C:x.md"] {
+ assert!(workspace_filename_on(colon, true).is_err(), "{colon:?} on Windows");
+ assert!(workspace_filename_on(colon, false).is_ok(), "{colon:?} elsewhere");
+ assert_eq!(workspace_filename(colon).is_err(), cfg!(windows), "{colon:?}");
+ }
assert_eq!(workspace_filename("console").unwrap(), "console.md");
}
diff --git a/src-tauri/plugins/tauri-plugin-vector-db/src/db.rs b/src-tauri/plugins/tauri-plugin-vector-db/src/db.rs
index 7f6c89301..6aef34a18 100644
--- a/src-tauri/plugins/tauri-plugin-vector-db/src/db.rs
+++ b/src-tauri/plugins/tauri-plugin-vector-db/src/db.rs
@@ -66,13 +66,22 @@ pub fn base_dir_in(data_root: &Path) -> PathBuf {
}
/// The database file for collection `name`, always a single file directly in
-/// `base`. Separators are folded to `_` as before; a `:` (a drive-relative path
-/// on Windows, which `push` would let replace `base`), a NUL, an empty name and
-/// a Windows device name are refused on every OS so a name means the same file
-/// everywhere.
+/// `base`. Separators are folded to `_` as before. An empty name, a NUL and a
+/// Windows device name are refused on every OS, and on Windows a `:` too
+/// (`C:x` is drive-relative, which `join` would let replace `base`).
pub fn collection_path(base: &Path, name: &str) -> Result<PathBuf, VectorDBError> {
+ collection_path_on(base, name, cfg!(windows))
+}
+
+/// [`collection_path`] with the Windows-only `:` rule as a flag, so both
+/// platforms' behaviour can be tested on any host.
+fn collection_path_on(base: &Path, name: &str, reject_colon: bool) -> Result<PathBuf, VectorDBError> {
let clean = name.replace(['/', '\\'], "_");
- if clean.is_empty() || clean.contains([':', '\0']) || is_windows_device_name(&clean) {
+ if clean.is_empty()
+ || clean.contains('\0')
+ || (reject_colon && clean.contains(':'))
+ || is_windows_device_name(&clean)
+ {
return Err(VectorDBError::InvalidInput(format!(
"invalid collection name '{name}'"
)));
@@ -795,8 +804,19 @@ mod tests {
base.join("attachments_01H8X.db")
);
assert_eq!(collection_path(base, "a/b\\c").unwrap(), base.join("a_b_c.db"));
- for bad in ["", "C:x", "c:", "a\0b", "nul", "CON.x", "com1"] {
- assert!(collection_path(base, bad).is_err(), "{bad:?}");
+ for bad in ["", "a\0b", "nul", "CON.x", "com1"] {
+ for reject_colon in [true, false] {
+ assert!(collection_path_on(base, bad, reject_colon).is_err(), "{bad:?}");
+ }
+ }
+ // `:` is refused on Windows only.
+ for colon in ["C:x", "c:", "a:b"] {
+ assert!(collection_path_on(base, colon, true).is_err(), "{colon:?} on Windows");
+ assert_eq!(
+ collection_path_on(base, colon, false).unwrap(),
+ base.join(format!("{colon}.db"))
+ );
+ assert_eq!(collection_path(base, colon).is_err(), cfg!(windows), "{colon:?}");
}
}