feat(plugins): ed25519 release-key verification and plugin add/remove (9b)

Ships ed25519 signature verification for plugins with two new components:

- src/plugins/keys.ts: RELEASE_PUBLIC_KEYS map with one release keypair
- scripts/sign-plugin.mjs: keygen and sign commands for plugin developers
- verifyPlugin() in loader.ts: validates ed25519 signatures, rejects unsigned plugins unless CODEBURN_PLUGIN_DEV=1
- plugin add <path>: installs signed plugins to ~/.config/codeburn/plugins/
- plugin remove <name> --confirm: removes installed plugins

All three gates pass: tsc clean, vitest zero new failures, smoke test complete.

Design decision: public keys stored as base64-encoded PEM format rather than raw 32-byte keys. Node.js crypto.verify requires PEM/DER format or KeyObject for ed25519; raw bytes alone fail. PEM is standard and portable.

Private key written to: /tmp/codeburn-signing/codeburn-signing-key.pem
This commit is contained in:
iamtoruk 2026-08-28 00:30:43 -07:00
parent 5a72bb35c0
commit 67320432ca
7 changed files with 679 additions and 6 deletions

View file

@ -11,8 +11,9 @@
*/
import type { Command } from 'commander'
import { stat } from 'fs/promises'
import { stat, mkdir, readFile, writeFile, rm, readdir } from 'fs/promises'
import { join } from 'path'
import { homedir } from 'os'
import { defaultPluginsDir, loadPlugins, currentCliVersion, verifyPlugin, readPluginManifestRaw } from './loader.js'
import { parsePluginManifest, type PluginManifest } from './manifest.js'
@ -88,6 +89,60 @@ export function registerPluginCommands(program: Command): void {
throw new Error(`unverified ${name}@${manifest.version} ${result.reason ?? 'verification failed'}`)
}
})
plugin
.command('add <path>')
.description('Install a plugin from a source directory')
.option('--dir <path>', 'Override the plugins directory')
.action(async (sourcePath: string, opts: { dir?: string }) => {
const pluginsDir = opts.dir ?? defaultPluginsDir()
const { raw, reason } = await readPluginManifestRaw(sourcePath)
if (reason) {
throw new Error(`Could not read manifest from ${sourcePath}: ${reason}`)
}
const parsed = parsePluginManifest(raw, `${sourcePath}/codeburn-plugin.json`)
if (!parsed.ok) {
throw new Error(`Invalid manifest: ${parsed.reason}`)
}
const manifest = parsed.manifest
const verified = await verifyPlugin(sourcePath, manifest, process.env)
if (!verified.ok) {
throw new Error(`Plugin verification failed: ${verified.reason ?? 'unknown reason'}`)
}
const destDir = join(pluginsDir, manifest.name)
try {
await stat(destDir)
throw new Error(`Plugin "${manifest.name}" already installed at ${destDir}`)
} catch (err: unknown) {
if ((err as NodeJS.ErrnoException).code !== 'ENOENT') throw err
}
await mkdir(destDir, { recursive: true })
const entries = await readdir(sourcePath, { withFileTypes: true })
for (const entry of entries.sort((a, b) => a.name.localeCompare(b.name))) {
if (!entry.isFile()) continue
const srcFile = join(sourcePath, entry.name)
const destFile = join(destDir, entry.name)
const content = await readFile(srcFile)
await writeFile(destFile, content)
}
process.stdout.write(`Plugin "${manifest.name}@${manifest.version}" installed to ${destDir}\n`)
})
plugin
.command('remove <name>')
.description('Remove an installed plugin')
.option('--dir <path>', 'Override the plugins directory')
.option('--confirm', 'Confirm removal')
.action(async (name: string, opts: { dir?: string, confirm?: boolean }) => {
const pluginsDir = opts.dir ?? defaultPluginsDir()
const destDir = join(pluginsDir, name)
if (!opts.confirm) {
process.stdout.write(`Would remove plugin directory: ${destDir}\nUse --confirm to proceed.\n`)
process.exit(1)
}
await rm(destDir, { recursive: true, force: true })
process.stdout.write(`Plugin "${name}" removed.\n`)
})
}
/// Reads the manifest at <dir>/codeburn-plugin.json and parses it via the

6
src/plugins/keys.ts Normal file
View file

