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): splice URL passwords by position, keep env URL passwords out of prose, block hide_secrets in /vibe-setting (#9107)

thinhlpgdd895f48a80a
diff --git a/src-tauri/src/core/agent/secrets.rs b/src-tauri/src/core/agent/secrets.rs
index 2284fa88c..ef55b323b 100644
--- a/src-tauri/src/core/agent/secrets.rs
+++ b/src-tauri/src/core/agent/secrets.rs
@@ -237,9 +237,21 @@ impl SecretFilter {
.re;
for (name, value) in vars {
for caps in url_password.captures_iter(value) {
- if let Some(m) = caps.get(1) {
- push(label_of(name), m.as_str());
+ let (Some(whole), Some(password)) = (caps.get(0), caps.get(1)) else {
+ continue;
+ };
+ // `postgres://postgres:postgres@db`: a password that is also
+ // the scheme or the user name would turn every "postgres" in
+ // the prompt into a placeholder. The URL shape still hides it
+ // where it sits in a URL.
+ let user = whole.as_str().trim_start_matches("://");
+ let user = user.split(':').next().unwrap_or("");
+ let before = &value[..whole.start()];
+ let scheme = before.rsplit(|c: char| !c.is_ascii_alphanumeric() && c != '+' && c != '-' && c != '.').next().unwrap_or("");
+ if password.as_str() == user || password.as_str().eq_ignore_ascii_case(scheme) {
+ continue;
}
+ push(label_of(name), password.as_str());
}
}
literals.sort_by_key(|(_, value)| std::cmp::Reverse(value.len()));
@@ -306,8 +318,12 @@ impl SecretFilter {
match caps.get(shape.group) {
Some(hidden) => {
let placeholder = self.placeholder(shape.label, hidden.as_str());
- // Keep the text around the hidden group (`://user:`).
- whole.replacen(hidden.as_str(), &placeholder, 1)
+ // Keep the text around the hidden group (`://user:`),
+ // splicing at its position: the same text can appear
+ // earlier in the match (`guest:guest@`).
+ let start = hidden.start() - caps.get(0).expect("group 0").start();
+ let end = hidden.end() - caps.get(0).expect("group 0").start();
+ format!("{}{}{}", &whole[..start], placeholder, &whole[end..])
}
None => whole.to_string(),
}
@@ -682,6 +698,35 @@ mod tests {
assert_eq!(restore(&once), text);
}
+ #[test]
+ fn a_password_equal_to_the_user_name_is_hidden_at_its_own_position() {
+ let f = filter(&[]);
+ let out = f.hide("amqp://guest:guest@host/vhost");
+ assert!(out.starts_with("amqp://guest:$$URLPASSWORD_"), "the user stays, the password goes: {out}");
+ assert!(out.ends_with("$$@host/vhost"), "{out}");
+ assert_eq!(restore(&out), "amqp://guest:guest@host/vhost");
+ }
+
+ #[test]
+ fn an_env_url_password_that_is_a_common_word_does_not_rewrite_the_prompt() {
+ for url in [
+ "postgres://postgres:[email protected]:5432/x",
+ "redis://default:[email protected]:6379",
+ ] {
+ let f = filter(&[("DATABASE_URL", url)]);
+ let prose = "the postgres docs say default settings apply; run postgres and redis default";
+ assert_eq!(f.hide(prose), prose, "{url}: ordinary words survive");
+ let hidden = f.hide(&format!("dsn {url}"));
+ let password = url.split(':').nth(2).unwrap().split('@').next().unwrap();
+ assert!(hidden.contains("$$URLPASSWORD_"), "{hidden}");
+ assert!(!hidden.contains(&format!(":{password}@")), "{url} leaked: {hidden}");
+ assert_eq!(restore(&hidden), format!("dsn {url}"));
+ }
+ // A distinct password is still hidden where it appears alone.
+ let f = filter(&[("DATABASE_URL", "postgres://app:hunter2hunter2@db/x")]);
+ assert!(!f.hide("password is hunter2hunter2").contains("hunter2hunter2"));
+ }
+
#[test]
fn restore_runs_until_no_known_placeholder_is_left() {
let f = filter(&[("MY_SECRET", "inner-secret-value")]);
diff --git a/src-tauri/src/core/cli/tui/vibe_setting.rs b/src-tauri/src/core/cli/tui/vibe_setting.rs
index b82f69242..5b04e3912 100644
--- a/src-tauri/src/core/cli/tui/vibe_setting.rs
+++ b/src-tauri/src/core/cli/tui/vibe_setting.rs
@@ -24,9 +24,10 @@ use super::{
};
/// `/settings` keys `/vibe-setting` may not propose. `claude_code_alias` decides
-/// whether Jan reuses another tool's keychain login: a credential decision, and
-/// the issue rules those out of anything the model can change.
-const EXCLUDED_KEYS: &[&str] = &["claude_code_alias"];
+/// whether Jan reuses another tool's keychain login, and `hide_secrets` whether
+/// credentials reach the provider: credential decisions, which the issue rules
+/// out of anything the model (or text it read) can change.
+const EXCLUDED_KEYS: &[&str] = &["claude_code_alias", "hide_secrets"];
/// `/settings` keys that `/reload config` re-applies to the running session
/// (see `reload_config` in tui.rs), so a written one needs no restart. Pinned
@@ -693,6 +694,24 @@ mod tests {
assert!(text.contains("Apply? [y/N]"), "{text}");
}
+ /// The privacy filter is a credential decision too: the model is never shown
+ /// the row, and a proposal for it is refused.
+ #[test]
+ fn hide_secrets_cannot_be_proposed() {
+ let body = build_request("m", "stop hiding secrets", &|_| None);
+ let system = body["messages"][0]["content"].as_str().unwrap();
+ assert!(!system.contains("hide_secrets"), "{system}");
+ match interpret_reply(r#"{"changes":[{"key":"hide_secrets","new_value":false}]}"#, &unset)
+ .expect("a valid reply")
+ {
+ VibeOutcome::Nothing { refused, .. } => assert!(
+ refused.join("\n").contains("hide_secrets: credential settings are never changed"),
+ "{refused:?}"
+ ),
+ _ => panic!("hide_secrets must not be proposed"),
+ }
+ }
+
/// Every value goes through the `/settings` parser: out of range, wrong
/// type, unknown key, credential key and duplicates are refused with a
/// reason, and the valid rest is still proposed.