fix(export): harden config export passphrase handling

This commit is contained in:
dadong authored and GitHub committed 2026-08-28 11:11:35 +08:00
1 parent d8ec9bb8d0
commit 99a9961712
7 files changed
+202 -10

No files matched your search

@@ -32,12 +32,14 @@ vi.mock("@/stores/connectionStore", () => ({ useConnectionStore: () => mocks.sto
vi.mock("@/composables/useToast", () => ({ useToast: () => ({ toast: mocks.toast }) }));
import { useDialogSources } from "@/composables/useDialogSources";
import { clearRememberedExportPassphrase, getRememberedExportPassphrase } from "@/lib/backend/exportPassphraseSession";
const mountedApps: App[] = [];
beforeEach(() => {
mocks.store.connections = [];
mocks.store.exportConnectionsToFile.mockReset().mockResolvedValue("saved");
clearRememberedExportPassphrase();
});
afterEach(() => {
@@ -178,4 +180,50 @@ describe("useDialogSources", () => {
await second;
expect(dialogs.configExportBusy.value).toBe(false);
});
it("keeps the passphrase dialog open and reports an error when writing the encrypted export fails", async () => {
mocks.store.connections = [conn("selected")];
mocks.store.exportConnectionsToFile.mockRejectedValue(new Error("disk full"));
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
await dialogs.onExportConfirm("passphrase");
expect(dialogs.showConfigPassphraseDialog.value).toBe(true);
expect(dialogs.configPassphraseError.value).toBe("Failed to export connections");
expect(mocks.toast).not.toHaveBeenCalled();
expect(getRememberedExportPassphrase()).toBe("");
});
it("reports an error instead of success when writing the plaintext export fails", async () => {
mocks.store.connections = [conn("selected")];
mocks.store.exportConnectionsToFile.mockRejectedValue(new Error("disk full"));
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
dialogs.onRequestUnencryptedExport();
await dialogs.onConfigUnencryptedExportConfirm();
expect(dialogs.showConfigUnencryptedExportConfirm.value).toBe(false);
expect(dialogs.showConfigPassphraseDialog.value).toBe(true);
expect(dialogs.configPassphraseError.value).toBe("Failed to export connections");
expect(mocks.toast).not.toHaveBeenCalled();
});
it("remembers the passphrase only after the export file is actually written", async () => {
mocks.store.connections = [conn("selected")];
mocks.store.exportConnectionsToFile.mockResolvedValueOnce("cancelled").mockResolvedValueOnce("saved");
const dialogs = await mountDialogs();
dialogs.onExportClick();
dialogs.onConfigConnectionSelectConfirm(["selected"]);
await dialogs.onExportConfirm("passphrase");
expect(getRememberedExportPassphrase()).toBe("");
await dialogs.onExportConfirm("passphrase");
expect(getRememberedExportPassphrase()).toBe("passphrase");
});
});
@@ -2,6 +2,7 @@ import { ref, watch } from "vue";
import { useI18n } from "vue-i18n";
import { useConnectionStore } from "@/stores/connectionStore";
import { useToast } from "@/composables/useToast";
import { rememberExportPassphrase } from "@/lib/backend/exportPassphraseSession";
import { hasSidebarLayoutEntries } from "@/lib/sidebar/sidebarLayout";
import type { ConnectionConfigBundle } from "@/lib/connection/connectionConfigTransfer";
import type { ConnectionConfig, SidebarLayout } from "@/types/database";
@@ -337,6 +338,8 @@ export function useDialogSources() {
try {
const result = await connectionStore.exportConnectionsToFile({ mode: "encrypted", passphrase }, pendingExportConnectionIds.value);
if (result === "cancelled") return;
// 仅在文件写入成功后才记住密码短语,供同一会话内下次导出对话框回显(仅内存,不落盘)
rememberExportPassphrase(passphrase);
showConfigPassphraseDialog.value = false;
clearPendingExportState();
toast(t("configExport.exportSuccess"), 2000);