doukui 0.6.0 → 0.9.0
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/README.en.md +85 -18
- package/README.md +32 -72
- package/bin/commands/help.js +2 -25
- package/bin/commands/plan-lint.js +105 -0
- package/bin/commands/verify.js +263 -0
- package/bin/doukui.js +3 -3
- package/package.json +2 -3
- package/plugins/core/hook-session-start/index.js +1 -1
- package/plugins/core/hook-skill-gate/index.js +64 -15
- package/plugins/core/hook-skill-gate/plugin.json +1 -1
- package/plugins/core/skill-agent-dispatcher/cleanup-reference.md +7 -28
- package/plugins/core/skill-agent-dispatcher/monitoring-reference.md +89 -0
- package/plugins/core/skill-agent-dispatcher/plugin.json +1 -1
- package/plugins/core/skill-agent-dispatcher/recovery-reference.md +132 -0
- package/plugins/core/skill-agent-dispatcher/roles-reference.md +9 -22
- package/plugins/core/skill-agent-dispatcher/skill.md +124 -796
- package/plugins/core/skill-agentic-loop/mirror-rules.md +95 -24
- package/plugins/core/skill-agentic-loop/plugin.json +2 -7
- package/plugins/core/skill-agentic-loop/skill.md +125 -354
- package/plugins/core/skill-batch-task-creator/plugin.json +1 -1
- package/plugins/core/skill-checklist/skill.md +15 -141
- package/plugins/core/skill-completion-report/plugin.json +1 -1
- package/plugins/core/skill-completion-report/skill.md +7 -1
- package/plugins/core/skill-doukui-plan/plugin.json +1 -1
- package/plugins/core/skill-doukui-plan/skill.md +24 -21
- package/plugins/core/skill-experience-logger/plugin.json +1 -1
- package/plugins/core/skill-human-checkpoint/skill.md +1 -0
- package/plugins/core/skill-split-work-package/plugin.json +1 -1
- package/plugins/core/skill-task-creator/plugin.json +1 -1
- package/plugins/core/skill-task-creator/skill.md +1 -139
- package/plugins/core/skill-workflow-orchestrator/plugin.json +1 -1
- package/plugins/plugin-registry.json +0 -48
- package/plugins/runtime/harness-build.js +5 -5
- package/plugins/runtime/plan-reader.js +54 -21
- package/plugins/runtime/resolve-plugin-path.js +2 -2
- package/templates/harness-config.yaml +0 -100
- package/bin/commands/loop-server.js +0 -357
- package/bin/commands/loop.js +0 -1508
- package/plugins/contracts/capabilities.js +0 -268
- package/plugins/contracts/checkresult-schema.json +0 -75
- package/plugins/core/provider-loop-engine/index.js +0 -1411
- package/plugins/core/provider-loop-engine/plugin.json +0 -15
- package/plugins/core/provider-memory-store/index.js +0 -510
- package/plugins/core/provider-memory-store/plugin.json +0 -9
- package/plugins/core/provider-state-store/index.js +0 -244
- package/plugins/core/provider-state-store/plugin.json +0 -9
- package/plugins/core/provider-watchdog/assets/daemon-config.template.json +0 -32
- package/plugins/core/provider-watchdog/assets/lib/watchdog-multi-window.js +0 -676
- package/plugins/core/provider-watchdog/assets/watchdog.js +0 -754
- package/plugins/core/provider-watchdog/index.js +0 -151
- package/plugins/core/provider-watchdog/plugin.json +0 -13
- package/plugins/core/skill-agent-dispatcher/daemon-helpers-reference.md +0 -249
- package/plugins/core/skill-watchdog-manager/plugin.json +0 -10
- package/plugins/core/skill-watchdog-manager/skill.md +0 -143
- package/plugins/core/validator-doc-sync/index.js +0 -248
- package/plugins/core/validator-doc-sync/plugin.json +0 -12
- package/plugins/core/validator-work-package/index.js +0 -300
- package/plugins/core/validator-work-package/plugin.json +0 -12
- package/plugins/runtime/audit-logger.js +0 -311
- package/plugins/runtime/build-cli.js +0 -89
- package/plugins/runtime/checkresult-validator.js +0 -131
- package/plugins/runtime/context-window.js +0 -81
- package/plugins/runtime/event-bus.js +0 -207
- package/plugins/runtime/executor-claude.js +0 -604
- package/plugins/runtime/executor-default.js +0 -448
- package/plugins/runtime/executor-local.js +0 -178
- package/plugins/runtime/executor-sdk.js +0 -415
- package/plugins/runtime/hook-dispatcher.js +0 -315
- package/plugins/runtime/loop-actuator.js +0 -292
- package/plugins/runtime/loop-coordinator.js +0 -347
- package/plugins/runtime/loop-defaults.js +0 -29
- package/plugins/runtime/loop-dispatch-batch.js +0 -351
- package/plugins/runtime/loop-executor.js +0 -120
- package/plugins/runtime/loop-report.js +0 -395
- package/plugins/runtime/loop-server-core.js +0 -750
- package/plugins/runtime/loop-snapshot.js +0 -581
- package/plugins/runtime/loop-trace.js +0 -242
- package/plugins/runtime/loop-worktree.js +0 -404
- package/plugins/runtime/multi-window-coordinator.js +0 -560
- package/plugins/runtime/objective-verifier.js +0 -136
- package/plugins/runtime/plan-lint.js +0 -106
- package/plugins/runtime/plugin-loader.js +0 -798
- package/plugins/runtime/plugin-trust.js +0 -182
- package/plugins/runtime/provider-failover.js +0 -114
- package/plugins/runtime/provider-resolver.js +0 -342
- package/plugins/runtime/reflection-evaluator.js +0 -545
- package/plugins/runtime/sandbox-context.js +0 -235
- package/plugins/runtime/sandbox-manager.js +0 -609
- package/plugins/runtime/sandbox-worker.js +0 -215
- package/plugins/runtime/self-rescue-ladder.js +0 -51
- package/plugins/runtime/text-tokenize.js +0 -25
- package/plugins/runtime/validator-pipeline.js +0 -475
|
@@ -1,609 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* SandboxManager - Worker Thread lifecycle management for plugin sandboxing
|
|
3
|
-
*
|
|
4
|
-
* Manages the creation, activation, execution, and termination of Worker Threads
|
|
5
|
-
* for external (npm/local) plugins. Core plugins are not sandboxed.
|
|
6
|
-
*
|
|
7
|
-
* Architecture:
|
|
8
|
-
* Main Thread Worker Thread
|
|
9
|
-
* ┌──────────────────────┐ ┌──────────────────────┐
|
|
10
|
-
* │ SandboxManager │ │ Sandbox Worker │
|
|
11
|
-
* │ ├─ createWorker() │ postMessage│ Plugin.onActivate() │
|
|
12
|
-
* │ ├─ activateInSandbox├───────────>│ Plugin.handle() etc │
|
|
13
|
-
* │ ├─ terminateWorker()│ │ SandboxContext proxy │
|
|
14
|
-
* │ └─ auditLogger │<───────────│ result / error │
|
|
15
|
-
* └──────────────────────┘ └──────────────────────┘
|
|
16
|
-
*
|
|
17
|
-
* RPC protocol:
|
|
18
|
-
* Main -> Worker: { type: 'rpc-request', id, method, args }
|
|
19
|
-
* Worker -> Main: { type: 'rpc-response', id, result?, error? }
|
|
20
|
-
* Worker -> Main: { type: 'activated' } | { type: 'activation-error', error }
|
|
21
|
-
* Main -> Worker: { type: 'terminate' }
|
|
22
|
-
*
|
|
23
|
-
* @module sandbox-manager
|
|
24
|
-
*/
|
|
25
|
-
|
|
26
|
-
// FILE-SIZE-MONITOR: 590 lines (as of 2026-05-31)
|
|
27
|
-
// SPLIT-THRESHOLD: 800 lines — if exceeded, consider splitting into:
|
|
28
|
-
// - sandbox-core.js (Worker Thread lifecycle management)
|
|
29
|
-
// - sandbox-rpc.js (RPC communication, message handling)
|
|
30
|
-
|
|
31
|
-
'use strict';
|
|
32
|
-
|
|
33
|
-
var path = require('path');
|
|
34
|
-
var workerThreads = require('worker_threads');
|
|
35
|
-
var AuditLogger = require('./audit-logger');
|
|
36
|
-
var capabilities = require('../contracts/capabilities');
|
|
37
|
-
var isCapabilityAllowed = capabilities.isCapabilityAllowed;
|
|
38
|
-
var shouldSandbox = capabilities.shouldSandbox;
|
|
39
|
-
var sandboxContext = require('./sandbox-context');
|
|
40
|
-
var callService = sandboxContext.callService;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* @typedef {object} WorkerEntry
|
|
44
|
-
* @property {Worker} worker - Worker Thread instance
|
|
45
|
-
* @property {string} plugin - plugin name
|
|
46
|
-
* @property {string} sourceType - 'npm' | 'local'
|
|
47
|
-
* @property {number} threadId - worker thread ID
|
|
48
|
-
* @property {boolean} active - whether the worker is currently active
|
|
49
|
-
*/
|
|
50
|
-
|
|
51
|
-
/**
|
|
52
|
-
* SandboxManager manages Worker Thread lifecycle for external plugins.
|
|
53
|
-
* @experimental 沙箱功能处于实验阶段,API 可能在未来版本中变更
|
|
54
|
-
*/
|
|
55
|
-
class SandboxManager {
|
|
56
|
-
/**
|
|
57
|
-
* @experimental
|
|
58
|
-
* @param {object} [options]
|
|
59
|
-
* @param {object} [options.auditLogger] - AuditLogger instance (created if not provided)
|
|
60
|
-
* @param {string} [options.logDir] - directory for audit logs (used if creating AuditLogger)
|
|
61
|
-
* @param {object} [options.logger] - Logger instance for internal logging
|
|
62
|
-
* @param {string} [options.sandboxScriptPath] - path to sandbox worker script (default: auto-detected)
|
|
63
|
-
*/
|
|
64
|
-
constructor(options) {
|
|
65
|
-
options = options || {};
|
|
66
|
-
this._logger = options.logger || null;
|
|
67
|
-
this._sandboxScriptPath = options.sandboxScriptPath || null;
|
|
68
|
-
|
|
69
|
-
// Initialize audit logger
|
|
70
|
-
if (options.auditLogger) {
|
|
71
|
-
this._auditLogger = options.auditLogger;
|
|
72
|
-
} else {
|
|
73
|
-
this._auditLogger = new AuditLogger({
|
|
74
|
-
logDir: options.logDir,
|
|
75
|
-
logger: this._logger,
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
/** @type {Map<string, WorkerEntry>} plugin name -> worker entry */
|
|
80
|
-
this._workers = new Map();
|
|
81
|
-
|
|
82
|
-
// Note: S3 dead-state cleanup — the previous `_pendingRpc` / `_rpcIdCounter`
|
|
83
|
-
// fields were declared but never read or written (RPC ids are managed inside
|
|
84
|
-
// SandboxContext in the worker, not on the main thread). Removed to avoid
|
|
85
|
-
// implying the main thread tracks pending RPCs (it does not).
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
// --- Public API ---
|
|
89
|
-
|
|
90
|
-
/**
|
|
91
|
-
* Check if a plugin should run in a sandbox.
|
|
92
|
-
*
|
|
93
|
-
* @public
|
|
94
|
-
* @param {string} sourceType - 'core' | 'npm' | 'local'
|
|
95
|
-
* @returns {boolean} true if the plugin should be sandboxed
|
|
96
|
-
*/
|
|
97
|
-
requiresSandbox(sourceType) {
|
|
98
|
-
return shouldSandbox(sourceType);
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
/**
|
|
102
|
-
* Create a sandboxed Worker Thread for a plugin and activate it.
|
|
103
|
-
*
|
|
104
|
-
* The plugin's onActivate() will be called inside the worker with a
|
|
105
|
-
* SandboxContext proxy. All context method calls are forwarded via RPC
|
|
106
|
-
* to the main thread, where capability checks are applied.
|
|
107
|
-
*
|
|
108
|
-
* @experimental
|
|
109
|
-
* @param {object} options
|
|
110
|
-
* @param {string} options.pluginName - plugin name
|
|
111
|
-
* @param {string} options.pluginPath - absolute path to the plugin directory
|
|
112
|
-
* @param {string} options.sourceType - 'npm' | 'local'
|
|
113
|
-
* @param {object} options.declaredCapabilities - capabilities from plugin.json
|
|
114
|
-
* @param {object} options.mainThreadServices - { eventBus, stateStore, logger, configManager, getProvider }
|
|
115
|
-
* @param {number} [options.timeout=30000] - activation timeout in ms
|
|
116
|
-
* @returns {Promise<void>}
|
|
117
|
-
*/
|
|
118
|
-
async createSandboxedWorker(options) {
|
|
119
|
-
var pluginName = options.pluginName;
|
|
120
|
-
var pluginPath = options.pluginPath;
|
|
121
|
-
var sourceType = options.sourceType;
|
|
122
|
-
var declaredCapabilities = options.declaredCapabilities || {};
|
|
123
|
-
var mainThreadServices = options.mainThreadServices || {};
|
|
124
|
-
var timeout = options.timeout || 30000;
|
|
125
|
-
|
|
126
|
-
// Check if already sandboxed
|
|
127
|
-
if (this._workers.has(pluginName)) {
|
|
128
|
-
throw new Error('Plugin "' + pluginName + '" already has an active sandbox');
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
// Validate pluginPath (basic path traversal protection)
|
|
132
|
-
var normalizedPath = path.normalize(pluginPath);
|
|
133
|
-
if (!path.isAbsolute(normalizedPath)) {
|
|
134
|
-
throw new Error('pluginPath must be absolute, got: ' + pluginPath);
|
|
135
|
-
}
|
|
136
|
-
var fs = require('fs');
|
|
137
|
-
if (!fs.existsSync(normalizedPath)) {
|
|
138
|
-
throw new Error('pluginPath does not exist: ' + normalizedPath);
|
|
139
|
-
}
|
|
140
|
-
var stat = fs.statSync(normalizedPath);
|
|
141
|
-
if (!stat.isDirectory()) {
|
|
142
|
-
throw new Error('pluginPath must be a directory, got: ' + normalizedPath);
|
|
143
|
-
}
|
|
144
|
-
pluginPath = normalizedPath;
|
|
145
|
-
|
|
146
|
-
// Determine sandbox worker script path
|
|
147
|
-
var sandboxScriptPath = this._getSandboxScriptPath();
|
|
148
|
-
|
|
149
|
-
this._log('info', 'Creating sandbox for plugin "' + pluginName + '" (source: ' + sourceType + ')');
|
|
150
|
-
|
|
151
|
-
// Create the Worker with plugin data
|
|
152
|
-
var workerData = {
|
|
153
|
-
pluginName: pluginName,
|
|
154
|
-
pluginPath: pluginPath,
|
|
155
|
-
sourceType: sourceType,
|
|
156
|
-
capabilities: declaredCapabilities,
|
|
157
|
-
};
|
|
158
|
-
|
|
159
|
-
var worker;
|
|
160
|
-
try {
|
|
161
|
-
worker = new workerThreads.Worker(sandboxScriptPath, {
|
|
162
|
-
workerData: workerData,
|
|
163
|
-
});
|
|
164
|
-
} catch (err) {
|
|
165
|
-
this._auditLogger.logSandboxEvent('sandbox.create', pluginName,
|
|
166
|
-
'failed: ' + err.message);
|
|
167
|
-
throw new Error('Failed to create sandbox worker for "' + pluginName + '": ' + err.message);
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
var threadId = worker.threadId;
|
|
171
|
-
|
|
172
|
-
// Store the worker entry
|
|
173
|
-
var entry = {
|
|
174
|
-
worker: worker,
|
|
175
|
-
plugin: pluginName,
|
|
176
|
-
sourceType: sourceType,
|
|
177
|
-
threadId: threadId,
|
|
178
|
-
active: false,
|
|
179
|
-
declaredCapabilities: declaredCapabilities,
|
|
180
|
-
mainThreadServices: mainThreadServices,
|
|
181
|
-
};
|
|
182
|
-
this._workers.set(pluginName, entry);
|
|
183
|
-
|
|
184
|
-
// Set up message handler for RPC and lifecycle events
|
|
185
|
-
var self = this;
|
|
186
|
-
worker.on('message', function (msg) {
|
|
187
|
-
self._handleWorkerMessage(pluginName, msg);
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
worker.on('error', function (err) {
|
|
191
|
-
self._log('error', 'Sandbox worker error for "' + pluginName + '": ' + err.message);
|
|
192
|
-
self._auditLogger.logSandboxEvent('sandbox.terminate', pluginName,
|
|
193
|
-
'error: ' + err.message);
|
|
194
|
-
self._workers.delete(pluginName);
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
worker.on('exit', function (code) {
|
|
198
|
-
if (code !== 0) {
|
|
199
|
-
self._log('warn', 'Sandbox worker for "' + pluginName + '" exited with code ' + code);
|
|
200
|
-
}
|
|
201
|
-
self._workers.delete(pluginName);
|
|
202
|
-
});
|
|
203
|
-
|
|
204
|
-
// Audit: sandbox created
|
|
205
|
-
this._auditLogger.logSandboxEvent('sandbox.create', pluginName,
|
|
206
|
-
'threadId=' + threadId + ', sourceType=' + sourceType);
|
|
207
|
-
|
|
208
|
-
// Wait for activation with timeout
|
|
209
|
-
return new Promise(function (resolve, reject) {
|
|
210
|
-
var timeoutId = setTimeout(function () {
|
|
211
|
-
self.terminateWorker(pluginName);
|
|
212
|
-
reject(new Error('Sandbox activation timeout for "' + pluginName + '" after ' + timeout + 'ms'));
|
|
213
|
-
}, timeout);
|
|
214
|
-
|
|
215
|
-
var activationHandler = function (msg) {
|
|
216
|
-
if (msg.type === 'activated') {
|
|
217
|
-
clearTimeout(timeoutId);
|
|
218
|
-
entry.active = true;
|
|
219
|
-
self._log('info', 'Sandbox activated for plugin "' + pluginName + '" (threadId=' + threadId + ')');
|
|
220
|
-
resolve();
|
|
221
|
-
} else if (msg.type === 'activation-error') {
|
|
222
|
-
clearTimeout(timeoutId);
|
|
223
|
-
self.terminateWorker(pluginName);
|
|
224
|
-
reject(new Error('Sandbox activation failed for "' + pluginName + '": ' + msg.error));
|
|
225
|
-
}
|
|
226
|
-
};
|
|
227
|
-
|
|
228
|
-
// Store the activation handler temporarily
|
|
229
|
-
entry._activationHandler = activationHandler;
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
/**
|
|
234
|
-
* Terminate a sandboxed worker.
|
|
235
|
-
*
|
|
236
|
-
* @experimental
|
|
237
|
-
* @param {string} pluginName - plugin name
|
|
238
|
-
* @param {string} [reason] - termination reason for audit
|
|
239
|
-
* @returns {Promise<void>}
|
|
240
|
-
*/
|
|
241
|
-
async terminateWorker(pluginName, reason) {
|
|
242
|
-
var entry = this._workers.get(pluginName);
|
|
243
|
-
if (!entry) {
|
|
244
|
-
return; // Already terminated or never created
|
|
245
|
-
}
|
|
246
|
-
|
|
247
|
-
this._log('info', 'Terminating sandbox for plugin "' + pluginName + '"' +
|
|
248
|
-
(reason ? ' (reason: ' + reason + ')' : ''));
|
|
249
|
-
|
|
250
|
-
try {
|
|
251
|
-
// Send terminate signal
|
|
252
|
-
entry.worker.postMessage({ type: 'terminate' });
|
|
253
|
-
|
|
254
|
-
// Give the worker a short grace period, then force terminate.
|
|
255
|
-
// The timeout is always cleared (resolve + clearTimeout) so we don't leak
|
|
256
|
-
// a dangling 2s timer when the worker exits quickly (S3 dead-state cleanup).
|
|
257
|
-
await new Promise(function (resolve) {
|
|
258
|
-
var graceTimer;
|
|
259
|
-
function done() {
|
|
260
|
-
if (graceTimer) clearTimeout(graceTimer);
|
|
261
|
-
resolve();
|
|
262
|
-
}
|
|
263
|
-
entry.worker.once('exit', done);
|
|
264
|
-
entry.worker.terminate().then(done, done);
|
|
265
|
-
graceTimer = setTimeout(done, 2000);
|
|
266
|
-
});
|
|
267
|
-
} catch (err) {
|
|
268
|
-
// Force terminate on error
|
|
269
|
-
try {
|
|
270
|
-
await entry.worker.terminate();
|
|
271
|
-
} catch (terminateErr) {
|
|
272
|
-
// Ignore
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
this._auditLogger.logSandboxEvent('sandbox.terminate', pluginName,
|
|
277
|
-
'reason=' + (reason || 'normal') + ', threadId=' + entry.threadId);
|
|
278
|
-
|
|
279
|
-
// Clean up activation handler reference to prevent memory leak
|
|
280
|
-
delete entry._activationHandler;
|
|
281
|
-
|
|
282
|
-
this._workers.delete(pluginName);
|
|
283
|
-
}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* Terminate all active sandbox workers.
|
|
287
|
-
*
|
|
288
|
-
* @experimental
|
|
289
|
-
* @returns {Promise<void>}
|
|
290
|
-
*/
|
|
291
|
-
async terminateAll() {
|
|
292
|
-
var names = Array.from(this._workers.keys());
|
|
293
|
-
var self = this;
|
|
294
|
-
|
|
295
|
-
for (var i = 0; i < names.length; i++) {
|
|
296
|
-
await self.terminateWorker(names[i], 'shutdown');
|
|
297
|
-
}
|
|
298
|
-
}
|
|
299
|
-
|
|
300
|
-
/**
|
|
301
|
-
* Check if a plugin has an active sandbox.
|
|
302
|
-
*
|
|
303
|
-
* @experimental
|
|
304
|
-
* @param {string} pluginName
|
|
305
|
-
* @returns {boolean}
|
|
306
|
-
*/
|
|
307
|
-
hasWorker(pluginName) {
|
|
308
|
-
return this._workers.has(pluginName);
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* Get info about a sandboxed plugin.
|
|
313
|
-
*
|
|
314
|
-
* @experimental
|
|
315
|
-
* @param {string} pluginName
|
|
316
|
-
* @returns {{ threadId: number, sourceType: string, active: boolean }|null}
|
|
317
|
-
*/
|
|
318
|
-
getWorkerInfo(pluginName) {
|
|
319
|
-
var entry = this._workers.get(pluginName);
|
|
320
|
-
if (!entry) return null;
|
|
321
|
-
return {
|
|
322
|
-
threadId: entry.threadId,
|
|
323
|
-
sourceType: entry.sourceType,
|
|
324
|
-
active: entry.active,
|
|
325
|
-
};
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
/**
|
|
329
|
-
* Get the AuditLogger instance.
|
|
330
|
-
* @experimental
|
|
331
|
-
* @returns {AuditLogger}
|
|
332
|
-
*/
|
|
333
|
-
getAuditLogger() {
|
|
334
|
-
return this._auditLogger;
|
|
335
|
-
}
|
|
336
|
-
|
|
337
|
-
/**
|
|
338
|
-
* Destroy the SandboxManager, terminating all workers and flushing audit logs.
|
|
339
|
-
* @experimental
|
|
340
|
-
*/
|
|
341
|
-
destroy() {
|
|
342
|
-
// Synchronous cleanup for process exit
|
|
343
|
-
var names = Array.from(this._workers.keys());
|
|
344
|
-
for (var i = 0; i < names.length; i++) {
|
|
345
|
-
var entry = this._workers.get(names[i]);
|
|
346
|
-
try {
|
|
347
|
-
entry.worker.terminate();
|
|
348
|
-
} catch (err) {
|
|
349
|
-
// Ignore
|
|
350
|
-
}
|
|
351
|
-
}
|
|
352
|
-
this._workers.clear();
|
|
353
|
-
|
|
354
|
-
if (this._auditLogger) {
|
|
355
|
-
this._auditLogger.destroy();
|
|
356
|
-
}
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
// --- Internal ---
|
|
360
|
-
|
|
361
|
-
/**
|
|
362
|
-
* Handle an incoming message from a worker thread.
|
|
363
|
-
*
|
|
364
|
-
* @param {string} pluginName
|
|
365
|
-
* @param {object} msg
|
|
366
|
-
* @private
|
|
367
|
-
*/
|
|
368
|
-
_handleWorkerMessage(pluginName, msg) {
|
|
369
|
-
var entry = this._workers.get(pluginName);
|
|
370
|
-
if (!entry) return;
|
|
371
|
-
|
|
372
|
-
// Activation response
|
|
373
|
-
if (msg.type === 'activated' || msg.type === 'activation-error') {
|
|
374
|
-
if (entry._activationHandler) {
|
|
375
|
-
entry._activationHandler(msg);
|
|
376
|
-
delete entry._activationHandler;
|
|
377
|
-
}
|
|
378
|
-
return;
|
|
379
|
-
}
|
|
380
|
-
|
|
381
|
-
// RPC request from worker
|
|
382
|
-
if (msg.type === 'rpc-request') {
|
|
383
|
-
this._handleRpcRequest(pluginName, msg);
|
|
384
|
-
return;
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
|
|
388
|
-
/**
|
|
389
|
-
* Handle an RPC request from a sandboxed worker.
|
|
390
|
-
* Applies capability checks before forwarding to main thread services.
|
|
391
|
-
*
|
|
392
|
-
* @param {string} pluginName
|
|
393
|
-
* @param {object} msg - { id, method, args }
|
|
394
|
-
* @private
|
|
395
|
-
*/
|
|
396
|
-
_handleRpcRequest(pluginName, msg) {
|
|
397
|
-
var entry = this._workers.get(pluginName);
|
|
398
|
-
if (!entry) return;
|
|
399
|
-
|
|
400
|
-
var id = msg.id;
|
|
401
|
-
var method = msg.method;
|
|
402
|
-
var args = msg.args || [];
|
|
403
|
-
var worker = entry.worker;
|
|
404
|
-
var sourceType = entry.sourceType;
|
|
405
|
-
var declaredCapabilities = entry.declaredCapabilities;
|
|
406
|
-
var services = entry.mainThreadServices;
|
|
407
|
-
|
|
408
|
-
// Map RPC method names to required capabilities
|
|
409
|
-
var requiredCapability = this._methodToCapability(method);
|
|
410
|
-
|
|
411
|
-
if (requiredCapability) {
|
|
412
|
-
var check = isCapabilityAllowed(sourceType, requiredCapability, declaredCapabilities);
|
|
413
|
-
|
|
414
|
-
if (!check.allowed) {
|
|
415
|
-
// Deny the request
|
|
416
|
-
worker.postMessage({
|
|
417
|
-
type: 'rpc-response',
|
|
418
|
-
id: id,
|
|
419
|
-
error: 'SecurityError: ' + check.reason,
|
|
420
|
-
});
|
|
421
|
-
|
|
422
|
-
this._auditLogger.logCapabilityCheck(
|
|
423
|
-
pluginName,
|
|
424
|
-
requiredCapability,
|
|
425
|
-
'deny',
|
|
426
|
-
check.reason + ' (method: ' + method + ')',
|
|
427
|
-
sourceType
|
|
428
|
-
);
|
|
429
|
-
|
|
430
|
-
// Also log a capability violation event
|
|
431
|
-
this._auditLogger.log('capability.violation', pluginName, {
|
|
432
|
-
capability: requiredCapability,
|
|
433
|
-
decision: 'deny',
|
|
434
|
-
detail: 'RPC method "' + method + '" blocked: ' + check.reason,
|
|
435
|
-
sourceType: sourceType,
|
|
436
|
-
});
|
|
437
|
-
|
|
438
|
-
return;
|
|
439
|
-
}
|
|
440
|
-
}
|
|
441
|
-
|
|
442
|
-
// Execute the method on the main thread
|
|
443
|
-
try {
|
|
444
|
-
var result = this._callMainThreadService(services, method, args);
|
|
445
|
-
|
|
446
|
-
// Handle async results
|
|
447
|
-
if (result && typeof result.then === 'function') {
|
|
448
|
-
var self = this;
|
|
449
|
-
result.then(function (resolved) {
|
|
450
|
-
worker.postMessage({
|
|
451
|
-
type: 'rpc-response',
|
|
452
|
-
id: id,
|
|
453
|
-
result: self._sanitizeForTransfer(resolved),
|
|
454
|
-
});
|
|
455
|
-
}).catch(function (err) {
|
|
456
|
-
worker.postMessage({
|
|
457
|
-
type: 'rpc-response',
|
|
458
|
-
id: id,
|
|
459
|
-
error: err.message,
|
|
460
|
-
});
|
|
461
|
-
});
|
|
462
|
-
} else {
|
|
463
|
-
worker.postMessage({
|
|
464
|
-
type: 'rpc-response',
|
|
465
|
-
id: id,
|
|
466
|
-
result: this._sanitizeForTransfer(result),
|
|
467
|
-
});
|
|
468
|
-
}
|
|
469
|
-
|
|
470
|
-
// Audit the allowed call
|
|
471
|
-
this._auditLogger.logCapabilityCheck(
|
|
472
|
-
pluginName,
|
|
473
|
-
requiredCapability || method,
|
|
474
|
-
'allow',
|
|
475
|
-
'method: ' + method,
|
|
476
|
-
sourceType
|
|
477
|
-
);
|
|
478
|
-
} catch (err) {
|
|
479
|
-
worker.postMessage({
|
|
480
|
-
type: 'rpc-response',
|
|
481
|
-
id: id,
|
|
482
|
-
error: err.message,
|
|
483
|
-
});
|
|
484
|
-
|
|
485
|
-
this._auditLogger.logCapabilityCheck(
|
|
486
|
-
pluginName,
|
|
487
|
-
requiredCapability || method,
|
|
488
|
-
'error',
|
|
489
|
-
err.message,
|
|
490
|
-
sourceType
|
|
491
|
-
);
|
|
492
|
-
}
|
|
493
|
-
}
|
|
494
|
-
|
|
495
|
-
/**
|
|
496
|
-
* Map an RPC method name to the required capability.
|
|
497
|
-
* Returns null for methods that are always allowed (eventBus.emit, stateStore, logger).
|
|
498
|
-
*
|
|
499
|
-
* @param {string} method
|
|
500
|
-
* @returns {string|null}
|
|
501
|
-
* @private
|
|
502
|
-
*/
|
|
503
|
-
_methodToCapability(method) {
|
|
504
|
-
// Methods that require specific capabilities
|
|
505
|
-
if (method === 'getProvider') return 'plugin.access';
|
|
506
|
-
|
|
507
|
-
// Basic services are always allowed — no capability check needed
|
|
508
|
-
if (method.indexOf('eventBus.') === 0) return null;
|
|
509
|
-
if (method.indexOf('stateStore.') === 0) return null;
|
|
510
|
-
if (method.indexOf('logger.') === 0) return null;
|
|
511
|
-
if (method.indexOf('config.') === 0) return null;
|
|
512
|
-
|
|
513
|
-
// Unknown method — deny
|
|
514
|
-
return method;
|
|
515
|
-
}
|
|
516
|
-
|
|
517
|
-
/**
|
|
518
|
-
* Call a method on main thread services based on the RPC method name.
|
|
519
|
-
* Delegates to the shared callService function from sandbox-context.
|
|
520
|
-
*
|
|
521
|
-
* @param {object} services - main thread services
|
|
522
|
-
* @param {string} method - RPC method name
|
|
523
|
-
* @param {Array} args - method arguments
|
|
524
|
-
* @returns {*}
|
|
525
|
-
* @private
|
|
526
|
-
*/
|
|
527
|
-
_callMainThreadService(services, method, args) {
|
|
528
|
-
return callService(services, method, args);
|
|
529
|
-
}
|
|
530
|
-
|
|
531
|
-
/**
|
|
532
|
-
* Sanitize a value for transfer across the Worker Thread boundary.
|
|
533
|
-
* Functions and non-serializable values are replaced with placeholders.
|
|
534
|
-
*
|
|
535
|
-
* Handles the following value types:
|
|
536
|
-
* - undefined/null: returned as-is
|
|
537
|
-
* - function: replaced with the string '[Function]'
|
|
538
|
-
* - primitives (string, number, boolean): returned as-is
|
|
539
|
-
* - arrays: each element is recursively sanitized via `.map()`
|
|
540
|
-
* - plain objects: each own enumerable property is inspected;
|
|
541
|
-
* function values become '[Function]', object values recurse,
|
|
542
|
-
* primitives are kept verbatim
|
|
543
|
-
*
|
|
544
|
-
* @param {*} value - the value to sanitize for structured clone transfer
|
|
545
|
-
* @returns {*} a sanitized copy safe for `postMessage()` serialization
|
|
546
|
-
* @private
|
|
547
|
-
*/
|
|
548
|
-
_sanitizeForTransfer(value) {
|
|
549
|
-
if (value === undefined) return undefined;
|
|
550
|
-
if (value === null) return null;
|
|
551
|
-
|
|
552
|
-
if (typeof value === 'function') {
|
|
553
|
-
return '[Function]';
|
|
554
|
-
}
|
|
555
|
-
|
|
556
|
-
if (typeof value !== 'object') {
|
|
557
|
-
return value;
|
|
558
|
-
}
|
|
559
|
-
|
|
560
|
-
if (Array.isArray(value)) {
|
|
561
|
-
return value.map(this._sanitizeForTransfer.bind(this));
|
|
562
|
-
}
|
|
563
|
-
|
|
564
|
-
// Plain object — recurse
|
|
565
|
-
var result = {};
|
|
566
|
-
var keys = Object.keys(value);
|
|
567
|
-
for (var i = 0; i < keys.length; i++) {
|
|
568
|
-
var k = keys[i];
|
|
569
|
-
if (typeof value[k] === 'function') {
|
|
570
|
-
result[k] = '[Function]';
|
|
571
|
-
} else if (typeof value[k] === 'object' && value[k] !== null) {
|
|
572
|
-
result[k] = this._sanitizeForTransfer(value[k]);
|
|
573
|
-
} else {
|
|
574
|
-
result[k] = value[k];
|
|
575
|
-
}
|
|
576
|
-
}
|
|
577
|
-
return result;
|
|
578
|
-
}
|
|
579
|
-
|
|
580
|
-
/**
|
|
581
|
-
* Get the path to the sandbox worker script.
|
|
582
|
-
* Defaults to sandbox-worker.js in the same directory as this module.
|
|
583
|
-
*
|
|
584
|
-
* @returns {string} absolute path to the sandbox worker script
|
|
585
|
-
* @private
|
|
586
|
-
*/
|
|
587
|
-
_getSandboxScriptPath() {
|
|
588
|
-
if (this._sandboxScriptPath) {
|
|
589
|
-
return this._sandboxScriptPath;
|
|
590
|
-
}
|
|
591
|
-
return path.join(__dirname, 'sandbox-worker.js');
|
|
592
|
-
}
|
|
593
|
-
|
|
594
|
-
/**
|
|
595
|
-
* Internal logging helper. Delegates to the injected logger when available,
|
|
596
|
-
* otherwise falls back to console.log with a prefixed tag.
|
|
597
|
-
*
|
|
598
|
-
* @param {'info'|'warn'|'error'} level - log severity level
|
|
599
|
-
* @param {string} message - human-readable log message
|
|
600
|
-
* @private
|
|
601
|
-
*/
|
|
602
|
-
_log(level, message) {
|
|
603
|
-
if (this._logger && typeof this._logger[level] === 'function') {
|
|
604
|
-
this._logger[level]('sandbox-manager', message);
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
}
|
|
608
|
-
|
|
609
|
-
module.exports = SandboxManager;
|