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 asset and print preview paths

tianma-if09b7a5aa5920
diff --git a/apps/desktop/src/main/index.mjs b/apps/desktop/src/main/index.mjs
index 76aeb6cf..7b777205 100644
--- a/apps/desktop/src/main/index.mjs
+++ b/apps/desktop/src/main/index.mjs
@@ -17,6 +17,7 @@ import {
mountedInstallerCandidates,
} from "./installation-location.mjs";
import { userDataDirectoryFromArguments } from "./user-data-directory.mjs";
+import { isAllowedPrintPreviewUrl } from "./window-open-policy.mjs";
import electronUpdater from "electron-updater";
const { autoUpdater } = electronUpdater;
@@ -484,6 +485,18 @@ const createWindow = async () => {
}
mainWindow.webContents.setWindowOpenHandler(({ url }) => {
if (url.startsWith("edgeever-resource://") || url.startsWith("edgeever-staged://")) return { action: "allow" };
+ if (isAllowedPrintPreviewUrl(url, mainWindow.webContents.getURL())) {
+ return {
+ action: "allow",
+ overrideBrowserWindowOptions: {
+ webPreferences: {
+ contextIsolation: true,
+ nodeIntegration: false,
+ sandbox: true,
+ },
+ },
+ };
+ }
if (url.startsWith("https://") || url.startsWith("http://")) void shell.openExternal(url);
return { action: "deny" };
});
diff --git a/apps/desktop/src/main/window-open-policy.mjs b/apps/desktop/src/main/window-open-policy.mjs
new file mode 100644
index 00000000..0d9dded9
--- /dev/null
+++ b/apps/desktop/src/main/window-open-policy.mjs
@@ -0,0 +1,19 @@
+import { dirname, join } from "node:path";
+import { fileURLToPath, pathToFileURL } from "node:url";
+
+export const isAllowedPrintPreviewUrl = (targetUrl, appUrl) => {
+ try {
+ const target = new URL(targetUrl);
+ const currentApp = new URL(appUrl);
+
+ if (currentApp.protocol === "file:") {
+ const expected = pathToFileURL(join(dirname(fileURLToPath(currentApp)), "note-print.html"));
+ return target.protocol === "file:" && target.pathname === expected.pathname;
+ }
+
+ const expected = new URL("/note-print.html", currentApp.origin);
+ return target.origin === expected.origin && target.pathname === expected.pathname;
+ } catch {
+ return false;
+ }
+};
diff --git a/apps/desktop/src/main/window-open-policy.test.mjs b/apps/desktop/src/main/window-open-policy.test.mjs
new file mode 100644
index 00000000..8a3bb7c5
--- /dev/null
+++ b/apps/desktop/src/main/window-open-policy.test.mjs
@@ -0,0 +1,26 @@
+import { describe, expect, test } from "bun:test";
+import { isAllowedPrintPreviewUrl } from "./window-open-policy.mjs";
+
+describe("desktop window open policy", () => {
+ test("allows only the packaged print preview beside the app entry", () => {
+ const appUrl = "file:///Applications/EdgeEver.app/Contents/Resources/web/index.html";
+
+ expect(isAllowedPrintPreviewUrl(
+ "file:///Applications/EdgeEver.app/Contents/Resources/web/note-print.html?token=abc",
+ appUrl,
+ )).toBe(true);
+ expect(isAllowedPrintPreviewUrl("file:///note-print.html?token=abc", appUrl)).toBe(false);
+ expect(isAllowedPrintPreviewUrl(
+ "file:///Applications/EdgeEver.app/Contents/Resources/web/mobile-edit.html",
+ appUrl,
+ )).toBe(false);
+ });
+
+ test("allows the same-origin print preview in desktop development", () => {
+ const appUrl = "http://127.0.0.1:5173/";
+
+ expect(isAllowedPrintPreviewUrl("http://127.0.0.1:5173/note-print.html?token=abc", appUrl)).toBe(true);
+ expect(isAllowedPrintPreviewUrl("https://example.com/note-print.html", appUrl)).toBe(false);
+ expect(isAllowedPrintPreviewUrl("http://127.0.0.1:5173/settings", appUrl)).toBe(false);
+ });
+});
diff --git a/apps/web/src/components/WeChatIcon.tsx b/apps/web/src/components/WeChatIcon.tsx
index 19ae33b5..515d6d26 100644
--- a/apps/web/src/components/WeChatIcon.tsx
+++ b/apps/web/src/components/WeChatIcon.tsx
@@ -1,6 +1,6 @@
export const WeChatIcon = ({ className }: { className?: string }) => (
<img
- src="/icons/wechat-official-account.png"
+ src={`${import.meta.env.BASE_URL}icons/wechat-official-account.png`}
alt=""
aria-hidden="true"
className={className}
diff --git a/apps/web/src/lib/app-page-path.test.mjs b/apps/web/src/lib/app-page-path.test.mjs
new file mode 100644
index 00000000..17d7405e
--- /dev/null
+++ b/apps/web/src/lib/app-page-path.test.mjs
@@ -0,0 +1,25 @@
+import { describe, expect, test } from "bun:test";
+import {
+ getAppEntryPath,
+ getAppPagePath,
+ getMessageTargetOrigin,
+} from "./app-page-path.ts";
+
+describe("app page paths", () => {
+ test("uses root paths for web builds", () => {
+ expect(getAppPagePath("note-print.html", "/")).toBe("/note-print.html");
+ expect(getAppPagePath("/mobile-edit.html", "/")).toBe("/mobile-edit.html");
+ expect(getAppEntryPath("/")).toBe("/");
+ });
+
+ test("uses sibling files for packaged desktop builds", () => {
+ expect(getAppPagePath("note-print.html", "./")).toBe("./note-print.html");
+ expect(getAppPagePath("/mobile-edit.html", "./")).toBe("./mobile-edit.html");
+ expect(getAppEntryPath("./")).toBe("./index.html");
+ });
+
+ test("uses a wildcard target only for opaque file origins", () => {
+ expect(getMessageTargetOrigin("https://notes.example.com")).toBe("https://notes.example.com");
+ expect(getMessageTargetOrigin("null")).toBe("*");
+ });
+});
diff --git a/apps/web/src/lib/app-page-path.ts b/apps/web/src/lib/app-page-path.ts
new file mode 100644
index 00000000..49c9718d
--- /dev/null
+++ b/apps/web/src/lib/app-page-path.ts
@@ -0,0 +1,10 @@
+const normalizePageName = (pageName: string) => pageName.replace(/^\/+/, "");
+
+export const getAppPagePath = (pageName: string, baseUrl: string) =>
+ `${baseUrl}${normalizePageName(pageName)}`;
+
+export const getAppEntryPath = (baseUrl: string) =>
+ baseUrl.startsWith(".") ? `${baseUrl}index.html` : baseUrl;
+
+export const getMessageTargetOrigin = (origin: string) =>
+ origin === "null" ? "*" : origin;
diff --git a/apps/web/src/lib/mobile-editor-standalone.ts b/apps/web/src/lib/mobile-editor-standalone.ts
index ef4cd2e1..4111f742 100644
--- a/apps/web/src/lib/mobile-editor-standalone.ts
+++ b/apps/web/src/lib/mobile-editor-standalone.ts
@@ -74,7 +74,17 @@ export const parseMobileEditorTags = (value: string) =>
.map((tag) => tag.trim())
.filter(Boolean);
-export const safeMobileEditorReturnPath = (value: string | null) => (value?.startsWith("/") ? value : "/");
+export const safeMobileEditorReturnPath = (value: string | null) => {
+ const fallback = import.meta.env.BASE_URL.startsWith(".")
+ ? `${import.meta.env.BASE_URL}index.html`
+ : import.meta.env.BASE_URL;
+
+ if (value?.startsWith("/")) return value;
+ if (import.meta.env.BASE_URL.startsWith(".") && value?.startsWith(`${import.meta.env.BASE_URL}index.html`)) {
+ return value;
+ }
+ return fallback;
+};
export const requestMobileEditorJson = async <T,>(path: string, init?: RequestInit): Promise<T> => {
const headers = new Headers(init?.headers);
diff --git a/apps/web/src/lib/mobile-editor.ts b/apps/web/src/lib/mobile-editor.ts
index 5d25a127..0aead849 100644
--- a/apps/web/src/lib/mobile-editor.ts
+++ b/apps/web/src/lib/mobile-editor.ts
@@ -1,4 +1,6 @@
-export const STANDALONE_MOBILE_EDITOR_PATH = "/mobile-edit.html";
+import { getAppEntryPath, getAppPagePath } from "@/lib/app-page-path";
+
+export const STANDALONE_MOBILE_EDITOR_PATH = getAppPagePath("mobile-edit.html", import.meta.env.BASE_URL);
export const MOBILE_EDITOR_RETURN_PARAM = "mobileEditorReturn";
const STANDALONE_MOBILE_EDITOR_MEMO_KEY = "edgeever-standalone-mobile-editor-memo-id";
const STANDALONE_MOBILE_EDITOR_RETURN_KEY = "edgeever-standalone-mobile-editor-return-memo-id";
@@ -18,7 +20,7 @@ export const getStandaloneMobileEditorReturnPath = (memoId: string) => {
[MOBILE_EDITOR_RETURN_PARAM]: memoId,
});
- return `/?${params.toString()}`;
+ return `${getAppEntryPath(import.meta.env.BASE_URL)}?${params.toString()}`;
};
export const getStandaloneMobileEditorHref = (memoId: string, returnTo = getStandaloneMobileEditorReturnPath(memoId)) => {
diff --git a/apps/web/src/lib/note-print.ts b/apps/web/src/lib/note-print.ts
index 7fd95870..cc1201f9 100644
--- a/apps/web/src/lib/note-print.ts
+++ b/apps/web/src/lib/note-print.ts
@@ -1,10 +1,11 @@
import { DOMSerializer } from "@tiptap/pm/model";
import type { Editor } from "@tiptap/react";
import type { TiptapDoc } from "@edgeever/shared";
+import { getAppPagePath, getMessageTargetOrigin } from "@/lib/app-page-path";
export const NOTE_PRINT_MESSAGE = "edgeever:note-print";
export const NOTE_PRINT_READY_MESSAGE = "edgeever:note-print-ready";
-export const NOTE_PRINT_PATH = "/note-print.html";
+export const NOTE_PRINT_PATH = getAppPagePath("note-print.html", import.meta.env.BASE_URL);
export type NotePrintLabels = {
close: string;
@@ -62,7 +63,7 @@ export const openNotePrintPreview = (
type: NOTE_PRINT_MESSAGE,
token,
} satisfies NotePrintPayload,
- window.location.origin
+ getMessageTargetOrigin(window.location.origin)
);
};
diff --git a/apps/web/src/note-print.ts b/apps/web/src/note-print.ts
index e77e77d0..cc775ea8 100644
--- a/apps/web/src/note-print.ts
+++ b/apps/web/src/note-print.ts
@@ -6,6 +6,7 @@ import {
NOTE_PRINT_READY_MESSAGE,
type NotePrintPayload,
} from "@/lib/note-print";
+import { getMessageTargetOrigin } from "@/lib/app-page-path";
import printStyles from "@/styles/note-print.css?inline";
import "@fontsource-variable/noto-sans-sc/wght.css";
import "@/styles/note-print-screen.css";
@@ -164,7 +165,7 @@ window.addEventListener("message", (event: MessageEvent<NotePrintPayload>) => {
if (window.opener && token) {
window.opener.postMessage(
{ type: NOTE_PRINT_READY_MESSAGE, token },
- window.location.origin
+ getMessageTargetOrigin(window.location.origin)
);
} else {
setText(status, "EdgeEver");
diff --git a/package.json b/package.json
index 0ebf6904..10a982b0 100644
--- a/package.json
+++ b/package.json
@@ -36,7 +36,7 @@
"verify:web-performance": "bun scripts/verify-web-performance.mjs",
"test:release-planning": "bun test scripts/plan-native-release.test.mjs scripts/check-native-release-assets.test.mjs",
"test:desktop:sidecar": "bun scripts/test-desktop-sidecar.mjs",
- "test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun run test:desktop:sidecar",
+ "test:desktop": "bun test apps/desktop/src/main/account-scope.test.mjs apps/desktop/src/main/diagnostic-log.test.mjs apps/desktop/src/main/file-association.test.mjs apps/desktop/src/main/file-permissions.test.mjs apps/desktop/src/main/installation-location.test.mjs apps/desktop/src/main/resource-url.test.mjs apps/desktop/src/main/staged-resource.test.mjs apps/desktop/src/main/user-data-directory.test.mjs apps/desktop/src/main/window-open-policy.test.mjs apps/web/src/lib/api.test.mjs apps/web/src/lib/app-page-path.test.mjs apps/web/src/lib/desktop-resources.test.mjs apps/web/src/lib/desktop-sync.test.mjs apps/web/src/lib/local-mirror.test.mjs apps/web/src/lib/repository.test.mjs apps/web/src/lib/sync-queue.test.mjs && bun run test:desktop:sidecar",
"verify:desktop-package": "bun scripts/verify-desktop-package.mjs",
"build:cloudflare": "bun run typecheck && bun run build",
"build:site": "bun --cwd apps/site build",