AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.
HTML ai-agentcloudflarecloudflare-d1cloudflare-r2cloudflare-workersdockerelectronevernoteevernote-alternativeknowledge-basemcpnote-takingobsidian-alternativeoffline-firstpersonal-knowledge-basepwaself-hostedsqlitetiptapweb-clipper
fix(backup): upload real WebDAV archives and keep worker write errors (#733)
Dariusz Koryto6e6eba0ed2e7
diff --git a/apps/desktop/src/backup/worker-integration.test.mjs b/apps/desktop/src/backup/worker-integration.test.mjsindex 2fc741d0..b3c267cf 100644--- a/apps/desktop/src/backup/worker-integration.test.mjs+++ b/apps/desktop/src/backup/worker-integration.test.mjs@@ -235,3 +235,73 @@ test("manager creates named files, restores from history and deletes only the seexpect(await readdir(status.actualDirectory)).toEqual(["Encrypted snapshot.edgeever.enc"]);expect(manager.status().history.map(entry => entry.name)).toEqual(["Encrypted snapshot.edgeever.enc"]);}, 15000);++const stateServer = (respond) => {+ const server = Bun.serve({ hostname: "127.0.0.1", port: 0, fetch: respond });+ cleanups.push(() => { server.stop(true); });+ return `http://127.0.0.1:${server.port}`;+};+const emptyCounts = { notebooks: 0, memos: 0, revisions: 0, resources: 0, prompts: 0 };+for (const [name, respond, accountId, code] of [+ ["servers without the backup state endpoint", () => new Response("Not found", { status: 404 }), null, "backup_server_upgrade_required"],+ ["unsupported backup protocols", () => Response.json({ protocolVersion: 2, userId: null, workspaceId: "ws", identity: "i", backupRevision: "0", counts: emptyCounts }), null, "backup_server_upgrade_required"],+ ["a source owned by another account", () => Response.json({ protocolVersion: 1, userId: "someone-else", workspaceId: "ws", identity: "i", backupRevision: "0", counts: emptyCounts }), "me", "backup_account_changed"],+]) test(`export rejects ${name} without writing an archive`, async () => {+ const root = await directory();+ const job = await launch({ operation: "export", context: { baseUrl: stateServer(respond), accountId, token: "" }, path: join(root, "backup.zip.part"), version: { edgeeverVersion: "2.2.0", buildId: "state" } });+ await expect(job.next("done")).rejects.toMatchObject({ code });+ expect(await readdir(root)).toEqual([]);+}, 10000);++test("export fails when the source changes while the archive is written", async () => {+ let stateRequests = 0;+ let source;+ source = await instance({ async intercept(request) {+ if (new URL(request.url).pathname === "/api/v1/backups/state" && ++stateRequests === 2) await source.client.createNotebook({ name: "Written during backup" });+ } });+ await source.client.createNotebook({ name: "Before backup" });+ const job = await launch({ operation: "export", context: { baseUrl: source.baseUrl, accountId: null, token: "" }, path: join(await directory(), "backup.zip.part"), version: { edgeeverVersion: "2.2.0", buildId: "changed" } });+ await expect(job.next("done")).rejects.toMatchObject({ code: "backup_source_changed" });+ expect(stateRequests).toBe(2);+}, 10000);++test("export rejects attachments whose bytes do not match the recorded digest", async () => {+ const source = await instance({ async intercept(request) {+ if (/^\/api\/v1\/resources\/[^/]+\/blob$/.test(new URL(request.url).pathname)) return new Response(new Uint8Array(64).fill(0), { headers: { "Content-Type": "application/octet-stream" } });+ } });+ const { notebook } = await source.client.createNotebook({ name: "Damaged attachment" });+ const { memo } = await source.client.createMemo({ notebookId: notebook.id, contentMarkdown: "attachment" });+ await source.client.uploadMemoResource(memo.id, new File([new Uint8Array(64).fill(7)], "original.bin", { type: "application/octet-stream" }));+ const job = await launch({ operation: "export", context: { baseUrl: source.baseUrl, accountId: null, token: "" }, path: join(await directory(), "backup.zip.part"), version: { edgeeverVersion: "2.2.0", buildId: "digest" } });+ await expect(job.next("done")).rejects.toMatchObject({ code: "incompleteResources" });+}, 10000);++test("verify checks archives offline and rejects damaged files and wrong passwords", async () => {+ const source = await instance();+ const { notebook } = await source.client.createNotebook({ name: "Verify" });+ await source.client.createMemo({ notebookId: notebook.id, contentMarkdown: "verify me" });+ const root = await directory();+ const plain = join(root, "backup.zip"); const encrypted = join(root, "backup.edgeever.enc");+ const counts = (await (await fetch(`${source.baseUrl}/api/v1/backups/state`)).json()).counts;+ for (const [path, password] of [[plain, undefined], [encrypted, "fixture password"]]) {+ await (await launch({ operation: "export", context: { baseUrl: source.baseUrl, accountId: null, token: "" }, path, password, version: { edgeeverVersion: "2.2.0", buildId: "verify" } })).next("done");+ }+ source.server.stop(true);+ // An unreachable server proves verification never touches the network.+ const offline = { baseUrl: "http://127.0.0.1:9", accountId: null, token: "" };+ for (const [path, password] of [[plain, undefined], [encrypted, "fixture password"]]) {+ const staging = await directory();+ const verified = await launch({ operation: "verify", context: offline, path, password, stagingDirectory: staging });+ expect((await verified.next("done")).result.manifest.counts).toEqual(counts);+ expect(verified.messages.some(message => message.type === "ready")).toBe(false);+ expect(await readdir(staging)).toEqual([]);+ }+ const wrongPassword = await launch({ operation: "verify", context: offline, path: encrypted, password: "wrong password", stagingDirectory: await directory() });+ await expect(wrongPassword.next("done")).rejects.toMatchObject({ code: "backup_decrypt_failed" });+ const damaged = join(root, "damaged.zip");+ await Bun.write(damaged, (await readFile(plain)).subarray(0, 100));+ const damagedStaging = await directory();+ const truncated = await launch({ operation: "verify", context: offline, path: damaged, stagingDirectory: damagedStaging });+ await expect(truncated.next("done")).rejects.toMatchObject({ code: "invalidZip" });+ expect(await readdir(damagedStaging)).toEqual([]);+}, 20000);diff --git a/apps/desktop/src/backup/worker.ts b/apps/desktop/src/backup/worker.tsindex fbeb9568..76c8289a 100644--- a/apps/desktop/src/backup/worker.ts+++ b/apps/desktop/src/backup/worker.ts@@ -62,6 +62,9 @@ const readState = async () => {}return state;};+// A failed pipeline destroys the write stream, which closes the shared fd despite+// autoClose: false. Ignore that close so the original failure reaches the caller.+const closeOutput = (handle: import("node:fs/promises").FileHandle) => handle.close().catch((error) => { if (error?.code !== "EBADF") throw error; });const digestFactory = () => {const hash = createHash("sha256");return { update: (chunk: Uint8Array) => { hash.update(chunk); }, digest: () => hash.digest("hex") };@@ -136,7 +139,7 @@ const runExport = async () => {await output.sync();const size = (await output.stat()).size;return { manifest, size, sourceState };- } finally { await output.close(); }+ } finally { await closeOutput(output); }};const diskArchive = async (path: string) => {@@ -175,7 +178,7 @@ const runImport = async () => {} else {const output = await open(staged, "wx", 0o600);try { await pipeline(createReadStream(input.path), createWriteStream(staged, { fd: output.fd, autoClose: false }), { signal }); }- finally { await output.close(); }+ finally { await closeOutput(output); }}archive = await diskArchive(staged);const backup = await parseEdgeEverZip(archive.blob, (percentage) => send("progress", { stage: "checking", completed: percentage, total: 100 }), digestFactory);diff --git a/apps/web/src/lib/webdav-backup.test.mjs b/apps/web/src/lib/webdav-backup.test.mjsnew file mode 100644index 00000000..5378a491--- /dev/null+++ b/apps/web/src/lib/webdav-backup.test.mjs@@ -0,0 +1,227 @@+import { afterEach, describe, expect, test } from "bun:test";+import {+ DEFAULT_WEBDAV_BACKUP_CONFIG,+ DEFAULT_WEBDAV_BACKUP_SCHEDULE,+ isWebDavBackupDue,+ loadWebDavBackupConfig,+ loadWebDavBackupPassword,+ loadWebDavBackupSchedule,+ normalizeWebDavBackupConfig,+ saveWebDavBackupConfig,+ saveWebDavBackupPassword,+ saveWebDavBackupSchedule,+ testWebDavConnection,+ uploadWebDavBackup,+} from "./webdav-backup.ts";++const DAY = 24 * 60 * 60 * 1000;+const originalWindow = Object.getOwnPropertyDescriptor(globalThis, "window");++const installStorage = (initial = {}, { throwOnRead = false } = {}) => {+ const values = new Map(Object.entries(initial));+ Object.defineProperty(globalThis, "window", {+ configurable: true,+ value: {+ localStorage: {+ getItem: (key) => {+ if (throwOnRead) throw new Error("storage disabled");+ return values.has(key) ? values.get(key) : null;+ },+ setItem: (key, value) => values.set(key, String(value)),+ removeItem: (key) => values.delete(key),+ },+ },+ });+ return values;+};++const servers = [];+afterEach(() => {+ if (originalWindow) Object.defineProperty(globalThis, "window", originalWindow);+ else delete globalThis.window;+ for (const server of servers.splice(0)) server.stop(true);+});++// Minimal WebDAV endpoint: enough of PROPFIND/MKCOL/PUT for the webdav client.+const startWebDav = ({ password = "secret", rejectPut = false } = {}) => {+ const requests = [];+ const files = new Map();+ const collections = new Set(["/"]);+ const server = Bun.serve({+ hostname: "127.0.0.1",+ port: 0,+ async fetch(request) {+ const path = decodeURIComponent(new URL(request.url).pathname).replace(/\/+$/, "") || "/";+ const body = request.method === "PUT" ? new Uint8Array(await request.arrayBuffer()) : null;+ requests.push({ method: request.method, path, headers: Object.fromEntries(request.headers), body });+ if (request.headers.get("authorization") !== `Basic ${btoa(`alice:${password}`)}`) return new Response(null, { status: 401 });+ if (request.method === "PROPFIND") {+ if (!collections.has(path)) return new Response(null, { status: 404 });+ return new Response(`<?xml version="1.0"?><d:multistatus xmlns:d="DAV:"><d:response><d:href>${path}/</d:href><d:propstat><d:prop><d:resourcetype><d:collection/></d:resourcetype></d:prop><d:status>HTTP/1.1 200 OK</d:status></d:propstat></d:response></d:multistatus>`, { status: 207, headers: { "Content-Type": "application/xml" } });+ }+ if (request.method === "MKCOL") {+ if (collections.has(path)) return new Response(null, { status: 405 });+ collections.add(path);+ return new Response(null, { status: 201 });+ }+ if (request.method === "PUT") {+ if (rejectPut || (files.has(path) && request.headers.get("if-none-match") === "*")) return new Response(null, { status: 412 });+ files.set(path, body);+ return new Response(null, { status: 201 });+ }+ return new Response(null, { status: 405 });+ },+ });+ servers.push(server);+ return { url: `http://127.0.0.1:${server.port}`, requests, files, collections };+};++describe("normalizeWebDavBackupConfig", () => {+ test("trims fields, drops trailing URL slashes and normalizes the remote path", () => {+ expect(normalizeWebDavBackupConfig({ url: " https://dav.example.com/remote.php/dav/// ", username: " alice ", remotePath: " \\Backups\\EdgeEver\\ " })).toEqual({+ url: "https://dav.example.com/remote.php/dav",+ username: "alice",+ remotePath: "/Backups/EdgeEver",+ });+ });++ test("collapses empty and root remote paths to /", () => {+ for (const remotePath of ["", " ", "/", "///"]) {+ expect(normalizeWebDavBackupConfig({ url: "", username: "", remotePath }).remotePath).toBe("/");+ }+ });+});++describe("config persistence", () => {+ test("returns defaults when nothing is stored, the JSON is corrupt or storage throws", () => {+ installStorage();+ expect(loadWebDavBackupConfig()).toEqual(DEFAULT_WEBDAV_BACKUP_CONFIG);+ installStorage({ "edgeever.webdav-backup-config": "{not json" });+ expect(loadWebDavBackupConfig()).toEqual(DEFAULT_WEBDAV_BACKUP_CONFIG);+ installStorage({}, { throwOnRead: true });+ expect(loadWebDavBackupConfig()).toEqual(DEFAULT_WEBDAV_BACKUP_CONFIG);+ });++ test("saves the normalized config and merges partial stored values with defaults", () => {+ const values = installStorage();+ const saved = saveWebDavBackupConfig({ url: "https://dav.example.com/", username: " alice ", remotePath: "notes/" });+ expect(saved).toEqual({ url: "https://dav.example.com", username: "alice", remotePath: "/notes" });+ expect(JSON.parse(values.get("edgeever.webdav-backup-config"))).toEqual(saved);+ expect(loadWebDavBackupConfig()).toEqual(saved);++ installStorage({ "edgeever.webdav-backup-config": JSON.stringify({ url: "https://dav.example.com" }) });+ expect(loadWebDavBackupConfig()).toEqual({ ...DEFAULT_WEBDAV_BACKUP_CONFIG, url: "https://dav.example.com" });+ });+});++describe("password persistence", () => {+ test("never returns a stored password and purges it while automatic backups are disabled", () => {+ const values = installStorage();+ saveWebDavBackupPassword("hunter2");+ expect(values.get("edgeever.webdav-backup-password")).toBe("hunter2");+ expect(loadWebDavBackupPassword()).toBe("");+ expect(values.has("edgeever.webdav-backup-password")).toBe(false);+ });++ test("returns an empty password when storage is unavailable", () => {+ Object.defineProperty(globalThis, "window", { configurable: true, value: {} });+ expect(loadWebDavBackupPassword()).toBe("");+ });+});++describe("schedule persistence", () => {+ test("round-trips a saved schedule", () => {+ installStorage();+ const schedule = { enabled: true, intervalDays: 14, lastSuccessAt: "2026-10-01T00:00:00.000Z", lastAttemptAt: "2026-10-02T00:00:00.000Z" };+ expect(saveWebDavBackupSchedule(schedule)).toBe(schedule);+ expect(loadWebDavBackupSchedule()).toEqual(schedule);+ });++ test("sanitizes unknown intervals, non-boolean flags and non-string timestamps", () => {+ installStorage({ "edgeever.webdav-backup-schedule": JSON.stringify({ enabled: "true", intervalDays: 3, lastSuccessAt: 123, lastAttemptAt: null }) });+ expect(loadWebDavBackupSchedule()).toEqual(DEFAULT_WEBDAV_BACKUP_SCHEDULE);+ installStorage({ "edgeever.webdav-backup-schedule": JSON.stringify({ enabled: true, intervalDays: "30" }) });+ expect(loadWebDavBackupSchedule()).toEqual({ ...DEFAULT_WEBDAV_BACKUP_SCHEDULE, enabled: true, intervalDays: 30 });+ });++ test("returns defaults when nothing is stored or the JSON is corrupt", () => {+ installStorage();+ expect(loadWebDavBackupSchedule()).toEqual(DEFAULT_WEBDAV_BACKUP_SCHEDULE);+ installStorage({ "edgeever.webdav-backup-schedule": "[" });+ expect(loadWebDavBackupSchedule()).toEqual(DEFAULT_WEBDAV_BACKUP_SCHEDULE);+ });+});++describe("isWebDavBackupDue", () => {+ const now = Date.parse("2026-10-29T12:00:00.000Z");+ const schedule = (overrides) => ({ ...DEFAULT_WEBDAV_BACKUP_SCHEDULE, enabled: true, intervalDays: 7, ...overrides });++ test("is never due while disabled", () => {+ expect(isWebDavBackupDue(schedule({ enabled: false }), now)).toBe(false);+ });++ test("is due when there is no previous run or the timestamp is unreadable", () => {+ expect(isWebDavBackupDue(schedule(), now)).toBe(true);+ expect(isWebDavBackupDue(schedule({ lastAttemptAt: "not a date" }), now)).toBe(true);+ });++ test("becomes due exactly when the interval has elapsed", () => {+ expect(isWebDavBackupDue(schedule({ lastSuccessAt: new Date(now - 7 * DAY + 1).toISOString() }), now)).toBe(false);+ expect(isWebDavBackupDue(schedule({ lastSuccessAt: new Date(now - 7 * DAY).toISOString() }), now)).toBe(true);+ });++ test("measures from the last attempt so failing servers are not retried on every tick", () => {+ expect(isWebDavBackupDue(schedule({ lastSuccessAt: new Date(now - 30 * DAY).toISOString(), lastAttemptAt: new Date(now - DAY).toISOString() }), now)).toBe(false);+ });+});++describe("WebDAV client", () => {+ test("rejects non-HTTP URLs and missing passwords before any request", async () => {+ await expect(testWebDavConnection({ url: "ftp://dav.example.com", username: "alice", remotePath: "/" }, "secret")).rejects.toThrow("WebDAV URL must start with http:// or https://.");+ await expect(uploadWebDavBackup({ url: "https://dav.example.com", username: "alice", remotePath: "/" }, "", new Blob(["x"]))).rejects.toThrow("WebDAV password is required.");+ });++ test("tests the connection by listing the normalized remote directory with basic auth", async () => {+ const dav = startWebDav();+ dav.collections.add("/EdgeEver/backups");+ await testWebDavConnection({ url: `${dav.url}/`, username: " alice ", remotePath: "EdgeEver/backups/" }, "secret");+ expect(dav.requests.map(({ method, path }) => [method, path])).toEqual([["PROPFIND", "/EdgeEver/backups"]]);+ });++ test("surfaces authentication and missing-directory failures", async () => {+ const dav = startWebDav();+ await expect(testWebDavConnection({ url: dav.url, username: "alice", remotePath: "/" }, "wrong")).rejects.toThrow();+ await expect(testWebDavConnection({ url: dav.url, username: "alice", remotePath: "/missing" }, "secret")).rejects.toThrow();+ });++ test("creates the remote directory and uploads a timestamped archive without overwriting", async () => {+ const dav = startWebDav();+ const archive = new Blob([new Uint8Array([80, 75, 3, 4, 1, 2, 3])], { type: "application/zip" });+ const result = await uploadWebDavBackup({ url: dav.url, username: "alice", remotePath: "/EdgeEver/backups" }, "secret", archive, new Date("2026-10-29T08:15:30.123Z"));+ expect(result).toEqual({ filename: "edgeever-2026-10-29T08-15-30-123Z.zip", remotePath: "/EdgeEver/backups/edgeever-2026-10-29T08-15-30-123Z.zip", size: 7 });+ expect(dav.collections.has("/EdgeEver/backups")).toBe(true);+ expect(dav.files.get(result.remotePath)).toEqual(new Uint8Array([80, 75, 3, 4, 1, 2, 3]));+ const put = dav.requests.find(({ method }) => method === "PUT");+ expect(put.headers["if-none-match"]).toBe("*");+ });++ test("reports a rejected upload instead of claiming success", async () => {+ const dav = startWebDav({ rejectPut: true });+ await expect(uploadWebDavBackup({ url: dav.url, username: "alice", remotePath: "/EdgeEver/backups" }, "secret", new Blob(["zip"]))).rejects.toThrow();+ });++ test("does not overwrite an existing archive with the same timestamp", async () => {+ const dav = startWebDav();+ const config = { url: dav.url, username: "alice", remotePath: "/EdgeEver/backups" };+ const timestamp = new Date("2026-10-29T08:15:30.123Z");+ await uploadWebDavBackup(config, "secret", new Blob(["first"]), timestamp);+ await expect(uploadWebDavBackup(config, "secret", new Blob(["second"]), timestamp)).rejects.toThrow();+ expect(new TextDecoder().decode(dav.files.get("/EdgeEver/backups/edgeever-2026-10-29T08-15-30-123Z.zip"))).toBe("first");+ });++ test("uploads into the server root without a double slash", async () => {+ const dav = startWebDav();+ const result = await uploadWebDavBackup({ url: dav.url, username: "alice", remotePath: "/" }, "secret", new Blob(["zip"]), new Date("2026-10-29T08:15:30.123Z"));+ expect(result.remotePath).toBe("/edgeever-2026-10-29T08-15-30-123Z.zip");+ });+});diff --git a/apps/web/src/lib/webdav-backup.ts b/apps/web/src/lib/webdav-backup.tsindex aac80d8a..36e11d29 100644--- a/apps/web/src/lib/webdav-backup.ts+++ b/apps/web/src/lib/webdav-backup.ts@@ -146,10 +146,9 @@ export const uploadWebDavBackup = async (await client.createDirectory(normalized.remotePath, { recursive: true });const filename = `edgeever-${timestamp.toISOString().replace(/[:.]/g, "-")}.zip`;- const remotePath = `${normalized.remotePath}/${filename}`;- // The fetch-backed browser client accepts Blob/File bodies even though its- // published TypeScript signature only lists ArrayBuffer-like payloads.- const uploaded = await client.putFileContents(remotePath, archive as never, {+ const remotePath = `${normalized.remotePath.replace(/\/$/, "")}/${filename}`;+ // The webdav client JSON-serializes Blob/File bodies, so send raw bytes.+ const uploaded = await client.putFileContents(remotePath, await archive.arrayBuffer(), {contentLength: archive.size,overwrite: false,});