wishforge-ide/apps/ide/policy/managed-plugins.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

109 lines
4.9 KiB
JavaScript

'use strict';
const fs = require('node:fs');
const path = require('node:path');
const { createHash } = require('node:crypto');
const { execFileSync } = require('node:child_process');
const sha256 = value => createHash('sha256').update(value).digest('hex');
const managedError = () =>
new Error('EXTENSIONS_MANAGED_BY_ADMIN: Extensions are installed and updated only through the operator image build.');
function treeDigest(root) {
const files = [];
function visit(directory, prefix = '') {
for (const entry of fs.readdirSync(directory, { withFileTypes: true })) {
const relative = prefix ? `${prefix}/${entry.name}` : entry.name;
const name = path.join(directory, entry.name);
if (entry.isSymbolicLink()) throw new Error('EXTENSION_SYMLINK_REJECTED');
if (entry.isDirectory()) visit(name, relative);
else if (entry.isFile()) files.push([relative, sha256(fs.readFileSync(name))]);
else throw new Error('EXTENSION_SPECIAL_FILE_REJECTED');
}
}
visit(root);
files.sort((left, right) => (left[0] < right[0] ? -1 : left[0] > right[0] ? 1 : 0));
return sha256(files.map(([name, digest]) => `${name}\0${digest}\n`).join(''));
}
function assertIdentity(directory, expected) {
if (fs.lstatSync(directory).isSymbolicLink()) throw new Error('EXTENSION_SYMLINK_REJECTED');
const manifest = JSON.parse(fs.readFileSync(path.join(directory, 'extension/package.json'), 'utf8'));
if (`${manifest.publisher}.${manifest.name}` !== expected.id || manifest.version !== expected.version)
throw new Error('EXTENSION_ID_VERSION_REJECTED');
}
function validateManagedPlugins(
root,
{
catalog = require('./managed-plugins.lock.json'),
pythonLock = require('../scripts/python-debugger.lock.json'),
platform = `${process.platform}-${process.arch}`,
} = {},
) {
if (fs.lstatSync(root).isSymbolicLink()) throw new Error('EXTENSION_SYMLINK_REJECTED');
const extensions = [...catalog.plugins];
if (pythonLock)
extensions.push(
{ directory: pythonLock.id, id: pythonLock.id, version: pythonLock.version },
{
directory: pythonLock.pythonDependency.id,
id: pythonLock.pythonDependency.id,
version: pythonLock.pythonDependency.version,
},
{
directory: pythonLock.languageServer.id,
id: pythonLock.languageServer.id,
version: pythonLock.languageServer.version,
},
);
const allowed = new Set(extensions.map(extension => extension.directory));
const found = fs.readdirSync(root).filter(name => !name.startsWith('.'));
if (found.length !== allowed.size || found.some(name => !allowed.has(name)))
throw new Error('EXTENSION_ALLOWLIST_MISMATCH');
for (const extension of extensions) {
const directory = path.join(root, extension.directory);
assertIdentity(directory, extension);
if (extension.sha256 && treeDigest(directory) !== extension.sha256)
throw new Error(`EXTENSION_DIGEST_REJECTED: ${extension.id}`);
}
if (pythonLock) {
const expected = pythonLock.platforms[platform];
if (!expected) throw new Error('EXTENSION_PLATFORM_NOT_PINNED');
const dependency = pythonLock.pythonDependency;
const packages = [
{ id: pythonLock.id, file: `${pythonLock.id}-${pythonLock.version}@${platform}.vsix`, sha256: expected },
{ id: dependency.id, file: `${dependency.id}-${dependency.version}.vsix`, sha256: dependency.sha256 },
{
id: pythonLock.languageServer.id,
file: `${pythonLock.languageServer.id}-${pythonLock.languageServer.version}.vsix`,
sha256: pythonLock.languageServer.sha256,
},
];
// Compare the unpacked bytes against the checksum-pinned official VSIX files,
// never against a manifest self-generated from the current runtime directory.
execFileSync(
'python3',
[
'-c',
`
import hashlib,json,pathlib,stat,sys,zipfile
root=pathlib.Path(sys.argv[1])
for package in json.loads(sys.argv[2]):
archive=root/'.downloads'/package['file']
if archive.is_symlink() or archive.stat().st_size>67108864: raise ValueError('EXTENSION_ARCHIVE_REJECTED')
if hashlib.sha256(archive.read_bytes()).hexdigest()!=package['sha256']: raise ValueError('EXTENSION_ARCHIVE_DIGEST_REJECTED')
target=root/package['id']
with zipfile.ZipFile(archive) as z:
expected={i.filename:hashlib.sha256(z.read(i)).hexdigest() for i in z.infolist() if not i.is_dir()}
actual={}
for p in target.rglob('*'):
if p.is_symlink(): raise ValueError('EXTENSION_SYMLINK_REJECTED')
if p.is_file(): actual[p.relative_to(target).as_posix()]=hashlib.sha256(p.read_bytes()).hexdigest()
if actual!=expected: raise ValueError('EXTENSION_CONTENT_DIGEST_REJECTED: '+package['id'])
`,
root,
JSON.stringify(packages),
],
{ timeout: 30_000, stdio: ['ignore', 'pipe', 'pipe'] },
);
}
return extensions;
}
module.exports = { treeDigest, assertIdentity, validateManagedPlugins, managedError };