Files
openidea/src/features/plugins/remoteAsk.tsx
T
rohitkumardandaandrohitdanda 8df59cf5a7 fix(remote): decide routing, trust and accepted events on the desktop (#2999)
In a remote window the desktop took several decisions about itself from the server: it routed commands by the server's `commands` reply (a command the server left out ran locally), authorized its own commands from the server's `project_current().trusted`, and showed or ran whatever events the server forwarded, including any workbench action named by a plugin `executeCommand` event.

- Routing: commands go by `rpc::client_commands()`, the list a server of this build serves, compiled into the desktop; a test builds the server's dispatcher and compares the two.
- Trust: commands that run on the desktop follow the desktop's own decision, stored in `remote-project-trust.json` by target and root (a dev container also follows its local folder). The trust prompt appears unless the desktop's and the server's decisions agree; the answer and Revoke Trust update both. New commands `remote_trust_status` / `remote_trust_set`.
- Events: server `event`s pass an exhaustive allow-list (MCP approvals, update notices and window-group events are dropped); plugin `executeCommand` may only name `plugin.<id>.<command>`; `openProject` is dropped.
- Paths: a project root that the desktop would read as a share or device path is refused at connect.
- A remote plugin's `openUrl` and `copyToClipboard` ask first, showing the full address or the text.

Co-authored-by: rohitdanda <10144914+rohitdanda@users.noreply.github.com>
2026-11-13 11:05:12 +08:00

59 lines
2.2 KiB
TypeScript

/**
* Plugin code of a remote window runs on its server. Opening a link or writing
* the clipboard on this machine waits for the user, who sees exactly what.
*/
import { tx } from "@/platform/i18n";
import { ask } from "@/workbench/messages";
import { usePlugins } from "./store";
import styles from "./remoteAsk.module.css";
const PREVIEW_CHARS = 500;
const PREVIEW_LINES = 12;
const pluginName = (pluginId: string) => usePlugins.getState().plugins.find((p) => p.id === pluginId)?.name ?? pluginId;
/** Whether the user lets a remote plugin open `url` with this machine's browser. */
export async function confirmRemoteOpen(pluginId: string, url: string): Promise<boolean> {
const name = pluginName(pluginId);
const r = await ask({
title: tx`Open a link from ${name}?`,
body: (
<>
<div>{tx("A plugin running on the remote host wants to open this address on this computer:")}</div>
<div className={styles.value}>{url}</div>
</>
),
buttons: [{ id: "open", label: tx("Open"), primary: true }],
});
return r.button === "open";
}
/** Whether the user lets a remote plugin put `text` on this machine's clipboard. */
export async function confirmRemoteCopy(pluginId: string, text: string): Promise<boolean> {
const name = pluginName(pluginId);
const preview = previewOf(text);
const count = text.length.toLocaleString();
const r = await ask({
title: tx`Copy text from ${name}?`,
body: (
<>
<div>
{tx("A plugin running on the remote host wants to copy this text to the clipboard of this computer:")}
</div>
<div className={styles.value}>{preview}</div>
{preview !== text && <div className={styles.note}>{tx`${count} characters in total`}</div>}
</>
),
buttons: [{ id: "copy", label: tx("Copy"), primary: true }],
});
return r.button === "copy";
}
/** The start of `text`, at most `PREVIEW_CHARS` characters on `PREVIEW_LINES` lines, marked when cut. */
function previewOf(text: string): string {
let preview = text.slice(0, PREVIEW_CHARS);
const lines = preview.split("\n");
if (lines.length > PREVIEW_LINES) preview = lines.slice(0, PREVIEW_LINES).join("\n");
return preview.length < text.length ? `${preview}…` : text;
}