pi-webapp 0.1.58-alpha.4 → 0.1.58-alpha.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bin/open-web-page.mjs +4 -1
- package/desktop/local-file-grant.mjs +99 -0
- package/dist/extension.js +396 -89
- package/dist/hosted-recovery.js +2 -2
- package/dist/hosted-worker.js +2 -2
- package/docs/desktop-testing.md +45 -3
- package/docs/desktop.md +31 -10
- package/package.json +3 -2
- package/web/dist/.vite/manifest.json +23 -23
- package/web/dist/assets/{MarkdownText-n7a5RNfV.js → MarkdownText-0TV5tM77.js} +4 -4
- package/web/dist/assets/WorkspacePanel.css +1 -1
- package/web/dist/assets/{WorkspacePanel.module-Dwe3nHeU.js → WorkspacePanel.module-CZiYvGfi.js} +2 -2
- package/web/dist/assets/app-CAQ6FgWC.js +53 -0
- package/web/dist/assets/app.css +1 -1
- package/web/dist/assets/app.js +4 -4
- package/web/dist/assets/{graphql-DByunozr.js → graphql-DR50OQaK.js} +2 -2
- package/web/dist/assets/{http-NwUdv8k4.js → http-B5JmsRyu.js} +2 -2
- package/web/dist/assets/{php-C6oEOKuG.js → php-BvVkDKIG.js} +2 -2
- package/web/dist/assets/{plugin-C3zB3qOG.js → plugin-CxocpsDR.js} +3 -3
- package/web/dist/assets/{plugin-DeW_DkTT.js → plugin-DYc245Ek.js} +3 -3
- package/web/dist/assets/{plugin-BmDUJyrG.js → plugin-Dg_YXYf8.js} +3 -3
- package/web/dist/assets/{plugin-CXVq2CZx.js → plugin-Do6Q-ck_.js} +2 -2
- package/web/dist/assets/{rst-DvMdVuu1.js → rst-ywld-qj9.js} +2 -2
- package/web/dist/assets/{ruby-CZD-cp5q.js → ruby-BrTvqk_a.js} +2 -2
- package/web/dist/assets/{svelte-Csgo12Mn.js → svelte-Bn10BeXe.js} +2 -2
- package/web/dist/assets/{telemetry-client-BXj9P2wd.js → telemetry-client-wNPyfb0N.js} +3 -3
- package/web/dist/assets/{vue-BDDB9Wgd.js → vue-D0oX8HSi.js} +2 -2
- package/web/dist/assets/app-c9jZdtMM.js +0 -53
package/bin/open-web-page.mjs
CHANGED
|
@@ -16,6 +16,9 @@ export async function openWebPage(url, platform = process.platform, env = proces
|
|
|
16
16
|
if (platform === "linux" && !env.DISPLAY && !env.WAYLAND_DISPLAY) return false;
|
|
17
17
|
const command = platform === "darwin" ? "open" : platform === "linux" ? "xdg-open" : platform === "win32" ? "explorer.exe" : null;
|
|
18
18
|
if (command === null) return false;
|
|
19
|
-
|
|
19
|
+
// The file grant is optional; it must never prevent opening the web page.
|
|
20
|
+
const launcher = platform === "darwin" || platform === "win32"
|
|
21
|
+
? await import("../desktop/local-file-grant.mjs").catch(() => undefined) : undefined;
|
|
22
|
+
await run(command, [launcher ? await launcher.localFileLaunchUrl(url, platform, env) : url]);
|
|
20
23
|
return true;
|
|
21
24
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createHmac, randomBytes, timingSafeEqual } from "node:crypto";
|
|
2
|
+
import { constants, statSync } from "node:fs";
|
|
3
|
+
import { link, mkdir, open, rm, writeFile } from "node:fs/promises";
|
|
4
|
+
import { get } from "node:http";
|
|
5
|
+
import { homedir } from "node:os";
|
|
6
|
+
import { join, resolve } from "node:path";
|
|
7
|
+
|
|
8
|
+
const keyName = "native-files.key";
|
|
9
|
+
const tokenPattern = /^[A-Za-z0-9_-]{32}$/;
|
|
10
|
+
const grantPattern = /^[A-Za-z0-9_-]{43}$/;
|
|
11
|
+
|
|
12
|
+
/** A native launch must be local and attended; a loopback HTTP peer alone proves neither. */
|
|
13
|
+
export function localFilePlatform(platform = process.platform, env = process.env, consoleUser = () => statSync("/dev/console").uid, uid = process.getuid?.()) {
|
|
14
|
+
if (env.SSH_CONNECTION || env.SSH_CLIENT || env.SSH_TTY || env.PI_WEBAPP_LISTEN_HOST === "0.0.0.0") return;
|
|
15
|
+
if (platform === "win32" && /^(?:Console|RDP-Tcp#\d+)$/i.test(env.SESSIONNAME ?? "")) return platform;
|
|
16
|
+
if (platform === "darwin" && uid) {
|
|
17
|
+
try { if (uid === consoleUser()) return platform; } catch { /* No console session. */ }
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
async function readKey(path) {
|
|
22
|
+
const posix = process.platform !== "win32";
|
|
23
|
+
const file = await open(path, constants.O_RDONLY | (posix ? constants.O_NOFOLLOW | constants.O_NONBLOCK : 0));
|
|
24
|
+
try {
|
|
25
|
+
const info = await file.stat();
|
|
26
|
+
if (!info.isFile() || info.size !== 32 || (posix && ((info.mode & 0o077) !== 0 || info.uid !== process.getuid?.()))) return;
|
|
27
|
+
const key = Buffer.alloc(33);
|
|
28
|
+
if ((await file.read(key)).bytesRead === 32) return key.subarray(0, 32);
|
|
29
|
+
} finally { await file.close(); }
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function localFileKey(agentDir, create = false) {
|
|
33
|
+
const dir = join(agentDir, "pi-web");
|
|
34
|
+
const path = join(dir, keyName);
|
|
35
|
+
try { return await readKey(path); } catch (error) {
|
|
36
|
+
if (!create || error?.code !== "ENOENT") return;
|
|
37
|
+
}
|
|
38
|
+
// Publish a complete private key atomically so sibling CLI/shell processes
|
|
39
|
+
// and concurrent starts either see the same key or fail closed.
|
|
40
|
+
const temporary = `${path}.${randomBytes(12).toString("hex")}`;
|
|
41
|
+
try {
|
|
42
|
+
await mkdir(dir, { recursive: true, mode: 0o700 });
|
|
43
|
+
await writeFile(temporary, randomBytes(32), { flag: "wx", mode: 0o600 });
|
|
44
|
+
try { await link(temporary, path); } catch (error) { if (error?.code !== "EEXIST") throw error; }
|
|
45
|
+
return await readKey(path);
|
|
46
|
+
} catch { /* Opening workspace files is optional; the web session still works. */ }
|
|
47
|
+
finally { await rm(temporary, { force: true }).catch(() => {}); }
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function makeGrant(key, token) {
|
|
51
|
+
return createHmac("sha256", key).update(`pi-web/native-files/v1\0${token}`).digest("base64url");
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export function matchesLocalFileGrant(key, token, input) {
|
|
55
|
+
return !!key && typeof input === "string" && grantPattern.test(input)
|
|
56
|
+
&& timingSafeEqual(Buffer.from(input), Buffer.from(makeGrant(key, token)));
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function acceptsGrant(target, token, grant, platform) {
|
|
60
|
+
// A direct, bounded loopback request: never send these headers through a
|
|
61
|
+
// proxy, follow a redirect, or trust a generic success page from an old host.
|
|
62
|
+
return new Promise(resolve => {
|
|
63
|
+
const request = get(new URL("/api/local-file-actions", target), {
|
|
64
|
+
agent: false, signal: AbortSignal.timeout(1500),
|
|
65
|
+
headers: { Authorization: `Bearer ${token}`, "X-Pi-Local-Files": grant },
|
|
66
|
+
}, response => {
|
|
67
|
+
if (response.statusCode !== 200) { response.destroy(); resolve(false); return; }
|
|
68
|
+
let text = "";
|
|
69
|
+
response.setEncoding("utf8");
|
|
70
|
+
response.on("data", chunk => {
|
|
71
|
+
text += chunk;
|
|
72
|
+
if (text.length > 256) { response.destroy(); resolve(false); }
|
|
73
|
+
});
|
|
74
|
+
response.once("end", () => {
|
|
75
|
+
try { resolve(JSON.parse(text)?.platform === platform); } catch { resolve(false); }
|
|
76
|
+
});
|
|
77
|
+
response.once("error", () => resolve(false));
|
|
78
|
+
});
|
|
79
|
+
request.once("error", () => resolve(false));
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** Only the URL actually handed to a local GUI gets the grant; status and logs keep the normal URL. */
|
|
84
|
+
export async function localFileLaunchUrl(url, platform = process.platform, env = process.env, agentDir = resolve((env.PI_CODING_AGENT_DIR ?? join(homedir(), ".pi", "agent")).replace(/^~(?=$|[\\/])/, homedir()))) {
|
|
85
|
+
if (!localFilePlatform(platform, env)) return url;
|
|
86
|
+
try {
|
|
87
|
+
const target = new URL(url);
|
|
88
|
+
const token = target.hash.slice(1);
|
|
89
|
+
if (target.protocol !== "http:" || target.hostname !== "127.0.0.1" || !target.port || target.username || target.password || target.pathname !== "/" || target.search || !tokenPattern.test(token)) return url;
|
|
90
|
+
const key = await localFileKey(agentDir);
|
|
91
|
+
if (!key) return url;
|
|
92
|
+
// A new launcher can reconnect to an older runtime. Only use the extended
|
|
93
|
+
// fragment after the running bridge confirms that it accepts this grant.
|
|
94
|
+
const grant = makeGrant(key, token);
|
|
95
|
+
if (!await acceptsGrant(target, token, grant, platform)) return url;
|
|
96
|
+
target.hash = `${token}.${grant}`;
|
|
97
|
+
return target.href;
|
|
98
|
+
} catch { return url; }
|
|
99
|
+
}
|