Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

fix(plugins): re-verify installed plugin content on every load (#36277)

Neil636164029cc0
diff --git a/src/main/plugins/plugin-content-integrity.ts b/src/main/plugins/plugin-content-integrity.ts
index ba69328f969..b662ad6ae57 100644
--- a/src/main/plugins/plugin-content-integrity.ts
+++ b/src/main/plugins/plugin-content-integrity.ts
@@ -32,29 +32,28 @@ export async function verifyHashAddressedPluginContent(
return { ok: true }
}
+// Why no cache: a hash costs ~1 ms for typical plugin trees, and bytes changed after a first
+// load must be caught on the next one. Only checks already in flight are shared.
export class PluginContentVerifier {
- private readonly verifications = new Map<string, Promise<PluginContentIntegrityResult>>()
-
- clear(): void {
- this.verifications.clear()
- }
+ private readonly inFlight = new Map<string, Promise<PluginContentIntegrityResult>>()
+ /** Re-hashes the tree on every load; concurrent loads of the same tree share one hash. */
async verify(plugin: HashAddressedPluginContent & { pluginKey: string }): Promise<void> {
- // Same-key installs can change during verification; cache by immutable content identity.
const identity = JSON.stringify([plugin.pluginKey, plugin.rootDir, plugin.contentHash])
- let verification = this.verifications.get(identity)
+ let verification = this.inFlight.get(identity)
if (!verification) {
- verification = verifyHashAddressedPluginContent(plugin)
- this.verifications.set(identity, verification)
+ verification = verifyHashAddressedPluginContent(plugin).finally(() => {
+ this.inFlight.delete(identity)
+ })
+ this.inFlight.set(identity, verification)
}
const result = await verification
if (!result.ok) {
- this.verifications.delete(identity)
throw new Error(`plugin ${plugin.pluginKey} failed integrity verification: ${result.error}`)
}
}
- /** Uncached integrity check for code about to run; bytes may have changed since `verify`. */
+ /** Unshared check for code about to run: a hash started before this call must not vouch for it. */
async verifyNow(plugin: HashAddressedPluginContent & { pluginKey: string }): Promise<void> {
const result = await verifyHashAddressedPluginContent(plugin)
if (!result.ok) {
diff --git a/src/main/plugins/plugin-service-integrity.test.ts b/src/main/plugins/plugin-service-integrity.test.ts
index efc0cbcdffe..b304dd07bf6 100644
--- a/src/main/plugins/plugin-service-integrity.test.ts
+++ b/src/main/plugins/plugin-service-integrity.test.ts
@@ -79,7 +79,7 @@ afterEach(async () => {
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })))
})
-describe('PluginService lazy content verification', () => {
+describe('PluginService content verification on load', () => {
it('does not share an in-flight verification across same-key content revisions', async () => {
const oldPlugin = await createInstalledPlugin({ worker: false })
const newPlugin = await createInstalledPlugin({ worker: false })
@@ -101,7 +101,7 @@ describe('PluginService lazy content verification', () => {
await expect(newVerification).resolves.toBeUndefined()
})
- it('detects tampering only when panel code is first consumed', async () => {
+ it('detects tampering on the first panel load after discovery', async () => {
const plugin = await createInstalledPlugin({ worker: false })
const service = createService(plugin)
await service.initialize()
@@ -113,6 +113,18 @@ describe('PluginService lazy content verification', () => {
await service.dispose()
})
+ it('detects tampering on a later panel load after a successful first load', async () => {
+ const plugin = await createInstalledPlugin({ worker: false })
+ const service = createService(plugin)
+ await service.initialize()
+ await expect(service.panels.readEntry(plugin.pluginKey, 'panel')).resolves.not.toBeNull()
+
+ await writeFile(join(plugin.rootDir, 'payload.txt'), 'tampered after first load')
+
+ await expect(service.panels.readEntry(plugin.pluginKey, 'panel')).resolves.toBeNull()
+ await service.dispose()
+ })
+
it('blocks a worker fork when installed content changed after discovery', async () => {
const plugin = await createInstalledPlugin({ worker: true })
const workerFactory = vi.fn<PluginWorkerFactory>()
@@ -127,3 +139,39 @@ describe('PluginService lazy content verification', () => {
await service.dispose()
})
})
+
+describe('PluginContentVerifier', () => {
+ it('re-hashes on every load, so a tree changed after a passing check fails the next', async () => {
+ const plugin = await createInstalledPlugin({ worker: false })
+ const verifier = new PluginContentVerifier()
+ const target = {
+ pluginKey: plugin.pluginKey,
+ rootDir: plugin.rootDir,
+ contentHash: plugin.contentHash
+ }
+
+ await expect(verifier.verify(target)).resolves.toBeUndefined()
+ await expect(verifier.verify(target)).resolves.toBeUndefined()
+ await writeFile(join(plugin.rootDir, 'payload.txt'), 'tampered after first load')
+
+ await expect(verifier.verify(target)).rejects.toThrow('integrity verification')
+ })
+
+ it('shares one hash between concurrent loads of the same tree', async () => {
+ const plugin = await createInstalledPlugin({ worker: false })
+ const verifier = new PluginContentVerifier()
+ const target = {
+ pluginKey: plugin.pluginKey,
+ rootDir: plugin.rootDir,
+ contentHash: plugin.contentHash
+ }
+ const hashes = await import('./plugin-content-hash')
+ const spy = vi.spyOn(hashes, 'hashPluginTree')
+
+ await Promise.all([verifier.verify(target), verifier.verify(target), verifier.verify(target)])
+ expect(spy).toHaveBeenCalledTimes(1)
+ await verifier.verify(target)
+ expect(spy).toHaveBeenCalledTimes(2)
+ spy.mockRestore()
+ })
+})
diff --git a/src/main/plugins/plugin-service.ts b/src/main/plugins/plugin-service.ts
index 717dab6f982..93c65015906 100644
--- a/src/main/plugins/plugin-service.ts
+++ b/src/main/plugins/plugin-service.ts
@@ -148,7 +148,6 @@ export class PluginService {
return
}
this.contentPacksReady = false
- this.contentVerifier.clear()
if (!enabled) {
this.panels.revokeAll()
}