mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-26 10:53:56 +00:00
Compare commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
04249a822c |
No files matched your search
@@ -305,13 +305,22 @@ function registerPluginBridge(ipcMain, options) {
|
||||
}
|
||||
return manager;
|
||||
};
|
||||
const handle = (channel, callback) => {
|
||||
const handle = (channel, callback, allowUnconfigured = false) => {
|
||||
ipcMain.handle(channel, async (event, payload) => {
|
||||
if (!isTrustedSender(event)) throw new Error("Untrusted plugin management sender");
|
||||
if (allowUnconfigured && !configured) return false;
|
||||
const activeManager = await resolveManager();
|
||||
return callback(activeManager, payload, event);
|
||||
});
|
||||
};
|
||||
// Ordinary renderer lifecycles also run with the plugin runtime disabled.
|
||||
// Only an unconfigured runtime is a no-op; initialization failures still reject.
|
||||
const handleLifecycle = (channel, callback) => handle(channel, callback, true);
|
||||
if (!configured) {
|
||||
// Enabled AI/Agent adapters own these channels; never register over them.
|
||||
handleLifecycle("netcatty:plugins:ai-host-ready", () => false);
|
||||
handleLifecycle("netcatty:plugins:agent-host-ready", () => false);
|
||||
}
|
||||
// These channels are called by ordinary host/terminal lifecycles even when
|
||||
// the experimental plugin runtime is disabled. Treat the disabled runtime as
|
||||
// an empty provider set instead of throwing (and making Electron print a
|
||||
@@ -709,7 +718,7 @@ function registerPluginBridge(ipcMain, options) {
|
||||
if (!options.catalogService) throw new Error("Plugin catalog unavailable");
|
||||
return options.catalogService.install(payload?.id, payload?.selectionToken, payload?.sourceId, payload?.acceptedSourceId);
|
||||
});
|
||||
handle(CHANNELS.interactionReady, async (_manager, payload, event) => {
|
||||
handleLifecycle(CHANNELS.interactionReady, async (_manager, payload, event) => {
|
||||
if (!options.interactionService) throw new Error("Host interactions are unavailable");
|
||||
return options.interactionService.setReady(event.sender, payload?.ready);
|
||||
});
|
||||
@@ -739,7 +748,7 @@ function registerPluginBridge(ipcMain, options) {
|
||||
if ("senderFrame" in event && (!event.senderFrame || event.senderFrame !== event.sender?.mainFrame)) throw new Error("Untrusted permission UI frame");
|
||||
return event.sender;
|
||||
};
|
||||
handle(CHANNELS.permissionReady, async (_manager, payload, event) => options.permissionDecisionHost?.setReady(permissionSender(event), payload?.ready));
|
||||
handleLifecycle(CHANNELS.permissionReady, async (_manager, payload, event) => options.permissionDecisionHost?.setReady(permissionSender(event), payload?.ready));
|
||||
handle(CHANNELS.permissionResponse, async (_manager, payload, event) => options.permissionDecisionHost?.respond(permissionSender(event), payload));
|
||||
handle(CHANNELS.setPermissionMode, async (_manager, payload) => {
|
||||
const pluginId = assertManagedPluginId(payload);
|
||||
@@ -800,7 +809,7 @@ function registerPluginBridge(ipcMain, options) {
|
||||
));
|
||||
handle(CHANNELS.restart, async (activeManager, payload) => activeManager.restart(payload?.pluginId));
|
||||
handle(CHANNELS.uninstall, async (activeManager, payload) => activeManager.uninstall(payload?.pluginId));
|
||||
handle(CHANNELS.terminalTarget, async (_manager, target, event) => {
|
||||
handleLifecycle(CHANNELS.terminalTarget, async (_manager, target, event) => {
|
||||
if (!options.interactionService) throw new Error("Host interactions unavailable");
|
||||
return options.interactionService.setTerminalTarget(event.sender, target);
|
||||
});
|
||||
|
||||
@@ -0,0 +1,143 @@
|
||||
"use strict";
|
||||
|
||||
const assert = require("node:assert/strict");
|
||||
const test = require("node:test");
|
||||
const { CHANNELS, createTrustedPluginBridgeSender, registerPluginBridge } = require("./pluginBridge.cjs");
|
||||
const { PluginAITrustedHostAdapter } = require("./ai/trustedHostAdapter.cjs");
|
||||
|
||||
const lifecycleCalls = [
|
||||
[CHANNELS.interactionReady, { ready: true }],
|
||||
[CHANNELS.permissionReady, { ready: true }],
|
||||
[CHANNELS.terminalTarget, { sessionId: "session-1" }],
|
||||
["netcatty:plugins:ai-host-ready", true],
|
||||
["netcatty:plugins:agent-host-ready", true],
|
||||
];
|
||||
|
||||
function fixture() {
|
||||
const handlers = new Map();
|
||||
const mainFrame = { url: "app://netcatty/index.html" };
|
||||
const sender = { mainFrame, isDestroyed: () => false };
|
||||
const event = { sender, senderFrame: mainFrame };
|
||||
const ipcMain = {
|
||||
handle(channel, handler) {
|
||||
assert.equal(handlers.has(channel), false, `duplicate handler: ${channel}`);
|
||||
handlers.set(channel, handler);
|
||||
},
|
||||
removeHandler(channel) { handlers.delete(channel); },
|
||||
on() {},
|
||||
};
|
||||
return { handlers, event, ipcMain, isTrustedSender: createTrustedPluginBridgeSender() };
|
||||
}
|
||||
|
||||
for (const [channel, payload] of lifecycleCalls) {
|
||||
test(`disabled runtime acknowledges trusted lifecycle without side effects: ${channel}`, async () => {
|
||||
for (const manager of [null, undefined]) {
|
||||
const f = fixture();
|
||||
const unexpected = () => assert.fail("disabled lifecycle invoked a service");
|
||||
registerPluginBridge(f.ipcMain, {
|
||||
manager, isTrustedSender: f.isTrustedSender,
|
||||
interactionService: { setReady: unexpected, setTerminalTarget: unexpected },
|
||||
permissionDecisionHost: { setReady: unexpected },
|
||||
});
|
||||
const handler = f.handlers.get(channel);
|
||||
assert.equal(typeof handler, "function");
|
||||
const inactive = typeof payload === "boolean" ? false
|
||||
: channel === CHANNELS.terminalTarget ? null : { ready: false };
|
||||
for (const value of [payload, inactive]) {
|
||||
assert.equal(await handler(f.event, value), false);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
test(`disabled lifecycle still rejects untrusted senders and embedded frames: ${channel}`, async () => {
|
||||
const f = fixture();
|
||||
registerPluginBridge(f.ipcMain, { manager: null, isTrustedSender: f.isTrustedSender });
|
||||
const handler = f.handlers.get(channel);
|
||||
assert.equal(typeof handler, "function");
|
||||
const untrusted = [
|
||||
{ senderFrame: { url: "netcatty-plugin://runtime/index.html" } },
|
||||
{ senderFrame: { url: "https://attacker.invalid/" } },
|
||||
{ sender: f.event.sender, senderFrame: { url: f.event.senderFrame.url } },
|
||||
{ sender: f.event.sender, senderFrame: null },
|
||||
];
|
||||
for (const event of untrusted) await assert.rejects(handler(event, payload), /Untrusted/);
|
||||
});
|
||||
}
|
||||
|
||||
test("enabled lifecycle forwards to the original services after initialization", async () => {
|
||||
const f = fixture(); const calls = []; let initialized = 0;
|
||||
const record = (kind) => (...args) => { calls.push([kind, ...args]); return kind; };
|
||||
registerPluginBridge(f.ipcMain, {
|
||||
manager: { initialize: async () => { initialized++; } }, isTrustedSender: f.isTrustedSender,
|
||||
interactionService: { setReady: record("interaction"), setTerminalTarget: record("terminal") },
|
||||
permissionDecisionHost: { setReady: record("permission") },
|
||||
});
|
||||
const kinds = ["interaction", "permission", "terminal"];
|
||||
for (const [index, [channel, payload]] of lifecycleCalls.slice(0, 3).entries()) {
|
||||
assert.equal(await f.handlers.get(channel)(f.event, payload), kinds[index]);
|
||||
}
|
||||
assert.equal(initialized, 3);
|
||||
assert.deepEqual(calls, [
|
||||
["interaction", f.event.sender, true], ["permission", f.event.sender, true],
|
||||
["terminal", f.event.sender, lifecycleCalls[2][1]],
|
||||
]);
|
||||
for (const [channel, payload] of lifecycleCalls.slice(0, 3)) {
|
||||
await assert.rejects(f.handlers.get(channel)({ senderFrame: { url: "https://attacker.invalid/" } }, payload), /Untrusted/);
|
||||
}
|
||||
assert.equal(initialized, 3);
|
||||
});
|
||||
|
||||
test("enabled lifecycle propagates initialization and service failures", async () => {
|
||||
for (const initializationFails of [true, false]) {
|
||||
const f = fixture(); let initialized = 0; let serviceCalls = 0;
|
||||
const failure = new Error("synthetic initialization or service failure");
|
||||
const failService = () => { serviceCalls++; throw failure; };
|
||||
registerPluginBridge(f.ipcMain, {
|
||||
manager: { initialize: async () => { initialized++; if (initializationFails) throw failure; } },
|
||||
isTrustedSender: f.isTrustedSender,
|
||||
interactionService: { setReady: failService, setTerminalTarget: failService },
|
||||
permissionDecisionHost: { setReady: failService },
|
||||
});
|
||||
for (const [channel, payload] of lifecycleCalls.slice(0, 3)) {
|
||||
await assert.rejects(f.handlers.get(channel)(f.event, payload), (error) => (
|
||||
initializationFails ? error.cause === failure : error === failure
|
||||
));
|
||||
}
|
||||
assert.equal(initialized, 3);
|
||||
assert.equal(serviceCalls, initializationFails ? 0 : 3);
|
||||
}
|
||||
});
|
||||
|
||||
test("enabled AI and Agent readiness remains owned by the original adapters", async (t) => {
|
||||
const f = fixture(); const adapters = [];
|
||||
for (const kind of ["ai", "agent"]) {
|
||||
const channels = {
|
||||
ready: `netcatty:plugins:${kind}-host-ready`,
|
||||
request: `netcatty:plugins:${kind}-host-request`,
|
||||
response: `netcatty:plugins:${kind}-host-response`,
|
||||
};
|
||||
const adapter = new PluginAITrustedHostAdapter({
|
||||
channels, getWindow: () => ({ webContents: f.event.sender }), cancelProviderStreams: () => {},
|
||||
validateRequest: () => true, validateResponse: () => true,
|
||||
});
|
||||
adapter.registerIpc(f.ipcMain);
|
||||
t.after(() => adapter.shutdown());
|
||||
adapters.push(adapter);
|
||||
}
|
||||
const originalHandlers = new Map(f.handlers);
|
||||
registerPluginBridge(f.ipcMain, {
|
||||
manager: { initialize: async () => { throw new Error("initialization failed"); } },
|
||||
isTrustedSender: f.isTrustedSender,
|
||||
});
|
||||
for (const adapter of adapters) {
|
||||
const handler = f.handlers.get(adapter.channels.ready);
|
||||
assert.equal(handler, originalHandlers.get(adapter.channels.ready));
|
||||
assert.equal(await handler(f.event, true), true);
|
||||
assert.equal(adapter.owner, f.event.sender);
|
||||
assert.throws(() => handler({ sender: {} }, true), /sender is not the main window/);
|
||||
assert.equal(await handler(f.event, false), true);
|
||||
assert.equal(adapter.owner, null);
|
||||
adapter.closed = true;
|
||||
assert.throws(() => handler(f.event, true), /host is closed/);
|
||||
}
|
||||
});
|
||||
Reference in new issue
Block a user