feat(connection): allow opting out of password storage

This commit is contained in:
Abeautifulsnow authored and GitHub committed 2026-08-12 19:34:00 +08:00
1 parent b8d37774fd
commit a029888725
47 files changed
+585 -12

No files matched your search

@@ -204,6 +204,7 @@ fn connection_config(id: &str, database: BenchDatabase) -> Result<ConnectionConf
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: Vec::new(),
+1
View File
@@ -684,6 +684,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -1291,6 +1291,7 @@ for line in sys.stdin:
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+3 -1
View File
@@ -511,8 +511,10 @@ pub async fn run_qoder_agent(
mod tests {
use super::{
build_qoder_command, classify_qoder_run_error, parse_qoder_jsonl_event, parse_qoder_models, qoder_cli_env,
qoder_process_env_with_path, QoderCommandSpec, QoderRunOptions,
QoderRunOptions,
};
#[cfg(not(windows))]
use super::{qoder_process_env_with_path, QoderCommandSpec};
use crate::agent_events::AgentEvent;
use crate::ai::{AiApiStyle, AiAuthMethod, AiConfig, AiEffortSelection, AiProvider, AiReasoningLevel};
+61 -8
View File
@@ -340,7 +340,7 @@ pub async fn apply_sync_snapshot(
storage.save_desktop_settings(&snapshot.desktop_settings).await?;
if let Some(payload) = &sensitive_payload {
clear_connection_secrets(storage, &connections).await?;
apply_sensitive_payload(storage, payload).await?;
apply_sensitive_payload(storage, payload, &connections).await?;
}
Ok(ApplySnapshotSummary { encrypted_secrets_present, secrets_applied: sensitive_payload.is_some() })
}
@@ -999,7 +999,10 @@ async fn build_sensitive_payload(
) -> Result<SensitiveSyncPayload, String> {
let mut connection_secrets = Vec::new();
for config in connections {
push_secret(&mut connection_secrets, &config.id, "password", &config.password);
// A transient password must not become durable through a sync snapshot.
if config.save_password {
push_secret(&mut connection_secrets, &config.id, "password", &config.password);
}
push_secret(&mut connection_secrets, &config.id, "init_script", config.init_script.as_deref().unwrap_or(""));
for (index, layer) in config.transport_layers.iter().enumerate() {
match layer {
@@ -1173,7 +1176,11 @@ fn scrub_json_secret(object: &mut serde_json::Map<String, serde_json::Value>, fi
}
}
async fn apply_sensitive_payload(storage: &Storage, payload: &SensitiveSyncPayload) -> Result<(), String> {
async fn apply_sensitive_payload(
storage: &Storage,
payload: &SensitiveSyncPayload,
connections: &[ConnectionConfig],
) -> Result<(), String> {
for secret in &payload.connection_secrets {
if !SECRET_KEYS.contains(&secret.key.as_str())
&& !secret.key.starts_with(SSH_TUNNEL_SECRET_PREFIX)
@@ -1181,6 +1188,13 @@ async fn apply_sensitive_payload(storage: &Storage, payload: &SensitiveSyncPaylo
{
continue;
}
// Metadata is applied before secrets. Never let an older encrypted snapshot
// restore a password after the user chose not to retain it locally.
if secret.key == "password"
&& connections.iter().any(|config| config.id == secret.connection_id && !config.save_password)
{
continue;
}
storage.set_secret(&secret.connection_id, &secret.key, &secret.secret).await?;
}
if let Some(configs) = &payload.ai_configs {
@@ -1744,6 +1758,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -1811,6 +1826,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -1962,6 +1978,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -2396,6 +2413,42 @@ mod tests {
}));
}
#[tokio::test]
async fn sync_restore_does_not_revive_password_when_connection_disables_saving() {
let source = Storage::open(&temp_db_path("sync-no-save-password-source")).await.unwrap();
source.save_connections(&[postgres_connection("pg", "remote-secret")]).await.unwrap();
let mut snapshot = build_sync_snapshot(&source, "test-version", None, Some("sync-pass")).await.unwrap();
snapshot.connections[0].save_password = false;
let target = Storage::open(&temp_db_path("sync-no-save-password-target")).await.unwrap();
apply_sync_snapshot(
&target,
&snapshot,
ApplySnapshotOptions { secrets_passphrase: Some("sync-pass"), restore_secrets: true },
)
.await
.unwrap();
assert_eq!(target.get_secret("pg", "password").await.unwrap(), None);
assert!(target.load_connections().await.unwrap()[0].password.is_empty());
}
#[tokio::test]
async fn metadata_only_sync_removes_existing_password_when_connection_disables_saving() {
let source = Storage::open(&temp_db_path("sync-no-save-password-metadata-source")).await.unwrap();
let mut source_connection = postgres_connection("pg", "unused");
source_connection.save_password = false;
source.save_connections(&[source_connection]).await.unwrap();
let snapshot = build_sync_snapshot(&source, "test-version", None, None).await.unwrap();
let target = Storage::open(&temp_db_path("sync-no-save-password-metadata-target")).await.unwrap();
target.save_connections(&[postgres_connection("pg", "local-secret")]).await.unwrap();
apply_sync_snapshot(&target, &snapshot, ApplySnapshotOptions::default()).await.unwrap();
assert_eq!(target.get_secret("pg", "password").await.unwrap(), None);
assert!(target.load_connections().await.unwrap()[0].password.is_empty());
}
#[tokio::test]
async fn saved_sync_passphrase_encrypts_nacos_auth_password_without_exposing_it() {
let storage = Storage::open(&temp_db_path("saved-sync-nacos-snapshot")).await.unwrap();
@@ -2470,7 +2523,7 @@ mod tests {
ai_config: None,
tunnel_profiles: None,
};
apply_sensitive_payload(&storage, &payload).await.unwrap();
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
let loaded = storage.load_ai_configs().await.unwrap();
assert!(loaded.is_empty(), "None → no configs written");
}
@@ -2490,7 +2543,7 @@ mod tests {
ai_config: None,
tunnel_profiles: None,
};
apply_sensitive_payload(&storage, &payload).await.unwrap();
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
let loaded = storage.load_ai_configs().await.unwrap();
assert!(loaded.is_empty(), "Some([]) → table cleared");
}
@@ -2515,7 +2568,7 @@ mod tests {
ai_config: None,
tunnel_profiles: None,
};
apply_sensitive_payload(&storage, &payload).await.unwrap();
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
let loaded = storage.load_ai_configs().await.unwrap();
assert_eq!(loaded.len(), 2);
let opencode = loaded.iter().find(|item| item.name == "synced").unwrap();
@@ -2548,9 +2601,9 @@ mod tests {
tunnel_profiles: None,
};
apply_sensitive_payload(&storage, &payload).await.unwrap();
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
// Reapplying the same old snapshot must not collide with the generated ID.
apply_sensitive_payload(&storage, &payload).await.unwrap();
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
let loaded = storage.load_ai_configs().await.unwrap();
assert_eq!(loaded.len(), 1);
+1
View File
@@ -5202,6 +5202,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -874,6 +874,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -5398,6 +5398,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+12
View File
@@ -170,6 +170,14 @@ pub struct ConnectionConfig {
pub jdbc_driver_paths: Vec<String>,
#[serde(default, skip_serializing_if = "is_false")]
pub one_time: bool,
/// Whether the database password may be persisted locally (SQLite
/// `connection_secrets`). When false, the `"password"` secret is never
/// written (or is deleted) and the user must type it on every connect.
/// Defaults to `true` so pre-existing saved connections keep current
/// behavior; a bare `#[serde(default)]` would upgrade them to "don't save"
/// and delete every stored password on the next save.
#[serde(default = "default_true")]
pub save_password: bool,
#[serde(default, skip_serializing_if = "is_false")]
pub read_only: bool,
/// Explicitly marks every database reachable through this connection as production.
@@ -691,6 +699,8 @@ struct ConnectionConfigData {
pub jdbc_driver_paths: Vec<String>,
#[serde(default)]
pub one_time: bool,
#[serde(default = "default_true")]
pub save_password: bool,
#[serde(default)]
pub read_only: bool,
#[serde(default)]
@@ -754,6 +764,7 @@ impl From<ConnectionConfigData> for ConnectionConfig {
jdbc_driver_class: data.jdbc_driver_class,
jdbc_driver_paths: data.jdbc_driver_paths,
one_time: data.one_time,
save_password: data.save_password,
read_only: data.read_only,
is_production: data.is_production,
production_databases: data.production_databases,
@@ -2425,6 +2436,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -229,6 +229,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -920,6 +920,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only,
is_production: false,
production_databases: Vec::new(),
+1
View File
@@ -333,6 +333,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+2
View File
@@ -403,6 +403,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: true,
is_production: false,
production_databases: Vec::new(),
@@ -477,6 +478,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: true,
is_production: false,
production_databases: Vec::new(),
+1
View File
@@ -688,6 +688,7 @@ mod tests {
informix_server: String::new(),
external_config: None,
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec!["prod_app".to_string()],
+2
View File
@@ -4788,6 +4788,7 @@ for line in sys.stdin:
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -6439,6 +6440,7 @@ for line in sys.stdin:
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -2819,6 +2819,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -190,6 +190,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+83 -1
View File
@@ -2222,7 +2222,14 @@ fn persist_connection_in_tx(tx: &rusqlite::Transaction<'_>, config: &ConnectionC
tx.execute("INSERT INTO connections (id, config_json) VALUES (?1, ?2)", params![config_id, json])
.map_err(|e| e.to_string())?;
persist_secret_in_tx(tx, &config.id, "password", &config.password)?;
if config.save_password {
persist_secret_in_tx(tx, &config.id, "password", &config.password)?;
} else {
// "Don't save password": write an empty value, which persist_secret_in_tx
// turns into a DELETE — the password secret is never persisted (and any
// previously stored secret is removed on this save).
persist_secret_in_tx(tx, &config.id, "password", "")?;
}
delete_secret_prefix_in_tx(tx, &config.id, TRANSPORT_LAYER_SECRET_PREFIX)?;
for (index, layer) in config.transport_layers.iter().enumerate() {
match layer {
@@ -2343,6 +2350,12 @@ impl Storage {
for config in &configs {
let config = config.canonicalized();
let config_id = config.id.clone();
if !config.save_password {
// Metadata-only imports/sync preserve existing secrets by default.
// This preference is an exception: retaining the old password would
// make a no-save connection silently authenticate without prompting.
persist_secret_in_tx(&tx, &config.id, "password", "")?;
}
let mut sanitized = config;
sanitized.password = String::new();
scrub_transport_layer_secrets(&mut sanitized);
@@ -4181,6 +4194,73 @@ mod tests {
let _ = std::fs::remove_file(path);
}
fn plain_connection(id: &str, password: &str) -> ConnectionConfig {
serde_json::from_value::<ConnectionConfig>(serde_json::json!({
"id": id,
"name": format!("conn {id}"),
"db_type": "postgres",
"host": "127.0.0.1",
"port": 5432,
"username": "postgres",
"password": password,
"database": "app"
}))
.unwrap()
}
#[tokio::test]
async fn save_connections_does_not_persist_password_when_save_password_false() {
let path = temp_db_path("save-password-false");
let storage = Storage::open(&path).await.unwrap();
let mut config = plain_connection("no-save", "hunter2");
config.save_password = false;
storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
assert_eq!(storage.get_secret(&config.id, "password").await.unwrap(), None);
let loaded = storage.load_connections().await.unwrap();
assert_eq!(loaded.len(), 1);
assert_eq!(loaded[0].password, "");
assert!(!loaded[0].save_password);
let _ = std::fs::remove_file(path);
}
#[tokio::test]
async fn save_connections_persists_password_when_save_password_true() {
let path = temp_db_path("save-password-true");
let storage = Storage::open(&path).await.unwrap();
let config = plain_connection("save-yes", "hunter2");
storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
assert_eq!(storage.get_secret(&config.id, "password").await.unwrap().as_deref(), Some("hunter2"));
let loaded = storage.load_connections().await.unwrap();
assert_eq!(loaded[0].password, "hunter2");
assert!(loaded[0].save_password);
let _ = std::fs::remove_file(path);
}
#[tokio::test]
async fn switching_save_password_off_removes_stored_password() {
let path = temp_db_path("save-password-switch-off");
let storage = Storage::open(&path).await.unwrap();
let mut config = plain_connection("switch", "hunter2");
storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
assert_eq!(storage.get_secret(&config.id, "password").await.unwrap().as_deref(), Some("hunter2"));
config.save_password = false;
storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
assert_eq!(storage.get_secret(&config.id, "password").await.unwrap(), None);
let loaded = storage.load_connections().await.unwrap();
assert_eq!(loaded[0].password, "");
assert!(!loaded[0].save_password);
let _ = std::fs::remove_file(path);
}
fn mq_connection(id: &str, token: &str) -> ConnectionConfig {
ConnectionConfig {
docs_notes_path: None,
@@ -4240,6 +4320,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -4307,6 +4388,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -4296,6 +4296,7 @@ async fn flush_sqlite_append_transaction(
Ok(rows_imported.saturating_add(rows))
}
#[allow(clippy::too_many_arguments)]
async fn finish_sqlite_append_transaction<F>(
state: &AppState,
pool_key: &str,
+2 -1
View File
@@ -4098,7 +4098,7 @@ fn transfer_ddl_statements(sql: &str, db_type: &DatabaseType) -> Vec<String> {
.collect()
}
} else if matches!(db_type, DatabaseType::Dameng) {
let statements = split_sql_statements_for_database(sql, db_type.clone());
let statements = split_sql_statements_for_database(sql, *db_type);
if statements.is_empty() {
vec![sql.trim().to_string()]
} else {
@@ -10338,6 +10338,7 @@ SELECT 1 FROM dual"#
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -134,6 +134,7 @@ fn postgres_test_config(id: &str, port: u16) -> ConnectionConfig {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -71,6 +71,7 @@ fn live_postgres_config(
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -67,6 +67,7 @@ fn live_postgres_config(
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -62,6 +62,7 @@ fn live_postgres_config(
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -67,6 +67,7 @@ fn live_postgres_config(
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -59,6 +59,7 @@ fn postgres_test_config(id: &str, database: &str) -> ConnectionConfig {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -68,6 +68,7 @@ fn live_sqlserver_config(id: &str, database: &str) -> dbx_core::models::connecti
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
@@ -57,6 +57,7 @@ fn live_sqlserver_config(id: &str, database: &str) -> dbx_core::models::connecti
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1 -1
View File
@@ -1,7 +1,7 @@
use std::{ffi::OsString, sync::Arc};
use dbx_core::{models::connection::ConnectionConfig, storage::Storage};
use dbx_mcp::{DbxMcpServer, LocalBackend, McpScope};
use dbx_mcp::{DbxBackend, DbxMcpServer, LocalBackend, McpScope};
use rmcp::{model::CallToolRequestParams, ServiceExt};
use serde_json::{json, Map, Value};
use tempfile::tempdir;
+1
View File
@@ -589,6 +589,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],
+1
View File
@@ -577,6 +577,7 @@ mod tests {
jdbc_driver_class: None,
jdbc_driver_paths: Vec::new(),
one_time: false,
save_password: true,
read_only: false,
is_production: false,
production_databases: vec![],