Skip to content

Build your personal knowledge base with Trilium Notes

fix(electron): desktop crashing due to new protocol

Elian Doran19e852a0b840
diff --git a/apps/desktop/src/main.ts b/apps/desktop/src/main.ts
index 8dfede535d..2e363d671d 100644
--- a/apps/desktop/src/main.ts
+++ b/apps/desktop/src/main.ts
@@ -48,8 +48,7 @@ async function main() {
standard: true,
secure: true,
supportFetchAPI: true,
- corsEnabled: true,
- stream: true
+ corsEnabled: true
}
}
]);
diff --git a/apps/server/src/routes/electron.ts b/apps/server/src/routes/electron.ts
index 6591c5cf6f..4601d930b8 100644
--- a/apps/server/src/routes/electron.ts
+++ b/apps/server/src/routes/electron.ts
@@ -11,13 +11,20 @@ import { createRequest, createResponse } from "node-mocks-http";
* `apps/server/src/services/window.ts` and `apps/desktop/src/main.ts`), so
* every request the page makes — page load, bootstrap, API calls, static
* assets — arrives here as a Web Fetch `Request`. We convert it into an
- * Express `req`/`res` pair via `node-mocks-http` and dispatch through
- * `app.handle`, giving us the real session, CSRF, body-parser and error
- * middleware without any FakeRequest workaround.
+ * Express `req`/`res` pair via `node-mocks-http` and dispatch through the
+ * app, giving us the real session, CSRF, body-parser and error middleware
+ * without any FakeRequest workaround.
*/
function init(app: Application) {
electron.app.whenReady().then(() => {
- electron.protocol.handle("trilium-app", (request) => dispatch(app, request));
+ electron.protocol.handle("trilium-app", async (request) => {
+ try {
+ return await dispatch(app, request);
+ } catch (err) {
+ console.error(`[trilium-app] dispatch failed for ${request.method} ${request.url}:`, err);
+ return new Response(String((err as Error)?.stack ?? err), { status: 500 });
+ }
+ });
});
}
@@ -35,12 +42,19 @@ async function dispatch(app: Application, request: Request): Promise<Response> {
method: request.method as any,
url: url.pathname + url.search,
headers,
- body: body as any
+ body: body as any,
+ // Give express-rate-limit and any other IP-based middleware
+ // something to key on; the bare mock would have `req.ip` undefined.
+ ...({
+ connection: { remoteAddress: "127.0.0.1" },
+ socket: { remoteAddress: "127.0.0.1" },
+ ip: "127.0.0.1"
+ } as object)
});
- // body-parser short-circuits when `_body` is already set, so pre-parsed
- // JSON / buffers above won't be re-read from an empty mock stream and
- // clobbered.
+ // body-parser short-circuits when `_body` is already set, so the
+ // pre-parsed JSON / buffer above won't be re-read from an empty mock
+ // stream and clobbered.
if (body !== undefined) {
(req as any)._body = true;
}
@@ -51,17 +65,27 @@ async function dispatch(app: Application, request: Request): Promise<Response> {
});
res.on("end", () => {
- const payload = res._getBuffer().length > 0 ? res._getBuffer() : res._getData();
- resolve(new Response(payload ?? null, {
- status: res.statusCode,
- headers: normalizeResponseHeaders(res.getHeaders())
- }));
+ const buf = typeof (res as any)._getBuffer === "function" ? (res as any)._getBuffer() : null;
+ const data = res._getData();
+ const rawPayload = buf && buf.length > 0 ? buf : data;
+ try {
+ resolve(new Response(toUint8Array(rawPayload) as BodyInit | null, {
+ status: res.statusCode,
+ headers: normalizeResponseHeaders(res.getHeaders())
+ }));
+ } catch (err) {
+ reject(err);
+ }
});
try {
- app(req as any, res as any, (err: unknown) => {
- if (err) reject(err);
- });
+ (app as unknown as (req: object, res: object, next: (err?: unknown) => void) => void)(
+ req,
+ res,
+ (err) => {
+ if (err) reject(err);
+ }
+ );
} catch (err) {
reject(err);
}
@@ -80,15 +104,37 @@ async function readBody(request: Request): Promise<unknown> {
}
// For form uploads and binary payloads, hand Express a Buffer; multer
- // / body-parser will treat it as the raw request body via the
- // content-type header.
+ // / body-parser will read it via the content-type header.
return Buffer.from(await request.arrayBuffer());
}
-function normalizeResponseHeaders(headers: Record<string, number | string | string[] | undefined>): HeadersInit {
+// Headers that either describe HTTP transport framing or assume an https
+// origin. Letting them through on the `trilium-app://` custom scheme
+// causes Chromium to abort the renderer with STATUS_BREAKPOINT — HSTS /
+// COOP / CORP / origin-agent-cluster all run as part of renderer process
+// setup and trip internal asserts when the scheme isn't http(s).
+// Content-Length / Transfer-Encoding from Express also don't match what
+// the renderer ends up reading, since we hand it a buffered body.
+const STRIPPED_HEADERS = new Set([
+ "content-length",
+ "transfer-encoding",
+ "connection",
+ "keep-alive",
+ "upgrade",
+ "te",
+ "trailer",
+ "strict-transport-security",
+ "cross-origin-opener-policy",
+ "cross-origin-resource-policy",
+ "cross-origin-embedder-policy",
+ "origin-agent-cluster"
+]);
+
+function normalizeResponseHeaders(headers: Record<string, number | string | string[] | undefined>): [string, string][] {
const out: [string, string][] = [];
for (const [name, value] of Object.entries(headers)) {
if (value === undefined) continue;
+ if (STRIPPED_HEADERS.has(name.toLowerCase())) continue;
if (Array.isArray(value)) {
for (const v of value) out.push([name, v]);
} else {
@@ -98,4 +144,15 @@ function normalizeResponseHeaders(headers: Record<string, number | string | stri
return out;
}
+// Copy whatever node-mocks-http handed us into a fresh Uint8Array the
+// Fetch `Response` fully owns. Passing a shared Buffer can leave Chromium
+// with a reference that's freed underneath it.
+function toUint8Array(payload: unknown): Uint8Array | null {
+ if (payload == null) return null;
+ if (payload instanceof Uint8Array) return new Uint8Array(payload);
+ if (typeof payload === "string") return new TextEncoder().encode(payload);
+ if (typeof payload === "object") return new TextEncoder().encode(JSON.stringify(payload));
+ return new TextEncoder().encode(String(payload));
+}
+
export default init;