wishforge-ide/apps/ide/policy/test/plugin-install-policy.test.cjs
eust-w 0ef99b1e6d style: add Prettier and EditorConfig, format the codebase
Adds prettier 3.9.8 (exact) with a shared config (single quotes,
120 columns, trailing commas), .prettierignore, .editorconfig and the
`format` / `format:check` scripts, then formats all TypeScript,
JavaScript, CSS, JSON and YAML sources.

Purely mechanical: no behaviour change. Typecheck passes, the vitest
suite and the 74 IDE node tests produce the same results as before
(socket-binding tests still cannot run inside this sandbox).
Previously the longest source line was ~3000 characters; reviewing the
following semantic changes now produces readable diffs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 13:32:43 +08:00

92 lines
3.9 KiB
JavaScript

'use strict';
require('reflect-metadata');
const test = require('node:test');
const assert = require('node:assert/strict');
const fs = require('node:fs');
const os = require('node:os');
const path = require('node:path');
const { treeDigest, validateManagedPlugins } = require('../managed-plugins.cjs');
const { ManagedPluginServer, ManagedPluginDeployer } = require('../plugin-install-policy.cjs');
function fixture(t) {
const root = fs.mkdtempSync(path.join(os.tmpdir(), 'wishforge-managed-plugins-'));
t.after(() => fs.rmSync(root, { recursive: true, force: true }));
const directory = path.join(root, 'test.approved');
fs.mkdirSync(path.join(directory, 'extension'), { recursive: true });
fs.writeFileSync(
path.join(directory, 'extension/package.json'),
JSON.stringify({ publisher: 'test', name: 'approved', version: '1.0.0' }),
);
fs.writeFileSync(path.join(directory, 'extension/main.js'), 'exports.activate = () => {};\n');
const catalog = {
plugins: [{ directory: 'test.approved', id: 'test.approved', version: '1.0.0', sha256: treeDigest(directory) }],
};
return { root, directory, options: { catalog, pythonLock: null } };
}
test('approved ID/version/content loads while unrelated hidden cache is ignored', t => {
const f = fixture(t);
fs.mkdirSync(path.join(f.root, '.downloads'));
assert.equal(validateManagedPlugins(f.root, f.options).length, 1);
});
test('same ID and version with changed implementation fails digest validation', t => {
const f = fixture(t);
fs.appendFileSync(path.join(f.directory, 'extension/main.js'), 'malicious();');
assert.throws(() => validateManagedPlugins(f.root, f.options), /DIGEST_REJECTED/);
});
test('changed version and additional plugin are refused', t => {
const f = fixture(t);
fs.writeFileSync(
path.join(f.directory, 'extension/package.json'),
JSON.stringify({ publisher: 'test', name: 'approved', version: '2.0.0' }),
);
assert.throws(() => validateManagedPlugins(f.root, f.options), /ID_VERSION_REJECTED/);
fs.mkdirSync(path.join(f.root, 'unapproved.plugin'));
assert.throws(() => validateManagedPlugins(f.root, f.options), /ALLOWLIST_MISMATCH/);
});
test('symlinks cannot substitute plugin code or plugin directories', t => {
const f = fixture(t);
fs.symlinkSync('/tmp', path.join(f.directory, 'escape'));
assert.throws(() => validateManagedPlugins(f.root, f.options), /SYMLINK_REJECTED/);
});
test('actual Theia PluginServer install routes reject registry, VSIX, update and forged system type before calling deployer', async () => {
const server = new ManagedPluginServer();
server.pluginDeployer = new Proxy(
{},
{
get() {
throw new Error('Unsafe deployer reached');
},
},
);
for (const id of [
'vscode:untrusted.extension',
'file:///tmp/plugin.vsix',
'https://example.invalid/plugin.vsix',
'vscode:ms-python.debugpy@2026.6.0',
]) {
await assert.rejects(server.install(id, 0, { ignoreDependencies: true }), /EXTENSIONS_MANAGED_BY_ADMIN/);
}
for (const method of ['doInstall', 'uninstall', 'enablePlugin', 'disablePlugin'])
await assert.rejects(server[method]('test.approved'), /EXTENSIONS_MANAGED_BY_ADMIN/);
});
test('direct deployer mutation paths and bulk/update paths are closed', async () => {
const deployer = new ManagedPluginDeployer();
for (const method of [
'deploy',
'deployMultipleEntries',
'deployPlugins',
'uninstall',
'undeploy',
'enablePlugin',
'disablePlugin',
]) {
await assert.rejects(deployer[method]({ id: 'local-dir:/tmp/evil', type: 0 }), /EXTENSIONS_MANAGED_BY_ADMIN/);
}
});
test('installed plugin reads still reach the normal Theia implementation', async () => {
const server = new ManagedPluginServer();
server.pluginDeployerHandler = { getDeployedPluginIds: () => ['test.approved@1.0.0'] };
assert.deepEqual(await server.getInstalledPlugins(), ['test.approved@1.0.0']);
});