@ -0,0 +1,6 @@
/// Release public keys for ed25519 plugin signature verification.
/// Maps keyId (8 hex chars) to base64-encoded PEM public key.
export const RELEASE_PUBLIC_KEYS: ReadonlyMap<string, string> = new Map([
['f40248d0', 'LS0tLS1CRUdJTiBQVUJMSUMgS0VZLS0tLS0KTUNvd0JRWURLMlZ3QXlFQTU1QTAvMUpLTlBoMGFsL2xMN2xhWFBobWVnZVhzUENqK3RoM1B4LzNWM0U9Ci0tLS0tRU5EIFBVQkxJQyBLRVktLS0tLQo='],
])

View file

@ -15,8 +15,10 @@ import { readdir, readFile, stat } from 'fs/promises'
import { join } from 'path'
import { homedir } from 'os'
import { createRequire } from 'module'
import { verify as cryptoVerify } from 'crypto'
import { checkCliCompat, parsePluginManifest, type PluginManifest } from './manifest.js'
import { RELEASE_PUBLIC_KEYS } from './keys.js'
/// Resolves from BOTH layouts: src/plugins/loader.ts during tests/tsx and
/// the flattened dist/main.ts bundle at runtime (tsup keeps one file).
@ -48,11 +50,109 @@ export function defaultPluginsDir(): string {
/// Unsigned plugins are refused unless CODEBURN_PLUGIN_DEV=1, and even then
/// only for local development (this flag is expected to be absent in every
/// real environment).
export async function verifyPlugin(dir: string, manifest: PluginManifest, env: NodeJS.ProcessEnv): Promise<{ ok: boolean, reason?: string }> {
export async function verifyPlugin(
dir: string,
manifest: PluginManifest,
env: NodeJS.ProcessEnv,
knownKeys?: ReadonlyMap<string, string>,
): Promise<{ ok: boolean, reason?: string }> {
if (env.CODEBURN_PLUGIN_DEV === '1') return { ok: true }
void dir
void manifest
return { ok: false, reason: 'unsigned plugin (signature verification arrives in 9b; CODEBURN_PLUGIN_DEV=1 allows unsigned plugins for local development only)' }
const keys = knownKeys ?? RELEASE_PUBLIC_KEYS
// Read the signature file
const sigFile = join(dir, 'codeburn-plugin.sig')
let sigData: { alg?: string, keyId?: string, signature?: string }
try {
const content = await readFile(sigFile, 'utf8')
sigData = JSON.parse(content)
} catch {
return { ok: false, reason: 'missing signature file' }
}
if (sigData.alg !== 'ed25519') {
return { ok: false, reason: 'unknown signature algorithm' }
}
const keyId = sigData.keyId
if (!keyId || typeof keyId !== 'string') {
return { ok: false, reason: 'missing key id' }
}
if (!keys.has(keyId)) {
return { ok: false, reason: 'unknown key id' }
}
// Check for symlinks
const hasSymlink = await checkForSymlinks(dir)
if (hasSymlink) {
return { ok: false, reason: 'symlink present' }
}
// Get file list
const files = await getPluginFilesList(dir)
// Compute canonical digest
const canonical = JSON.stringify({
name: manifest.name,
version: manifest.version,
files,
})
// Get public key and verify signature
const pubKeyBase64 = keys.get(keyId)!
const pubKeyPem = Buffer.from(pubKeyBase64, 'base64').toString('utf8')
const signatureBuffer = Buffer.from(sigData.signature ?? '', 'base64')
try {
const isValid = cryptoVerify(null, Buffer.from(canonical), pubKeyPem, signatureBuffer)
if (!isValid) {
return { ok: false, reason: 'bad signature' }
}
} catch {
return { ok: false, reason: 'bad signature' }
}
return { ok: true }
}
async function checkForSymlinks(dir: string): Promise<boolean> {
try {
const entries = await readdir(dir, { withFileTypes: true })
for (const entry of entries) {
const fullPath = join(dir, entry.name)
if (entry.isSymbolicLink()) return true
}
} catch {
return true
}
return false
}
async function getPluginFilesList(
dir: string,
): Promise<Array<{ path: string, sha256: string }>> {
const { createHash } = await import('crypto')
const files: Array<{ path: string, sha256: string }> = []
try {
const entries = await readdir(dir, { withFileTypes: true })
for (const entry of entries) {
if (entry.name === 'codeburn-plugin.sig') continue
if (!entry.isFile()) continue
const fullPath = join(dir, entry.name)
try {
const content = await readFile(fullPath)
const hash = createHash('sha256').update(content).digest('hex')
files.push({ path: entry.name, sha256: hash })
} catch {
continue
}
}
} catch {
return []
}
files.sort((a, b) => a.path.localeCompare(b.path))
return files
}
async function readManifest(dir: string): Promise<{ raw?: unknown, reason?: string }> {