Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

fix: app secret only for the window page and the current server

ogsamratd0deb676f1e5
diff --git a/apps/desktop/electron/app-request-secret.mjs b/apps/desktop/electron/app-request-secret.mjs
index 99790b63..2be6c18b 100644
--- a/apps/desktop/electron/app-request-secret.mjs
+++ b/apps/desktop/electron/app-request-secret.mjs
@@ -22,10 +22,12 @@ function originOf(url) {
}
}
-/** The local server's address (each start may bind another port). */
+/** The local server's address: only the current one (each start may bind another port; an old port may be anyone's now). */
export function rememberLocalServer(baseUrl) {
const origin = originOf(baseUrl);
- if (origin && /^https?:\/\/(?:127\.0\.0\.1|localhost|\[::1\])(?::\d+)?$/.test(origin)) origins.add(origin);
+ if (!origin || !/^https?:\/\/(?:127\.0\.0\.1|localhost|\[::1\])(?::\d+)?$/.test(origin)) return;
+ origins.clear();
+ origins.add(origin);
}
/** Headers for Electron main's own request to `url`: the secret only for the local server. */
@@ -35,11 +37,12 @@ export function appSecretHeaders(url) {
}
/**
- * Adds the secret to the app window's requests to the local server (and only those: another page,
- * another webContents or another address never gets it), and drops any copy a page set itself.
- * `isAppContents(webContentsId)` names the app's own window(s).
+ * Adds the secret to the app window's own requests to the local server (and only those: a frame inside
+ * the window, such as an HTML preview or an MCP app, another webContents or another address never gets
+ * it), and drops any copy a page set itself. `isAppFrame(details)` says whether the request comes from
+ * the app window's main frame.
*/
-export function installAppRequestSecret(targetSession, isAppContents) {
+export function installAppRequestSecret(targetSession, isAppFrame) {
targetSession.webRequest.onBeforeSendHeaders(
{ urls: ["http://127.0.0.1/*", "http://localhost/*", "http://[::1]/*"] },
(details, callback) => {
@@ -49,7 +52,7 @@ export function installAppRequestSecret(targetSession, isAppContents) {
}
const origin = originOf(details.url);
let fromApp = false;
- try { fromApp = typeof details.webContentsId === "number" && isAppContents(details.webContentsId); } catch { fromApp = false; }
+ try { fromApp = typeof details.webContentsId === "number" && isAppFrame(details) === true; } catch { fromApp = false; }
if (fromApp && origin && origins.has(origin)) requestHeaders[APP_SECRET_HEADER] = SECRET;
callback({ requestHeaders });
},
diff --git a/apps/desktop/electron/app-request-secret.test.mjs b/apps/desktop/electron/app-request-secret.test.mjs
index 0cab544c..6458820c 100644
--- a/apps/desktop/electron/app-request-secret.test.mjs
+++ b/apps/desktop/electron/app-request-secret.test.mjs
@@ -25,11 +25,17 @@ test("the app window's requests to the local server get it; other pages, ports a
rememberLocalServer("http://127.0.0.1:43123");
let listener = null;
const session = { webRequest: { onBeforeSendHeaders: (filter, fn) => { assert.ok(filter.urls.includes("http://127.0.0.1/*")); listener = fn; } } };
- installAppRequestSecret(session, (id) => id === 7);
+ installAppRequestSecret(session, (details) => details.webContentsId === 7 && details.frame?.routingId === 1);
const run = (details) => { let out = null; listener(details, (result) => { out = result.requestHeaders; }); return out; };
- assert.equal(run({ webContentsId: 7, url: "http://127.0.0.1:43123/runtime-config/approvals", requestHeaders: { Authorization: "Bearer x" } })[APP_SECRET_HEADER], SECRET);
+ assert.equal(run({ webContentsId: 7, frame: { routingId: 1 }, url: "http://127.0.0.1:43123/runtime-config/approvals", requestHeaders: { Authorization: "Bearer x" } })[APP_SECRET_HEADER], SECRET);
+ // a frame inside the app window (an HTML preview, an MCP app)
+ assert.equal(run({ webContentsId: 7, frame: { routingId: 5 }, url: "http://127.0.0.1:43123/runtime-config/approvals", requestHeaders: {} })[APP_SECRET_HEADER], undefined);
assert.equal(run({ webContentsId: 8, url: "http://127.0.0.1:43123/runtime-config/approvals", requestHeaders: {} })[APP_SECRET_HEADER], undefined);
- assert.equal(run({ webContentsId: 7, url: "http://127.0.0.1:5173/", requestHeaders: {} })[APP_SECRET_HEADER], undefined);
+ assert.equal(run({ webContentsId: 7, frame: { routingId: 1 }, url: "http://127.0.0.1:5173/", requestHeaders: {} })[APP_SECRET_HEADER], undefined);
+ // only the current server address: after a restart on another port the old one gets nothing
+ rememberLocalServer("http://127.0.0.1:43999");
+ assert.equal(run({ webContentsId: 7, frame: { routingId: 1 }, url: "http://127.0.0.1:43123/tokens", requestHeaders: {} })[APP_SECRET_HEADER], undefined);
+ assert.equal(run({ webContentsId: 7, frame: { routingId: 1 }, url: "http://127.0.0.1:43999/tokens", requestHeaders: {} })[APP_SECRET_HEADER], SECRET);
assert.equal(run({ webContentsId: undefined, url: "http://127.0.0.1:43123/tokens", requestHeaders: {} })[APP_SECRET_HEADER], undefined);
const forged = run({ webContentsId: 9, url: "http://127.0.0.1:43123/tokens", requestHeaders: { "X-OmniRush-App-Secret": "forged" } });
assert.ok(!Object.keys(forged).some((name) => name.toLowerCase() === APP_SECRET_HEADER));
diff --git a/apps/desktop/electron/main.mjs b/apps/desktop/electron/main.mjs
index 055af081..b92ed4b0 100644
--- a/apps/desktop/electron/main.mjs
+++ b/apps/desktop/electron/main.mjs
@@ -3456,7 +3456,13 @@ or use: pnpm dev:worktree`);
const systemCaCertificates = await runtimeManager.systemCaCertificates();
session.defaultSession.setCertificateVerifyProc(createSystemCaCertificateVerifyProc(systemCaCertificates));
// Settings writes to the local server need this app's window (app-request-secret.mjs).
- installAppRequestSecret(session.defaultSession, (id) => Boolean(mainWindow && !mainWindow.isDestroyed() && mainWindow.webContents.id === id));
+ installAppRequestSecret(session.defaultSession, (details) => {
+ if (!mainWindow || mainWindow.isDestroyed()) return false;
+ const contents = mainWindow.webContents;
+ const frame = details.frame, main = contents.mainFrame;
+ // The window's own page only: a frame inside it (an HTML preview, an MCP app) is not the app.
+ return details.webContentsId === contents.id && Boolean(frame && main) && frame.processId === main.processId && frame.routingId === main.routingId;
+ });
installMediaPermissionHandlers(session, () => mainWindow);
await runPendingNukeCleanup({
env: process.env,
diff --git a/apps/server/src/app-request-gate.test.ts b/apps/server/src/app-request-gate.test.ts
index 57373e27..7f4cafa3 100644
--- a/apps/server/src/app-request-gate.test.ts
+++ b/apps/server/src/app-request-gate.test.ts
@@ -43,6 +43,14 @@ describe("settings writes need the app", () => {
["PATCH", "/opencode/config"],
["PUT", "/opencode/auth/openai"],
["POST", "/opencode/mcp"],
+ ["POST", "/opencode/session/ses%2Fx/permissions/per_1"],
+ ["POST", "/workspace/ws_1/opencode/session/ses%2Fx/permissions/per_1"],
+ ["POST", "/workspace/ws_1/opencode2/api/session/ses_1/form/f_1/reply"],
+ ["POST", "/workspace/ws_1/opencode2/api/session/ses_1/form/f_1/cancel"],
+ ["PATCH", "/workspace/ws_1/opencode2/api/config"],
+ ["POST", "/workspace/ws_1/opencode2/api/mcp"],
+ ["POST", "/workspace/ws_1/opencode2/api/provider"],
+ ["PUT", "/workspace/ws_1/opencode2/api/model/default"],
// a mounted base URL and path tricks
["PUT", "/w/ws_1/runtime-config/approvals"],
["PUT", "/x/../runtime-config/approvals"],
diff --git a/apps/server/src/app-request-gate.ts b/apps/server/src/app-request-gate.ts
index 0b91442e..b9842d16 100644
--- a/apps/server/src/app-request-gate.ts
+++ b/apps/server/src/app-request-gate.ts
@@ -59,12 +59,14 @@ const SECRET_READS: RegExp[] = [
// Engine routes behind the proxy (path after the /opencode or /opencode2 segment), any method but
// GET/HEAD/OPTIONS: answers to approval questions, and the engine's own config, keys and MCP list.
const ENGINE_WRITES: RegExp[] = [
- /(?:^|\/)(?:permission|question)\/[^/]+\/(?:reply|reject)$/,
- /(?:^|\/)session\/[^/]+\/permissions\/[^/]+$/,
- /^\/(?:global\/)?config(?:\/|$)/,
- /^\/auth(?:\/|$)/,
- /^\/mcp(?:\/|$)/,
- /^\/(?:api\/)?(?:permission|question)(?:\/|$)/,
+ // any answer to a question: permission/permissions, question, or the preview engine's form replies,
+ // wherever the segment sits (a session id with an encoded slash must not move it out of place)
+ /(?:^|\/)(?:permission|permissions|question|form)(?:\/|$)/,
+ /^\/(?:api\/)?(?:global\/)?config(?:\/|$)/,
+ /^\/(?:api\/)?auth(?:\/|$)/,
+ /^\/(?:api\/)?mcp(?:\/|$)/,
+ /^\/(?:api\/)?provider(?:\/|$)/,
+ /^\/(?:api\/)?model\/default(?:\/|$)/,
];
const READ_METHODS = new Set(["GET", "HEAD", "OPTIONS"]);