Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(ipc): one document service, a protocol allowlist, and a loopback-only STT server

Etienne Lescot5936e5f66930
diff --git a/electron/ai-edition/document-service.test.ts b/electron/ai-edition/document-service.test.ts
index 15adbd515..1a2915751 100644
--- a/electron/ai-edition/document-service.test.ts
+++ b/electron/ai-edition/document-service.test.ts
@@ -247,6 +247,11 @@ describe("DocumentService", () => {
// one long/short race spliced the file ~5 times out of 10. A single attempt
// passed against the very bug it was written for. 12 rounds miss it once in
// ~4000 runs; the loop is the test, not decoration.
+ //
+ // Explicit timeout: 12 rounds of real create + fsync + rename take ~1.7s on
+ // an idle machine but blow vitest's 5s default when the full suite runs its
+ // files in parallel — observed failing there and passing alone. 30s is the
+ // "this actually hung" threshold, not the expected runtime.
it("leaves valid JSON when a long and a short save race", async () => {
for (let round = 0; round < 12; round++) {
const doc = await service.createProject(`Race ${round}`);
@@ -267,7 +272,7 @@ describe("DocumentService", () => {
expect([1, 400]).toContain(parsed.annotations.length);
expect(raw.trimEnd().endsWith("}")).toBe(true);
}
- });
+ }, 30_000);
it("applies racing saves in call order, last one winning", async () => {
const doc = await service.createProject("Order");
diff --git a/electron/ipc/handlers.ts b/electron/ipc/handlers.ts
index 3fdbc681e..54c195a6f 100644
--- a/electron/ipc/handlers.ts
+++ b/electron/ipc/handlers.ts
@@ -2663,9 +2663,21 @@ export function registerIpcHandlers(
return readCursorTelemetryFile(targetVideoPath);
});
+ // Protocol allowlist. `shell.openExternal` hands the string to the OS handler,
+ // so `file:`, `ms-msdt:`, a UNC path, or any registered custom scheme is a
+ // launch primitive — the renderer runs with webSecurity:false and now renders
+ // model-generated content, so "the renderer is trusted" is not a strong enough
+ // premise to skip this. http/https/mailto is everything the app actually opens.
+ const EXTERNAL_URL_PROTOCOLS = new Set(["http:", "https:", "mailto:"]);
+
ipcMain.handle("open-external-url", async (_, url: string) => {
try {
- await shell.openExternal(url);
+ const parsed = new URL(url);
+ if (!EXTERNAL_URL_PROTOCOLS.has(parsed.protocol)) {
+ console.warn(`Refused to open external URL with protocol ${parsed.protocol}`);
+ return { success: false, error: `Unsupported URL protocol: ${parsed.protocol}` };
+ }
+ await shell.openExternal(parsed.toString());
return { success: true };
} catch (error) {
console.error("Failed to open URL:", error);
@@ -3343,6 +3355,16 @@ export function registerIpcHandlers(
},
);
+ // One instance each, not one per call. DocumentService serialises saves of a
+ // project through a per-INSTANCE queue (see its writeProject comment — this
+ // race destroyed two real project files), so a second instance means a second
+ // queue racing for the same path: temp+rename still keeps the file valid, but
+ // a save can land under a concurrent one and be silently lost. LlmConfigStore
+ // is hoisted for a duller reason: its constructor does two sync readFileSync
+ // plus a safeStorage decrypt, and it was running on every chat message.
+ const aiEditionDocuments = new DocumentService(path.join(app.getPath("userData"), "projects"));
+ const aiEditionLlmConfig = new LlmConfigStore(app.getPath("userData"));
+
registerNativeBridgeHandlers({
getPlatform: () => process.platform,
getCurrentProjectPath: () => currentProjectPath,
@@ -3376,15 +3398,14 @@ export function registerIpcHandlers(
return null;
}
},
- getAiEditionDocuments: () =>
- new DocumentService(path.join(app.getPath("userData"), "projects")),
- getAiEditionLlmConfig: () => new LlmConfigStore(app.getPath("userData")),
+ getAiEditionDocuments: () => aiEditionDocuments,
+ getAiEditionLlmConfig: () => aiEditionLlmConfig,
runAiEditionChat: (projectId, sessionId, message, document, sink) =>
runChat(
projectId,
sessionId,
message,
- new LlmConfigStore(app.getPath("userData")),
+ aiEditionLlmConfig,
document,
sink,
),
@@ -3395,18 +3416,18 @@ export function registerIpcHandlers(
rewindToMessage: (projectId, sessionId, messageId) =>
rewindToMessage(projectId, sessionId, messageId),
compactNow: (projectId, sessionId) =>
- compactSessionNow(projectId, sessionId, new LlmConfigStore(app.getPath("userData"))),
+ compactSessionNow(projectId, sessionId, aiEditionLlmConfig),
runTimelineOperation: (projectId, sessionId, op, conversationMessage) =>
runTimelineOperation(
projectId,
sessionId,
op,
conversationMessage,
- new DocumentService(path.join(app.getPath("userData"), "projects")),
+ aiEditionDocuments,
),
getContextUsage: getSessionContextUsage,
runAiEditionChatDefault: (projectId, message, sink) =>
- runChatDefault(projectId, message, new LlmConfigStore(app.getPath("userData")), sink),
+ runChatDefault(projectId, message, aiEditionLlmConfig, sink),
getAiEditionChatHistoryDefault: (projectId) => getDefaultChatHistory(projectId),
clearAiEditionChatHistoryDefault: (projectId) => clearDefaultChatHistory(projectId),
listAiEditionChatSessions: (projectId) => listSessions(projectId),
diff --git a/electron/ipc/nativeBridge.ts b/electron/ipc/nativeBridge.ts
index d5911e1c9..c6e7a228f 100644
--- a/electron/ipc/nativeBridge.ts
+++ b/electron/ipc/nativeBridge.ts
@@ -164,6 +164,19 @@ function createErrorResponse(
} satisfies NativeBridgeResponse;
}
+/**
+ * Strips absolute filesystem paths out of an error message before it crosses to
+ * the renderer. Main-process errors quote the path they failed on (`ENOENT: no
+ * such file or directory, open 'C:\Users\alice\…'`), and that string is rendered
+ * verbatim in toasts and the chat error rail. Keeps the basename, which is the
+ * part a user can act on. The full message still goes to the main-process log.
+ */
+function redactPaths(message: string): string {
+ return message
+ .replace(/[A-Za-z]:\\(?:[^\\/:*?"<>|\r\n]+\\)*([^\\/:*?"<>|\r\n]*)/g, "…\\$1")
+ .replace(/\/(?:[^/\0\s'"]+\/)+([^/\0\s'"]*)/g, "…/$1");
+}
+
function isBridgeRequest(value: unknown): value is NativeBridgeRequest {
if (!value || typeof value !== "object") {
return false;
@@ -658,11 +671,15 @@ export function registerNativeBridgeHandlers(context: NativeBridgeContext) {
);
}
} catch (error) {
+ // Not retryable by default: most failures here are permanent (a missing
+ // file, a bad payload, an unavailable addon), and a blanket `true` tells
+ // the client to spin on them. The message keeps the reason but drops any
+ // absolute path — it crosses to the renderer and ends up in UI strings.
+ console.error(`native bridge ${domain}.${request.action} failed:`, error);
return createErrorResponse(
requestId,
"INTERNAL_ERROR",
- error instanceof Error ? error.message : "Unknown native bridge error.",
- true,
+ redactPaths(error instanceof Error ? error.message : "Unknown native bridge error."),
);
}
});
diff --git a/electron/native/whisper-stt/src/main.cpp b/electron/native/whisper-stt/src/main.cpp
index e4a70c95d..1cc067697 100644
--- a/electron/native/whisper-stt/src/main.cpp
+++ b/electron/native/whisper-stt/src/main.cpp
@@ -207,13 +207,14 @@ struct Word {
int main(int argc, char** argv) {
std::string model_path;
std::string host = "127.0.0.1";
+ bool host_from_flag = false;
int port = 0;
int threads = std::max(1u, std::thread::hardware_concurrency());
for (int i = 1; i < argc; ++i) {
const std::string a = argv[i];
if (a == "--model" && i + 1 < argc) model_path = argv[++i];
- else if (a == "--host" && i + 1 < argc) host = argv[++i];
+ else if (a == "--host" && i + 1 < argc) { host = argv[++i]; host_from_flag = true; }
else if (a == "--port" && i + 1 < argc) port = std::atoi(argv[++i]);
else if (a == "--threads" && i + 1 < argc) threads = std::atoi(argv[++i]);
}
@@ -226,7 +227,19 @@ int main(int argc, char** argv) {
if (const char* p = std::getenv("OPENSCREEN_WHISPER_PORT")) port = std::atoi(p);
}
if (const char* p = std::getenv("OPENSCREEN_WHISPER_THREADS")) threads = std::atoi(p);
- if (const char* p = std::getenv("OPENSCREEN_WHISPER_HOST")) host = p;
+ // Only a fallback, never an override: the app always passes --host 127.0.0.1,
+ // and an env var that can widen a shipped binary's bind address behind an
+ // explicit flag is a hole, not a knob. Anything but loopback is refused
+ // outright — this server has no authentication of any kind.
+ if (!host_from_flag) {
+ if (const char* p = std::getenv("OPENSCREEN_WHISPER_HOST")) host = p;
+ }
+ if (host != "127.0.0.1" && host != "::1" && host != "localhost") {
+ std::cerr << "FATAL: refusing to bind " << host
+ << " — whisper-stt-server is unauthenticated and loopback-only"
+ << std::endl;
+ return 2;
+ }
if (model_path.empty()) {
std::cerr << "FATAL: --model <path-to-ggml.bin> or "