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(server): validate assistant_id before loading assistant config

James Damdc0a7fcac846
diff --git a/src-tauri/src/core/agent/upstream.rs b/src-tauri/src/core/agent/upstream.rs
index 4635f4b6e..eb58aa907 100644
--- a/src-tauri/src/core/agent/upstream.rs
+++ b/src-tauri/src/core/agent/upstream.rs
@@ -31,20 +31,47 @@ use crate::core::{
state::{ProviderConfig, SharedMcpServers},
};
-fn assistant_json_path(jan_data_folder: &str, assistant_id: &str) -> PathBuf {
- PathBuf::from(jan_data_folder)
- .join("assistants")
- .join(assistant_id)
- .join("assistant.json")
+/// Assistant ids are generated by the app (`jan`, or a short base36 string),
+/// so anything outside `[A-Za-z0-9_-]` is not an id this app wrote. Rejecting
+/// it keeps a caller-supplied id from naming a path outside `assistants/`.
+pub(crate) fn is_valid_assistant_id(assistant_id: &str) -> bool {
+ !assistant_id.is_empty()
+ && assistant_id.len() <= 128
+ && assistant_id
+ .bytes()
+ .all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'-')
+}
+
+/// Resolves `<data>/assistants/<id>/assistant.json`, refusing ids that are not
+/// plain names and any resolved file (e.g. via a symlink) outside the
+/// assistants directory. Errors never include the filesystem path.
+pub(crate) fn assistant_json_path(
+ jan_data_folder: &str,
+ assistant_id: &str,
+) -> Result<PathBuf, String> {
+ if !is_valid_assistant_id(assistant_id) {
+ return Err("Invalid assistant_id".to_string());
+ }
+ let assistants_dir = PathBuf::from(jan_data_folder).join("assistants");
+ let path = assistants_dir.join(assistant_id).join("assistant.json");
+ let not_found = || format!("Assistant not found: {assistant_id}");
+ let root = assistants_dir.canonicalize().map_err(|_| not_found())?;
+ let resolved = path.canonicalize().map_err(|_| not_found())?;
+ if !resolved.starts_with(&root) {
+ return Err(not_found());
+ }
+ Ok(resolved)
}
pub(crate) fn load_assistant_config(
jan_data_folder: &str,
assistant_id: &str,
) -> Result<(Option<String>, Option<String>), String> {
- let assistant_path = assistant_json_path(jan_data_folder, assistant_id);
- let raw = fs::read_to_string(&assistant_path)
- .map_err(|e| format!("Failed to read assistant.json: {assistant_path:?}: {e}"))?;
+ let assistant_path = assistant_json_path(jan_data_folder, assistant_id)?;
+ let raw = fs::read_to_string(&assistant_path).map_err(|e| {
+ log::warn!("Failed to read assistant.json for {assistant_id}: {e}");
+ format!("Failed to read assistant config: {assistant_id}")
+ })?;
let parsed: serde_json::Value = serde_json::from_str(&raw)
.map_err(|e| format!("Invalid assistant.json ({assistant_id}): {e}"))?;
@@ -1912,6 +1939,85 @@ mod tests {
use super::*;
use serde_json::json;
+ #[test]
+ fn assistant_id_charset() {
+ for ok in ["jan", "k3x9q2", "my_assistant-2"] {
+ assert!(is_valid_assistant_id(ok), "{ok}");
+ }
+ for bad in [
+ "",
+ "..",
+ "../x",
+ "../../etc",
+ "/abs",
+ "/etc/passwd",
+ "\\\\host\\share",
+ "C:\\Windows",
+ "a/b",
+ "a.b",
+ "a b",
+ "jan\0",
+ ] {
+ assert!(!is_valid_assistant_id(bad), "{bad:?}");
+ }
+ assert!(!is_valid_assistant_id(&"a".repeat(129)));
+ }
+
+ #[test]
+ fn load_assistant_config_rejects_paths_and_hides_data_folder() {
+ let data = tempfile::tempdir().unwrap();
+ let assistants = data.path().join("assistants");
+ fs::create_dir_all(assistants.join("jan")).unwrap();
+ fs::write(
+ assistants.join("jan").join("assistant.json"),
+ r#"{"instructions":"hi","model":"m"}"#,
+ )
+ .unwrap();
+ // A readable assistant.json outside the assistants dir.
+ fs::create_dir_all(data.path().join("outside")).unwrap();
+ fs::write(
+ data.path().join("outside").join("assistant.json"),
+ r#"{"instructions":"leak"}"#,
+ )
+ .unwrap();
+ let root = data.path().to_str().unwrap();
+
+ assert_eq!(
+ load_assistant_config(root, "jan").unwrap(),
+ (Some("hi".to_string()), Some("m".to_string()))
+ );
+
+ let outside_abs = data.path().join("outside");
+ for bad in [
+ "../outside",
+ outside_abs.to_str().unwrap(),
+ "\\\\host\\share",
+ "missing",
+ ] {
+ let err = load_assistant_config(root, bad).unwrap_err();
+ assert!(!err.contains(root), "error leaks the data folder: {err}");
+ assert!(!err.contains("leak"));
+ }
+ }
+
+ #[cfg(unix)]
+ #[test]
+ fn assistant_symlink_out_of_assistants_dir_is_refused() {
+ let data = tempfile::tempdir().unwrap();
+ let assistants = data.path().join("assistants");
+ fs::create_dir_all(&assistants).unwrap();
+ fs::create_dir_all(data.path().join("outside")).unwrap();
+ fs::write(
+ data.path().join("outside").join("assistant.json"),
+ r#"{"instructions":"leak"}"#,
+ )
+ .unwrap();
+ std::os::unix::fs::symlink(data.path().join("outside"), assistants.join("linked"))
+ .unwrap();
+ let root = data.path().to_str().unwrap();
+ assert!(load_assistant_config(root, "linked").is_err());
+ }
+
#[test]
fn compaction_summary_survives_a_system_prompt_update() {
let marker = crate::core::agent::compaction::SUMMARY_MARKER;