mirror of
https://github.com/farion1231/cc-switch.git
synced 2026-04-19 01:43:08 +08:00
fix(backup): restrict SQL import to CC Switch exported backups only
- Add validation to reject SQL files without CC Switch export header - Remove redundant sanitize_import_sql (sqlite_* objects already excluded at export time) - Fix backup filename collision by appending counter suffix - Update i18n hints to clarify import restriction
This commit is contained in:
@@ -1003,3 +1003,76 @@ fn export_sql_returns_error_for_invalid_path() {
|
||||
other => panic!("expected IoContext or Io error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_sql_rejects_non_cc_switch_backup() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
let state = create_test_state().expect("create test state");
|
||||
|
||||
let import_path = home.join("not-cc-switch.sql");
|
||||
fs::write(&import_path, "CREATE TABLE x (id INTEGER);").expect("write import sql");
|
||||
|
||||
let err = state
|
||||
.db
|
||||
.import_sql(&import_path)
|
||||
.expect_err("non-cc-switch sql should be rejected");
|
||||
|
||||
match err {
|
||||
AppError::Localized { key, .. } => {
|
||||
assert_eq!(key, "backup.sql.invalid_format");
|
||||
}
|
||||
other => panic!("expected Localized error, got {other:?}"),
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn import_sql_accepts_cc_switch_exported_backup() {
|
||||
let _guard = test_mutex().lock().expect("acquire test mutex");
|
||||
reset_test_fs();
|
||||
let home = ensure_test_home();
|
||||
|
||||
// Create a database with some data and export it.
|
||||
let mut config = MultiAppConfig::default();
|
||||
{
|
||||
let manager = config
|
||||
.get_manager_mut(&AppType::Claude)
|
||||
.expect("claude manager");
|
||||
manager.current = "test-provider".to_string();
|
||||
manager.providers.insert(
|
||||
"test-provider".to_string(),
|
||||
Provider::with_id(
|
||||
"test-provider".to_string(),
|
||||
"Test Provider".to_string(),
|
||||
json!({"env": {"ANTHROPIC_API_KEY": "test-key"}}),
|
||||
None,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
let state = create_test_state_with_config(&config).expect("create test state");
|
||||
let export_path = home.join("cc-switch-export.sql");
|
||||
state
|
||||
.db
|
||||
.export_sql(&export_path)
|
||||
.expect("export should succeed");
|
||||
|
||||
// Reset database, then import into a fresh one.
|
||||
reset_test_fs();
|
||||
let state = create_test_state().expect("create test state");
|
||||
state
|
||||
.db
|
||||
.import_sql(&export_path)
|
||||
.expect("import should succeed");
|
||||
|
||||
let providers = state
|
||||
.db
|
||||
.get_all_providers(AppType::Claude.as_str())
|
||||
.expect("load providers");
|
||||
assert!(
|
||||
providers.contains_key("test-provider"),
|
||||
"imported providers should contain test-provider"
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user