Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(desktop): preserve attachment downloads behind navigation guard

tianma-iffda8cf9ba683
diff --git a/apps/desktop/src/main/index.mjs b/apps/desktop/src/main/index.mjs
index 4f70160e..8d242ee4 100644
--- a/apps/desktop/src/main/index.mjs
+++ b/apps/desktop/src/main/index.mjs
@@ -30,7 +30,7 @@ import {
mountedInstallerCandidates,
} from "./installation-location.mjs";
import { userDataDirectoryFromArguments } from "./user-data-directory.mjs";
-import { isAllowedAppNavigation, isAllowedPrintPreviewUrl } from "./window-open-policy.mjs";
+import { isAllowedAppNavigation, isAllowedPrintPreviewUrl, isResourceDownloadUrl } from "./window-open-policy.mjs";
import { showWindow } from "./window-visibility.mjs";
import { globalShortcutAccelerator, normalizeGlobalShortcutBinding, replaceGlobalShortcut, toggleMainWindow } from "./global-shortcut.mjs";
import { trayIconPath } from "./tray-icon.mjs";
@@ -1575,7 +1575,10 @@ const createWindow = async () => {
if (isAllowedAppNavigation(url, rendererEntryUrl)) return;
event.preventDefault();
};
- mainWindow.webContents.on("will-navigate", guardNavigation);
+ mainWindow.webContents.on("will-navigate", (event, url) => {
+ guardNavigation(event, url);
+ if (isResourceDownloadUrl(url)) mainWindow.webContents.downloadURL(url);
+ });
mainWindow.webContents.on("will-redirect", guardNavigation);
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
diff --git a/apps/desktop/src/main/window-open-policy.mjs b/apps/desktop/src/main/window-open-policy.mjs
index 8df4f814..9f921424 100644
--- a/apps/desktop/src/main/window-open-policy.mjs
+++ b/apps/desktop/src/main/window-open-policy.mjs
@@ -1,5 +1,19 @@
import { dirname, join } from "node:path";
import { fileURLToPath, pathToFileURL } from "node:url";
+import { downloadContentDispositionFromRequest, resourceIdFromRequest } from "./resource-url.mjs";
+
+export const isResourceDownloadUrl = (targetUrl) => {
+ try {
+ const target = new URL(targetUrl);
+ return ["edgeever-resource:", "edgeever-staged:"].includes(target.protocol)
+ && !target.username && !target.password && !target.port
+ && (target.protocol === "edgeever-resource:"
+ ? target.hostname === "resource" && target.pathname.length > 1
+ : Boolean(target.hostname) && ["", "/"].includes(target.pathname))
+ && Boolean(resourceIdFromRequest(targetUrl))
+ && Boolean(downloadContentDispositionFromRequest(targetUrl));
+ } catch { return false; }
+};
export const isAllowedAppNavigation = (targetUrl, appUrl) => {
try {
diff --git a/apps/desktop/src/main/window-open-policy.test.mjs b/apps/desktop/src/main/window-open-policy.test.mjs
index 034e8caa..85c7b824 100644
--- a/apps/desktop/src/main/window-open-policy.test.mjs
+++ b/apps/desktop/src/main/window-open-policy.test.mjs
@@ -1,5 +1,14 @@
import { describe, expect, test } from "bun:test";
-import { isAllowedPrintPreviewUrl } from "./window-open-policy.mjs";
+import { isAllowedPrintPreviewUrl, isResourceDownloadUrl } from "./window-open-policy.mjs";
+
+test("resource downloads require a safe resource ID and explicit download filename", () => {
+ for (const url of ["edgeever-resource://resource/note?download=note.html", "edgeever-staged://pending?download=note.txt"]) {
+ expect(isResourceDownloadUrl(url)).toBe(true);
+ }
+ for (const url of ["edgeever-resource://resource/note", "edgeever-staged://pending", "https://example.org/note?download=note.html", "edgeever-resource://user@resource/note?download=note.html", "edgeever-resource://resource/%2e%2e?download=note.html", "edgeever-staged://pending?download=%00"]) {
+ expect(isResourceDownloadUrl(url)).toBe(false);
+ }
+});
describe("desktop window open policy", () => {
test("allows only the packaged print preview beside the app entry", () => {
diff --git a/crates/desktop-sidecar/src/memo.rs b/crates/desktop-sidecar/src/memo.rs
index 7f46fde2..d76c5972 100644
--- a/crates/desktop-sidecar/src/memo.rs
+++ b/crates/desktop-sidecar/src/memo.rs
@@ -603,17 +603,35 @@ pub(crate) fn update_memo(database: &Connection, params: &Value) -> Result<Value
let guarded = params.get("expectedLocalSnapshot");
if let Some(expected) = guarded {
previous = memo_value(&tx, &memo_id, true)?;
- let fields = ["revision", "contentHash", "title", "tags", "contentMarkdown", "contentJson"];
- if previous.get("isDeleted").and_then(Value::as_bool).unwrap_or(false)
- || fields.iter().any(|key| expected.get(*key).is_none() || previous.get(*key) != expected.get(*key)) {
- return Err("NOTE_CONFLICT: The note changed locally. Read it again before editing.".to_owned());
+ let fields = [
+ "revision",
+ "contentHash",
+ "title",
+ "tags",
+ "contentMarkdown",
+ "contentJson",
+ ];
+ if previous
+ .get("isDeleted")
+ .and_then(Value::as_bool)
+ .unwrap_or(false)
+ || fields
+ .iter()
+ .any(|key| expected.get(*key).is_none() || previous.get(*key) != expected.get(*key))
+ {
+ return Err(
+ "NOTE_CONFLICT: The note changed locally. Read it again before editing.".to_owned(),
+ );
}
let conflicts: i64 = tx.query_row(
"SELECT COUNT(*) FROM _edgeever_sidecar_outbox WHERE entity_id = ?1 AND status = 'conflict'",
[&memo_id], |row| row.get(0),
).map_err(|e| e.to_string())?;
if conflicts > 0 {
- return Err("NOTE_CONFLICT: Resolve the note's sync conflict before editing from a plugin.".to_owned());
+ return Err(
+ "NOTE_CONFLICT: Resolve the note's sync conflict before editing from a plugin."
+ .to_owned(),
+ );
}
}
insert_revision(&tx, &previous, "desktop")?;
diff --git a/crates/desktop-sidecar/src/rpc.rs b/crates/desktop-sidecar/src/rpc.rs
index 130b737f..0edb0657 100644
--- a/crates/desktop-sidecar/src/rpc.rs
+++ b/crates/desktop-sidecar/src/rpc.rs
@@ -146,11 +146,15 @@ pub(crate) fn handle(
"memo.create" => create_memo(database, &request.params).map(|memo| json!({ "memo": memo })),
"memo.update" => update_memo(database, &request.params).map(|memo| json!({ "memo": memo })),
"memo.updateGuarded" => {
- if !request.params.get("expectedLocalSnapshot").is_some_and(Value::is_object) {
+ if !request
+ .params
+ .get("expectedLocalSnapshot")
+ .is_some_and(Value::is_object)
+ {
return Err("A local snapshot is required for guarded note edits".to_owned());
}
update_memo(database, &request.params).map(|memo| json!({ "memo": memo }))
- },
+ }
"memo.delete" => delete_memo(database, &request.params),
"memo.restore" => {
restore_memo(database, &request.params).map(|memo| json!({ "memo": memo }))