4303 lines
187 KiB
Rust
4303 lines
187 KiB
Rust
use aes_gcm::{
|
|
aead::{rand_core::RngCore, Aead, OsRng},
|
|
Aes256Gcm, KeyInit, Nonce,
|
|
};
|
|
use argon2::{Algorithm, Argon2, Params, Version};
|
|
use base64::{engine::general_purpose::STANDARD as BASE64, engine::general_purpose::URL_SAFE_NO_PAD, Engine as _};
|
|
use chrono::Utc;
|
|
use reqwest::{header, Client, Method, StatusCode, Url};
|
|
use serde::{Deserialize, Serialize};
|
|
use sha2::{Digest, Sha256};
|
|
use std::collections::HashSet;
|
|
use std::net::{IpAddr, Ipv4Addr};
|
|
|
|
use crate::ai::AiConfigItem;
|
|
use crate::connection_secrets::{
|
|
plugin_connection_secret_key, CASSANDRA_KEYSTORE_PASSWORD_KEY, CASSANDRA_TRUSTSTORE_PASSWORD_KEY,
|
|
MQTT_AUTH_PASSWORD_KEY, MQ_AUTH_API_KEY_VALUE_KEY, MQ_AUTH_CLIENT_SECRET_KEY, MQ_AUTH_PASSWORD_KEY,
|
|
MQ_AUTH_TOKEN_KEY, MQ_TOKEN_SIGNING_KEY, NACOS_AUTH_PASSWORD_KEY, NACOS_RNACOS_CONSOLE_PASSWORD_KEY,
|
|
PLUGIN_CONNECTION_SECRET_PREFIX,
|
|
};
|
|
use crate::models::connection::{ConnectionConfig, DatabaseType, TransportLayerConfig};
|
|
use crate::saved_sql::SavedSqlLibrary;
|
|
use crate::storage::{
|
|
DesktopSettings, SnippetPendingCleanup, Storage, SyncImportCredential, SyncImportPlan, SyncImportSecret,
|
|
};
|
|
|
|
/// Version 2 introduces an explicit, versioned secrets transport payload. We
|
|
/// still accept version 1 snapshots on import so upgrading does not strand
|
|
/// existing devices.
|
|
const SNAPSHOT_SCHEMA_VERSION: u32 = 2;
|
|
const LEGACY_SNAPSHOT_SCHEMA_VERSION: u32 = 1;
|
|
const SENSITIVE_PAYLOAD_VERSION: u32 = 2;
|
|
const SENSITIVE_PAYLOAD_TYPE: &str = "dbx-sync-secrets";
|
|
const ENCRYPTED_SNIPPET_SNAPSHOT_FORMAT: &str = "dbx-encrypted-sync-snapshot";
|
|
const ENCRYPTED_SNIPPET_SNAPSHOT_VERSION: u32 = 1;
|
|
const DEFAULT_REMOTE_PATH: &str = "DBX/sync/snapshot.json";
|
|
const DEFAULT_SNIPPET_FILE_NAME: &str = "dbx-sync.json";
|
|
const GITHUB_API_BASE: &str = "https://api.github.com";
|
|
const GITEE_API_BASE: &str = "https://gitee.com/api/v5";
|
|
const GITLAB_DEFAULT_INSTANCE: &str = "https://gitlab.com";
|
|
const SECRET_KEYS: &[&str] = &[
|
|
"password",
|
|
"ssh_password",
|
|
"ssh_key_passphrase",
|
|
"proxy_password",
|
|
"redis_sentinel_password",
|
|
"connection_string",
|
|
"url_params",
|
|
"init_script",
|
|
MQ_AUTH_TOKEN_KEY,
|
|
MQ_AUTH_PASSWORD_KEY,
|
|
MQ_AUTH_API_KEY_VALUE_KEY,
|
|
MQ_AUTH_CLIENT_SECRET_KEY,
|
|
MQ_TOKEN_SIGNING_KEY,
|
|
MQTT_AUTH_PASSWORD_KEY,
|
|
NACOS_AUTH_PASSWORD_KEY,
|
|
NACOS_RNACOS_CONSOLE_PASSWORD_KEY,
|
|
CASSANDRA_TRUSTSTORE_PASSWORD_KEY,
|
|
CASSANDRA_KEYSTORE_PASSWORD_KEY,
|
|
];
|
|
const SSH_TUNNEL_SECRET_PREFIX: &str = "ssh_tunnels.";
|
|
const TRANSPORT_LAYER_SECRET_PREFIX: &str = "transport_layers.";
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct WebDavConfig {
|
|
pub endpoint: String,
|
|
pub username: Option<String>,
|
|
pub password: Option<String>,
|
|
pub remote_path: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
|
|
pub enum SnippetProvider {
|
|
#[serde(rename = "github", alias = "git_hub")]
|
|
GitHub,
|
|
#[serde(rename = "gitee")]
|
|
Gitee,
|
|
#[serde(rename = "gitlab")]
|
|
GitLab,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SnippetSyncConfig {
|
|
pub provider: SnippetProvider,
|
|
#[serde(default)]
|
|
pub instance_url: Option<String>,
|
|
pub token: Option<String>,
|
|
pub snippet_id: Option<String>,
|
|
/// Explicitly requested one-time migration for a legacy plaintext snippet.
|
|
/// The remote plaintext snippet is only deleted after a new encrypted one
|
|
/// has been created successfully.
|
|
#[serde(default)]
|
|
pub replace_legacy_snippet: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SnippetTokenStatus {
|
|
pub has_saved_token: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SnippetSyncSettings {
|
|
pub snippet_id: Option<String>,
|
|
#[serde(default)]
|
|
pub legacy_cleanup_required_id: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct WebDavPasswordStatus {
|
|
pub has_saved_password: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct WebDavSyncSecretsStatus {
|
|
pub enabled: bool,
|
|
pub has_saved_passphrase: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SyncSnapshot {
|
|
pub schema_version: u32,
|
|
pub exported_at: String,
|
|
pub app_version: String,
|
|
pub connections: Vec<ConnectionConfig>,
|
|
/// Explicit MQTT subscription metadata. `None` means this is a legacy
|
|
/// snapshot that predates MQTT subscription sync and must not clear local
|
|
/// subscriptions when applied.
|
|
#[serde(default)]
|
|
pub mqtt_subscriptions: Option<Vec<MqttSubscriptionSyncEntry>>,
|
|
/// Shared tunnel profiles (secrets scrubbed). `None` means the snapshot
|
|
/// predates tunnel profiles — applying it leaves local profiles alone.
|
|
#[serde(default)]
|
|
pub tunnel_profiles: Option<Vec<TransportLayerConfig>>,
|
|
pub sidebar_layout: Option<serde_json::Value>,
|
|
pub pinned_tree_node_ids: Vec<String>,
|
|
pub saved_sql: SavedSqlLibrary,
|
|
pub desktop_settings: DesktopSettings,
|
|
pub editor_settings: Option<serde_json::Value>,
|
|
pub encrypted_secrets: Option<EncryptedSecretsBlob>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct MqttSubscriptionSyncEntry {
|
|
pub connection_id: String,
|
|
pub subscriptions: Vec<MqttSubscriptionSyncTopic>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct MqttSubscriptionSyncTopic {
|
|
pub topic: String,
|
|
#[serde(default = "default_sync_qos")]
|
|
pub qos: String,
|
|
#[serde(default)]
|
|
pub no_local: bool,
|
|
#[serde(default = "default_sync_enabled")]
|
|
pub enabled: bool,
|
|
}
|
|
|
|
fn default_sync_qos() -> String {
|
|
"atmostonce".to_string()
|
|
}
|
|
|
|
fn default_sync_enabled() -> bool {
|
|
true
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct EncryptedSecretsBlob {
|
|
pub version: u32,
|
|
pub kdf: String,
|
|
pub cipher: String,
|
|
pub salt: String,
|
|
pub nonce: String,
|
|
pub ciphertext: String,
|
|
/// Present for the v2 sync transport envelope. Optional for backwards
|
|
/// compatibility with v1 blobs used by local saved credentials.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub payload_type: Option<String>,
|
|
/// Context authenticated as AES-GCM additional authenticated data.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub aad: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
struct EncryptedSnippetSnapshot {
|
|
format: String,
|
|
version: u32,
|
|
payload: EncryptedSecretsBlob,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SensitiveSyncPayload {
|
|
pub connection_secrets: Vec<ConnectionSecretSnapshot>,
|
|
/// Whether plugin connection secrets are authoritative in this payload.
|
|
/// Older payloads did not carry this marker and always included plugin
|
|
/// secrets when secrets were enabled, so the compatibility default is true.
|
|
#[serde(default = "default_plugin_secrets_included")]
|
|
pub plugin_secrets_included: bool,
|
|
/// WebDAV and GitHub/Gitee credentials are included only inside the
|
|
/// passphrase-protected payload. They are re-wrapped with the destination
|
|
/// device key on import and never appear in the public snapshot.
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
pub sync_credentials: Option<Vec<SyncCredentialSnapshot>>,
|
|
// None = legacy snapshot (fall through to ai_config migration),
|
|
// Some(vec) = explicit state (empty vec means all configs were deleted)
|
|
pub ai_configs: Option<Vec<AiConfigItem>>,
|
|
/// Legacy field, used only for deserializing old data; not serialized
|
|
#[serde(default, skip_serializing)]
|
|
pub ai_config: Option<crate::ai::AiConfig>,
|
|
/// Full tunnel profiles including their secrets.
|
|
#[serde(default)]
|
|
pub tunnel_profiles: Option<Vec<TransportLayerConfig>>,
|
|
}
|
|
|
|
fn default_plugin_secrets_included() -> bool {
|
|
true
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SyncCredentialSnapshot {
|
|
pub account: String,
|
|
pub secret: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ConnectionSecretSnapshot {
|
|
pub connection_id: String,
|
|
pub key: String,
|
|
pub secret: String,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
pub struct ApplySnapshotOptions<'a> {
|
|
pub secrets_passphrase: Option<&'a str>,
|
|
/// Whether encrypted secrets in the remote snapshot may replace local
|
|
/// secrets. Metadata is always applied, but callers can explicitly keep
|
|
/// device-local credentials while restoring the rest of a snapshot.
|
|
pub restore_secrets: bool,
|
|
}
|
|
|
|
/// Controls which sensitive values are placed in a sync snapshot. A
|
|
/// passphrase is intentionally not enough to opt in: callers must explicitly
|
|
/// set `include_secrets`, which prevents an accidental upload of credentials
|
|
/// when a saved passphrase happens to be available.
|
|
#[derive(Debug, Clone, Copy, Default)]
|
|
pub struct SyncExportOptions<'a> {
|
|
pub include_secrets: bool,
|
|
pub sync_passphrase: Option<&'a str>,
|
|
pub include_ai_secrets: bool,
|
|
pub include_tunnel_secrets: bool,
|
|
pub include_plugin_secrets: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct ApplySnapshotSummary {
|
|
pub encrypted_secrets_present: bool,
|
|
pub secrets_applied: bool,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct WebDavSyncSummary {
|
|
pub remote_path: String,
|
|
pub bytes: usize,
|
|
pub exported_at: Option<String>,
|
|
pub app_version: Option<String>,
|
|
}
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
#[serde(rename_all = "camelCase")]
|
|
pub struct SnippetSyncSummary {
|
|
pub provider: SnippetProvider,
|
|
pub snippet_id: String,
|
|
pub bytes: usize,
|
|
pub exported_at: Option<String>,
|
|
pub app_version: Option<String>,
|
|
/// A new encrypted snippet was created, but the old plaintext snippet
|
|
/// could not be removed. The caller must surface this id for manual cleanup.
|
|
#[serde(default)]
|
|
pub legacy_cleanup_required_id: Option<String>,
|
|
/// Internal guard for a later legacy cleanup. It is deliberately omitted
|
|
/// from the API response so remote snapshot content never leaves the
|
|
/// process through a status payload.
|
|
#[serde(skip)]
|
|
legacy_cleanup_expected_content_hash: Option<String>,
|
|
}
|
|
|
|
pub async fn build_sync_snapshot(
|
|
storage: &Storage,
|
|
app_version: impl Into<String>,
|
|
editor_settings: Option<serde_json::Value>,
|
|
secrets_passphrase: Option<&str>,
|
|
) -> Result<SyncSnapshot, String> {
|
|
// Keep the historical API source-compatible. Existing callers that pass
|
|
// a non-empty passphrase have always explicitly requested a secrets sync;
|
|
// new callers should use `build_sync_snapshot_with_options` so the intent
|
|
// is represented directly.
|
|
let include_secrets = normalized_passphrase(secrets_passphrase).is_some();
|
|
build_sync_snapshot_with_options(
|
|
storage,
|
|
app_version,
|
|
editor_settings,
|
|
SyncExportOptions {
|
|
include_secrets,
|
|
sync_passphrase: secrets_passphrase,
|
|
include_ai_secrets: include_secrets,
|
|
include_tunnel_secrets: include_secrets,
|
|
include_plugin_secrets: include_secrets,
|
|
},
|
|
)
|
|
.await
|
|
}
|
|
|
|
pub async fn build_sync_snapshot_with_options(
|
|
storage: &Storage,
|
|
app_version: impl Into<String>,
|
|
editor_settings: Option<serde_json::Value>,
|
|
options: SyncExportOptions<'_>,
|
|
) -> Result<SyncSnapshot, String> {
|
|
let mut connections = storage.load_connections().await?;
|
|
let mut tunnel_profiles = storage.load_tunnel_profiles().await?;
|
|
let encrypted_secrets = if options.include_secrets {
|
|
let passphrase = normalized_passphrase(options.sync_passphrase)
|
|
.ok_or_else(|| "A sync password is required when including synced secrets.".to_string())?;
|
|
let payload = build_sensitive_payload_with_options(storage, &connections, &tunnel_profiles, options).await?;
|
|
Some(encrypt_sensitive_payload(&payload, passphrase)?)
|
|
} else {
|
|
None
|
|
};
|
|
let mqtt_subscriptions = Some(extract_mqtt_subscriptions(&connections)?);
|
|
for config in &mut connections {
|
|
scrub_connection_secrets(config);
|
|
}
|
|
for profile in &mut tunnel_profiles {
|
|
profile.scrub_secrets();
|
|
}
|
|
|
|
Ok(SyncSnapshot {
|
|
schema_version: SNAPSHOT_SCHEMA_VERSION,
|
|
exported_at: Utc::now().to_rfc3339(),
|
|
app_version: app_version.into(),
|
|
connections,
|
|
mqtt_subscriptions,
|
|
tunnel_profiles: Some(tunnel_profiles),
|
|
sidebar_layout: storage.load_sidebar_layout().await?,
|
|
pinned_tree_node_ids: storage.load_pinned_tree_node_ids().await?,
|
|
saved_sql: storage.load_saved_sql_library().await?,
|
|
desktop_settings: storage.load_desktop_settings().await?,
|
|
editor_settings,
|
|
encrypted_secrets,
|
|
})
|
|
}
|
|
|
|
pub async fn build_sync_snapshot_with_saved_secrets(
|
|
storage: &Storage,
|
|
app_version: impl Into<String>,
|
|
editor_settings: Option<serde_json::Value>,
|
|
secrets_passphrase: Option<&str>,
|
|
) -> Result<SyncSnapshot, String> {
|
|
match normalized_passphrase(secrets_passphrase) {
|
|
Some(passphrase) => {
|
|
build_sync_snapshot_with_options(
|
|
storage,
|
|
app_version,
|
|
editor_settings,
|
|
SyncExportOptions {
|
|
include_secrets: true,
|
|
sync_passphrase: Some(passphrase),
|
|
include_ai_secrets: true,
|
|
include_tunnel_secrets: true,
|
|
include_plugin_secrets: true,
|
|
},
|
|
)
|
|
.await
|
|
}
|
|
None => {
|
|
let saved_passphrase = resolve_webdav_sync_secrets_passphrase(storage).await?;
|
|
build_sync_snapshot_with_options(
|
|
storage,
|
|
app_version,
|
|
editor_settings,
|
|
SyncExportOptions {
|
|
include_secrets: saved_passphrase.is_some(),
|
|
sync_passphrase: saved_passphrase.as_deref(),
|
|
include_ai_secrets: saved_passphrase.is_some(),
|
|
include_tunnel_secrets: saved_passphrase.is_some(),
|
|
include_plugin_secrets: saved_passphrase.is_some(),
|
|
},
|
|
)
|
|
.await
|
|
}
|
|
}
|
|
}
|
|
|
|
pub async fn apply_sync_snapshot(
|
|
storage: &Storage,
|
|
snapshot: &SyncSnapshot,
|
|
options: ApplySnapshotOptions<'_>,
|
|
) -> Result<ApplySnapshotSummary, String> {
|
|
if !matches!(snapshot.schema_version, LEGACY_SNAPSHOT_SCHEMA_VERSION | SNAPSHOT_SCHEMA_VERSION) {
|
|
return Err(format!("Unsupported sync snapshot schema version: {}", snapshot.schema_version));
|
|
}
|
|
validate_sync_snapshot_metadata(snapshot)?;
|
|
|
|
let encrypted_secrets_present = snapshot.encrypted_secrets.is_some();
|
|
let mut sensitive_payload =
|
|
match (options.restore_secrets, &snapshot.encrypted_secrets, normalized_passphrase(options.secrets_passphrase))
|
|
{
|
|
(true, Some(blob), Some(passphrase)) => Some(decrypt_sensitive_payload(blob, passphrase)?),
|
|
// Restore intent is explicit. Do not silently leave a user with a
|
|
// partial restore when the remote snapshot contains secrets.
|
|
(true, Some(_), None) => return Err("A sync password is required to restore synced secrets.".to_string()),
|
|
_ => None,
|
|
};
|
|
// Version-1 snapshots could carry hydrated credentials directly in the
|
|
// public connection JSON. Treat an explicit secret restore as the
|
|
// migration consent: extract those values before scrubbing metadata, then
|
|
// send them through the same destination SecretStore transaction used by
|
|
// modern encrypted payloads. With restore disabled, metadata still
|
|
// imports but legacy plaintext credentials are discarded.
|
|
if sensitive_payload.is_none() && options.restore_secrets && snapshot.encrypted_secrets.is_none() {
|
|
let tunnel_profiles = snapshot.tunnel_profiles.clone().unwrap_or_default();
|
|
let has_legacy_secrets = snapshot.connections.iter().any(connection_has_inline_secrets)
|
|
|| tunnel_profiles.iter().any(|profile| {
|
|
let mut scrubbed = profile.clone();
|
|
scrubbed.scrub_secrets();
|
|
scrubbed != *profile
|
|
});
|
|
if has_legacy_secrets {
|
|
sensitive_payload = Some(
|
|
build_sensitive_payload_with_options(
|
|
storage,
|
|
&snapshot.connections,
|
|
&tunnel_profiles,
|
|
SyncExportOptions {
|
|
include_secrets: false,
|
|
sync_passphrase: None,
|
|
include_ai_secrets: false,
|
|
include_tunnel_secrets: true,
|
|
include_plugin_secrets: true,
|
|
},
|
|
)
|
|
.await?,
|
|
);
|
|
}
|
|
}
|
|
if let Some(payload) = &sensitive_payload {
|
|
validate_sensitive_payload_targets(payload, &snapshot.connections, snapshot.tunnel_profiles.as_deref())?;
|
|
}
|
|
if let Some(payload) = &sensitive_payload {
|
|
// Validate the complete decrypted payload before touching metadata or
|
|
// secrets. This keeps malformed/ambiguous transport data from
|
|
// producing a partially-applied restore.
|
|
validate_sensitive_payload(payload)?;
|
|
}
|
|
|
|
let mut connections = snapshot.connections.clone();
|
|
if let Some(mqtt_subscriptions) = &snapshot.mqtt_subscriptions {
|
|
apply_mqtt_subscriptions(&mut connections, mqtt_subscriptions)?;
|
|
} else {
|
|
// Snapshots created before MQTT subscription sync may still contain a
|
|
// stale `savedTopics` value inside `externalConfig`. Preserve the
|
|
// current device value instead of allowing that legacy metadata to
|
|
// overwrite local subscriptions during restore.
|
|
preserve_local_mqtt_subscriptions_for_legacy_snapshot(storage, &mut connections).await?;
|
|
}
|
|
for config in &mut connections {
|
|
scrub_connection_secrets(config);
|
|
}
|
|
|
|
let (connection_secrets, preserve_plugin_secrets, ai_configs, tunnel_secret_profiles, sync_payload_credentials) =
|
|
if let Some(payload) = &sensitive_payload {
|
|
let ai_configs = if let Some(configs) = &payload.ai_configs {
|
|
Some(configs.clone())
|
|
} else {
|
|
payload.ai_config.as_ref().map(|old_config| {
|
|
vec![AiConfigItem {
|
|
id: AiConfigItem::new_id(),
|
|
name: old_config.provider.as_str().to_string(),
|
|
is_default: true,
|
|
config: old_config.clone(),
|
|
}]
|
|
})
|
|
};
|
|
(
|
|
Some(
|
|
payload
|
|
.connection_secrets
|
|
.iter()
|
|
.filter(|secret| {
|
|
!(matches!(
|
|
secret.key.as_str(),
|
|
"password" | NACOS_AUTH_PASSWORD_KEY | NACOS_RNACOS_CONSOLE_PASSWORD_KEY
|
|
) && connections
|
|
.iter()
|
|
.any(|config| config.id == secret.connection_id && !config.save_password))
|
|
})
|
|
.map(|secret| SyncImportSecret {
|
|
connection_id: secret.connection_id.clone(),
|
|
key: secret.key.clone(),
|
|
secret: secret.secret.clone(),
|
|
})
|
|
.collect(),
|
|
),
|
|
!payload.plugin_secrets_included,
|
|
ai_configs,
|
|
payload.tunnel_profiles.clone(),
|
|
payload.sync_credentials.clone(),
|
|
)
|
|
} else {
|
|
(None, false, None, None, None)
|
|
};
|
|
let sync_credentials = if let Some(credentials) = sync_payload_credentials {
|
|
let local_secret =
|
|
if credentials.is_empty() { None } else { Some(storage.load_or_create_local_device_secret().await?) };
|
|
Some(
|
|
credentials
|
|
.into_iter()
|
|
.map(|credential| {
|
|
let blob = local_secret
|
|
.as_deref()
|
|
.ok_or_else(|| "local sync credential key is unavailable".to_string())
|
|
.and_then(|secret| encrypt_text_with_secret(&credential.secret, secret))
|
|
.and_then(|blob| serde_json::to_string(&blob).map_err(|error| error.to_string()))?;
|
|
Ok(SyncImportCredential { account: credential.account.trim().to_string(), blob })
|
|
})
|
|
.collect::<Result<Vec<_>, String>>()?,
|
|
)
|
|
} else {
|
|
None
|
|
};
|
|
let sync_tunnel_profiles = snapshot.tunnel_profiles.clone().or_else(|| {
|
|
tunnel_secret_profiles.as_ref().map(|profiles| {
|
|
profiles
|
|
.iter()
|
|
.map(|profile| {
|
|
let mut scrubbed = profile.clone();
|
|
scrubbed.scrub_secrets();
|
|
scrubbed
|
|
})
|
|
.collect()
|
|
})
|
|
});
|
|
storage
|
|
.apply_sync_import_transaction(SyncImportPlan {
|
|
connections,
|
|
tunnel_profiles: sync_tunnel_profiles,
|
|
tunnel_secret_profiles,
|
|
sidebar_layout: snapshot.sidebar_layout.clone(),
|
|
pinned_tree_node_ids: snapshot.pinned_tree_node_ids.clone(),
|
|
saved_sql: snapshot.saved_sql.clone(),
|
|
desktop_settings: snapshot.desktop_settings.clone(),
|
|
editor_settings: snapshot.editor_settings.clone(),
|
|
connection_secrets,
|
|
preserve_plugin_secrets,
|
|
sync_credentials,
|
|
ai_configs,
|
|
})
|
|
.await?;
|
|
Ok(ApplySnapshotSummary { encrypted_secrets_present, secrets_applied: sensitive_payload.is_some() })
|
|
}
|
|
|
|
fn validate_sensitive_payload_targets(
|
|
payload: &SensitiveSyncPayload,
|
|
connections: &[ConnectionConfig],
|
|
tunnel_profiles: Option<&[TransportLayerConfig]>,
|
|
) -> Result<(), String> {
|
|
let connection_ids = connections.iter().map(|config| config.id.as_str()).collect::<HashSet<_>>();
|
|
if let Some(secret) =
|
|
payload.connection_secrets.iter().find(|secret| !connection_ids.contains(secret.connection_id.as_str()))
|
|
{
|
|
return Err(format!("Synced secret targets unknown connection '{}'.", secret.connection_id));
|
|
}
|
|
if let Some(synced_profiles) = &payload.tunnel_profiles {
|
|
if let Some(public_profiles) = tunnel_profiles {
|
|
let profile_ids = public_profiles.iter().map(TransportLayerConfig::id).collect::<HashSet<_>>();
|
|
if let Some(profile) = synced_profiles.iter().find(|profile| !profile_ids.contains(profile.id())) {
|
|
return Err(format!("Synced secret targets unknown tunnel profile '{}'.", profile.id()));
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_sync_snapshot_metadata(snapshot: &SyncSnapshot) -> Result<(), String> {
|
|
let mut connection_ids = HashSet::new();
|
|
for config in &snapshot.connections {
|
|
if config.id.trim().is_empty() {
|
|
return Err("Sync snapshot contains a connection with an empty id.".to_string());
|
|
}
|
|
if !connection_ids.insert(config.id.as_str()) {
|
|
return Err(format!("Sync snapshot contains duplicate connection id '{}'.", config.id));
|
|
}
|
|
}
|
|
if let Some(profiles) = &snapshot.tunnel_profiles {
|
|
let mut profile_ids = HashSet::new();
|
|
for profile in profiles {
|
|
let id = profile.id().trim();
|
|
if id.is_empty() {
|
|
return Err("Sync snapshot contains a tunnel profile with an empty id.".to_string());
|
|
}
|
|
if !profile_ids.insert(id) {
|
|
return Err(format!("Sync snapshot contains duplicate tunnel profile id '{id}'."));
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub struct WebDavClient {
|
|
http: Client,
|
|
config: WebDavConfig,
|
|
}
|
|
|
|
pub struct SnippetSyncClient {
|
|
http: Client,
|
|
config: SnippetSyncConfig,
|
|
api_base: String,
|
|
}
|
|
|
|
pub async fn webdav_saved_password_status(
|
|
storage: &Storage,
|
|
config: &WebDavConfig,
|
|
) -> Result<WebDavPasswordStatus, String> {
|
|
let account = webdav_password_account(config);
|
|
Ok(WebDavPasswordStatus { has_saved_password: storage.load_webdav_password_blob(&account).await?.is_some() })
|
|
}
|
|
|
|
pub async fn save_webdav_password(storage: &Storage, config: &WebDavConfig, password: &str) -> Result<(), String> {
|
|
let secret = storage.load_or_create_local_device_secret().await?;
|
|
let blob = encrypt_text_with_secret(password, &secret)?;
|
|
let value = serde_json::to_value(blob).map_err(|e| e.to_string())?;
|
|
storage.save_webdav_password_blob(&webdav_password_account(config), &value).await
|
|
}
|
|
|
|
pub async fn forget_webdav_password(storage: &Storage, config: &WebDavConfig) -> Result<(), String> {
|
|
storage.delete_webdav_password_blob(&webdav_password_account(config)).await
|
|
}
|
|
|
|
pub async fn resolve_webdav_password(storage: &Storage, config: &mut WebDavConfig) -> Result<(), String> {
|
|
if config.password.as_deref().is_some_and(|password| !password.is_empty()) {
|
|
return Ok(());
|
|
}
|
|
let Some(value) = storage.load_webdav_password_blob(&webdav_password_account(config)).await? else {
|
|
return Ok(());
|
|
};
|
|
let blob: EncryptedSecretsBlob = serde_json::from_value(value).map_err(|e| e.to_string())?;
|
|
let secret = storage.load_or_create_local_device_secret().await?;
|
|
config.password = Some(decrypt_text_with_secret(&blob, &secret)?);
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn snippet_saved_token_status(
|
|
storage: &Storage,
|
|
config: &SnippetSyncConfig,
|
|
) -> Result<SnippetTokenStatus, String> {
|
|
let account = snippet_token_account(config)?;
|
|
Ok(SnippetTokenStatus { has_saved_token: storage.load_webdav_password_blob(&account).await?.is_some() })
|
|
}
|
|
|
|
pub async fn save_snippet_token(storage: &Storage, config: &SnippetSyncConfig, token: &str) -> Result<(), String> {
|
|
let secret = storage.load_or_create_local_device_secret().await?;
|
|
let blob = encrypt_text_with_secret(token, &secret)?;
|
|
let value = serde_json::to_value(blob).map_err(|e| e.to_string())?;
|
|
storage.save_webdav_password_blob(&snippet_token_account(config)?, &value).await
|
|
}
|
|
|
|
pub async fn forget_snippet_token(storage: &Storage, config: &SnippetSyncConfig) -> Result<(), String> {
|
|
storage.delete_webdav_password_blob(&snippet_token_account(config)?).await
|
|
}
|
|
|
|
pub async fn snippet_sync_settings(
|
|
storage: &Storage,
|
|
provider: SnippetProvider,
|
|
) -> Result<SnippetSyncSettings, String> {
|
|
snippet_sync_settings_for_instance(storage, provider, None).await
|
|
}
|
|
|
|
pub async fn snippet_sync_settings_for_instance(
|
|
storage: &Storage,
|
|
provider: SnippetProvider,
|
|
instance_url: Option<&str>,
|
|
) -> Result<SnippetSyncSettings, String> {
|
|
let key = snippet_provider_storage_key(provider, instance_url)?;
|
|
let state = storage.load_snippet_sync_state(&key).await?;
|
|
Ok(SnippetSyncSettings {
|
|
snippet_id: state.snippet_id,
|
|
legacy_cleanup_required_id: state.pending_cleanup.map(|cleanup| cleanup.snippet_id),
|
|
})
|
|
}
|
|
|
|
pub async fn save_snippet_sync_id(
|
|
storage: &Storage,
|
|
provider: SnippetProvider,
|
|
snippet_id: Option<&str>,
|
|
) -> Result<(), String> {
|
|
save_snippet_sync_id_for_instance(storage, provider, None, snippet_id).await
|
|
}
|
|
|
|
pub async fn save_snippet_sync_id_for_instance(
|
|
storage: &Storage,
|
|
provider: SnippetProvider,
|
|
instance_url: Option<&str>,
|
|
snippet_id: Option<&str>,
|
|
) -> Result<(), String> {
|
|
if let Some(id) = normalized_snippet_id(snippet_id) {
|
|
validate_snippet_id(provider, id)?;
|
|
}
|
|
let key = snippet_provider_storage_key(provider, instance_url)?;
|
|
storage.save_snippet_sync_id(&key, snippet_id).await
|
|
}
|
|
|
|
pub async fn finalize_snippet_migration(
|
|
storage: &Storage,
|
|
client: &SnippetSyncClient,
|
|
summary: &mut SnippetSyncSummary,
|
|
) -> Result<(), String> {
|
|
let Some(pending_cleanup) = snippet_pending_cleanup(summary)? else {
|
|
return Ok(());
|
|
};
|
|
let provider_key = client.storage_key()?;
|
|
storage
|
|
.save_snippet_migration_state(
|
|
&provider_key,
|
|
&summary.snippet_id,
|
|
&pending_cleanup.snippet_id,
|
|
&pending_cleanup.expected_content_hash,
|
|
)
|
|
.await?;
|
|
if client.delete_legacy_snippet_if_unchanged(&pending_cleanup).await.unwrap_or(false)
|
|
&& storage.clear_snippet_pending_cleanup_if_matches(&provider_key, &pending_cleanup).await?
|
|
{
|
|
summary.legacy_cleanup_required_id = None;
|
|
summary.legacy_cleanup_expected_content_hash = None;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn retry_pending_snippet_cleanup(
|
|
storage: &Storage,
|
|
provider: SnippetProvider,
|
|
client: &SnippetSyncClient,
|
|
) -> Result<SnippetSyncSettings, String> {
|
|
if provider != client.config.provider {
|
|
return Err("Snippet provider mismatch".to_string());
|
|
}
|
|
let provider_key = client.storage_key()?;
|
|
let state = storage.load_snippet_sync_state(&provider_key).await?;
|
|
if let Some(pending_cleanup) = state.pending_cleanup {
|
|
if client.delete_legacy_snippet_if_unchanged(&pending_cleanup).await? {
|
|
storage.clear_snippet_pending_cleanup_if_matches(&provider_key, &pending_cleanup).await?;
|
|
}
|
|
}
|
|
snippet_sync_settings_for_instance(storage, provider, client.config.instance_url.as_deref()).await
|
|
}
|
|
|
|
pub async fn resolve_snippet_token(storage: &Storage, config: &mut SnippetSyncConfig) -> Result<(), String> {
|
|
if config.token.as_deref().is_some_and(|token| !token.trim().is_empty()) {
|
|
return Ok(());
|
|
}
|
|
let Some(value) = storage.load_webdav_password_blob(&snippet_token_account(config)?).await? else {
|
|
return Ok(());
|
|
};
|
|
let blob: EncryptedSecretsBlob = serde_json::from_value(value).map_err(|e| e.to_string())?;
|
|
let secret = storage.load_or_create_local_device_secret().await?;
|
|
config.token = Some(decrypt_text_with_secret(&blob, &secret)?);
|
|
Ok(())
|
|
}
|
|
|
|
pub async fn webdav_sync_secrets_status(storage: &Storage) -> Result<WebDavSyncSecretsStatus, String> {
|
|
Ok(WebDavSyncSecretsStatus {
|
|
enabled: storage.load_webdav_sync_secrets_enabled().await?,
|
|
has_saved_passphrase: storage.load_webdav_sync_secrets_passphrase_blob().await?.is_some(),
|
|
})
|
|
}
|
|
|
|
pub async fn save_webdav_sync_secrets_preference(
|
|
storage: &Storage,
|
|
enabled: bool,
|
|
passphrase: Option<&str>,
|
|
) -> Result<(), String> {
|
|
let normalized = normalized_passphrase(passphrase);
|
|
let blob = match normalized {
|
|
Some(passphrase) => {
|
|
let secret = storage.load_or_create_local_device_secret().await?;
|
|
let blob = encrypt_text_with_secret(passphrase, &secret)?;
|
|
Some(serde_json::to_value(blob).map_err(|e| e.to_string())?)
|
|
}
|
|
None => None,
|
|
};
|
|
storage.save_webdav_sync_secrets_preference(enabled, blob.as_ref()).await
|
|
}
|
|
|
|
pub async fn forget_webdav_sync_secrets_passphrase(storage: &Storage) -> Result<(), String> {
|
|
storage.delete_webdav_sync_secrets_passphrase_blob().await
|
|
}
|
|
|
|
pub async fn resolve_webdav_sync_secrets_passphrase(storage: &Storage) -> Result<Option<String>, String> {
|
|
if !storage.load_webdav_sync_secrets_enabled().await? {
|
|
return Ok(None);
|
|
}
|
|
let Some(value) = storage.load_webdav_sync_secrets_passphrase_blob().await? else {
|
|
return Ok(None);
|
|
};
|
|
let blob: EncryptedSecretsBlob = serde_json::from_value(value).map_err(|e| e.to_string())?;
|
|
let secret = storage.load_or_create_local_device_secret().await?;
|
|
decrypt_text_with_secret(&blob, &secret).map(Some)
|
|
}
|
|
|
|
impl WebDavClient {
|
|
pub fn new(config: WebDavConfig) -> Self {
|
|
let builder = Client::builder();
|
|
let builder =
|
|
if webdav_endpoint_uses_direct_connection(&config.endpoint) { builder.no_proxy() } else { builder };
|
|
let http = builder.build().expect("failed to build WebDAV HTTP client");
|
|
Self { http, config }
|
|
}
|
|
|
|
pub fn remote_path(&self) -> String {
|
|
normalized_remote_path(self.config.remote_path.as_deref())
|
|
}
|
|
|
|
pub async fn test(&self) -> Result<(), String> {
|
|
let method = Method::from_bytes(b"PROPFIND").map_err(|e| e.to_string())?;
|
|
let response = self.request(method, "")?.header("Depth", "0").send().await.map_err(|e| e.to_string())?;
|
|
let status = response.status();
|
|
if status.is_success() {
|
|
Ok(())
|
|
} else {
|
|
Err(format!("WebDAV test failed with HTTP {status}"))
|
|
}
|
|
}
|
|
|
|
pub async fn put_snapshot(&self, snapshot: &SyncSnapshot) -> Result<WebDavSyncSummary, String> {
|
|
let remote_path = self.remote_path();
|
|
self.ensure_parent_collections(&remote_path).await?;
|
|
let bytes = serde_json::to_vec_pretty(snapshot).map_err(|e| e.to_string())?;
|
|
let response = self
|
|
.request(Method::PUT, &remote_path)?
|
|
.header(header::CONTENT_TYPE, "application/json")
|
|
.body(bytes.clone())
|
|
.send()
|
|
.await
|
|
.map_err(|e| e.to_string())?;
|
|
let status = response.status();
|
|
if !status.is_success() {
|
|
return Err(format!("WebDAV upload failed with HTTP {status}"));
|
|
}
|
|
Ok(WebDavSyncSummary {
|
|
remote_path,
|
|
bytes: bytes.len(),
|
|
exported_at: Some(snapshot.exported_at.clone()),
|
|
app_version: Some(snapshot.app_version.clone()),
|
|
})
|
|
}
|
|
|
|
pub async fn get_snapshot(&self) -> Result<(SyncSnapshot, WebDavSyncSummary), String> {
|
|
let remote_path = self.remote_path();
|
|
let response = self.request(Method::GET, &remote_path)?.send().await.map_err(|e| e.to_string())?;
|
|
let status = response.status();
|
|
if !status.is_success() {
|
|
return Err(format!("WebDAV download failed with HTTP {status}"));
|
|
}
|
|
let bytes = response.bytes().await.map_err(|e| e.to_string())?;
|
|
let snapshot: SyncSnapshot = serde_json::from_slice(&bytes).map_err(|e| e.to_string())?;
|
|
let summary = WebDavSyncSummary {
|
|
remote_path,
|
|
bytes: bytes.len(),
|
|
exported_at: Some(snapshot.exported_at.clone()),
|
|
app_version: Some(snapshot.app_version.clone()),
|
|
};
|
|
Ok((snapshot, summary))
|
|
}
|
|
|
|
async fn ensure_parent_collections(&self, remote_path: &str) -> Result<(), String> {
|
|
let method = Method::from_bytes(b"MKCOL").map_err(|e| e.to_string())?;
|
|
for parent in parent_collection_paths(remote_path) {
|
|
let collection_path = format!("{parent}/");
|
|
let response = self.request(method.clone(), &collection_path)?.send().await.map_err(|e| e.to_string())?;
|
|
let status = response.status();
|
|
if status.is_success() || status == StatusCode::METHOD_NOT_ALLOWED {
|
|
continue;
|
|
}
|
|
return Err(format!("Failed to create WebDAV collection '{parent}' with HTTP {status}"));
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn request(&self, method: Method, remote_path: &str) -> Result<reqwest::RequestBuilder, String> {
|
|
let url = self.remote_url(remote_path)?;
|
|
let mut request = self.http.request(method, url);
|
|
if let Some(username) = self.config.username.as_deref().filter(|value| !value.is_empty()) {
|
|
request = request.basic_auth(username, self.config.password.clone());
|
|
}
|
|
Ok(request)
|
|
}
|
|
|
|
fn remote_url(&self, remote_path: &str) -> Result<Url, String> {
|
|
let endpoint = self.config.endpoint.trim();
|
|
if endpoint.is_empty() {
|
|
return Err("WebDAV endpoint is required".to_string());
|
|
}
|
|
let base = if endpoint.ends_with('/') { endpoint.to_string() } else { format!("{endpoint}/") };
|
|
let base = Url::parse(&base).map_err(|e| e.to_string())?;
|
|
base.join(remote_path.trim_start_matches('/')).map_err(|e| e.to_string())
|
|
}
|
|
}
|
|
|
|
impl SnippetSyncClient {
|
|
pub fn new(config: SnippetSyncConfig) -> Result<Self, String> {
|
|
let api_base = match config.provider {
|
|
SnippetProvider::GitHub => GITHUB_API_BASE,
|
|
SnippetProvider::Gitee => GITEE_API_BASE,
|
|
SnippetProvider::GitLab => {
|
|
let instance = gitlab_instance_url(config.instance_url.as_deref())?;
|
|
return Ok(Self {
|
|
http: Client::builder()
|
|
.redirect(reqwest::redirect::Policy::none())
|
|
.build()
|
|
.map_err(|e| e.to_string())?,
|
|
config,
|
|
api_base: format!("{instance}/api/v4"),
|
|
});
|
|
}
|
|
};
|
|
Ok(Self { http: Client::new(), config, api_base: api_base.to_string() })
|
|
}
|
|
|
|
#[cfg(test)]
|
|
fn with_api_base(config: SnippetSyncConfig, api_base: String) -> Self {
|
|
Self { http: Client::new(), config, api_base }
|
|
}
|
|
|
|
fn storage_key(&self) -> Result<String, String> {
|
|
snippet_provider_storage_key(self.config.provider, self.config.instance_url.as_deref())
|
|
}
|
|
|
|
fn snippet_url(&self, id: Option<&str>) -> Result<String, String> {
|
|
let path = if self.config.provider == SnippetProvider::GitLab { "snippets" } else { "gists" };
|
|
if let Some(id) = id {
|
|
validate_snippet_id(self.config.provider, id)?;
|
|
Ok(format!("{}/{path}/{id}", self.api_base))
|
|
} else {
|
|
Ok(format!("{}/{path}", self.api_base))
|
|
}
|
|
}
|
|
|
|
pub async fn test(&self) -> Result<(), String> {
|
|
self.require_token()?;
|
|
let url = match (self.config.provider, normalized_snippet_id(self.config.snippet_id.as_deref())) {
|
|
(_, Some(id)) => self.snippet_url(Some(id))?,
|
|
(_, None) => format!("{}/user", self.api_base),
|
|
};
|
|
let response = self.request(Method::GET, &url)?.send().await.map_err(|e| e.to_string())?;
|
|
ensure_snippet_success(response.status(), "test")
|
|
}
|
|
|
|
pub async fn put_snapshot(
|
|
&self,
|
|
snapshot: &SyncSnapshot,
|
|
snippet_passphrase: Option<&str>,
|
|
secrets_passphrase: Option<&str>,
|
|
) -> Result<SnippetSyncSummary, String> {
|
|
self.require_token()?;
|
|
let passphrase = required_snippet_passphrase(snippet_passphrase)?;
|
|
let existing_id = normalized_snippet_id(self.config.snippet_id.as_deref());
|
|
let legacy_snapshot = if let Some(id) = existing_id {
|
|
let existing_content = self.load_snippet_content(id).await?;
|
|
if !is_encrypted_snippet_snapshot(&existing_content) {
|
|
// Never delete an arbitrary snippet merely because it is not an
|
|
// encrypted DBX envelope. It must first prove to be a legacy DBX
|
|
// snapshot, and the caller must explicitly request migration.
|
|
if !is_legacy_dbx_snapshot(&existing_content) {
|
|
return Err("The selected snippet is not a DBX sync snapshot; refusing to replace or delete it."
|
|
.to_string());
|
|
}
|
|
if !self.config.replace_legacy_snippet {
|
|
return Err(
|
|
"This snippet contains a legacy unencrypted DBX snapshot. Use the secure migration action to create an encrypted replacement and delete the legacy snippet only after the new one is created."
|
|
.to_string(),
|
|
);
|
|
}
|
|
let legacy_snapshot = parse_legacy_dbx_snapshot(&existing_content)?;
|
|
Some((
|
|
id,
|
|
prepare_legacy_snippet_snapshot(legacy_snapshot, secrets_passphrase)?,
|
|
content_hash(&existing_content),
|
|
))
|
|
} else {
|
|
if self.config.replace_legacy_snippet {
|
|
return Err("The selected snippet is already encrypted; use the normal upload action.".to_string());
|
|
}
|
|
// Refuse a PATCH unless the supplied snippet encryption
|
|
// password decrypts the currently stored envelope. Otherwise
|
|
// an accidental password change would silently lock out the
|
|
// user's other devices.
|
|
parse_snippet_snapshot(&existing_content, Some(passphrase))?;
|
|
None
|
|
}
|
|
} else {
|
|
None
|
|
};
|
|
// Migration encrypts the already-read remote snapshot rather than the
|
|
// caller's current local state. Otherwise a stale second device could
|
|
// erase the only copy of newer remote settings or saved SQL.
|
|
let snapshot_to_upload =
|
|
snapshot_for_snippet_upload(snapshot, legacy_snapshot.as_ref().map(|(_, snapshot, _)| snapshot));
|
|
let encrypted = encrypt_snippet_snapshot(snapshot_to_upload, passphrase)?;
|
|
let bytes = serde_json::to_vec_pretty(&encrypted).map_err(|e| e.to_string())?;
|
|
let content = String::from_utf8(bytes.clone()).map_err(|e| e.to_string())?;
|
|
// A migration must create a separate snippet. Updating the legacy
|
|
// snippet would retain its plaintext revision history on the provider.
|
|
let update_id = if legacy_snapshot.is_some() { None } else { existing_id };
|
|
let (method, url) = match (self.config.provider, update_id) {
|
|
(SnippetProvider::GitLab, Some(id)) => (Method::PUT, self.snippet_url(Some(id))?),
|
|
(_, Some(id)) => (Method::PATCH, self.snippet_url(Some(id))?),
|
|
(_, None) => (Method::POST, self.snippet_url(None)?),
|
|
};
|
|
|
|
let response = match self.config.provider {
|
|
SnippetProvider::GitHub => {
|
|
let payload = serde_json::json!({
|
|
"description": "DBX encrypted configuration sync",
|
|
"public": false,
|
|
"files": { DEFAULT_SNIPPET_FILE_NAME: { "content": content } }
|
|
});
|
|
self.request(method, &url)?.json(&payload).send().await
|
|
}
|
|
SnippetProvider::Gitee => {
|
|
let payload = gitee_snippet_payload(content);
|
|
// Gitee validates `files` as a nested object; form encoding turns it into a string and is rejected.
|
|
self.request(method, &url)?.json(&payload).send().await
|
|
}
|
|
SnippetProvider::GitLab => {
|
|
let payload = if update_id.is_some() {
|
|
serde_json::json!({"files": [{"action": "update", "file_path": DEFAULT_SNIPPET_FILE_NAME, "content": content}]})
|
|
} else {
|
|
serde_json::json!({"title": "DBX encrypted configuration sync", "visibility": "private", "files": [{"file_path": DEFAULT_SNIPPET_FILE_NAME, "content": content}]})
|
|
};
|
|
self.request(method, &url)?.json(&payload).send().await
|
|
}
|
|
}
|
|
.map_err(|e| e.to_string())?;
|
|
let status = response.status();
|
|
let response_body = response.text().await.map_err(|e| e.to_string())?;
|
|
ensure_snippet_response_success(status, "upload", &response_body)?;
|
|
let value: serde_json::Value = serde_json::from_str(&response_body).map_err(|e| e.to_string())?;
|
|
let snippet_id = snippet_response_id(&value)
|
|
.or_else(|| update_id.map(str::to_string))
|
|
.ok_or_else(|| "Snippet API response did not include an id".to_string())?;
|
|
// The command layer persists the replacement id before calling
|
|
// `delete_legacy_snippet`. If the app stops before persistence, the
|
|
// old plaintext snippet remains available instead of leaving users
|
|
// without a pointer to either remote snippet.
|
|
let exported_at = Some(snapshot_to_upload.exported_at.clone());
|
|
let app_version = Some(snapshot_to_upload.app_version.clone());
|
|
let legacy_cleanup_required_id = legacy_snapshot.as_ref().map(|(id, _, _)| (*id).to_string());
|
|
Ok(SnippetSyncSummary {
|
|
provider: self.config.provider,
|
|
snippet_id,
|
|
bytes: bytes.len(),
|
|
exported_at,
|
|
app_version,
|
|
legacy_cleanup_required_id,
|
|
legacy_cleanup_expected_content_hash: legacy_snapshot.map(|(_, _, hash)| hash),
|
|
})
|
|
}
|
|
|
|
pub async fn get_snapshot(
|
|
&self,
|
|
secrets_passphrase: Option<&str>,
|
|
) -> Result<(SyncSnapshot, SnippetSyncSummary), String> {
|
|
self.require_token()?;
|
|
let snippet_id = normalized_snippet_id(self.config.snippet_id.as_deref())
|
|
.ok_or_else(|| "Snippet id is required for download".to_string())?;
|
|
let content = self.load_snippet_content(snippet_id).await?;
|
|
let snapshot = parse_snippet_snapshot(&content, secrets_passphrase)?;
|
|
let summary = SnippetSyncSummary {
|
|
provider: self.config.provider,
|
|
snippet_id: snippet_id.to_string(),
|
|
bytes: content.len(),
|
|
exported_at: Some(snapshot.exported_at.clone()),
|
|
app_version: Some(snapshot.app_version.clone()),
|
|
legacy_cleanup_required_id: None,
|
|
legacy_cleanup_expected_content_hash: None,
|
|
};
|
|
Ok((snapshot, summary))
|
|
}
|
|
|
|
async fn load_snippet_content(&self, snippet_id: &str) -> Result<String, String> {
|
|
let url = self.snippet_url(Some(snippet_id))?;
|
|
let response = self.request(Method::GET, &url)?.send().await.map_err(|e| e.to_string())?;
|
|
let status = response.status();
|
|
let response_body = response.text().await.map_err(|e| e.to_string())?;
|
|
ensure_snippet_response_success(status, "download", &response_body)?;
|
|
if self.config.provider == SnippetProvider::GitLab {
|
|
let value: serde_json::Value = serde_json::from_str(&response_body).map_err(|e| e.to_string())?;
|
|
let files = value
|
|
.get("files")
|
|
.and_then(serde_json::Value::as_array)
|
|
.ok_or_else(|| "Snippet response did not include files".to_string())?;
|
|
let file = files
|
|
.iter()
|
|
.find(|file| file.get("path").and_then(serde_json::Value::as_str) == Some(DEFAULT_SNIPPET_FILE_NAME))
|
|
.ok_or_else(|| format!("Snippet does not contain {DEFAULT_SNIPPET_FILE_NAME}"))?;
|
|
// Snippet repositories default to `main` on current instances but
|
|
// `master` on older ones; the per-file `raw_url` always carries the
|
|
// snippet's actual default branch, so prefer it over guessing.
|
|
let constructed_main = format!("{url}/files/main/{DEFAULT_SNIPPET_FILE_NAME}/raw");
|
|
let raw_url = file
|
|
.get("raw_url")
|
|
.and_then(serde_json::Value::as_str)
|
|
.filter(|value| !value.is_empty())
|
|
.map(str::to_string)
|
|
.unwrap_or_else(|| constructed_main.clone());
|
|
let response = self.request(Method::GET, &raw_url)?.send().await.map_err(|e| e.to_string())?;
|
|
if response.status() == StatusCode::NOT_FOUND && raw_url == constructed_main {
|
|
let master_url = format!("{url}/files/master/{DEFAULT_SNIPPET_FILE_NAME}/raw");
|
|
let response = self.request(Method::GET, &master_url)?.send().await.map_err(|e| e.to_string())?;
|
|
ensure_snippet_success(response.status(), "raw download")?;
|
|
return response.text().await.map_err(|e| e.to_string());
|
|
}
|
|
ensure_snippet_success(response.status(), "raw download")?;
|
|
return response.text().await.map_err(|e| e.to_string());
|
|
}
|
|
let value: serde_json::Value = serde_json::from_str(&response_body).map_err(|e| e.to_string())?;
|
|
let (content, raw_url) = snippet_file_content(&value, DEFAULT_SNIPPET_FILE_NAME)?;
|
|
let content = match content {
|
|
Some(content) => content,
|
|
None => {
|
|
let raw_url = raw_url.ok_or_else(|| "Snippet file content is unavailable".to_string())?;
|
|
let response = self.request(Method::GET, &raw_url)?.send().await.map_err(|e| e.to_string())?;
|
|
ensure_snippet_success(response.status(), "raw download")?;
|
|
response.text().await.map_err(|e| e.to_string())?
|
|
}
|
|
};
|
|
Ok(content)
|
|
}
|
|
|
|
pub async fn delete_legacy_snippet_if_unchanged(
|
|
&self,
|
|
pending_cleanup: &SnippetPendingCleanup,
|
|
) -> Result<bool, String> {
|
|
// Neither provider documents a conditional DELETE for snippets. Read
|
|
// again after creating the replacement and refuse cleanup when another
|
|
// device has changed the legacy content in the meantime.
|
|
if content_hash(&self.load_snippet_content(&pending_cleanup.snippet_id).await?)
|
|
!= pending_cleanup.expected_content_hash
|
|
{
|
|
return Ok(false);
|
|
}
|
|
let url = self.snippet_url(Some(&pending_cleanup.snippet_id))?;
|
|
let response = self.request(Method::DELETE, &url)?.send().await.map_err(|e| e.to_string())?;
|
|
// If the provider reports that the old snippet is already absent, the
|
|
// cleanup goal is satisfied and the newly created encrypted snippet is
|
|
// still safe to use.
|
|
if response.status() == StatusCode::NOT_FOUND {
|
|
return Ok(true);
|
|
}
|
|
ensure_snippet_success(response.status(), "delete legacy snippet")?;
|
|
Ok(true)
|
|
}
|
|
|
|
fn require_token(&self) -> Result<&str, String> {
|
|
self.config
|
|
.token
|
|
.as_deref()
|
|
.map(str::trim)
|
|
.filter(|token| !token.is_empty())
|
|
.ok_or_else(|| "Access token is required".to_string())
|
|
}
|
|
|
|
fn request(&self, method: Method, url: &str) -> Result<reqwest::RequestBuilder, String> {
|
|
let token = self.require_token()?;
|
|
let request = self.http.request(method, url);
|
|
Ok(match self.config.provider {
|
|
SnippetProvider::GitHub => request
|
|
.header(header::ACCEPT, "application/vnd.github+json")
|
|
.header(header::USER_AGENT, "DBX")
|
|
.header("X-GitHub-Api-Version", "2022-11-28")
|
|
.bearer_auth(token),
|
|
// Gitee API v5 documents access_token as a request parameter rather than an Authorization header.
|
|
SnippetProvider::Gitee => request.query(&[("access_token", token)]),
|
|
SnippetProvider::GitLab => request.header("PRIVATE-TOKEN", token),
|
|
})
|
|
}
|
|
}
|
|
|
|
fn extract_mqtt_subscriptions(connections: &[ConnectionConfig]) -> Result<Vec<MqttSubscriptionSyncEntry>, String> {
|
|
connections
|
|
.iter()
|
|
.filter(|config| config.db_type == DatabaseType::Mqtt)
|
|
.map(|config| {
|
|
let subscriptions = config
|
|
.external_config
|
|
.as_ref()
|
|
.and_then(|external| external.get("savedTopics"))
|
|
.cloned()
|
|
.unwrap_or_else(|| serde_json::Value::Array(Vec::new()));
|
|
let subscriptions: Vec<MqttSubscriptionSyncTopic> = serde_json::from_value(subscriptions)
|
|
.map_err(|error| format!("Invalid MQTT subscriptions for connection {}: {error}", config.id))?;
|
|
validate_mqtt_subscriptions(config, &subscriptions)?;
|
|
Ok(MqttSubscriptionSyncEntry { connection_id: config.id.clone(), subscriptions })
|
|
})
|
|
.collect()
|
|
}
|
|
|
|
fn apply_mqtt_subscriptions(
|
|
connections: &mut [ConnectionConfig],
|
|
entries: &[MqttSubscriptionSyncEntry],
|
|
) -> Result<(), String> {
|
|
let mut seen_connections = HashSet::new();
|
|
for entry in entries {
|
|
if !seen_connections.insert(entry.connection_id.clone()) {
|
|
return Err(format!("重复的 MQTT 同步连接 ID: {}", entry.connection_id));
|
|
}
|
|
let config = connections
|
|
.iter_mut()
|
|
.find(|config| config.id == entry.connection_id)
|
|
.ok_or_else(|| format!("MQTT 同步配置引用了不存在的连接: {}", entry.connection_id))?;
|
|
if config.db_type != DatabaseType::Mqtt {
|
|
return Err(format!("同步连接 {} 不是 MQTT 连接", entry.connection_id));
|
|
}
|
|
validate_mqtt_subscriptions(config, &entry.subscriptions)?;
|
|
let mut external = config.external_config.take().unwrap_or_else(|| serde_json::json!({}));
|
|
let object = external
|
|
.as_object_mut()
|
|
.ok_or_else(|| format!("MQTT 连接 {} 的 externalConfig 必须是 JSON 对象", entry.connection_id))?;
|
|
object
|
|
.insert("savedTopics".to_string(), serde_json::to_value(&entry.subscriptions).map_err(|e| e.to_string())?);
|
|
config.external_config = Some(external);
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
async fn preserve_local_mqtt_subscriptions_for_legacy_snapshot(
|
|
storage: &Storage,
|
|
connections: &mut [ConnectionConfig],
|
|
) -> Result<(), String> {
|
|
let local_connections = storage.load_connections().await?;
|
|
for config in connections.iter_mut().filter(|config| config.db_type == DatabaseType::Mqtt) {
|
|
let Some(local_config) = local_connections.iter().find(|local| local.id == config.id) else {
|
|
continue;
|
|
};
|
|
let Some(local_saved_topics) =
|
|
local_config.external_config.as_ref().and_then(|external| external.get("savedTopics")).cloned()
|
|
else {
|
|
continue;
|
|
};
|
|
let mut external = config.external_config.take().unwrap_or_else(|| serde_json::json!({}));
|
|
if let Some(object) = external.as_object_mut() {
|
|
object.insert("savedTopics".to_string(), local_saved_topics);
|
|
config.external_config = Some(external);
|
|
} else {
|
|
config.external_config = Some(serde_json::json!({ "savedTopics": local_saved_topics }));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_mqtt_subscriptions(
|
|
config: &ConnectionConfig,
|
|
subscriptions: &[MqttSubscriptionSyncTopic],
|
|
) -> Result<(), String> {
|
|
let protocol = config
|
|
.external_config
|
|
.as_ref()
|
|
.and_then(|external| external.get("protocolVersion"))
|
|
.and_then(serde_json::Value::as_str)
|
|
.unwrap_or("v5");
|
|
let mut topics = HashSet::new();
|
|
for subscription in subscriptions {
|
|
if !valid_mqtt_filter(&subscription.topic) {
|
|
return Err(format!("MQTT 连接 {} 包含无效 Topic Filter: {}", config.id, subscription.topic));
|
|
}
|
|
if !topics.insert(subscription.topic.clone()) {
|
|
return Err(format!("MQTT 连接 {} 包含重复 Topic Filter: {}", config.id, subscription.topic));
|
|
}
|
|
if !matches!(subscription.qos.as_str(), "atmostonce" | "atleastonce" | "exactlyonce") {
|
|
return Err(format!("MQTT 连接 {} 包含无效 QoS: {}", config.id, subscription.qos));
|
|
}
|
|
if subscription.no_local && protocol != "v5" {
|
|
return Err(format!("MQTT 连接 {} 使用了 MQTT 3.x 不支持的 No Local", config.id));
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn valid_mqtt_filter(topic: &str) -> bool {
|
|
if topic.is_empty() || topic.chars().any(char::is_whitespace) {
|
|
return false;
|
|
}
|
|
let segments: Vec<&str> = topic.split('/').collect();
|
|
segments.iter().enumerate().all(|(index, segment)| {
|
|
if *segment == "#" {
|
|
return index == segments.len() - 1;
|
|
}
|
|
!segment.contains('#') && (!segment.contains('+') || *segment == "+")
|
|
})
|
|
}
|
|
|
|
fn scrub_connection_secrets(config: &mut ConnectionConfig) {
|
|
config.password.clear();
|
|
for layer in &mut config.transport_layers {
|
|
match layer {
|
|
TransportLayerConfig::Ssh(ssh) => {
|
|
ssh.password.clear();
|
|
ssh.key_passphrase.clear();
|
|
}
|
|
TransportLayerConfig::Proxy(proxy) => {
|
|
proxy.password.clear();
|
|
}
|
|
TransportLayerConfig::HttpTunnel(http) => {
|
|
http.token.clear();
|
|
}
|
|
}
|
|
}
|
|
config.redis_sentinel_password.clear();
|
|
config.url_params = config.url_params.as_deref().map(scrub_url_params);
|
|
config.connection_string = None;
|
|
config.init_script = None;
|
|
scrub_mqtt_auth_secrets(config);
|
|
for secret in config.connection_secrets.values_mut() {
|
|
secret.clear();
|
|
}
|
|
scrub_mq_external_config_secrets(config);
|
|
scrub_nacos_auth_secrets(config);
|
|
scrub_cassandra_tls_secrets(config);
|
|
config.connection_secrets.clear();
|
|
}
|
|
|
|
fn connection_has_inline_secrets(config: &ConnectionConfig) -> bool {
|
|
let mut scrubbed = config.clone();
|
|
scrub_connection_secrets(&mut scrubbed);
|
|
scrubbed != *config
|
|
}
|
|
|
|
fn scrub_url_params(value: &str) -> String {
|
|
value
|
|
.split('&')
|
|
.map(|part| {
|
|
let Some((key, _value)) = part.split_once('=') else {
|
|
return part.to_string();
|
|
};
|
|
let normalized = key.trim().to_ascii_lowercase().replace(['_', '-'], "");
|
|
if normalized.contains("password")
|
|
|| normalized.contains("passphrase")
|
|
|| normalized.contains("secret")
|
|
|| normalized.contains("token")
|
|
|| normalized.contains("apikey")
|
|
|| normalized.contains("privatekey")
|
|
|| normalized.contains("clientsecret")
|
|
{
|
|
format!("{key}=")
|
|
} else {
|
|
part.to_string()
|
|
}
|
|
})
|
|
.collect::<Vec<_>>()
|
|
.join("&")
|
|
}
|
|
|
|
fn webdav_password_account(config: &WebDavConfig) -> String {
|
|
let mut hasher = Sha256::new();
|
|
hasher.update(config.endpoint.trim().as_bytes());
|
|
hasher.update(b"\n");
|
|
hasher.update(config.username.as_deref().unwrap_or("").trim().as_bytes());
|
|
URL_SAFE_NO_PAD.encode(hasher.finalize())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
async fn build_sensitive_payload(
|
|
storage: &Storage,
|
|
connections: &[ConnectionConfig],
|
|
tunnel_profiles: &[TransportLayerConfig],
|
|
) -> Result<SensitiveSyncPayload, String> {
|
|
build_sensitive_payload_with_options(
|
|
storage,
|
|
connections,
|
|
tunnel_profiles,
|
|
SyncExportOptions {
|
|
include_secrets: true,
|
|
sync_passphrase: None,
|
|
include_ai_secrets: true,
|
|
include_tunnel_secrets: true,
|
|
include_plugin_secrets: true,
|
|
},
|
|
)
|
|
.await
|
|
}
|
|
|
|
async fn build_sensitive_payload_with_options(
|
|
storage: &Storage,
|
|
connections: &[ConnectionConfig],
|
|
tunnel_profiles: &[TransportLayerConfig],
|
|
options: SyncExportOptions<'_>,
|
|
) -> Result<SensitiveSyncPayload, String> {
|
|
let mut connection_secrets = Vec::new();
|
|
for config in connections {
|
|
// 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(""));
|
|
push_secret(&mut connection_secrets, &config.id, "url_params", config.url_params.as_deref().unwrap_or(""));
|
|
for (index, layer) in config.transport_layers.iter().enumerate() {
|
|
match layer {
|
|
TransportLayerConfig::Ssh(ssh) => {
|
|
push_secret(
|
|
&mut connection_secrets,
|
|
&config.id,
|
|
&transport_layer_ssh_password_key(index, layer),
|
|
&ssh.password,
|
|
);
|
|
push_secret(
|
|
&mut connection_secrets,
|
|
&config.id,
|
|
&transport_layer_ssh_key_passphrase_key(index, layer),
|
|
&ssh.key_passphrase,
|
|
);
|
|
}
|
|
TransportLayerConfig::Proxy(proxy) => {
|
|
push_secret(
|
|
&mut connection_secrets,
|
|
&config.id,
|
|
&transport_layer_proxy_password_key(index, layer),
|
|
&proxy.password,
|
|
);
|
|
}
|
|
TransportLayerConfig::HttpTunnel(http) => {
|
|
push_secret(
|
|
&mut connection_secrets,
|
|
&config.id,
|
|
&transport_layer_http_tunnel_token_key(index, layer),
|
|
&http.token,
|
|
);
|
|
}
|
|
}
|
|
}
|
|
push_secret(&mut connection_secrets, &config.id, "redis_sentinel_password", &config.redis_sentinel_password);
|
|
if let Some(connection_string) = &config.connection_string {
|
|
push_secret(&mut connection_secrets, &config.id, "connection_string", connection_string);
|
|
}
|
|
push_mq_external_config_secrets(&mut connection_secrets, config);
|
|
push_mqtt_external_config_secret(&mut connection_secrets, config);
|
|
push_cassandra_tls_secrets(&mut connection_secrets, config);
|
|
if config.save_password {
|
|
push_nacos_external_config_secrets(&mut connection_secrets, config);
|
|
}
|
|
// Plugin secrets are independent of the primary connection password.
|
|
// A plugin may persist a token while `save_password` is disabled, so
|
|
// gate these values only on the explicit plugin export option.
|
|
if options.include_plugin_secrets {
|
|
for (key, secret) in &config.connection_secrets {
|
|
push_secret(&mut connection_secrets, &config.id, &plugin_connection_secret_key(key)?, secret);
|
|
}
|
|
}
|
|
}
|
|
|
|
// Loading AI configurations includes decrypting their secret blobs. A
|
|
// provider/decryption failure must abort the export: treating it as an
|
|
// empty list would produce a valid-looking snapshot that clears AI
|
|
// configurations on the destination during restore.
|
|
let ai_configs = if options.include_ai_secrets { Some(storage.load_ai_configs().await?) } else { None };
|
|
let sync_credentials = if options.include_secrets { Some(load_sync_credentials(storage).await?) } else { None };
|
|
Ok(SensitiveSyncPayload {
|
|
connection_secrets,
|
|
plugin_secrets_included: options.include_plugin_secrets,
|
|
sync_credentials,
|
|
ai_configs,
|
|
ai_config: None,
|
|
tunnel_profiles: options.include_tunnel_secrets.then(|| tunnel_profiles.to_vec()),
|
|
})
|
|
}
|
|
|
|
async fn load_sync_credentials(storage: &Storage) -> Result<Vec<SyncCredentialSnapshot>, String> {
|
|
let accounts = storage.load_webdav_password_accounts().await?;
|
|
if accounts.is_empty() {
|
|
return Ok(Vec::new());
|
|
}
|
|
let local_secret = storage.load_or_create_local_device_secret().await?;
|
|
let mut credentials = Vec::new();
|
|
for account in accounts {
|
|
let Some(value) = storage.load_webdav_password_blob(&account).await? else {
|
|
continue;
|
|
};
|
|
let blob: EncryptedSecretsBlob = serde_json::from_value(value).map_err(|error| error.to_string())?;
|
|
let secret = decrypt_text_with_secret(&blob, &local_secret)?;
|
|
credentials.push(SyncCredentialSnapshot { account, secret });
|
|
}
|
|
Ok(credentials)
|
|
}
|
|
|
|
fn push_mq_external_config_secrets(secrets: &mut Vec<ConnectionSecretSnapshot>, config: &ConnectionConfig) {
|
|
let Some(external_config) = config.external_config.as_ref() else {
|
|
return;
|
|
};
|
|
if let Some(auth) = external_config.get("auth").and_then(serde_json::Value::as_object) {
|
|
match auth.get("kind").and_then(serde_json::Value::as_str) {
|
|
Some("token") => push_json_secret(secrets, &config.id, MQ_AUTH_TOKEN_KEY, auth, "token"),
|
|
Some("basic") => push_json_secret(secrets, &config.id, MQ_AUTH_PASSWORD_KEY, auth, "password"),
|
|
Some("apiKey") | Some("api_key") | Some("apikey") => {
|
|
push_json_secret(secrets, &config.id, MQ_AUTH_API_KEY_VALUE_KEY, auth, "value")
|
|
}
|
|
Some("oauth2") => push_json_secret(secrets, &config.id, MQ_AUTH_CLIENT_SECRET_KEY, auth, "clientSecret"),
|
|
_ => {}
|
|
}
|
|
}
|
|
if let Some(signing) = external_config.get("tokenSigning").and_then(serde_json::Value::as_object) {
|
|
push_json_secret(secrets, &config.id, MQ_TOKEN_SIGNING_KEY, signing, "key");
|
|
}
|
|
}
|
|
|
|
fn push_mqtt_external_config_secret(secrets: &mut Vec<ConnectionSecretSnapshot>, config: &ConnectionConfig) {
|
|
if config.db_type != DatabaseType::Mqtt {
|
|
return;
|
|
}
|
|
let Some(auth) = config
|
|
.external_config
|
|
.as_ref()
|
|
.and_then(|external| external.get("auth"))
|
|
.and_then(serde_json::Value::as_object)
|
|
else {
|
|
return;
|
|
};
|
|
if auth.get("kind").and_then(serde_json::Value::as_str) == Some("password") {
|
|
push_json_secret(secrets, &config.id, MQTT_AUTH_PASSWORD_KEY, auth, "password");
|
|
}
|
|
}
|
|
|
|
fn scrub_mq_external_config_secrets(config: &mut ConnectionConfig) {
|
|
if config.db_type != DatabaseType::MessageQueue {
|
|
return;
|
|
}
|
|
let Some(external_config) = config.external_config.as_mut() else {
|
|
return;
|
|
};
|
|
if let Some(auth) = external_config.get_mut("auth").and_then(serde_json::Value::as_object_mut) {
|
|
match auth.get("kind").and_then(serde_json::Value::as_str) {
|
|
Some("token") => scrub_json_secret(auth, "token"),
|
|
Some("basic") => scrub_json_secret(auth, "password"),
|
|
Some("apiKey") | Some("api_key") | Some("apikey") => scrub_json_secret(auth, "value"),
|
|
Some("oauth2") => scrub_json_secret(auth, "clientSecret"),
|
|
_ => {}
|
|
}
|
|
}
|
|
if let Some(signing) = external_config.get_mut("tokenSigning").and_then(serde_json::Value::as_object_mut) {
|
|
scrub_json_secret(signing, "key");
|
|
}
|
|
}
|
|
|
|
fn scrub_mqtt_auth_secrets(config: &mut ConnectionConfig) {
|
|
if config.db_type != DatabaseType::Mqtt {
|
|
return;
|
|
}
|
|
let Some(auth) = config
|
|
.external_config
|
|
.as_mut()
|
|
.and_then(|external| external.get_mut("auth"))
|
|
.and_then(serde_json::Value::as_object_mut)
|
|
else {
|
|
return;
|
|
};
|
|
if auth.get("kind").and_then(serde_json::Value::as_str) == Some("password") {
|
|
scrub_json_secret(auth, "password");
|
|
}
|
|
}
|
|
|
|
fn push_cassandra_tls_secrets(secrets: &mut Vec<ConnectionSecretSnapshot>, config: &ConnectionConfig) {
|
|
if config.db_type != DatabaseType::Cassandra {
|
|
return;
|
|
}
|
|
let Some(tls) = config
|
|
.external_config
|
|
.as_ref()
|
|
.and_then(|external_config| external_config.get("tls"))
|
|
.and_then(serde_json::Value::as_object)
|
|
else {
|
|
return;
|
|
};
|
|
push_json_secret(secrets, &config.id, CASSANDRA_TRUSTSTORE_PASSWORD_KEY, tls, "truststore_password");
|
|
push_json_secret(secrets, &config.id, CASSANDRA_KEYSTORE_PASSWORD_KEY, tls, "keystore_password");
|
|
}
|
|
|
|
fn scrub_cassandra_tls_secrets(config: &mut ConnectionConfig) {
|
|
if config.db_type != DatabaseType::Cassandra {
|
|
return;
|
|
}
|
|
let Some(tls) = config
|
|
.external_config
|
|
.as_mut()
|
|
.and_then(|external_config| external_config.get_mut("tls"))
|
|
.and_then(serde_json::Value::as_object_mut)
|
|
else {
|
|
return;
|
|
};
|
|
scrub_json_secret(tls, "truststore_password");
|
|
scrub_json_secret(tls, "keystore_password");
|
|
}
|
|
|
|
fn push_nacos_external_config_secrets(secrets: &mut Vec<ConnectionSecretSnapshot>, config: &ConnectionConfig) {
|
|
if config.db_type != DatabaseType::Nacos {
|
|
return;
|
|
}
|
|
if let Some(auth) = config
|
|
.external_config
|
|
.as_ref()
|
|
.and_then(|external_config| external_config.get("auth"))
|
|
.and_then(serde_json::Value::as_object)
|
|
{
|
|
if auth.get("kind").and_then(serde_json::Value::as_str) == Some("usernamePassword") {
|
|
push_json_secret(secrets, &config.id, NACOS_AUTH_PASSWORD_KEY, auth, "password");
|
|
}
|
|
}
|
|
if let Some(auth) = config
|
|
.external_config
|
|
.as_ref()
|
|
.and_then(|external_config| external_config.get("rnacosConsoleAuth"))
|
|
.and_then(serde_json::Value::as_object)
|
|
{
|
|
if auth.get("kind").and_then(serde_json::Value::as_str) == Some("usernamePassword") {
|
|
push_json_secret(secrets, &config.id, NACOS_RNACOS_CONSOLE_PASSWORD_KEY, auth, "password");
|
|
}
|
|
}
|
|
}
|
|
|
|
fn push_json_secret(
|
|
secrets: &mut Vec<ConnectionSecretSnapshot>,
|
|
connection_id: &str,
|
|
key: &str,
|
|
object: &serde_json::Map<String, serde_json::Value>,
|
|
field: &str,
|
|
) {
|
|
if let Some(secret) = object.get(field).and_then(serde_json::Value::as_str) {
|
|
push_secret(secrets, connection_id, key, secret);
|
|
}
|
|
}
|
|
|
|
fn push_secret(secrets: &mut Vec<ConnectionSecretSnapshot>, connection_id: &str, key: &str, secret: &str) {
|
|
if secret.is_empty() {
|
|
return;
|
|
}
|
|
secrets.push(ConnectionSecretSnapshot {
|
|
connection_id: connection_id.to_string(),
|
|
key: key.to_string(),
|
|
secret: secret.to_string(),
|
|
});
|
|
}
|
|
|
|
fn scrub_nacos_auth_secrets(config: &mut ConnectionConfig) {
|
|
if config.db_type != DatabaseType::Nacos {
|
|
return;
|
|
}
|
|
if let Some(auth) = config
|
|
.external_config
|
|
.as_mut()
|
|
.and_then(|external_config| external_config.get_mut("auth"))
|
|
.and_then(serde_json::Value::as_object_mut)
|
|
{
|
|
if auth.get("kind").and_then(serde_json::Value::as_str) == Some("usernamePassword") {
|
|
scrub_json_secret(auth, "password");
|
|
}
|
|
}
|
|
if let Some(auth) = config
|
|
.external_config
|
|
.as_mut()
|
|
.and_then(|external_config| external_config.get_mut("rnacosConsoleAuth"))
|
|
.and_then(serde_json::Value::as_object_mut)
|
|
{
|
|
if auth.get("kind").and_then(serde_json::Value::as_str) == Some("usernamePassword") {
|
|
scrub_json_secret(auth, "password");
|
|
}
|
|
}
|
|
}
|
|
|
|
fn scrub_json_secret(object: &mut serde_json::Map<String, serde_json::Value>, field: &str) {
|
|
if object.contains_key(field) {
|
|
object.insert(field.to_string(), serde_json::Value::String(String::new()));
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
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)
|
|
&& !secret.key.starts_with(TRANSPORT_LAYER_SECRET_PREFIX)
|
|
&& !secret.key.starts_with(PLUGIN_CONNECTION_SECRET_PREFIX)
|
|
{
|
|
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 matches!(secret.key.as_str(), "password" | NACOS_AUTH_PASSWORD_KEY | NACOS_RNACOS_CONSOLE_PASSWORD_KEY)
|
|
&& 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 {
|
|
// New format: save directly (empty = all configs were deleted)
|
|
storage.save_ai_configs(configs).await?;
|
|
} else if let Some(old_config) = &payload.ai_config {
|
|
// A legacy snapshot still represents the complete AI configuration state.
|
|
// Replace local configs just like the new list format so a generated ID
|
|
// cannot conflict with an existing config that has the same name.
|
|
let provider_name = old_config.provider.as_str().to_string();
|
|
let item = AiConfigItem {
|
|
id: AiConfigItem::new_id(),
|
|
name: provider_name,
|
|
is_default: true,
|
|
config: old_config.clone(),
|
|
};
|
|
storage.save_ai_configs(&[item]).await?;
|
|
}
|
|
if let Some(profiles) = &payload.tunnel_profiles {
|
|
storage.save_tunnel_profiles(profiles).await?;
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn validate_sensitive_payload(payload: &SensitiveSyncPayload) -> Result<(), String> {
|
|
let mut seen = HashSet::new();
|
|
for secret in &payload.connection_secrets {
|
|
if secret.connection_id.trim().is_empty() || secret.key.trim().is_empty() {
|
|
return Err("Synced secrets contain an empty connection id or key.".to_string());
|
|
}
|
|
let accepted = SECRET_KEYS.contains(&secret.key.as_str())
|
|
|| secret.key.starts_with(SSH_TUNNEL_SECRET_PREFIX)
|
|
|| secret.key.starts_with(TRANSPORT_LAYER_SECRET_PREFIX)
|
|
|| secret.key.starts_with(PLUGIN_CONNECTION_SECRET_PREFIX);
|
|
if !accepted {
|
|
return Err(format!("Synced secrets contain unsupported key '{}'.", secret.key));
|
|
}
|
|
if secret.key.starts_with(PLUGIN_CONNECTION_SECRET_PREFIX)
|
|
&& secret.key.len() == PLUGIN_CONNECTION_SECRET_PREFIX.len()
|
|
{
|
|
return Err("Synced plugin secrets contain an empty field name.".to_string());
|
|
}
|
|
if !seen.insert((&secret.connection_id, &secret.key)) {
|
|
return Err(format!("Synced secrets contain a duplicate key: {} / {}", secret.connection_id, secret.key));
|
|
}
|
|
}
|
|
if let Some(configs) = &payload.ai_configs {
|
|
let mut ids = HashSet::new();
|
|
for config in configs {
|
|
if config.id.trim().is_empty() {
|
|
return Err("Synced AI configs contain an empty id.".to_string());
|
|
}
|
|
if !ids.insert(config.id.as_str()) {
|
|
return Err(format!("Synced AI configs contain duplicate id '{}'.", config.id));
|
|
}
|
|
}
|
|
}
|
|
let mut seen_accounts = HashSet::new();
|
|
for credential in payload.sync_credentials.iter().flatten() {
|
|
let account = credential.account.trim();
|
|
if account.is_empty() || account.len() > 512 || account.contains('\0') {
|
|
return Err("Synced credentials contain an invalid account name.".to_string());
|
|
}
|
|
if credential.secret.is_empty() {
|
|
return Err(format!("Synced credential '{account}' has an empty secret."));
|
|
}
|
|
if !seen_accounts.insert(account) {
|
|
return Err(format!("Synced credentials contain duplicate account '{account}'."));
|
|
}
|
|
}
|
|
if let Some(profiles) = &payload.tunnel_profiles {
|
|
let mut profile_ids = HashSet::new();
|
|
for profile in profiles {
|
|
let id = profile.id().trim();
|
|
if id.is_empty() {
|
|
return Err("Synced secrets contain a tunnel profile with an empty id.".to_string());
|
|
}
|
|
if !profile_ids.insert(id) {
|
|
return Err(format!("Synced secrets contain duplicate tunnel profile id '{id}'."));
|
|
}
|
|
}
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn transport_layer_secret_segment(index: usize, layer: &TransportLayerConfig) -> String {
|
|
let id = layer.id().trim();
|
|
if id.is_empty() {
|
|
index.to_string()
|
|
} else {
|
|
id.to_string()
|
|
}
|
|
}
|
|
|
|
fn transport_layer_ssh_password_key(index: usize, layer: &TransportLayerConfig) -> String {
|
|
format!("{}{}.ssh_password", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
|
}
|
|
|
|
fn transport_layer_ssh_key_passphrase_key(index: usize, layer: &TransportLayerConfig) -> String {
|
|
format!("{}{}.ssh_key_passphrase", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
|
}
|
|
|
|
fn transport_layer_proxy_password_key(index: usize, layer: &TransportLayerConfig) -> String {
|
|
format!("{}{}.proxy_password", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
|
}
|
|
|
|
fn transport_layer_http_tunnel_token_key(index: usize, layer: &TransportLayerConfig) -> String {
|
|
format!("{}{}.http_tunnel_token", TRANSPORT_LAYER_SECRET_PREFIX, transport_layer_secret_segment(index, layer))
|
|
}
|
|
|
|
fn encrypt_sensitive_payload(payload: &SensitiveSyncPayload, passphrase: &str) -> Result<EncryptedSecretsBlob, String> {
|
|
let plaintext = serde_json::to_vec(payload).map_err(|e| e.to_string())?;
|
|
encrypt_bytes_with_secret_context(&plaintext, passphrase, SENSITIVE_PAYLOAD_TYPE)
|
|
}
|
|
|
|
fn decrypt_sensitive_payload(blob: &EncryptedSecretsBlob, passphrase: &str) -> Result<SensitiveSyncPayload, String> {
|
|
let plaintext = decrypt_sensitive_bytes(blob, passphrase)
|
|
.map_err(|_| "Failed to decrypt synced secrets. Check the sync password.".to_string())?;
|
|
serde_json::from_slice(&plaintext).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn encrypt_snippet_snapshot(snapshot: &SyncSnapshot, passphrase: &str) -> Result<EncryptedSnippetSnapshot, String> {
|
|
let plaintext = serde_json::to_vec(snapshot).map_err(|e| e.to_string())?;
|
|
Ok(EncryptedSnippetSnapshot {
|
|
format: ENCRYPTED_SNIPPET_SNAPSHOT_FORMAT.to_string(),
|
|
version: ENCRYPTED_SNIPPET_SNAPSHOT_VERSION,
|
|
payload: encrypt_bytes_with_secret(&plaintext, passphrase)?,
|
|
})
|
|
}
|
|
|
|
fn parse_snippet_snapshot(content: &str, secrets_passphrase: Option<&str>) -> Result<SyncSnapshot, String> {
|
|
if is_encrypted_snippet_snapshot(content) {
|
|
let envelope: EncryptedSnippetSnapshot = serde_json::from_str(content).map_err(|e| e.to_string())?;
|
|
if envelope.format != ENCRYPTED_SNIPPET_SNAPSHOT_FORMAT
|
|
|| envelope.version != ENCRYPTED_SNIPPET_SNAPSHOT_VERSION
|
|
{
|
|
return Err("Unsupported encrypted sync snapshot format".to_string());
|
|
}
|
|
let passphrase = required_snippet_passphrase(secrets_passphrase)?;
|
|
let plaintext = decrypt_bytes_with_secret(&envelope.payload, passphrase)
|
|
.map_err(|_| "Failed to decrypt the synced snapshot. Check the snippet encryption password.".to_string())?;
|
|
return serde_json::from_slice(&plaintext).map_err(|e| e.to_string());
|
|
}
|
|
serde_json::from_str(content).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn is_encrypted_snippet_snapshot(content: &str) -> bool {
|
|
serde_json::from_str::<EncryptedSnippetSnapshot>(content)
|
|
.ok()
|
|
.is_some_and(|envelope| envelope.format == ENCRYPTED_SNIPPET_SNAPSHOT_FORMAT)
|
|
}
|
|
|
|
/// Keep the migration guard deliberately more tolerant than deserializing the
|
|
/// current `SyncSnapshot`: older DBX releases may not contain fields added
|
|
/// since their snapshot was written. At the same time, require the stable DBX
|
|
/// snapshot markers before a destructive remote delete is allowed.
|
|
fn is_legacy_dbx_snapshot(content: &str) -> bool {
|
|
serde_json::from_str::<serde_json::Value>(content).ok().is_some_and(|snapshot| {
|
|
snapshot.get("schemaVersion").and_then(serde_json::Value::as_u64).is_some()
|
|
&& snapshot.get("exportedAt").and_then(serde_json::Value::as_str).is_some()
|
|
&& snapshot.get("appVersion").and_then(serde_json::Value::as_str).is_some()
|
|
&& snapshot.get("connections").is_some_and(serde_json::Value::is_array)
|
|
&& snapshot.get("savedSql").is_some_and(serde_json::Value::is_object)
|
|
&& snapshot.get("desktopSettings").is_some_and(serde_json::Value::is_object)
|
|
})
|
|
}
|
|
|
|
fn parse_legacy_dbx_snapshot(content: &str) -> Result<SyncSnapshot, String> {
|
|
if !is_legacy_dbx_snapshot(content) {
|
|
return Err("The selected snippet is not a DBX sync snapshot; refusing to replace or delete it.".to_string());
|
|
}
|
|
serde_json::from_str(content).map_err(|_| {
|
|
"The legacy DBX snapshot is incompatible with this version, so it will not be replaced or deleted.".to_string()
|
|
})
|
|
}
|
|
|
|
fn prepare_legacy_snippet_snapshot(
|
|
mut snapshot: SyncSnapshot,
|
|
secrets_passphrase: Option<&str>,
|
|
) -> Result<SyncSnapshot, String> {
|
|
if let Some(encrypted_secrets) = snapshot.encrypted_secrets.as_ref() {
|
|
// The legacy snapshot can contain an independently encrypted secrets
|
|
// payload. Verify it with its own password before deleting the only
|
|
// legacy copy; the outer snippet password is intentionally separate.
|
|
let passphrase = required_sync_passphrase(secrets_passphrase)?;
|
|
let secrets = decrypt_sensitive_payload(encrypted_secrets, passphrase).map_err(|_| {
|
|
"The legacy snapshot contains encrypted secrets that cannot be verified with this sync password, so it will not be replaced or deleted."
|
|
.to_string()
|
|
})?;
|
|
snapshot.encrypted_secrets = Some(encrypt_sensitive_payload(&secrets, passphrase)?);
|
|
}
|
|
Ok(snapshot)
|
|
}
|
|
|
|
fn snapshot_for_snippet_upload<'a>(
|
|
local_snapshot: &'a SyncSnapshot,
|
|
legacy_snapshot: Option<&'a SyncSnapshot>,
|
|
) -> &'a SyncSnapshot {
|
|
legacy_snapshot.unwrap_or(local_snapshot)
|
|
}
|
|
|
|
fn encrypt_text_with_secret(value: &str, secret: &str) -> Result<EncryptedSecretsBlob, String> {
|
|
encrypt_bytes_with_secret(value.as_bytes(), secret)
|
|
}
|
|
|
|
fn decrypt_text_with_secret(blob: &EncryptedSecretsBlob, secret: &str) -> Result<String, String> {
|
|
let plaintext = decrypt_bytes_with_secret(blob, secret)?;
|
|
String::from_utf8(plaintext).map_err(|e| e.to_string())
|
|
}
|
|
|
|
fn encrypt_bytes_with_secret(plaintext: &[u8], secret: &str) -> Result<EncryptedSecretsBlob, String> {
|
|
let mut salt = [0u8; 16];
|
|
let mut nonce = [0u8; 12];
|
|
OsRng.fill_bytes(&mut salt);
|
|
OsRng.fill_bytes(&mut nonce);
|
|
let key = derive_secret_key(secret, &salt)?;
|
|
let cipher = Aes256Gcm::new_from_slice(&key).map_err(|e| e.to_string())?;
|
|
let ciphertext = cipher.encrypt(Nonce::from_slice(&nonce), plaintext).map_err(|e| e.to_string())?;
|
|
Ok(EncryptedSecretsBlob {
|
|
version: 1,
|
|
kdf: "argon2id".to_string(),
|
|
cipher: "aes-256-gcm".to_string(),
|
|
salt: BASE64.encode(salt),
|
|
nonce: BASE64.encode(nonce),
|
|
ciphertext: BASE64.encode(ciphertext),
|
|
payload_type: None,
|
|
aad: None,
|
|
})
|
|
}
|
|
|
|
fn encrypt_bytes_with_secret_context(
|
|
plaintext: &[u8],
|
|
secret: &str,
|
|
context: &str,
|
|
) -> Result<EncryptedSecretsBlob, String> {
|
|
let mut salt = [0u8; 16];
|
|
let mut nonce = [0u8; 12];
|
|
OsRng.fill_bytes(&mut salt);
|
|
OsRng.fill_bytes(&mut nonce);
|
|
let key = derive_secret_key(secret, &salt)?;
|
|
let cipher = Aes256Gcm::new_from_slice(&key).map_err(|e| e.to_string())?;
|
|
let ciphertext = cipher
|
|
.encrypt(Nonce::from_slice(&nonce), aes_gcm::aead::Payload { msg: plaintext, aad: context.as_bytes() })
|
|
.map_err(|e| e.to_string())?;
|
|
Ok(EncryptedSecretsBlob {
|
|
version: SENSITIVE_PAYLOAD_VERSION,
|
|
kdf: "argon2id".to_string(),
|
|
cipher: "aes-256-gcm".to_string(),
|
|
salt: BASE64.encode(salt),
|
|
nonce: BASE64.encode(nonce),
|
|
ciphertext: BASE64.encode(ciphertext),
|
|
payload_type: Some(context.to_string()),
|
|
aad: Some(context.to_string()),
|
|
})
|
|
}
|
|
|
|
fn decrypt_sensitive_bytes(blob: &EncryptedSecretsBlob, secret: &str) -> Result<Vec<u8>, String> {
|
|
if blob.version == 1 {
|
|
return decrypt_bytes_with_secret(blob, secret);
|
|
}
|
|
if blob.version != SENSITIVE_PAYLOAD_VERSION
|
|
|| blob.kdf != "argon2id"
|
|
|| blob.cipher != "aes-256-gcm"
|
|
|| blob.payload_type.as_deref() != Some(SENSITIVE_PAYLOAD_TYPE)
|
|
|| blob.aad.as_deref() != Some(SENSITIVE_PAYLOAD_TYPE)
|
|
{
|
|
return Err("Unsupported encrypted secrets format".to_string());
|
|
}
|
|
let salt = BASE64.decode(&blob.salt).map_err(|e| e.to_string())?;
|
|
let nonce = BASE64.decode(&blob.nonce).map_err(|e| e.to_string())?;
|
|
let ciphertext = BASE64.decode(&blob.ciphertext).map_err(|e| e.to_string())?;
|
|
if nonce.len() != 12 {
|
|
return Err("Invalid encrypted secrets nonce".to_string());
|
|
}
|
|
let key = derive_secret_key(secret, &salt)?;
|
|
let cipher = Aes256Gcm::new_from_slice(&key).map_err(|e| e.to_string())?;
|
|
cipher
|
|
.decrypt(
|
|
Nonce::from_slice(&nonce),
|
|
aes_gcm::aead::Payload { msg: ciphertext.as_ref(), aad: SENSITIVE_PAYLOAD_TYPE.as_bytes() },
|
|
)
|
|
.map_err(|_| "Failed to decrypt synced secrets.".to_string())
|
|
}
|
|
|
|
fn decrypt_bytes_with_secret(blob: &EncryptedSecretsBlob, secret: &str) -> Result<Vec<u8>, String> {
|
|
if blob.version != 1 || blob.kdf != "argon2id" || blob.cipher != "aes-256-gcm" {
|
|
return Err("Unsupported encrypted secrets format".to_string());
|
|
}
|
|
let salt = BASE64.decode(&blob.salt).map_err(|e| e.to_string())?;
|
|
let nonce = BASE64.decode(&blob.nonce).map_err(|e| e.to_string())?;
|
|
let ciphertext = BASE64.decode(&blob.ciphertext).map_err(|e| e.to_string())?;
|
|
if nonce.len() != 12 {
|
|
return Err("Invalid encrypted secrets nonce".to_string());
|
|
}
|
|
let key = derive_secret_key(secret, &salt)?;
|
|
let cipher = Aes256Gcm::new_from_slice(&key).map_err(|e| e.to_string())?;
|
|
cipher
|
|
.decrypt(Nonce::from_slice(&nonce), ciphertext.as_ref())
|
|
.map_err(|_| "Failed to decrypt saved secret.".to_string())
|
|
}
|
|
|
|
fn derive_secret_key(passphrase: &str, salt: &[u8]) -> Result<[u8; 32], String> {
|
|
let params = Params::new(19 * 1024, 2, 1, Some(32)).map_err(|e| e.to_string())?;
|
|
let argon2 = Argon2::new(Algorithm::Argon2id, Version::V0x13, params);
|
|
let mut key = [0u8; 32];
|
|
argon2.hash_password_into(passphrase.as_bytes(), salt, &mut key).map_err(|e| e.to_string())?;
|
|
Ok(key)
|
|
}
|
|
|
|
fn normalized_passphrase(passphrase: Option<&str>) -> Option<&str> {
|
|
passphrase.map(str::trim).filter(|value| !value.is_empty())
|
|
}
|
|
|
|
fn normalized_snippet_id(snippet_id: Option<&str>) -> Option<&str> {
|
|
snippet_id.map(str::trim).filter(|value| !value.is_empty())
|
|
}
|
|
|
|
fn content_hash(content: &str) -> String {
|
|
let mut hasher = Sha256::new();
|
|
hasher.update(content.as_bytes());
|
|
format!("{:x}", hasher.finalize())
|
|
}
|
|
|
|
fn snippet_pending_cleanup(summary: &SnippetSyncSummary) -> Result<Option<SnippetPendingCleanup>, String> {
|
|
match (summary.legacy_cleanup_required_id.as_deref(), summary.legacy_cleanup_expected_content_hash.as_deref()) {
|
|
(None, None) => Ok(None),
|
|
(Some(snippet_id), Some(expected_content_hash)) => Ok(Some(SnippetPendingCleanup {
|
|
snippet_id: snippet_id.to_string(),
|
|
expected_content_hash: expected_content_hash.to_string(),
|
|
})),
|
|
_ => Err("Legacy snippet cleanup is missing its persisted verification state.".to_string()),
|
|
}
|
|
}
|
|
|
|
fn required_snippet_passphrase(passphrase: Option<&str>) -> Result<&str, String> {
|
|
normalized_passphrase(passphrase)
|
|
.ok_or_else(|| "A snippet encryption password is required for snippet sync.".to_string())
|
|
}
|
|
|
|
fn required_sync_passphrase(passphrase: Option<&str>) -> Result<&str, String> {
|
|
normalized_passphrase(passphrase).ok_or_else(|| "A sync password is required for snippet sync.".to_string())
|
|
}
|
|
|
|
fn gitlab_instance_url(value: Option<&str>) -> Result<String, String> {
|
|
let value = value.unwrap_or(GITLAB_DEFAULT_INSTANCE).trim();
|
|
let url = Url::parse(value).map_err(|_| "Enter a valid GitLab HTTPS instance URL".to_string())?;
|
|
if !matches!(url.scheme(), "https" | "http")
|
|
|| url.host_str().is_none()
|
|
|| !url.username().is_empty()
|
|
|| url.password().is_some()
|
|
|| url.query().is_some()
|
|
|| url.fragment().is_some()
|
|
{
|
|
return Err("GitLab instance must be an HTTP or HTTPS URL without credentials, query, or fragment".to_string());
|
|
}
|
|
Ok(url.as_str().trim_end_matches('/').to_string())
|
|
}
|
|
|
|
fn snippet_provider_storage_key(provider: SnippetProvider, instance_url: Option<&str>) -> Result<String, String> {
|
|
Ok(match provider {
|
|
SnippetProvider::GitHub => "github".to_string(),
|
|
SnippetProvider::Gitee => "gitee".to_string(),
|
|
SnippetProvider::GitLab => {
|
|
format!("gitlab:{:x}", Sha256::digest(gitlab_instance_url(instance_url)?.as_bytes()))
|
|
}
|
|
})
|
|
}
|
|
|
|
fn snippet_token_account(config: &SnippetSyncConfig) -> Result<String, String> {
|
|
Ok(format!("snippet-token:{}", snippet_provider_storage_key(config.provider, config.instance_url.as_deref())?))
|
|
}
|
|
|
|
fn validate_snippet_id(provider: SnippetProvider, id: &str) -> Result<(), String> {
|
|
if provider == SnippetProvider::GitLab
|
|
&& (id.is_empty() || id.bytes().all(|byte| byte == b'0') || !id.bytes().all(|byte| byte.is_ascii_digit()))
|
|
{
|
|
return Err("GitLab snippet ID must be a positive numeric ID".to_string());
|
|
}
|
|
Ok(())
|
|
}
|
|
|
|
fn ensure_snippet_success(status: StatusCode, operation: &str) -> Result<(), String> {
|
|
if status.is_success() {
|
|
Ok(())
|
|
} else {
|
|
Err(format!("Snippet {operation} failed with HTTP {status}"))
|
|
}
|
|
}
|
|
|
|
fn ensure_snippet_response_success(status: StatusCode, operation: &str, response_body: &str) -> Result<(), String> {
|
|
if status.is_success() {
|
|
return Ok(());
|
|
}
|
|
let message = serde_json::from_str::<serde_json::Value>(response_body)
|
|
.ok()
|
|
.and_then(|value| {
|
|
value
|
|
.get("message")
|
|
.or_else(|| value.get("error_description"))
|
|
.and_then(serde_json::Value::as_str)
|
|
.map(str::to_string)
|
|
})
|
|
.unwrap_or_else(|| response_body.trim().chars().take(300).collect());
|
|
if message.is_empty() {
|
|
Err(format!("Snippet {operation} failed with HTTP {status}"))
|
|
} else {
|
|
Err(format!("Snippet {operation} failed with HTTP {status}: {message}"))
|
|
}
|
|
}
|
|
|
|
fn snippet_response_id(value: &serde_json::Value) -> Option<String> {
|
|
if let Some(id) = value.get("id").and_then(serde_json::Value::as_str) {
|
|
return Some(id.to_string());
|
|
}
|
|
if let Some(id) = value.get("id").and_then(serde_json::Value::as_u64) {
|
|
return Some(id.to_string());
|
|
}
|
|
// Some Gitee endpoints historically document an array response despite returning one created snippet.
|
|
value.as_array()?.first()?.get("id")?.as_str().map(str::to_string)
|
|
}
|
|
|
|
fn gitee_snippet_payload(content: String) -> serde_json::Value {
|
|
serde_json::json!({
|
|
"description": "DBX configuration sync",
|
|
"public": false,
|
|
"files": { DEFAULT_SNIPPET_FILE_NAME: { "content": content } }
|
|
})
|
|
}
|
|
|
|
fn snippet_file_content(
|
|
value: &serde_json::Value,
|
|
file_name: &str,
|
|
) -> Result<(Option<String>, Option<String>), String> {
|
|
let files = value.get("files").ok_or_else(|| "Snippet response did not include files".to_string())?;
|
|
let files = if let Some(value) = files.as_str() {
|
|
serde_json::from_str::<serde_json::Value>(value).map_err(|e| e.to_string())?
|
|
} else {
|
|
files.clone()
|
|
};
|
|
let file = files
|
|
.get(file_name)
|
|
.or_else(|| files.as_object().and_then(|files| files.values().next()))
|
|
.ok_or_else(|| format!("Snippet does not contain {file_name}"))?;
|
|
let truncated = file.get("truncated").and_then(serde_json::Value::as_bool).unwrap_or(false);
|
|
let content =
|
|
if truncated { None } else { file.get("content").and_then(serde_json::Value::as_str).map(str::to_string) };
|
|
let raw_url = file.get("raw_url").and_then(serde_json::Value::as_str).map(str::to_string);
|
|
Ok((content, raw_url))
|
|
}
|
|
|
|
fn normalized_remote_path(value: Option<&str>) -> String {
|
|
let value = value.unwrap_or(DEFAULT_REMOTE_PATH).trim().replace('\\', "/");
|
|
let mut parts: Vec<&str> = Vec::new();
|
|
for part in value.split('/') {
|
|
let part = part.trim();
|
|
if part.is_empty() || part == "." {
|
|
continue;
|
|
}
|
|
if part == ".." {
|
|
// Keep the WebDAV target inside the configured endpoint when users paste OS paths.
|
|
parts.pop();
|
|
continue;
|
|
}
|
|
parts.push(part);
|
|
}
|
|
|
|
if parts.is_empty() {
|
|
DEFAULT_REMOTE_PATH.to_string()
|
|
} else {
|
|
parts.join("/")
|
|
}
|
|
}
|
|
|
|
fn webdav_endpoint_uses_direct_connection(endpoint: &str) -> bool {
|
|
let Ok(url) = Url::parse(endpoint.trim()) else {
|
|
return false;
|
|
};
|
|
let Some(host) = url.host_str() else {
|
|
return false;
|
|
};
|
|
let host = host.strip_prefix('[').and_then(|host| host.strip_suffix(']')).unwrap_or(host);
|
|
if host.rsplit('.').next().is_some_and(|label| label.eq_ignore_ascii_case("localhost")) {
|
|
return true;
|
|
}
|
|
host.parse::<IpAddr>().is_ok_and(webdav_ip_uses_direct_connection)
|
|
}
|
|
|
|
fn webdav_ip_uses_direct_connection(address: IpAddr) -> bool {
|
|
match address {
|
|
IpAddr::V4(address) => webdav_ipv4_uses_direct_connection(address),
|
|
IpAddr::V6(address) => {
|
|
address.is_loopback()
|
|
|| address.is_unspecified()
|
|
|| address.is_unique_local()
|
|
|| address.is_unicast_link_local()
|
|
|| address.to_ipv4_mapped().is_some_and(webdav_ipv4_uses_direct_connection)
|
|
}
|
|
}
|
|
}
|
|
|
|
fn webdav_ipv4_uses_direct_connection(address: Ipv4Addr) -> bool {
|
|
let [first, second, _, _] = address.octets();
|
|
address.is_private()
|
|
|| address.is_loopback()
|
|
|| address.is_link_local()
|
|
|| address.is_unspecified()
|
|
|| (first == 100 && (64..=127).contains(&second))
|
|
}
|
|
|
|
fn parent_collection_paths(remote_path: &str) -> Vec<String> {
|
|
let parts = remote_path.trim_matches('/').split('/').filter(|part| !part.is_empty()).collect::<Vec<_>>();
|
|
if parts.len() <= 1 {
|
|
return Vec::new();
|
|
}
|
|
|
|
let mut paths = Vec::with_capacity(parts.len() - 1);
|
|
for index in 1..parts.len() {
|
|
paths.push(parts[..index].join("/"));
|
|
}
|
|
paths
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::{
|
|
apply_sensitive_payload, apply_sync_snapshot, build_sensitive_payload, build_sync_snapshot,
|
|
build_sync_snapshot_with_options, build_sync_snapshot_with_saved_secrets, decrypt_sensitive_payload,
|
|
encrypt_sensitive_payload, encrypt_snippet_snapshot, finalize_snippet_migration,
|
|
forget_webdav_sync_secrets_passphrase, gitee_snippet_payload, gitlab_instance_url, is_legacy_dbx_snapshot,
|
|
normalized_remote_path, parent_collection_paths, parse_legacy_dbx_snapshot, parse_snippet_snapshot,
|
|
prepare_legacy_snippet_snapshot, resolve_snippet_token, resolve_webdav_password,
|
|
resolve_webdav_sync_secrets_passphrase, retry_pending_snippet_cleanup, save_snippet_sync_id,
|
|
save_snippet_sync_id_for_instance, save_snippet_token, save_webdav_password,
|
|
save_webdav_sync_secrets_preference, scrub_connection_secrets, snapshot_for_snippet_upload,
|
|
snippet_file_content, snippet_provider_storage_key, snippet_response_id, snippet_saved_token_status,
|
|
snippet_sync_settings, snippet_sync_settings_for_instance, validate_snippet_id,
|
|
webdav_endpoint_uses_direct_connection, webdav_sync_secrets_status, ApplySnapshotOptions,
|
|
ConnectionSecretSnapshot, SensitiveSyncPayload, SnippetProvider, SnippetSyncClient, SnippetSyncConfig,
|
|
SyncExportOptions, WebDavClient, WebDavConfig, DEFAULT_SNIPPET_FILE_NAME, LEGACY_SNAPSHOT_SCHEMA_VERSION,
|
|
};
|
|
use crate::ai::{AiApiStyle, AiAuthMethod, AiConfig, AiConfigItem};
|
|
use crate::connection_secrets::{
|
|
CASSANDRA_KEYSTORE_PASSWORD_KEY, CASSANDRA_TRUSTSTORE_PASSWORD_KEY, NACOS_AUTH_PASSWORD_KEY,
|
|
NACOS_RNACOS_CONSOLE_PASSWORD_KEY, PLUGIN_CONNECTION_SECRET_PREFIX,
|
|
};
|
|
use crate::models::connection::{
|
|
default_redis_key_separator, ConnectionConfig, DatabaseType, SshTunnelConfig, TransportLayerConfig,
|
|
};
|
|
use crate::persistence::secret_codec::{managed_key_path, SecretKeyPolicy};
|
|
use crate::storage::Storage;
|
|
|
|
fn make_test_config(name: &str, is_default: bool) -> AiConfigItem {
|
|
AiConfigItem {
|
|
id: format!("cfg-{name}"),
|
|
name: name.to_string(),
|
|
is_default,
|
|
config: AiConfig {
|
|
provider: crate::ai::AiProvider::Openai,
|
|
api_key: String::new(),
|
|
auth_method: AiAuthMethod::Bearer,
|
|
endpoint: "https://api.openai.com/v1".to_string(),
|
|
model: "gpt-4o-mini".to_string(),
|
|
models: vec![],
|
|
api_style: AiApiStyle::Completions,
|
|
custom_headers: Default::default(),
|
|
proxy_enabled: false,
|
|
proxy_url: String::new(),
|
|
skip_tls_verify: false,
|
|
enable_thinking: true,
|
|
reasoning_level: crate::ai::AiReasoningLevel::Default,
|
|
max_output_tokens: None,
|
|
runtime_effort: None,
|
|
context_window: None,
|
|
max_retries: None,
|
|
codex_cli_path: None,
|
|
codex_cli_env: Default::default(),
|
|
claude_code_cli_path: None,
|
|
claude_code_cli_env: Default::default(),
|
|
pi_agent_cli_path: None,
|
|
pi_agent_cli_env: Default::default(),
|
|
opencode_cli_path: None,
|
|
opencode_cli_env: Default::default(),
|
|
cursor_cli_path: None,
|
|
cursor_cli_env: Default::default(),
|
|
grok_cli_path: None,
|
|
grok_cli_env: Default::default(),
|
|
codebuddy_cli_path: None,
|
|
codebuddy_cli_env: Default::default(),
|
|
qoder_cli_path: None,
|
|
qoder_cli_env: Default::default(),
|
|
},
|
|
}
|
|
}
|
|
|
|
fn temp_db_path(name: &str) -> std::path::PathBuf {
|
|
std::env::temp_dir().join(format!("dbx-cloud-sync-{name}-{}.db", uuid::Uuid::new_v4()))
|
|
}
|
|
|
|
async fn spawn_snippet_server(responses: Vec<String>) -> (String, tokio::task::JoinHandle<Vec<String>>) {
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let address = listener.local_addr().unwrap();
|
|
let server = tokio::spawn(async move {
|
|
let mut methods = Vec::new();
|
|
for body in responses {
|
|
let (mut socket, _) = listener.accept().await.unwrap();
|
|
let mut request = Vec::new();
|
|
let mut chunk = [0_u8; 4096];
|
|
while !request.windows(4).any(|window| window == b"\r\n\r\n") {
|
|
let read = socket.read(&mut chunk).await.unwrap();
|
|
assert!(read > 0, "request ended before headers were complete");
|
|
request.extend_from_slice(&chunk[..read]);
|
|
}
|
|
let request = String::from_utf8(request).unwrap();
|
|
methods.push(request.lines().next().unwrap().to_string());
|
|
let response = format!(
|
|
"HTTP/1.1 200 OK\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
|
body.len()
|
|
);
|
|
socket.write_all(response.as_bytes()).await.unwrap();
|
|
}
|
|
methods
|
|
});
|
|
(format!("http://{address}"), server)
|
|
}
|
|
|
|
async fn spawn_gitlab_server(responses: Vec<String>) -> (String, tokio::task::JoinHandle<Vec<String>>) {
|
|
spawn_gitlab_server_with_status(responses.into_iter().map(|body| (200, body)).collect()).await
|
|
}
|
|
|
|
#[allow(clippy::type_complexity)]
|
|
async fn spawn_gitlab_server_with_status(
|
|
responses: Vec<(u16, String)>,
|
|
) -> (String, tokio::task::JoinHandle<Vec<String>>) {
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let address = listener.local_addr().unwrap();
|
|
let server = tokio::spawn(async move {
|
|
let mut requests = Vec::new();
|
|
for response in responses {
|
|
let (mut socket, _) = listener.accept().await.unwrap();
|
|
let mut request = Vec::new();
|
|
let header_end = loop {
|
|
let mut chunk = [0_u8; 4096];
|
|
let size = socket.read(&mut chunk).await.unwrap();
|
|
assert!(size > 0, "request ended before headers were complete");
|
|
request.extend_from_slice(&chunk[..size]);
|
|
if let Some(pos) = request.windows(4).position(|window| window == b"\r\n\r\n") {
|
|
break pos + 4;
|
|
}
|
|
};
|
|
let headers = String::from_utf8_lossy(&request[..header_end]);
|
|
let content_length = headers
|
|
.lines()
|
|
.find_map(|line| {
|
|
line.to_ascii_lowercase()
|
|
.strip_prefix("content-length: ")
|
|
.and_then(|value| value.trim().parse::<usize>().ok())
|
|
})
|
|
.unwrap_or(0);
|
|
while request.len() - header_end < content_length {
|
|
let mut chunk = [0_u8; 4096];
|
|
let size = socket.read(&mut chunk).await.unwrap();
|
|
assert!(size > 0, "request ended before body was complete");
|
|
request.extend_from_slice(&chunk[..size]);
|
|
}
|
|
requests.push(String::from_utf8(request).unwrap());
|
|
let (status, body) = response;
|
|
let body = body.replace("{SERVER_BASE}", &format!("http://{address}"));
|
|
let reason = match status {
|
|
200 => "OK",
|
|
404 => "Not Found",
|
|
_ => "Status",
|
|
};
|
|
let response = format!(
|
|
"HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n{body}",
|
|
body.len()
|
|
);
|
|
socket.write_all(response.as_bytes()).await.unwrap();
|
|
}
|
|
requests
|
|
});
|
|
(format!("http://{address}/api/v4"), server)
|
|
}
|
|
|
|
async fn spawn_webdav_server(
|
|
responses: Vec<u16>,
|
|
reject_non_collection_mkcol: bool,
|
|
) -> (String, tokio::task::JoinHandle<Vec<String>>) {
|
|
use tokio::io::{AsyncReadExt, AsyncWriteExt};
|
|
|
|
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
|
|
let address = listener.local_addr().unwrap();
|
|
let server = tokio::spawn(async move {
|
|
let mut request_lines = Vec::new();
|
|
for configured_status in responses {
|
|
let (mut socket, _) = listener.accept().await.unwrap();
|
|
let mut request = Vec::new();
|
|
let mut chunk = [0_u8; 4096];
|
|
while !request.windows(4).any(|window| window == b"\r\n\r\n") {
|
|
let read = socket.read(&mut chunk).await.unwrap();
|
|
assert!(read > 0, "request ended before headers were complete");
|
|
request.extend_from_slice(&chunk[..read]);
|
|
}
|
|
let request = String::from_utf8(request).unwrap();
|
|
let request_line = request.lines().next().unwrap().to_string();
|
|
let invalid_nginx_collection_uri = reject_non_collection_mkcol
|
|
&& request_line.starts_with("MKCOL ")
|
|
&& request_line.split_whitespace().nth(1).is_some_and(|target| !target.ends_with('/'));
|
|
let status = if invalid_nginx_collection_uri { 409 } else { configured_status };
|
|
let reason = match status {
|
|
200 => "OK",
|
|
201 => "Created",
|
|
207 => "Multi-Status",
|
|
405 => "Method Not Allowed",
|
|
409 => "Conflict",
|
|
_ => "Test Response",
|
|
};
|
|
request_lines.push(request_line);
|
|
let response = format!("HTTP/1.1 {status} {reason}\r\nContent-Length: 0\r\nConnection: close\r\n\r\n");
|
|
socket.write_all(response.as_bytes()).await.unwrap();
|
|
}
|
|
request_lines
|
|
});
|
|
(format!("http://{address}/"), server)
|
|
}
|
|
|
|
fn github_snippet_response(content: &str) -> String {
|
|
serde_json::json!({
|
|
"files": { DEFAULT_SNIPPET_FILE_NAME: { "content": content } }
|
|
})
|
|
.to_string()
|
|
}
|
|
|
|
#[test]
|
|
fn gitee_snippet_payload_keeps_files_as_nested_object() {
|
|
let payload = gitee_snippet_payload("snapshot".to_string());
|
|
|
|
assert_eq!(payload["files"]["dbx-sync.json"]["content"], "snapshot");
|
|
assert!(payload["files"].is_object());
|
|
assert_eq!(payload["public"], false);
|
|
}
|
|
|
|
fn postgres_connection(id: &str, password: &str) -> ConnectionConfig {
|
|
ConnectionConfig {
|
|
docs_notes_path: None,
|
|
id: id.to_string(),
|
|
name: "Postgres".to_string(),
|
|
note: String::new(),
|
|
db_type: DatabaseType::Postgres,
|
|
driver_profile: None,
|
|
driver_label: None,
|
|
url_params: None,
|
|
agent_java_options: Vec::new(),
|
|
host: "127.0.0.1".to_string(),
|
|
port: 5432,
|
|
username: "app".to_string(),
|
|
password: password.to_string(),
|
|
database: Some("app_db".to_string()),
|
|
default_schema: None,
|
|
visible_databases: None,
|
|
visible_database_patterns: None,
|
|
visible_schemas: None,
|
|
show_system_schemas: false,
|
|
attached_databases: Vec::new(),
|
|
init_script: None,
|
|
color: None,
|
|
transport_layers: Vec::new(),
|
|
connect_timeout_secs: 5,
|
|
query_timeout_secs: 30,
|
|
idle_timeout_secs: 60,
|
|
keepalive_interval_secs: 0,
|
|
ssl: false,
|
|
ca_cert_path: String::new(),
|
|
client_cert_path: String::new(),
|
|
client_key_path: String::new(),
|
|
sysdba: false,
|
|
oracle_connection_type: None,
|
|
connection_string: None,
|
|
redis_connection_mode: None,
|
|
redis_sentinel_master: String::new(),
|
|
redis_sentinel_nodes: String::new(),
|
|
redis_sentinel_username: String::new(),
|
|
redis_sentinel_password: String::new(),
|
|
redis_sentinel_tls: false,
|
|
redis_cluster_nodes: String::new(),
|
|
redis_key_separator: default_redis_key_separator(),
|
|
redis_scan_page_size: None,
|
|
redis_database_aliases: Default::default(),
|
|
redis_key_templates: Vec::new(),
|
|
redis_key_grouping: None,
|
|
etcd_endpoints: String::new(),
|
|
gbase_server: String::new(),
|
|
informix_server: String::new(),
|
|
external_config: None,
|
|
plugin_id: None,
|
|
plugin_connection_provider: None,
|
|
plugin_connection_type: None,
|
|
connection_secrets: Default::default(),
|
|
jdbc_driver_class: None,
|
|
jdbc_driver_paths: Vec::new(),
|
|
one_time: false,
|
|
save_password: true,
|
|
read_only: false,
|
|
is_production: false,
|
|
production_databases: vec![],
|
|
database_info: None,
|
|
}
|
|
}
|
|
|
|
fn cassandra_connection(id: &str) -> ConnectionConfig {
|
|
let mut config = postgres_connection(id, "");
|
|
config.name = "Cassandra".to_string();
|
|
config.db_type = DatabaseType::Cassandra;
|
|
config.port = 9042;
|
|
config.external_config = Some(serde_json::json!({
|
|
"tls": {
|
|
"truststore_path": "/certs/client.truststore",
|
|
"truststore_password": "trust-secret",
|
|
"keystore_path": "/certs/client.keystore",
|
|
"keystore_password": "key-secret"
|
|
}
|
|
}));
|
|
config
|
|
}
|
|
|
|
fn nacos_connection(id: &str, password: &str) -> ConnectionConfig {
|
|
ConnectionConfig {
|
|
docs_notes_path: None,
|
|
id: id.to_string(),
|
|
name: "Nacos".to_string(),
|
|
note: String::new(),
|
|
db_type: DatabaseType::Nacos,
|
|
driver_profile: None,
|
|
driver_label: None,
|
|
url_params: None,
|
|
agent_java_options: Vec::new(),
|
|
host: "127.0.0.1".to_string(),
|
|
port: 8848,
|
|
username: "nacos".to_string(),
|
|
password: String::new(),
|
|
database: None,
|
|
default_schema: None,
|
|
visible_databases: None,
|
|
visible_database_patterns: None,
|
|
visible_schemas: None,
|
|
show_system_schemas: false,
|
|
attached_databases: Vec::new(),
|
|
init_script: None,
|
|
color: None,
|
|
transport_layers: Vec::new(),
|
|
connect_timeout_secs: 5,
|
|
query_timeout_secs: 30,
|
|
idle_timeout_secs: 60,
|
|
keepalive_interval_secs: 0,
|
|
ssl: false,
|
|
ca_cert_path: String::new(),
|
|
client_cert_path: String::new(),
|
|
client_key_path: String::new(),
|
|
sysdba: false,
|
|
oracle_connection_type: None,
|
|
connection_string: None,
|
|
redis_connection_mode: None,
|
|
redis_sentinel_master: String::new(),
|
|
redis_sentinel_nodes: String::new(),
|
|
redis_sentinel_username: String::new(),
|
|
redis_sentinel_password: String::new(),
|
|
redis_sentinel_tls: false,
|
|
redis_cluster_nodes: String::new(),
|
|
redis_key_separator: default_redis_key_separator(),
|
|
redis_scan_page_size: None,
|
|
redis_database_aliases: Default::default(),
|
|
redis_key_templates: Vec::new(),
|
|
redis_key_grouping: None,
|
|
etcd_endpoints: String::new(),
|
|
gbase_server: String::new(),
|
|
informix_server: String::new(),
|
|
external_config: Some(serde_json::json!({
|
|
"namespace": "public",
|
|
"group": "DEFAULT_GROUP",
|
|
"auth": {
|
|
"kind": "usernamePassword",
|
|
"username": "nacos",
|
|
"password": password
|
|
}
|
|
})),
|
|
plugin_id: None,
|
|
plugin_connection_provider: None,
|
|
plugin_connection_type: None,
|
|
connection_secrets: Default::default(),
|
|
jdbc_driver_class: None,
|
|
jdbc_driver_paths: Vec::new(),
|
|
one_time: false,
|
|
save_password: true,
|
|
read_only: false,
|
|
is_production: false,
|
|
production_databases: vec![],
|
|
database_info: None,
|
|
}
|
|
}
|
|
|
|
fn nacos_auth_password(config: &ConnectionConfig) -> Option<&str> {
|
|
config.external_config.as_ref()?.get("auth")?.get("password")?.as_str()
|
|
}
|
|
|
|
#[test]
|
|
fn normalizes_empty_remote_path_to_default() {
|
|
assert_eq!(normalized_remote_path(None), "DBX/sync/snapshot.json");
|
|
assert_eq!(normalized_remote_path(Some("")), "DBX/sync/snapshot.json");
|
|
assert_eq!(normalized_remote_path(Some("///\\\\//")), "DBX/sync/snapshot.json");
|
|
}
|
|
|
|
#[test]
|
|
fn normalizes_remote_path_separators() {
|
|
assert_eq!(normalized_remote_path(Some("/custom/snapshot.json")), "custom/snapshot.json");
|
|
assert_eq!(normalized_remote_path(Some(r"\DBX\sync\snapshot.json")), "DBX/sync/snapshot.json");
|
|
assert_eq!(normalized_remote_path(Some("///DBX//sync/./snapshot.json")), "DBX/sync/snapshot.json");
|
|
assert_eq!(normalized_remote_path(Some("DBX/sync/../snapshot.json")), "DBX/snapshot.json");
|
|
}
|
|
|
|
#[test]
|
|
fn bypasses_proxy_for_local_webdav_endpoints() {
|
|
for endpoint in [
|
|
"http://172.27.31.29:8088/dbx/",
|
|
"https://10.0.0.8/webdav",
|
|
"http://192.168.1.9/",
|
|
"http://100.64.0.1/",
|
|
"http://127.0.0.1:8080/",
|
|
"http://169.254.1.2/",
|
|
"http://localhost:8080/",
|
|
"http://dbx.localhost/",
|
|
"http://[::1]/",
|
|
"http://[fd00::1]/",
|
|
"http://[fe80::1]/",
|
|
"http://[::ffff:172.27.31.29]/",
|
|
] {
|
|
assert!(webdav_endpoint_uses_direct_connection(endpoint), "expected direct WebDAV connection: {endpoint}");
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn preserves_proxy_for_public_webdav_endpoints() {
|
|
for endpoint in [
|
|
"https://dav.example.com/remote.php/dav/files/user/",
|
|
"http://8.8.8.8/webdav/",
|
|
"http://[2606:4700:4700::1111]/webdav/",
|
|
"not a URL",
|
|
] {
|
|
assert!(
|
|
!webdav_endpoint_uses_direct_connection(endpoint),
|
|
"expected configured proxy behavior: {endpoint}"
|
|
);
|
|
}
|
|
}
|
|
|
|
#[test]
|
|
fn returns_parent_collection_paths_from_leaf() {
|
|
assert_eq!(parent_collection_paths("dbx/sync/snapshot.json"), vec!["dbx".to_string(), "dbx/sync".to_string()]);
|
|
assert_eq!(
|
|
parent_collection_paths(&normalized_remote_path(Some(r"\DBX\sync\snapshot.json"))),
|
|
vec!["DBX".to_string(), "DBX/sync".to_string()]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn webdav_upload_terminates_collection_request_paths() {
|
|
let storage = Storage::open(&temp_db_path("webdav-collection-paths")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let (endpoint, server) = spawn_webdav_server(vec![201, 201, 200], true).await;
|
|
let client = WebDavClient::new(WebDavConfig {
|
|
endpoint,
|
|
username: None,
|
|
password: None,
|
|
remote_path: Some("DBX-home/sync/snapshot.json".to_string()),
|
|
});
|
|
|
|
client.put_snapshot(&snapshot).await.unwrap();
|
|
|
|
assert_eq!(
|
|
server.await.unwrap(),
|
|
vec![
|
|
"MKCOL /DBX-home/ HTTP/1.1",
|
|
"MKCOL /DBX-home/sync/ HTTP/1.1",
|
|
"PUT /DBX-home/sync/snapshot.json HTTP/1.1",
|
|
]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn webdav_upload_accepts_existing_collections() {
|
|
let storage = Storage::open(&temp_db_path("webdav-existing-collections")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let (endpoint, server) = spawn_webdav_server(vec![405, 405, 200], false).await;
|
|
let client = WebDavClient::new(WebDavConfig {
|
|
endpoint,
|
|
username: None,
|
|
password: None,
|
|
remote_path: Some("DBX-home/sync/snapshot.json".to_string()),
|
|
});
|
|
|
|
client.put_snapshot(&snapshot).await.unwrap();
|
|
|
|
assert_eq!(
|
|
server.await.unwrap(),
|
|
vec![
|
|
"MKCOL /DBX-home/ HTTP/1.1",
|
|
"MKCOL /DBX-home/sync/ HTTP/1.1",
|
|
"PUT /DBX-home/sync/snapshot.json HTTP/1.1",
|
|
]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn webdav_upload_preserves_collection_conflicts() {
|
|
let storage = Storage::open(&temp_db_path("webdav-collection-conflict")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let (endpoint, server) = spawn_webdav_server(vec![409], false).await;
|
|
let client = WebDavClient::new(WebDavConfig {
|
|
endpoint,
|
|
username: None,
|
|
password: None,
|
|
remote_path: Some("DBX-home/sync/snapshot.json".to_string()),
|
|
});
|
|
|
|
let error = client.put_snapshot(&snapshot).await.unwrap_err();
|
|
|
|
assert_eq!(error, "Failed to create WebDAV collection 'DBX-home' with HTTP 409 Conflict");
|
|
assert_eq!(server.await.unwrap(), vec!["MKCOL /DBX-home/ HTTP/1.1"]);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn webdav_upload_skips_collection_requests_for_single_file() {
|
|
let storage = Storage::open(&temp_db_path("webdav-single-file")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let (endpoint, server) = spawn_webdav_server(vec![200], false).await;
|
|
let client = WebDavClient::new(WebDavConfig {
|
|
endpoint,
|
|
username: None,
|
|
password: None,
|
|
remote_path: Some("snapshot.json".to_string()),
|
|
});
|
|
|
|
client.put_snapshot(&snapshot).await.unwrap();
|
|
|
|
assert_eq!(server.await.unwrap(), vec!["PUT /snapshot.json HTTP/1.1"]);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn webdav_test_keeps_propfind_request() {
|
|
let (endpoint, server) = spawn_webdav_server(vec![207], false).await;
|
|
let client = WebDavClient::new(WebDavConfig {
|
|
endpoint,
|
|
username: None,
|
|
password: None,
|
|
remote_path: Some("DBX-home/sync/snapshot.json".to_string()),
|
|
});
|
|
|
|
client.test().await.unwrap();
|
|
|
|
assert_eq!(server.await.unwrap(), vec!["PROPFIND / HTTP/1.1"]);
|
|
}
|
|
|
|
#[test]
|
|
fn scrubs_connection_secret_fields() {
|
|
let mut config = ConnectionConfig {
|
|
docs_notes_path: None,
|
|
id: "id".to_string(),
|
|
name: "name".to_string(),
|
|
note: String::new(),
|
|
db_type: DatabaseType::Postgres,
|
|
driver_profile: None,
|
|
driver_label: None,
|
|
agent_java_options: Vec::new(),
|
|
host: "localhost".to_string(),
|
|
port: 5432,
|
|
username: "user".to_string(),
|
|
password: "secret".to_string(),
|
|
database: None,
|
|
default_schema: None,
|
|
visible_databases: None,
|
|
visible_database_patterns: None,
|
|
visible_schemas: None,
|
|
show_system_schemas: false,
|
|
attached_databases: Vec::new(),
|
|
init_script: Some("CREATE SECRET (TYPE quack, TOKEN 'token-value');".to_string()),
|
|
color: None,
|
|
transport_layers: vec![
|
|
TransportLayerConfig::Ssh(crate::models::connection::SshTunnelConfig {
|
|
profile_id: String::new(),
|
|
id: "hop-1".to_string(),
|
|
name: String::new(),
|
|
enabled: true,
|
|
host: "bastion".to_string(),
|
|
port: 22,
|
|
user: "user".to_string(),
|
|
password: "hop-password".to_string(),
|
|
key_path: String::new(),
|
|
key_passphrase: "hop-passphrase".to_string(),
|
|
connect_timeout_secs: 5,
|
|
expose_lan: false,
|
|
use_ssh_agent: false,
|
|
ssh_agent_sock_path: String::new(),
|
|
auth_method: "password".to_string(),
|
|
allow_exec_channel_proxy: false,
|
|
}),
|
|
TransportLayerConfig::HttpTunnel(crate::models::connection::HttpTunnelConfig {
|
|
profile_id: String::new(),
|
|
id: "http".to_string(),
|
|
name: String::new(),
|
|
enabled: true,
|
|
url: "https://dbx.example.com/dbx_tunnel.php".to_string(),
|
|
token: "tunnel-token".to_string(),
|
|
connect_timeout_secs: 10,
|
|
}),
|
|
],
|
|
connect_timeout_secs: 5,
|
|
query_timeout_secs: 30,
|
|
idle_timeout_secs: 60,
|
|
keepalive_interval_secs: 0,
|
|
ssl: false,
|
|
ca_cert_path: String::new(),
|
|
client_cert_path: String::new(),
|
|
client_key_path: String::new(),
|
|
sysdba: false,
|
|
oracle_connection_type: None,
|
|
connection_string: Some("postgres://secret".to_string()),
|
|
url_params: Some("applicationName=dbx&PASSWORD=url-secret&sslmode=require".to_string()),
|
|
redis_connection_mode: None,
|
|
redis_sentinel_master: String::new(),
|
|
redis_sentinel_nodes: String::new(),
|
|
redis_sentinel_username: String::new(),
|
|
redis_sentinel_password: "sentinel".to_string(),
|
|
redis_sentinel_tls: false,
|
|
redis_cluster_nodes: String::new(),
|
|
redis_key_separator: default_redis_key_separator(),
|
|
redis_scan_page_size: None,
|
|
redis_database_aliases: Default::default(),
|
|
redis_key_templates: Vec::new(),
|
|
redis_key_grouping: None,
|
|
etcd_endpoints: String::new(),
|
|
gbase_server: String::new(),
|
|
informix_server: String::new(),
|
|
external_config: None,
|
|
plugin_id: None,
|
|
plugin_connection_provider: None,
|
|
plugin_connection_type: None,
|
|
connection_secrets: Default::default(),
|
|
jdbc_driver_class: None,
|
|
jdbc_driver_paths: Vec::new(),
|
|
one_time: false,
|
|
save_password: true,
|
|
read_only: false,
|
|
is_production: false,
|
|
production_databases: vec![],
|
|
database_info: None,
|
|
};
|
|
config.connection_secrets.insert("api_token".to_string(), "plugin-secret".to_string());
|
|
scrub_connection_secrets(&mut config);
|
|
assert!(config.password.is_empty());
|
|
match &config.transport_layers[0] {
|
|
TransportLayerConfig::Ssh(ssh) => {
|
|
assert!(ssh.password.is_empty());
|
|
assert!(ssh.key_passphrase.is_empty());
|
|
}
|
|
_ => panic!("expected ssh layer"),
|
|
}
|
|
match &config.transport_layers[1] {
|
|
TransportLayerConfig::HttpTunnel(http) => assert!(http.token.is_empty()),
|
|
_ => panic!("expected http tunnel layer"),
|
|
}
|
|
assert!(config.redis_sentinel_password.is_empty());
|
|
assert!(config.connection_string.is_none());
|
|
assert_eq!(config.url_params.as_deref(), Some("applicationName=dbx&PASSWORD=&sslmode=require"));
|
|
assert!(config.init_script.is_none());
|
|
assert_eq!(config.connection_secrets.get("api_token").map(String::as_str), None);
|
|
let public_json = serde_json::to_string(&config).unwrap();
|
|
assert!(!public_json.contains("token-value"));
|
|
assert!(!public_json.contains("plugin-secret"));
|
|
assert!(super::SECRET_KEYS.contains(&"init_script"));
|
|
|
|
let mut mqtt = config.clone();
|
|
mqtt.db_type = DatabaseType::Mqtt;
|
|
mqtt.external_config = Some(serde_json::json!({
|
|
"auth": { "kind": "password", "username": "mqtt-user", "password": "mqtt-secret" }
|
|
}));
|
|
scrub_connection_secrets(&mut mqtt);
|
|
assert_eq!(mqtt.external_config.as_ref().unwrap()["auth"]["password"], "");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn cassandra_tls_store_passwords_move_to_sensitive_sync_payload() {
|
|
let config = cassandra_connection("cassandra");
|
|
let mut public_config = config.clone();
|
|
scrub_connection_secrets(&mut public_config);
|
|
let tls =
|
|
public_config.external_config.as_ref().and_then(|external_config| external_config.get("tls")).unwrap();
|
|
assert_eq!(tls["truststore_password"], "");
|
|
assert_eq!(tls["keystore_password"], "");
|
|
|
|
let storage = Storage::open(&temp_db_path("cassandra-sensitive-payload")).await.unwrap();
|
|
let payload = build_sensitive_payload(&storage, &[config], &[]).await.unwrap();
|
|
assert!(payload.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "cassandra"
|
|
&& secret.key == CASSANDRA_TRUSTSTORE_PASSWORD_KEY
|
|
&& secret.secret == "trust-secret"
|
|
}));
|
|
assert!(payload.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "cassandra"
|
|
&& secret.key == CASSANDRA_KEYSTORE_PASSWORD_KEY
|
|
&& secret.secret == "key-secret"
|
|
}));
|
|
}
|
|
|
|
#[test]
|
|
fn encrypted_sensitive_payload_round_trips() {
|
|
let payload = SensitiveSyncPayload {
|
|
tunnel_profiles: None,
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![
|
|
ConnectionSecretSnapshot {
|
|
connection_id: "c1".to_string(),
|
|
key: "password".to_string(),
|
|
secret: "secret".to_string(),
|
|
},
|
|
ConnectionSecretSnapshot {
|
|
connection_id: "c1".to_string(),
|
|
key: "transport_layers.hop-1.ssh_password".to_string(),
|
|
secret: "hop-secret".to_string(),
|
|
},
|
|
],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: None,
|
|
ai_config: None,
|
|
};
|
|
let encrypted = encrypt_sensitive_payload(&payload, "sync-pass").unwrap();
|
|
assert_ne!(encrypted.ciphertext, "secret");
|
|
let decrypted = decrypt_sensitive_payload(&encrypted, "sync-pass").unwrap();
|
|
assert_eq!(decrypted.connection_secrets[0].secret, "secret");
|
|
assert_eq!(decrypted.connection_secrets[1].secret, "hop-secret");
|
|
}
|
|
|
|
#[test]
|
|
fn encrypted_sensitive_payload_rejects_wrong_passphrase() {
|
|
let payload = SensitiveSyncPayload {
|
|
tunnel_profiles: None,
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![ConnectionSecretSnapshot {
|
|
connection_id: "c1".to_string(),
|
|
key: "password".to_string(),
|
|
secret: "secret".to_string(),
|
|
}],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: None,
|
|
ai_config: None,
|
|
};
|
|
let encrypted = encrypt_sensitive_payload(&payload, "sync-pass").unwrap();
|
|
assert!(decrypt_sensitive_payload(&encrypted, "wrong-pass").is_err());
|
|
}
|
|
|
|
#[test]
|
|
fn encrypted_sensitive_payload_keeps_v1_compatibility() {
|
|
let payload = SensitiveSyncPayload {
|
|
tunnel_profiles: None,
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![ConnectionSecretSnapshot {
|
|
connection_id: "legacy".to_string(),
|
|
key: "password".to_string(),
|
|
secret: "legacy-secret".to_string(),
|
|
}],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: None,
|
|
ai_config: None,
|
|
};
|
|
let plaintext = serde_json::to_vec(&payload).unwrap();
|
|
let legacy_blob = super::encrypt_bytes_with_secret(&plaintext, "sync-pass").unwrap();
|
|
assert_eq!(legacy_blob.version, 1);
|
|
let restored = decrypt_sensitive_payload(&legacy_blob, "sync-pass").unwrap();
|
|
assert_eq!(restored.connection_secrets[0].secret, "legacy-secret");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn encrypted_snippet_snapshot_hides_and_restores_the_full_snapshot() {
|
|
let storage = Storage::open(&temp_db_path("encrypted-snippet-snapshot")).await.unwrap();
|
|
storage.save_connections(&[postgres_connection("pg", "db-secret")]).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, Some("sync-pass")).await.unwrap();
|
|
|
|
let encrypted = encrypt_snippet_snapshot(&snapshot, "sync-pass").unwrap();
|
|
let content = serde_json::to_string(&encrypted).unwrap();
|
|
assert!(!content.contains("127.0.0.1"));
|
|
assert!(!content.contains("app_db"));
|
|
assert!(!content.contains("db-secret"));
|
|
|
|
let restored = parse_snippet_snapshot(&content, Some("sync-pass")).unwrap();
|
|
assert_eq!(restored.connections[0].database.as_deref(), Some("app_db"));
|
|
assert!(parse_snippet_snapshot(&content, Some("wrong-pass")).is_err());
|
|
assert!(parse_snippet_snapshot(&content, None).is_err());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn encrypted_snippet_can_exclude_secrets_and_keep_local_credentials_on_restore() {
|
|
let source = Storage::open(&temp_db_path("snippet-without-secrets-source")).await.unwrap();
|
|
source.save_connections(&[postgres_connection("pg", "remote-secret")]).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&source, "test-version", None, None).await.unwrap();
|
|
assert!(snapshot.encrypted_secrets.is_none());
|
|
|
|
let encrypted = encrypt_snippet_snapshot(&snapshot, "snippet-password").unwrap();
|
|
let restored =
|
|
parse_snippet_snapshot(&serde_json::to_string(&encrypted).unwrap(), Some("snippet-password")).unwrap();
|
|
let target = Storage::open(&temp_db_path("snippet-without-secrets-target")).await.unwrap();
|
|
target.save_connections(&[postgres_connection("pg", "local-secret")]).await.unwrap();
|
|
|
|
let summary = apply_sync_snapshot(
|
|
&target,
|
|
&restored,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert!(!summary.encrypted_secrets_present);
|
|
assert!(!summary.secrets_applied);
|
|
assert_eq!(target.load_connections().await.unwrap()[0].password, "local-secret");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn skipping_snippet_secret_restore_keeps_local_credentials() {
|
|
let source = Storage::open(&temp_db_path("snippet-skip-secrets-source")).await.unwrap();
|
|
source.save_connections(&[postgres_connection("pg", "remote-secret")]).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&source, "test-version", None, Some("secrets-password")).await.unwrap();
|
|
assert!(snapshot.encrypted_secrets.is_some());
|
|
|
|
let encrypted = encrypt_snippet_snapshot(&snapshot, "snippet-password").unwrap();
|
|
let restored =
|
|
parse_snippet_snapshot(&serde_json::to_string(&encrypted).unwrap(), Some("snippet-password")).unwrap();
|
|
let target = Storage::open(&temp_db_path("snippet-skip-secrets-target")).await.unwrap();
|
|
target.save_connections(&[postgres_connection("pg", "local-secret")]).await.unwrap();
|
|
|
|
let summary = apply_sync_snapshot(
|
|
&target,
|
|
&restored,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert!(summary.encrypted_secrets_present);
|
|
assert!(!summary.secrets_applied);
|
|
assert_eq!(target.load_connections().await.unwrap()[0].password, "local-secret");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn existing_encrypted_snippet_rejects_wrong_password_without_patch() {
|
|
let storage = Storage::open(&temp_db_path("snippet-password-guard")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = encrypt_snippet_snapshot(&snapshot, "correct-password").unwrap();
|
|
let (base, server) =
|
|
spawn_snippet_server(vec![github_snippet_response(&serde_json::to_string(&encrypted).unwrap())]).await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("existing-id".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
base,
|
|
);
|
|
|
|
assert!(client.put_snapshot(&snapshot, Some("wrong-password"), None).await.is_err());
|
|
assert_eq!(server.await.unwrap(), vec!["GET /gists/existing-id HTTP/1.1"]);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn existing_encrypted_snippet_accepts_correct_password_before_patch() {
|
|
let storage = Storage::open(&temp_db_path("snippet-password-update")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = encrypt_snippet_snapshot(&snapshot, "correct-password").unwrap();
|
|
let (base, server) = spawn_snippet_server(vec![
|
|
github_snippet_response(&serde_json::to_string(&encrypted).unwrap()),
|
|
serde_json::json!({ "id": "existing-id" }).to_string(),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("existing-id".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
base,
|
|
);
|
|
|
|
client.put_snapshot(&snapshot, Some("correct-password"), None).await.unwrap();
|
|
assert_eq!(server.await.unwrap(), vec!["GET /gists/existing-id HTTP/1.1", "PATCH /gists/existing-id HTTP/1.1"]);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn legacy_migration_skips_delete_when_remote_content_changes() {
|
|
let storage = Storage::open(&temp_db_path("legacy-snippet-change-guard")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let legacy_content = serde_json::to_string(&snapshot).unwrap();
|
|
let changed_content =
|
|
serde_json::to_string(&build_sync_snapshot(&storage, "newer-version", None, None).await.unwrap()).unwrap();
|
|
let (base, server) = spawn_snippet_server(vec![
|
|
github_snippet_response(&legacy_content),
|
|
serde_json::json!({ "id": "new-id" }).to_string(),
|
|
github_snippet_response(&changed_content),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("legacy-id".to_string()),
|
|
replace_legacy_snippet: true,
|
|
},
|
|
base,
|
|
);
|
|
|
|
let mut summary = client.put_snapshot(&snapshot, Some("snippet-password"), None).await.unwrap();
|
|
assert_eq!(summary.snippet_id, "new-id");
|
|
assert_eq!(summary.legacy_cleanup_required_id.as_deref(), Some("legacy-id"));
|
|
finalize_snippet_migration(&storage, &client, &mut summary).await.unwrap();
|
|
assert_eq!(summary.legacy_cleanup_required_id.as_deref(), Some("legacy-id"));
|
|
let settings = snippet_sync_settings(&storage, SnippetProvider::GitHub).await.unwrap();
|
|
assert_eq!(settings.snippet_id.as_deref(), Some("new-id"));
|
|
assert_eq!(settings.legacy_cleanup_required_id.as_deref(), Some("legacy-id"));
|
|
assert_eq!(
|
|
server.await.unwrap(),
|
|
vec!["GET /gists/legacy-id HTTP/1.1", "POST /gists HTTP/1.1", "GET /gists/legacy-id HTTP/1.1",]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn pending_legacy_cleanup_survives_response_loss_and_retries_after_restart() {
|
|
let db = temp_db_path("legacy-snippet-cleanup-retry");
|
|
let storage = Storage::open(&db).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let legacy_content = serde_json::to_string(&snapshot).unwrap();
|
|
let (base, server) = spawn_snippet_server(vec![
|
|
github_snippet_response(&legacy_content),
|
|
serde_json::json!({ "id": "new-id" }).to_string(),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("legacy-id".to_string()),
|
|
replace_legacy_snippet: true,
|
|
},
|
|
base,
|
|
);
|
|
let mut summary = client.put_snapshot(&snapshot, Some("snippet-password"), None).await.unwrap();
|
|
|
|
finalize_snippet_migration(&storage, &client, &mut summary).await.unwrap();
|
|
assert_eq!(summary.legacy_cleanup_required_id.as_deref(), Some("legacy-id"));
|
|
assert_eq!(server.await.unwrap(), vec!["GET /gists/legacy-id HTTP/1.1", "POST /gists HTTP/1.1"]);
|
|
drop(storage);
|
|
|
|
let storage = Storage::open(&db).await.unwrap();
|
|
let settings = snippet_sync_settings(&storage, SnippetProvider::GitHub).await.unwrap();
|
|
assert_eq!(settings.snippet_id.as_deref(), Some("new-id"));
|
|
assert_eq!(settings.legacy_cleanup_required_id.as_deref(), Some("legacy-id"));
|
|
|
|
let (retry_base, retry_server) =
|
|
spawn_snippet_server(vec![github_snippet_response(&legacy_content), "{}".to_string()]).await;
|
|
let retry_client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("new-id".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
retry_base,
|
|
);
|
|
let settings = retry_pending_snippet_cleanup(&storage, SnippetProvider::GitHub, &retry_client).await.unwrap();
|
|
assert_eq!(settings.snippet_id.as_deref(), Some("new-id"));
|
|
assert_eq!(settings.legacy_cleanup_required_id, None);
|
|
assert_eq!(
|
|
retry_server.await.unwrap(),
|
|
vec!["GET /gists/legacy-id HTTP/1.1", "DELETE /gists/legacy-id HTTP/1.1"]
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn legacy_snippet_migration_preserves_remote_snapshot() {
|
|
let storage = Storage::open(&temp_db_path("legacy-snippet-migration-guard")).await.unwrap();
|
|
let local_snapshot = build_sync_snapshot(&storage, "local-version", None, None).await.unwrap();
|
|
let remote_snapshot = build_sync_snapshot(&storage, "remote-version", None, None).await.unwrap();
|
|
let mut legacy = serde_json::to_value(remote_snapshot).unwrap();
|
|
// This field did not exist in snapshots written by older DBX versions.
|
|
legacy.as_object_mut().unwrap().remove("tunnelProfiles");
|
|
|
|
let content = serde_json::to_string(&legacy).unwrap();
|
|
assert!(is_legacy_dbx_snapshot(&content));
|
|
let parsed_legacy = parse_legacy_dbx_snapshot(&content).unwrap();
|
|
let selected = snapshot_for_snippet_upload(&local_snapshot, Some(&parsed_legacy));
|
|
assert_eq!(selected.app_version, "remote-version");
|
|
|
|
let encrypted = encrypt_snippet_snapshot(selected, "sync-pass").unwrap();
|
|
let restored = parse_snippet_snapshot(&serde_json::to_string(&encrypted).unwrap(), Some("sync-pass")).unwrap();
|
|
assert_eq!(restored.app_version, "remote-version");
|
|
assert!(!is_legacy_dbx_snapshot(r#"{"schemaVersion":1,"connections":[]}"#));
|
|
assert!(parse_legacy_dbx_snapshot(r#"{"schemaVersion":1,"connections":[]}"#).is_err());
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn legacy_snippet_migration_refuses_unverifiable_encrypted_secrets() {
|
|
let storage = Storage::open(&temp_db_path("legacy-snippet-migration-secrets")).await.unwrap();
|
|
storage.save_connections(&[postgres_connection("pg", "db-secret")]).await.unwrap();
|
|
let remote_snapshot = build_sync_snapshot(&storage, "remote-version", None, Some("remote-pass")).await.unwrap();
|
|
let content = serde_json::to_string(&remote_snapshot).unwrap();
|
|
|
|
let legacy = parse_legacy_dbx_snapshot(&content).unwrap();
|
|
assert!(prepare_legacy_snippet_snapshot(legacy.clone(), Some("wrong-pass")).is_err());
|
|
let prepared = prepare_legacy_snippet_snapshot(legacy, Some("remote-pass")).unwrap();
|
|
let secrets = decrypt_sensitive_payload(prepared.encrypted_secrets.as_ref().unwrap(), "remote-pass").unwrap();
|
|
assert!(secrets.connection_secrets.iter().any(|secret| secret.secret == "db-secret"));
|
|
}
|
|
|
|
#[test]
|
|
fn legacy_snippet_sync_requests_default_to_no_remote_deletion() {
|
|
let config: SnippetSyncConfig = serde_json::from_value(serde_json::json!({
|
|
"provider": "github",
|
|
"token": "token",
|
|
"snippetId": "legacy-id"
|
|
}))
|
|
.unwrap();
|
|
|
|
assert!(!config.replace_legacy_snippet);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn snippet_sync_id_is_persisted_per_provider() {
|
|
let storage = Storage::open(&temp_db_path("snippet-sync-id")).await.unwrap();
|
|
|
|
save_snippet_sync_id(&storage, SnippetProvider::GitHub, Some("github-id")).await.unwrap();
|
|
save_snippet_sync_id(&storage, SnippetProvider::Gitee, Some("gitee-id")).await.unwrap();
|
|
assert_eq!(
|
|
snippet_sync_settings(&storage, SnippetProvider::GitHub).await.unwrap().snippet_id.as_deref(),
|
|
Some("github-id")
|
|
);
|
|
assert_eq!(
|
|
snippet_sync_settings(&storage, SnippetProvider::GitHub).await.unwrap().legacy_cleanup_required_id,
|
|
None
|
|
);
|
|
assert_eq!(
|
|
snippet_sync_settings(&storage, SnippetProvider::Gitee).await.unwrap().snippet_id.as_deref(),
|
|
Some("gitee-id")
|
|
);
|
|
|
|
save_snippet_sync_id(&storage, SnippetProvider::GitHub, None).await.unwrap();
|
|
assert_eq!(snippet_sync_settings(&storage, SnippetProvider::GitHub).await.unwrap().snippet_id, None);
|
|
assert_eq!(
|
|
snippet_sync_settings(&storage, SnippetProvider::Gitee).await.unwrap().snippet_id.as_deref(),
|
|
Some("gitee-id")
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn gitlab_instance_validation_and_state_key() {
|
|
assert_eq!(gitlab_instance_url(None).unwrap(), "https://gitlab.com");
|
|
assert_eq!(
|
|
gitlab_instance_url(Some(" HTTPS://GITLAB.EXAMPLE.COM:443/ ")).unwrap(),
|
|
"https://gitlab.example.com"
|
|
);
|
|
assert_eq!(
|
|
snippet_provider_storage_key(SnippetProvider::GitLab, Some("https://gitlab.example.com/")).unwrap(),
|
|
snippet_provider_storage_key(SnippetProvider::GitLab, Some("https://gitlab.example.com")).unwrap()
|
|
);
|
|
for url in [
|
|
"ftp://gitlab.example.com",
|
|
"https://user:password@gitlab.example.com",
|
|
"https://gitlab.example.com/?token=x",
|
|
"https://gitlab.example.com/#fragment",
|
|
] {
|
|
assert!(gitlab_instance_url(Some(url)).is_err(), "accepted {url}");
|
|
}
|
|
assert_eq!(gitlab_instance_url(Some("http://gitlab.internal:8080/")).unwrap(), "http://gitlab.internal:8080");
|
|
assert_ne!(
|
|
snippet_provider_storage_key(SnippetProvider::GitLab, Some("http://gitlab.example.com")).unwrap(),
|
|
snippet_provider_storage_key(SnippetProvider::GitLab, Some("https://gitlab.example.com")).unwrap()
|
|
);
|
|
assert!(validate_snippet_id(SnippetProvider::GitLab, "12/evil").is_err());
|
|
assert_eq!(snippet_response_id(&serde_json::json!({"id": 42})).as_deref(), Some("42"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_instances_keep_saved_tokens_ids_and_cleanup_separate() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-instance-isolation")).await.unwrap();
|
|
let first = "https://gitlab.example.com";
|
|
let second = "https://gitlab.other.com";
|
|
let config = |instance: &str| SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: Some(instance.to_string()),
|
|
token: None,
|
|
snippet_id: None,
|
|
replace_legacy_snippet: false,
|
|
};
|
|
save_snippet_token(&storage, &config(first), "first-secret").await.unwrap();
|
|
save_snippet_sync_id_for_instance(&storage, SnippetProvider::GitLab, Some(first), Some("42")).await.unwrap();
|
|
let first_key = snippet_provider_storage_key(SnippetProvider::GitLab, Some(first)).unwrap();
|
|
storage.save_snippet_migration_state(&first_key, "43", "42", "hash").await.unwrap();
|
|
assert!(!snippet_saved_token_status(&storage, &config(second)).await.unwrap().has_saved_token);
|
|
assert!(snippet_sync_settings_for_instance(&storage, SnippetProvider::GitLab, Some(second))
|
|
.await
|
|
.unwrap()
|
|
.snippet_id
|
|
.is_none());
|
|
assert!(snippet_sync_settings_for_instance(&storage, SnippetProvider::GitLab, Some(second))
|
|
.await
|
|
.unwrap()
|
|
.legacy_cleanup_required_id
|
|
.is_none());
|
|
let mut first_config = config("https://gitlab.example.com/");
|
|
resolve_snippet_token(&storage, &mut first_config).await.unwrap();
|
|
assert_eq!(first_config.token.as_deref(), Some("first-secret"));
|
|
assert_eq!(
|
|
snippet_sync_settings_for_instance(&storage, SnippetProvider::GitLab, Some(first))
|
|
.await
|
|
.unwrap()
|
|
.snippet_id
|
|
.as_deref(),
|
|
Some("43")
|
|
);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_create_and_download_use_private_personal_snippet_and_raw_file() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-create-download")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = encrypt_snippet_snapshot(&snapshot, "password").unwrap();
|
|
let (base, server) = spawn_gitlab_server(vec![
|
|
serde_json::json!({"id": 42}).to_string(),
|
|
serde_json::json!({"files": [{"path": "dbx-sync.json"}]}).to_string(),
|
|
serde_json::to_string(&encrypted).unwrap(),
|
|
])
|
|
.await;
|
|
let config = SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: None,
|
|
replace_legacy_snippet: false,
|
|
};
|
|
let client = SnippetSyncClient::with_api_base(config.clone(), base.clone());
|
|
let summary = client.put_snapshot(&snapshot, Some("password"), None).await.unwrap();
|
|
assert_eq!(summary.snippet_id, "42");
|
|
let download =
|
|
SnippetSyncClient::with_api_base(SnippetSyncConfig { snippet_id: Some("42".to_string()), ..config }, base);
|
|
let (restored, _) = download.get_snapshot(Some("password")).await.unwrap();
|
|
assert_eq!(restored.app_version, snapshot.app_version);
|
|
let requests = server.await.unwrap();
|
|
assert!(requests[0].starts_with("POST /api/v4/snippets HTTP/1.1"));
|
|
assert!(requests[0].to_ascii_lowercase().contains("private-token: test-token"));
|
|
let body: serde_json::Value = serde_json::from_str(requests[0].split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
|
assert_eq!(body["visibility"], "private");
|
|
assert_eq!(body["files"][0]["file_path"], "dbx-sync.json");
|
|
assert!(body["files"][0]["content"].as_str().unwrap().contains("dbx-encrypted-sync-snapshot"));
|
|
assert!(requests[1].starts_with("GET /api/v4/snippets/42 HTTP/1.1"));
|
|
assert!(requests[2].starts_with("GET /api/v4/snippets/42/files/main/dbx-sync.json/raw HTTP/1.1"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_download_uses_snippet_raw_url_branch() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-raw-url-branch")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = serde_json::to_string(&encrypt_snippet_snapshot(&snapshot, "password").unwrap()).unwrap();
|
|
let (base, server) = spawn_gitlab_server_with_status(vec![
|
|
(
|
|
200,
|
|
serde_json::json!({"files": [{"path": "dbx-sync.json", "raw_url": "{SERVER_BASE}/api/v4/snippets/42/files/master/dbx-sync.json/raw"}]}).to_string(),
|
|
),
|
|
(200, encrypted),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("42".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
base,
|
|
);
|
|
let (restored, _) = client.get_snapshot(Some("password")).await.unwrap();
|
|
assert_eq!(restored.app_version, snapshot.app_version);
|
|
let requests = server.await.unwrap();
|
|
assert_eq!(requests.len(), 2);
|
|
assert!(requests[0].starts_with("GET /api/v4/snippets/42 HTTP/1.1"));
|
|
assert!(requests[1].starts_with("GET /api/v4/snippets/42/files/master/dbx-sync.json/raw HTTP/1.1"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_download_falls_back_to_master_when_main_raw_file_is_missing() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-master-fallback")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = serde_json::to_string(&encrypt_snippet_snapshot(&snapshot, "password").unwrap()).unwrap();
|
|
let (base, server) = spawn_gitlab_server_with_status(vec![
|
|
(200, serde_json::json!({"files": [{"path": "dbx-sync.json"}]}).to_string()),
|
|
(404, String::new()),
|
|
(200, encrypted),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("42".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
base,
|
|
);
|
|
let (restored, _) = client.get_snapshot(Some("password")).await.unwrap();
|
|
assert_eq!(restored.app_version, snapshot.app_version);
|
|
let requests = server.await.unwrap();
|
|
assert_eq!(requests.len(), 3);
|
|
assert!(requests[1].starts_with("GET /api/v4/snippets/42/files/main/dbx-sync.json/raw HTTP/1.1"));
|
|
assert!(requests[2].starts_with("GET /api/v4/snippets/42/files/master/dbx-sync.json/raw HTTP/1.1"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_http_instance_uses_configured_api_base() {
|
|
let (base, server) = spawn_gitlab_server(vec![serde_json::json!({"id": 42}).to_string()]).await;
|
|
let instance = base.strip_suffix("/api/v4").unwrap();
|
|
let client = SnippetSyncClient::new(SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: Some(instance.to_string()),
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("42".to_string()),
|
|
replace_legacy_snippet: false,
|
|
})
|
|
.unwrap();
|
|
client.test().await.unwrap();
|
|
let requests = server.await.unwrap();
|
|
assert!(requests[0].starts_with("GET /api/v4/snippets/42 HTTP/1.1"));
|
|
assert!(requests[0].to_ascii_lowercase().contains("private-token: test-token"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_update_checks_existing_password_before_put() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-password-guard")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = serde_json::to_string(&encrypt_snippet_snapshot(&snapshot, "password").unwrap()).unwrap();
|
|
let (base, server) = spawn_gitlab_server(vec![
|
|
serde_json::json!({"files": [{"path": "dbx-sync.json"}]}).to_string(),
|
|
encrypted,
|
|
serde_json::json!({"id": 42}).to_string(),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("42".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
base,
|
|
);
|
|
client.put_snapshot(&snapshot, Some("password"), None).await.unwrap();
|
|
let requests = server.await.unwrap();
|
|
assert_eq!(requests.len(), 3);
|
|
assert!(requests[2].starts_with("PUT /api/v4/snippets/42 HTTP/1.1"));
|
|
let body: serde_json::Value = serde_json::from_str(requests[2].split("\r\n\r\n").nth(1).unwrap()).unwrap();
|
|
assert_eq!(body["files"][0]["action"], "update");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_wrong_password_never_updates_existing_snippet() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-wrong-password")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let encrypted = serde_json::to_string(&encrypt_snippet_snapshot(&snapshot, "correct").unwrap()).unwrap();
|
|
let (base, server) =
|
|
spawn_gitlab_server(vec![serde_json::json!({"files": [{"path": "dbx-sync.json"}]}).to_string(), encrypted])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: None,
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("42".to_string()),
|
|
replace_legacy_snippet: false,
|
|
},
|
|
base,
|
|
);
|
|
assert!(client.put_snapshot(&snapshot, Some("wrong"), None).await.is_err());
|
|
assert_eq!(server.await.unwrap().len(), 2);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn gitlab_legacy_migration_creates_before_deleting_and_tracks_instance() {
|
|
let storage = Storage::open(&temp_db_path("gitlab-legacy-migration")).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, None).await.unwrap();
|
|
let legacy_content = serde_json::to_string(&snapshot).unwrap();
|
|
let metadata = serde_json::json!({"files": [{"path": "dbx-sync.json"}]}).to_string();
|
|
let (base, server) = spawn_gitlab_server(vec![
|
|
metadata.clone(),
|
|
legacy_content.clone(),
|
|
serde_json::json!({"id": 43}).to_string(),
|
|
metadata,
|
|
legacy_content,
|
|
String::new(),
|
|
])
|
|
.await;
|
|
let client = SnippetSyncClient::with_api_base(
|
|
SnippetSyncConfig {
|
|
provider: SnippetProvider::GitLab,
|
|
instance_url: Some("https://gitlab.example.com".to_string()),
|
|
token: Some("test-token".to_string()),
|
|
snippet_id: Some("42".to_string()),
|
|
replace_legacy_snippet: true,
|
|
},
|
|
base,
|
|
);
|
|
let mut summary = client.put_snapshot(&snapshot, Some("password"), None).await.unwrap();
|
|
assert_eq!(summary.snippet_id, "43");
|
|
finalize_snippet_migration(&storage, &client, &mut summary).await.unwrap();
|
|
assert!(summary.legacy_cleanup_required_id.is_none());
|
|
assert_eq!(
|
|
snippet_sync_settings_for_instance(
|
|
&storage,
|
|
SnippetProvider::GitLab,
|
|
client.config.instance_url.as_deref()
|
|
)
|
|
.await
|
|
.unwrap()
|
|
.snippet_id
|
|
.as_deref(),
|
|
Some("43")
|
|
);
|
|
let requests = server.await.unwrap();
|
|
assert!(requests[2].starts_with("POST /api/v4/snippets HTTP/1.1"));
|
|
assert!(requests[5].starts_with("DELETE /api/v4/snippets/42 HTTP/1.1"));
|
|
}
|
|
|
|
#[test]
|
|
fn snippet_response_id_supports_github_object_and_gitee_array() {
|
|
assert_eq!(snippet_response_id(&serde_json::json!({ "id": "github-id" })).as_deref(), Some("github-id"));
|
|
assert_eq!(snippet_response_id(&serde_json::json!([{ "id": "gitee-id" }])).as_deref(), Some("gitee-id"));
|
|
}
|
|
|
|
#[test]
|
|
fn snippet_provider_uses_frontend_wire_values() {
|
|
assert_eq!(serde_json::to_string(&super::SnippetProvider::GitHub).unwrap(), "\"github\"");
|
|
assert_eq!(
|
|
serde_json::from_str::<super::SnippetProvider>("\"github\"").unwrap(),
|
|
super::SnippetProvider::GitHub
|
|
);
|
|
assert_eq!(
|
|
serde_json::from_str::<super::SnippetProvider>("\"git_hub\"").unwrap(),
|
|
super::SnippetProvider::GitHub
|
|
);
|
|
assert_eq!(serde_json::to_string(&super::SnippetProvider::Gitee).unwrap(), "\"gitee\"");
|
|
}
|
|
|
|
#[test]
|
|
fn snippet_file_content_uses_raw_url_for_truncated_github_files() {
|
|
let value = serde_json::json!({
|
|
"files": {
|
|
"dbx-sync.json": {
|
|
"content": "truncated",
|
|
"truncated": true,
|
|
"raw_url": "https://example.com/raw"
|
|
}
|
|
}
|
|
});
|
|
assert_eq!(
|
|
snippet_file_content(&value, "dbx-sync.json").unwrap(),
|
|
(None, Some("https://example.com/raw".to_string()))
|
|
);
|
|
}
|
|
|
|
#[test]
|
|
fn snippet_file_content_parses_gitee_string_files() {
|
|
let files = serde_json::json!({ "dbx-sync.json": { "content": "{}" } }).to_string();
|
|
let value = serde_json::json!({ "files": files });
|
|
assert_eq!(snippet_file_content(&value, "dbx-sync.json").unwrap(), (Some("{}".to_string()), None));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn webdav_sync_secrets_preference_round_trips_and_clears_passphrase() {
|
|
let storage = Storage::open(&temp_db_path("sync-secrets-preference")).await.unwrap();
|
|
|
|
let status = webdav_sync_secrets_status(&storage).await.unwrap();
|
|
assert!(!status.enabled);
|
|
assert!(!status.has_saved_passphrase);
|
|
assert_eq!(resolve_webdav_sync_secrets_passphrase(&storage).await.unwrap(), None);
|
|
|
|
save_webdav_sync_secrets_preference(&storage, true, Some("sync-pass")).await.unwrap();
|
|
|
|
let status = webdav_sync_secrets_status(&storage).await.unwrap();
|
|
assert!(status.enabled);
|
|
assert!(status.has_saved_passphrase);
|
|
assert_eq!(resolve_webdav_sync_secrets_passphrase(&storage).await.unwrap().as_deref(), Some("sync-pass"));
|
|
|
|
forget_webdav_sync_secrets_passphrase(&storage).await.unwrap();
|
|
let status = webdav_sync_secrets_status(&storage).await.unwrap();
|
|
assert!(status.enabled);
|
|
assert!(!status.has_saved_passphrase);
|
|
assert_eq!(resolve_webdav_sync_secrets_passphrase(&storage).await.unwrap(), None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn saved_sync_passphrase_encrypts_snapshot_secrets_without_exposing_connection_passwords() {
|
|
let storage = Storage::open(&temp_db_path("saved-sync-snapshot")).await.unwrap();
|
|
storage.save_connections(&[postgres_connection("pg", "db-secret")]).await.unwrap();
|
|
|
|
let plain_snapshot =
|
|
build_sync_snapshot_with_saved_secrets(&storage, "test-version", None, None).await.unwrap();
|
|
assert!(plain_snapshot.encrypted_secrets.is_none());
|
|
assert_eq!(plain_snapshot.connections[0].password, "");
|
|
|
|
save_webdav_sync_secrets_preference(&storage, true, Some("sync-pass")).await.unwrap();
|
|
let encrypted_snapshot =
|
|
build_sync_snapshot_with_saved_secrets(&storage, "test-version", None, None).await.unwrap();
|
|
|
|
assert_eq!(encrypted_snapshot.connections[0].password, "");
|
|
let encrypted = encrypted_snapshot.encrypted_secrets.as_ref().expect("encrypted secrets");
|
|
let decrypted = decrypt_sensitive_payload(encrypted, "sync-pass").unwrap();
|
|
assert!(decrypted.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "pg" && secret.key == "password" && secret.secret == "db-secret"
|
|
}));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn legacy_sync_payload_preserves_credentials_but_explicit_empty_clears_them() {
|
|
let directory = tempfile::tempdir().unwrap();
|
|
let database_path = directory.path().join("dbx.db");
|
|
let target = Storage::open_unmigrated(&database_path)
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir);
|
|
let webdav = WebDavConfig {
|
|
endpoint: "https://dav.example.test".to_string(),
|
|
username: Some("alice".to_string()),
|
|
password: None,
|
|
remote_path: None,
|
|
};
|
|
let snippet = SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: None,
|
|
snippet_id: None,
|
|
replace_legacy_snippet: false,
|
|
};
|
|
save_webdav_password(&target, &webdav, "local-password").await.unwrap();
|
|
save_snippet_token(&target, &snippet, "local-token").await.unwrap();
|
|
let mut snapshot = build_sync_snapshot(&target, "test", None, None).await.unwrap();
|
|
snapshot.schema_version = LEGACY_SNAPSHOT_SCHEMA_VERSION;
|
|
let legacy_json = r#"{"connectionSecrets":[]}"#;
|
|
let mut payload: SensitiveSyncPayload = serde_json::from_str(legacy_json).unwrap();
|
|
assert!(payload.sync_credentials.is_none());
|
|
assert!(serde_json::to_value(&payload).unwrap().get("syncCredentials").is_none());
|
|
snapshot.encrypted_secrets = Some(super::encrypt_text_with_secret(legacy_json, "transport-pass").unwrap());
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("transport-pass"), restore_secrets: true },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
drop(target);
|
|
let target = Storage::open_unmigrated(&database_path)
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir);
|
|
|
|
payload.sync_credentials = Some(vec![]);
|
|
assert_eq!(serde_json::to_value(&payload).unwrap()["syncCredentials"], serde_json::json!([]));
|
|
snapshot.encrypted_secrets = Some(encrypt_sensitive_payload(&payload, "transport-pass").unwrap());
|
|
for restore_secrets in [false, true] {
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("transport-pass"), restore_secrets },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let mut restored_webdav = webdav.clone();
|
|
resolve_webdav_password(&target, &mut restored_webdav).await.unwrap();
|
|
assert_eq!(restored_webdav.password.as_deref(), (!restore_secrets).then_some("local-password"));
|
|
let mut restored_snippet = snippet.clone();
|
|
resolve_snippet_token(&target, &mut restored_snippet).await.unwrap();
|
|
assert_eq!(restored_snippet.token.as_deref(), (!restore_secrets).then_some("local-token"));
|
|
}
|
|
drop(target);
|
|
let target = Storage::open_unmigrated(&database_path)
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir);
|
|
let mut restored_webdav = webdav;
|
|
resolve_webdav_password(&target, &mut restored_webdav).await.unwrap();
|
|
assert_eq!(restored_webdav.password, None);
|
|
let mut restored_snippet = snippet;
|
|
resolve_snippet_token(&target, &mut restored_snippet).await.unwrap();
|
|
assert_eq!(restored_snippet.token, None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn metadata_only_sync_roundtrip_preserves_public_url_params_and_target_secrets() {
|
|
let source_directory = tempfile::tempdir().unwrap();
|
|
let target_directory = tempfile::tempdir().unwrap();
|
|
let source = Storage::open_unmigrated(&source_directory.path().join("dbx.db"))
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir);
|
|
let target_path = target_directory.path().join("dbx.db");
|
|
let target = Storage::open_unmigrated(&target_path)
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir);
|
|
let mut fresh = postgres_connection("fresh", "source-password");
|
|
fresh.url_params = Some("applicationName=dbx&PASSWORD=source-secret&sslmode=require".to_string());
|
|
let mut existing = fresh.clone();
|
|
existing.id = "existing".to_string();
|
|
source.save_connections(&[fresh, existing.clone()]).await.unwrap();
|
|
existing.password = "target-password".to_string();
|
|
existing.url_params = Some("applicationName=local&PASSWORD=target-secret".to_string());
|
|
target.save_connections(std::slice::from_ref(&existing)).await.unwrap();
|
|
target.set_secret("existing", "init_script", "target-script").await.unwrap();
|
|
|
|
let snapshot = build_sync_snapshot(&source, "test", None, None).await.unwrap();
|
|
assert!(snapshot.encrypted_secrets.is_none());
|
|
let serialized = serde_json::to_string(&snapshot).unwrap();
|
|
assert!(!serialized.contains("source-secret"));
|
|
assert!(!serialized.contains("source-password"));
|
|
let snapshot = serde_json::from_str(&serialized).unwrap();
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
drop(target);
|
|
let target = Storage::open_unmigrated(&target_path)
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir);
|
|
let loaded = target.load_connections().await.unwrap();
|
|
let fresh = loaded.iter().find(|config| config.id == "fresh").unwrap();
|
|
assert_eq!(fresh.url_params.as_deref(), Some("applicationName=dbx&PASSWORD=&sslmode=require"));
|
|
assert!(fresh.password.is_empty());
|
|
let restored = loaded.iter().find(|config| config.id == "existing").unwrap();
|
|
assert_eq!(restored.password, "target-password");
|
|
assert_eq!(restored.url_params, existing.url_params);
|
|
assert_eq!(restored.init_script.as_deref(), Some("target-script"));
|
|
let database = rusqlite::Connection::open(&target_path).unwrap();
|
|
let (plaintext, encrypted): (String, String) = database
|
|
.query_row(
|
|
"SELECT secret, secret_enc FROM connection_secrets WHERE connection_id = 'fresh' AND key = 'url_params'",
|
|
[],
|
|
|row| Ok((row.get(0)?, row.get(1)?)),
|
|
)
|
|
.unwrap();
|
|
assert!(plaintext.is_empty());
|
|
assert!(encrypted.starts_with("dbxenc1."));
|
|
|
|
let locked_directory = tempfile::tempdir().unwrap();
|
|
let locked_target = Storage::open_unmigrated(&locked_directory.path().join("dbx.db"))
|
|
.await
|
|
.unwrap()
|
|
.with_secret_key_policy(SecretKeyPolicy::ManagedDataDir)
|
|
.with_secret_key_creation(false);
|
|
assert_eq!(
|
|
apply_sync_snapshot(
|
|
&locked_target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap_err(),
|
|
"MISSING_MANAGED_KEY"
|
|
);
|
|
assert!(locked_target.load_connections().await.unwrap().is_empty());
|
|
assert!(!managed_key_path(locked_directory.path()).exists());
|
|
|
|
let mut without_url_params = snapshot.clone();
|
|
for config in &mut without_url_params.connections {
|
|
config.url_params = None;
|
|
}
|
|
apply_sync_snapshot(
|
|
&locked_target,
|
|
&without_url_params,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(locked_target.load_connections().await.unwrap().len(), 2);
|
|
assert!(!managed_key_path(locked_directory.path()).exists());
|
|
|
|
let unlocked_target = locked_target.with_secret_key_creation(true);
|
|
apply_sync_snapshot(
|
|
&unlocked_target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert!(managed_key_path(locked_directory.path()).exists());
|
|
assert!(unlocked_target.load_connections().await.unwrap().iter().all(|config| {
|
|
config.url_params.as_deref() == Some("applicationName=dbx&PASSWORD=&sslmode=require")
|
|
&& config.password.is_empty()
|
|
}));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sync_credentials_are_rewrapped_for_the_destination_device() {
|
|
let source = Storage::open(&temp_db_path("sync-global-credentials-source")).await.unwrap();
|
|
let webdav = WebDavConfig {
|
|
endpoint: "https://dav.example.test/remote.php/dav/files/alice".to_string(),
|
|
username: Some("alice".to_string()),
|
|
password: None,
|
|
remote_path: Some("DBX/sync/snapshot.json".to_string()),
|
|
};
|
|
save_webdav_password(&source, &webdav, "webdav-secret").await.unwrap();
|
|
let snippet = SnippetSyncConfig {
|
|
provider: SnippetProvider::GitHub,
|
|
instance_url: None,
|
|
token: None,
|
|
snippet_id: None,
|
|
replace_legacy_snippet: false,
|
|
};
|
|
save_snippet_token(&source, &snippet, "github-token").await.unwrap();
|
|
|
|
let snapshot = build_sync_snapshot(&source, "test-version", None, Some("transport-pass")).await.unwrap();
|
|
let public_json = serde_json::to_string(&snapshot).unwrap();
|
|
assert!(!public_json.contains("webdav-secret"));
|
|
assert!(!public_json.contains("github-token"));
|
|
let payload =
|
|
decrypt_sensitive_payload(snapshot.encrypted_secrets.as_ref().unwrap(), "transport-pass").unwrap();
|
|
let credentials = payload.sync_credentials.as_ref().unwrap();
|
|
assert_eq!(credentials.len(), 2);
|
|
assert!(credentials.iter().any(|credential| credential.secret == "webdav-secret"));
|
|
assert!(credentials.iter().any(|credential| credential.secret == "github-token"));
|
|
|
|
let target_path = temp_db_path("sync-global-credentials-target");
|
|
let target = Storage::open(&target_path).await.unwrap();
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("transport-pass"), restore_secrets: true },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let mut restored_webdav = webdav.clone();
|
|
resolve_webdav_password(&target, &mut restored_webdav).await.unwrap();
|
|
assert_eq!(restored_webdav.password.as_deref(), Some("webdav-secret"));
|
|
let mut restored_snippet = snippet.clone();
|
|
resolve_snippet_token(&target, &mut restored_snippet).await.unwrap();
|
|
assert_eq!(restored_snippet.token.as_deref(), Some("github-token"));
|
|
|
|
// The destination uses its own local wrapping key; deleting the source
|
|
// database does not affect the restored credentials.
|
|
drop(target);
|
|
drop(source);
|
|
let target = Storage::open(&target_path).await.unwrap();
|
|
let mut reopened_webdav = webdav.clone();
|
|
resolve_webdav_password(&target, &mut reopened_webdav).await.unwrap();
|
|
assert_eq!(reopened_webdav.password.as_deref(), Some("webdav-secret"));
|
|
|
|
// The encrypted credential list is authoritative. Removing credentials
|
|
// on the source must not leave stale target tokens behind.
|
|
// Build an empty credential payload explicitly so the test does not
|
|
// depend on a second live provider.
|
|
let mut empty_snapshot = snapshot.clone();
|
|
let mut empty_payload = payload.clone();
|
|
empty_payload.sync_credentials = Some(vec![]);
|
|
empty_snapshot.encrypted_secrets = Some(encrypt_sensitive_payload(&empty_payload, "transport-pass").unwrap());
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&empty_snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("transport-pass"), restore_secrets: true },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let mut removed_webdav = webdav.clone();
|
|
resolve_webdav_password(&target, &mut removed_webdav).await.unwrap();
|
|
assert_eq!(removed_webdav.password, None);
|
|
let mut removed_snippet = snippet.clone();
|
|
resolve_snippet_token(&target, &mut removed_snippet).await.unwrap();
|
|
assert_eq!(removed_snippet.token, None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn plugin_connection_secrets_are_scrubbed_from_public_sync_metadata() {
|
|
let storage = Storage::open(&temp_db_path("plugin-public-sync")).await.unwrap();
|
|
let mut config = postgres_connection("plugin", "");
|
|
config.db_type = DatabaseType::Plugin;
|
|
config.plugin_id = Some("example.plugin".to_string());
|
|
config.plugin_connection_provider = Some("example.connection".to_string());
|
|
config.connection_secrets.insert("api_token".to_string(), "plugin-secret".to_string());
|
|
storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
|
|
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, Some("sync-pass")).await.unwrap();
|
|
let public_json = serde_json::to_string(&snapshot.connections).unwrap();
|
|
assert!(!public_json.contains("plugin-secret"));
|
|
let encrypted = snapshot.encrypted_secrets.as_ref().expect("encrypted secrets");
|
|
let decrypted = decrypt_sensitive_payload(encrypted, "sync-pass").unwrap();
|
|
assert!(decrypted.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "plugin"
|
|
&& secret.key == format!("{PLUGIN_CONNECTION_SECRET_PREFIX}api_token")
|
|
&& secret.secret == "plugin-secret"
|
|
}));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn plugin_secrets_export_even_when_primary_password_is_not_saved() {
|
|
let storage = Storage::open(&temp_db_path("plugin-secret-without-password")).await.unwrap();
|
|
let mut config = postgres_connection("plugin-no-password", "");
|
|
config.db_type = DatabaseType::Plugin;
|
|
config.save_password = false;
|
|
config.plugin_id = Some("example.plugin".to_string());
|
|
config.plugin_connection_provider = Some("example.connection".to_string());
|
|
config.connection_secrets.insert("api_token".to_string(), "plugin-secret".to_string());
|
|
storage.save_connections(std::slice::from_ref(&config)).await.unwrap();
|
|
|
|
let snapshot = build_sync_snapshot_with_options(
|
|
&storage,
|
|
"test-version",
|
|
None,
|
|
SyncExportOptions {
|
|
include_secrets: true,
|
|
sync_passphrase: Some("sync-pass"),
|
|
include_ai_secrets: false,
|
|
include_tunnel_secrets: false,
|
|
include_plugin_secrets: true,
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
let decrypted = decrypt_sensitive_payload(snapshot.encrypted_secrets.as_ref().unwrap(), "sync-pass").unwrap();
|
|
assert!(decrypted.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "plugin-no-password"
|
|
&& secret.key == format!("{PLUGIN_CONNECTION_SECRET_PREFIX}api_token")
|
|
&& secret.secret == "plugin-secret"
|
|
}));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn excluded_plugin_secrets_do_not_clear_destination_credentials() {
|
|
let source = Storage::open(&temp_db_path("plugin-secret-excluded-source")).await.unwrap();
|
|
let mut config = postgres_connection("plugin-preserve", "");
|
|
config.db_type = DatabaseType::Plugin;
|
|
config.plugin_id = Some("example.plugin".to_string());
|
|
config.plugin_connection_provider = Some("example.connection".to_string());
|
|
source.save_connections(std::slice::from_ref(&config)).await.unwrap();
|
|
|
|
let snapshot = build_sync_snapshot_with_options(
|
|
&source,
|
|
"test-version",
|
|
None,
|
|
SyncExportOptions {
|
|
include_secrets: true,
|
|
sync_passphrase: Some("sync-pass"),
|
|
include_ai_secrets: false,
|
|
include_tunnel_secrets: false,
|
|
include_plugin_secrets: false,
|
|
},
|
|
)
|
|
.await
|
|
.unwrap();
|
|
|
|
let target = Storage::open(&temp_db_path("plugin-secret-excluded-target")).await.unwrap();
|
|
let mut target_config = config.clone();
|
|
target_config.connection_secrets.insert("api_token".to_string(), "local-plugin-secret".to_string());
|
|
target.save_connections(std::slice::from_ref(&target_config)).await.unwrap();
|
|
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("sync-pass"), restore_secrets: true },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(
|
|
target.get_secret("plugin-preserve", "plugin_connection.api_token").await.unwrap().as_deref(),
|
|
Some("local-plugin-secret")
|
|
);
|
|
}
|
|
|
|
#[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 wrong_sync_passphrase_does_not_modify_the_destination() {
|
|
let source = Storage::open(&temp_db_path("sync-wrong-pass-source")).await.unwrap();
|
|
source.save_connections(&[postgres_connection("remote", "remote-secret")]).await.unwrap();
|
|
let snapshot = build_sync_snapshot(&source, "test-version", None, Some("correct-pass")).await.unwrap();
|
|
|
|
let target = Storage::open(&temp_db_path("sync-wrong-pass-target")).await.unwrap();
|
|
target.save_connections(&[postgres_connection("local", "local-secret")]).await.unwrap();
|
|
let before = target.load_connections().await.unwrap();
|
|
assert!(apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("wrong-pass"), restore_secrets: true },
|
|
)
|
|
.await
|
|
.is_err());
|
|
assert_eq!(target.load_connections().await.unwrap(), before);
|
|
assert_eq!(target.get_secret("local", "password").await.unwrap().as_deref(), Some("local-secret"));
|
|
assert_eq!(target.get_secret("remote", "password").await.unwrap(), None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn legacy_plaintext_snapshot_secrets_are_migrated_only_on_explicit_restore() {
|
|
let source = Storage::open(&temp_db_path("legacy-plaintext-sync-source")).await.unwrap();
|
|
source.save_connections(&[postgres_connection("legacy", "unused")]).await.unwrap();
|
|
let mut snapshot = build_sync_snapshot(&source, "legacy-version", None, None).await.unwrap();
|
|
snapshot.schema_version = LEGACY_SNAPSHOT_SCHEMA_VERSION;
|
|
snapshot.connections[0].password = "legacy-password".to_string();
|
|
snapshot.connections[0].init_script = Some("CREATE SECRET legacy".to_string());
|
|
|
|
let target = Storage::open(&temp_db_path("legacy-plaintext-sync-target")).await.unwrap();
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: true },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(target.get_secret("legacy", "password").await.unwrap().as_deref(), Some("legacy-password"));
|
|
assert_eq!(target.get_secret("legacy", "init_script").await.unwrap().as_deref(), Some("CREATE SECRET legacy"));
|
|
|
|
let metadata_only_target = Storage::open(&temp_db_path("legacy-plaintext-sync-metadata-only")).await.unwrap();
|
|
apply_sync_snapshot(
|
|
&metadata_only_target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: None, restore_secrets: false },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(metadata_only_target.get_secret("legacy", "password").await.unwrap(), None);
|
|
assert_eq!(metadata_only_target.get_secret("legacy", "init_script").await.unwrap(), None);
|
|
}
|
|
|
|
#[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();
|
|
storage.save_connections(&[nacos_connection("nacos", "nacos-secret")]).await.unwrap();
|
|
|
|
save_webdav_sync_secrets_preference(&storage, true, Some("sync-pass")).await.unwrap();
|
|
let encrypted_snapshot =
|
|
build_sync_snapshot_with_saved_secrets(&storage, "test-version", None, None).await.unwrap();
|
|
|
|
assert_eq!(nacos_auth_password(&encrypted_snapshot.connections[0]), Some(""));
|
|
let public_json = serde_json::to_string(&encrypted_snapshot.connections).unwrap();
|
|
assert!(!public_json.contains("nacos-secret"));
|
|
let encrypted = encrypted_snapshot.encrypted_secrets.as_ref().expect("encrypted secrets");
|
|
let decrypted = decrypt_sensitive_payload(encrypted, "sync-pass").unwrap();
|
|
assert!(decrypted.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "nacos" && secret.key == NACOS_AUTH_PASSWORD_KEY && secret.secret == "nacos-secret"
|
|
}));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sync_never_snapshots_or_restores_nacos_passwords_when_saving_is_disabled() {
|
|
let source = Storage::open(&temp_db_path("sync-no-save-nacos-source")).await.unwrap();
|
|
let mut config = nacos_connection("nacos", "transient-secret");
|
|
config.save_password = false;
|
|
let payload = build_sensitive_payload(&source, std::slice::from_ref(&config), &[]).await.unwrap();
|
|
assert!(!payload.connection_secrets.iter().any(|secret| {
|
|
secret.connection_id == "nacos"
|
|
&& matches!(secret.key.as_str(), NACOS_AUTH_PASSWORD_KEY | NACOS_RNACOS_CONSOLE_PASSWORD_KEY)
|
|
}));
|
|
|
|
let legacy_payload = SensitiveSyncPayload {
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![
|
|
ConnectionSecretSnapshot {
|
|
connection_id: "nacos".to_string(),
|
|
key: NACOS_AUTH_PASSWORD_KEY.to_string(),
|
|
secret: "legacy-primary-secret".to_string(),
|
|
},
|
|
ConnectionSecretSnapshot {
|
|
connection_id: "nacos".to_string(),
|
|
key: NACOS_RNACOS_CONSOLE_PASSWORD_KEY.to_string(),
|
|
secret: "legacy-console-secret".to_string(),
|
|
},
|
|
],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: None,
|
|
ai_config: None,
|
|
tunnel_profiles: None,
|
|
};
|
|
apply_sensitive_payload(&source, &legacy_payload, &[config]).await.unwrap();
|
|
assert_eq!(source.get_secret("nacos", NACOS_AUTH_PASSWORD_KEY).await.unwrap(), None);
|
|
assert_eq!(source.get_secret("nacos", NACOS_RNACOS_CONSOLE_PASSWORD_KEY).await.unwrap(), None);
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sync_snapshot_round_trips_tunnel_profiles() {
|
|
let storage = Storage::open(&temp_db_path("tunnel-profiles-src")).await.unwrap();
|
|
let profile = TransportLayerConfig::Ssh(SshTunnelConfig {
|
|
id: "profile-1".to_string(),
|
|
name: "Bastion".to_string(),
|
|
enabled: true,
|
|
host: "bastion.example.com".to_string(),
|
|
port: 22,
|
|
user: "deploy".to_string(),
|
|
password: "tunnel-secret".to_string(),
|
|
key_path: String::new(),
|
|
key_passphrase: String::new(),
|
|
connect_timeout_secs: 5,
|
|
expose_lan: false,
|
|
use_ssh_agent: false,
|
|
ssh_agent_sock_path: String::new(),
|
|
auth_method: "password".to_string(),
|
|
allow_exec_channel_proxy: false,
|
|
profile_id: String::new(),
|
|
});
|
|
storage.save_tunnel_profiles(std::slice::from_ref(&profile)).await.unwrap();
|
|
|
|
let snapshot = build_sync_snapshot(&storage, "test-version", None, Some("sync-pass")).await.unwrap();
|
|
|
|
// The plain snapshot carries the profiles with secrets scrubbed.
|
|
let public_profiles = snapshot.tunnel_profiles.as_ref().expect("tunnel profiles in snapshot");
|
|
let public_json = serde_json::to_string(public_profiles).unwrap();
|
|
assert!(!public_json.contains("tunnel-secret"));
|
|
|
|
// Applying with the passphrase restores the full profile on the target.
|
|
let target = Storage::open(&temp_db_path("tunnel-profiles-dst")).await.unwrap();
|
|
apply_sync_snapshot(
|
|
&target,
|
|
&snapshot,
|
|
ApplySnapshotOptions { secrets_passphrase: Some("sync-pass"), restore_secrets: true },
|
|
)
|
|
.await
|
|
.unwrap();
|
|
assert_eq!(target.load_tunnel_profiles().await.unwrap(), vec![profile]);
|
|
}
|
|
|
|
// ---- AI configs sync tests ----
|
|
|
|
#[tokio::test]
|
|
async fn sensitive_payload_ai_configs_none_falls_through_to_legacy() {
|
|
let storage = Storage::open(&temp_db_path("ai-cfg-none")).await.unwrap();
|
|
|
|
// No ai_configs in payload — fall through to ai_config (legacy) branch
|
|
let payload = SensitiveSyncPayload {
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: None,
|
|
ai_config: None,
|
|
tunnel_profiles: None,
|
|
};
|
|
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
|
|
let loaded = storage.load_ai_configs().await.unwrap();
|
|
assert!(loaded.is_empty(), "None → no configs written");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sensitive_payload_ai_configs_empty_clears_table() {
|
|
let storage = Storage::open(&temp_db_path("ai-cfg-empty")).await.unwrap();
|
|
|
|
// Pre-populate with a config
|
|
let cfg = make_test_config("to-be-cleared", true);
|
|
storage.save_ai_config_item(&cfg).await.unwrap();
|
|
|
|
// Some([]) — explicit clear
|
|
let payload = SensitiveSyncPayload {
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: Some(vec![]),
|
|
ai_config: None,
|
|
tunnel_profiles: None,
|
|
};
|
|
apply_sensitive_payload(&storage, &payload, &[]).await.unwrap();
|
|
let loaded = storage.load_ai_configs().await.unwrap();
|
|
assert!(loaded.is_empty(), "Some([]) → table cleared");
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sensitive_payload_ai_configs_some_saves_configs() {
|
|
let storage = Storage::open(&temp_db_path("ai-cfg-some")).await.unwrap();
|
|
|
|
let mut cfg = make_test_config("synced", true);
|
|
cfg.config.provider = crate::ai::AiProvider::OpenCodeCli;
|
|
cfg.config.model = "openai/gpt-5.4-mini".to_string();
|
|
cfg.config.opencode_cli_path = Some("/opt/homebrew/bin/opencode".to_string());
|
|
cfg.config.opencode_cli_env.insert("HTTPS_PROXY".to_string(), "http://127.0.0.1:7890".to_string());
|
|
let mut cursor_cfg = make_test_config("cursor-synced", false);
|
|
cursor_cfg.config.provider = crate::ai::AiProvider::CursorCli;
|
|
cursor_cfg.config.model = "composer-2.5".to_string();
|
|
cursor_cfg.config.cursor_cli_path = Some("~/.local/bin/agent".to_string());
|
|
cursor_cfg.config.cursor_cli_env.insert("NO_PROXY".to_string(), "localhost".to_string());
|
|
let payload = SensitiveSyncPayload {
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: Some(vec![cfg, cursor_cfg]),
|
|
ai_config: None,
|
|
tunnel_profiles: None,
|
|
};
|
|
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();
|
|
assert!(matches!(opencode.config.provider, crate::ai::AiProvider::OpenCodeCli));
|
|
assert_eq!(opencode.config.model, "openai/gpt-5.4-mini");
|
|
assert_eq!(opencode.config.opencode_cli_path.as_deref(), Some("/opt/homebrew/bin/opencode"));
|
|
assert_eq!(
|
|
opencode.config.opencode_cli_env.get("HTTPS_PROXY").map(String::as_str),
|
|
Some("http://127.0.0.1:7890")
|
|
);
|
|
let cursor = loaded.iter().find(|item| item.name == "cursor-synced").unwrap();
|
|
assert!(matches!(cursor.config.provider, crate::ai::AiProvider::CursorCli));
|
|
assert_eq!(cursor.config.model, "composer-2.5");
|
|
assert_eq!(cursor.config.cursor_cli_path.as_deref(), Some("~/.local/bin/agent"));
|
|
assert_eq!(cursor.config.cursor_cli_env.get("NO_PROXY").map(String::as_str), Some("localhost"));
|
|
}
|
|
|
|
#[tokio::test]
|
|
async fn sensitive_payload_legacy_ai_config_replaces_local_configs_with_same_name() {
|
|
let storage = Storage::open(&temp_db_path("ai-cfg-legacy-replace")).await.unwrap();
|
|
storage.save_ai_config_item(&make_test_config("openai", true)).await.unwrap();
|
|
storage.save_ai_config_item(&make_test_config("local-only", false)).await.unwrap();
|
|
|
|
let mut legacy_config = make_test_config("unused", true).config;
|
|
legacy_config.model = "snapshot-model".to_string();
|
|
let payload = SensitiveSyncPayload {
|
|
plugin_secrets_included: true,
|
|
connection_secrets: vec![],
|
|
sync_credentials: Some(vec![]),
|
|
ai_configs: None,
|
|
ai_config: Some(legacy_config),
|
|
tunnel_profiles: None,
|
|
};
|
|
|
|
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();
|
|
|
|
let loaded = storage.load_ai_configs().await.unwrap();
|
|
assert_eq!(loaded.len(), 1);
|
|
assert_eq!(loaded[0].name, "openai");
|
|
assert_eq!(loaded[0].config.model, "snapshot-model");
|
|
assert!(loaded[0].is_default);
|
|
}
|
|
}
|