Open links from the desktop app in the default browser

AI Assisted
This commit is contained in:
jparkerweb committed 2026-10-25 17:04:21 -07:00
1 parent 37f2735294
commit 7a9f56a452
5 files changed
+105 -2

No files matched your search

+33
View File
@@ -853,3 +853,36 @@ test("the built skills tree serves the same /app.css as the source console", asy
execFileSync(process.execPath, [path.join(built, "console.mjs"), "stop", "--session", json.sid], { env: ENV, cwd: ROOT });
}
});
// The desktop app's webview opens no new windows, so a link the page shows
// there goes through /open-link, which hands it to the default browser. Only
// http and https: anything else would be the page asking the machine to run
// whatever a link names.
test("/open-link takes an http or https link and refuses every other kind", async () => {
const res = cli(["open", "--workflow", "git", "--no-open"]);
const m = res.json.url.match(/^(http:\/\/127\.0\.0\.1:\d+)\/s\/([^/]+)\//);
const cookie = cookieFor(m[1], m[2]);
const open = (url) =>
fetch(`${m[1]}/open-link`, {
method: "POST",
headers: { "content-type": "application/json", cookie, origin: m[1], connection: "close" },
body: JSON.stringify({ url }),
});
try {
const ok = await open("https://github.com/jparkerweb/plan2code/compare/main...feature?expand=1");
assert.equal(ok.status, 200);
assert.deepEqual(await ok.json(), { ok: true, url: "https://github.com/jparkerweb/plan2code/compare/main...feature?expand=1" });
for (const bad of ["javascript:alert(1)", "file:///C:/Windows/System32/calc.exe", "ms-settings:", "not a link", "", 42]) {
const no = await open(bad);
assert.equal(no.status, 400, `${JSON.stringify(bad)} was not refused`);
}
const foreign = await fetch(`${m[1]}/open-link`, {
method: "POST",
headers: { "content-type": "application/json", cookie, origin: "https://evil.example", connection: "close" },
body: JSON.stringify({ url: "https://example.com/" }),
});
assert.notEqual(foreign.status, 200, "another site's page cannot open links through the console");
} finally {
cli(["stop", "--session", res.json.sid]);
}
});
+1 -1
View File
@@ -188,7 +188,7 @@ const ROUTE_ORDER = [
"/looks", "/looks", "/subagents", "/pr-host", "/models", "/models", "/cleanup", "/upload",
"/notepad", "/notepad/text", "/notepad/image", "/notepad/thumb", "/notepad/image/delete", "/notepad/tab",
"/notepad/tab/rename", "/notepad/tab/delete", "/notepad/tab/order", "/notepad/tab/last", "/notepad/images/",
"/attachments/", "/shots/", "/uploads/", "/submit", "/chat", "/chat/decision", "/workspace",
"/attachments/", "/shots/", "/uploads/", "/submit", "/open-link", "/chat", "/chat/decision", "/workspace",
"WORKSPACE_EDITS", "/workspace/scope", "/workspace/browse", "/cancel", "/ping", "/bye",
];
+41
View File
@@ -0,0 +1,41 @@
// Plan2Code Web Console - hands an http or https link to the person's default browser, for the page inside the desktop app; defines only.
//
// The app's webview opens no new windows, so a link the page shows there (Open
// pull request, a link in a doc, the feedback button) would do nothing. The
// page posts it to /open-link instead, and this opens it. Never through a
// shell: the link is the page's to name, so it travels as one argument to a
// program that only opens links.
import { spawn } from "node:child_process";
/** The link to open, normalised, or null when it is not a plain http or https link. */
export function linkToOpen(raw) {
if (typeof raw !== "string" || raw.length > 4096) return null;
let url;
try {
url = new URL(raw);
} catch {
return null;
}
return url.protocol === "http:" || url.protocol === "https:" ? url.href : null;
}
/** Opens `href` (from linkToOpen) in the default browser; false when it could not start one. */
export function openLink(href) {
// Tests, and a person who turned the console's own browser launches off.
if (process.env.NO_BROWSER || process.env.PLAN2CODE_NO_BROWSER) return true;
const [cmd, args] =
process.platform === "win32"
? ["rundll32.exe", ["url.dll,FileProtocolHandler", href]]
: process.platform === "darwin"
? ["open", [href]]
: [process.env.BROWSER || "xdg-open", [href]];
try {
const child = spawn(cmd, args, { detached: true, stdio: "ignore", windowsHide: true });
child.on("error", () => {});
child.unref();
return true;
} catch {
return false;
}
}
+16 -1
View File
@@ -1,4 +1,4 @@
// Plan2Code Web Console - the few messages this page sends the desktop app that frames it; defines only.
// Plan2Code Web Console - the few messages this page sends the desktop app that frames it, and which links it must open another way there; defines only.
//
// Each one is a named request with no free text in it, so the app decides what
// it does: a page can ask for "continue" to be typed, never for anything else.
@@ -8,6 +8,21 @@ export function framed() {
return Boolean(window.parent) && window.parent !== window;
}
/**
* True for a link that leaves the console for the web: http or https on
* another origin. Inside the app those open through /open-link, since the
* app's window opens no new ones; the console's own files stay put.
*/
export function linkLeavesPage(href, origin) {
let url;
try {
url = new URL(href, origin);
} catch {
return false;
}
return (url.protocol === "http:" || url.protocol === "https:") && url.origin !== origin;
}
/** Post `message` to the frame's parent, as `{ plan2code: kind, ...extra }`; nothing outside a frame. */
export function tellApp(kind, extra = {}) {
if (!framed()) return;
+14
View File
@@ -42,6 +42,7 @@ import { cleanSetting, offered } from "./public/subagents.js";
import { ACCENTS, DEFAULT_ACCENT, DEFAULT_CARD_WIDTH, cardWidth } from "./public/palette.js";
import { SERVABLE, TYPES, asset } from "./static.mjs";
import { handoffsDir, isHandoffId } from "./handoffs.mjs";
import { linkToOpen, openLink } from "./open-link.mjs";
import {
NotepadError,
addImage,
@@ -99,6 +100,7 @@ const ROUTES = [
{ match: (p, req) => p.startsWith("/shots/") || req.url.startsWith("/shots/"), run: shot },
{ match: (p, req) => p.startsWith("/uploads/") || req.url.startsWith("/uploads/"), run: uploads },
{ path: "/submit", method: "POST", origin: true, run: submit },
{ path: "/open-link", method: "POST", origin: true, run: openLinkRoute },
{ path: "/chat", method: "POST", origin: true, run: chat },
{ path: "/chat/decision", method: "POST", origin: true, run: chatDecision },
{ path: "/workspace", method: "GET", run: workspaceGet },
@@ -1025,6 +1027,18 @@ function recordSubmitted(state, actions) {
}
}
// A link the page inside the desktop app cannot open itself (open-link.mjs).
// http and https only; the page shows the link to paste when this says no.
function openLinkRoute(req, res) {
return readBody(req, (err, body) => {
if (err) return json(res, err.tooBig ? 413 : 400, { error: err.tooBig ? "too large" : "body must be JSON" });
const href = linkToOpen(body && body.url);
if (!href) return json(res, 400, { error: "only http and https links open" });
if (!openLink(href)) return json(res, 500, { error: "no browser could be started" });
return json(res, 200, { ok: true, url: href });
});
}
function submit(req, res, ctx) {
return readBody(req, (err, body) => {
if (err) return json(res, err.tooBig ? 413 : 400, { error: err.tooBig ? "too large" : "body must be JSON" });