Skip to content

Soul-driven AI agent with permission-hardened tools, token budgets, and multi-channel access. Runs 24/7 from CLI, Telegram or More.

fix(bots): malformed permissions.yaml scope entries skip + warn, never crash turns

Salman Qureshi14b36cf39ba1
diff --git a/src/bots/bot-manager.test.ts b/src/bots/bot-manager.test.ts
index ffdaa2e7..ff515032 100644
--- a/src/bots/bot-manager.test.ts
+++ b/src/bots/bot-manager.test.ts
@@ -407,6 +407,31 @@ describe('Per-bot permission isolation (fail-closed)', () => {
expect(scopes.some(s => s.path === '/tmp/execdir' && s.execute)).toBe(true);
});
+ it('a malformed permissions.yaml entry (missing "scope") is skipped, not fatal', () => {
+ // Hand-edit typo class: `socpe:` instead of `scope:` — the registry build
+ // must never crash every turn over it (the entry is just skipped).
+ const manifest = store.create({ id: 'typo', name: 'Typo' }) as BotManifest;
+ store.writePermissions('typo', {
+ paths: [
+ { scope: 'self', read: true, write: true },
+ { socpe: '/tmp/cookies', read: true } as unknown as { scope: string; read: boolean },
+ ],
+ });
+ const registry = createBotCapabilityRegistry({
+ botId: 'typo',
+ manifest,
+ botDir: store.botDir('typo'),
+ permissions: store.readPermissions('typo'),
+ persona: store.readPersona('typo'),
+ userMemory: null,
+ config: getDefaultConfig() as MercuryConfig,
+ });
+ const scopes = registry.permissions.getManifest().capabilities.filesystem.scopes;
+ // The malformed entry contributed nothing; the valid self scope survived.
+ expect(scopes).toHaveLength(1);
+ expect(scopes[0].path).toBe(store.botDir('typo'));
+ });
+
it('a persona without an Access section changes nothing (current permissions apply)', () => {
const manifest = store.create({ id: 'plainbot', name: 'Plainbot' }) as BotManifest;
const registry = createBotCapabilityRegistry({
diff --git a/src/bots/doctor.test.ts b/src/bots/doctor.test.ts
index 58b735ce..e8b47a5d 100644
--- a/src/bots/doctor.test.ts
+++ b/src/bots/doctor.test.ts
@@ -103,6 +103,21 @@ describe('runBotDoctor', () => {
expect(report.healthy).toBe(false);
});
+ it('flags malformed permissions.yaml entries (missing "scope") as an error', () => {
+ store.create({ id: 'researcher', name: 'Research' });
+ store.writePermissions('researcher', {
+ paths: [
+ { scope: 'self', read: true, write: true },
+ { socpe: '/tmp/cookies', read: true } as unknown as { scope: string; read: boolean },
+ ],
+ });
+ const report = runBotDoctor({ store, queue, journalFor: () => journal });
+ const finding = report.findings.find(f => f.botId === 'researcher' && f.check === 'permissions');
+ expect(finding?.severity).toBe('error');
+ expect(finding?.detail).toContain('socpe');
+ expect(report.healthy).toBe(false);
+ });
+
it('formatDoctorReport maps unhealthy to an actionable message', () => {
const healthy = formatDoctorReport({ findings: [], healthy: true, checked: 2 });
expect(healthy).toContain('healthy');
diff --git a/src/bots/doctor.ts b/src/bots/doctor.ts
index 2d771d25..3d9a8eec 100644
--- a/src/bots/doctor.ts
+++ b/src/bots/doctor.ts
@@ -46,6 +46,21 @@ export function runBotDoctor(deps: DoctorDeps): DoctorReport {
findings.push({ botId: manifest.id, severity: 'error', check: 'config', detail: errors.join('; ') });
}
+ // Malformed permissions.yaml entries (missing/empty `scope` — typically a
+ // hand-edit typo) are skipped by the registry at build time; flag them so
+ // the grant silently NOT applying is visible (the cron-doctor class of
+ // "configured but not effective").
+ const perms = deps.store.readPermissions(manifest.id);
+ const malformed = (perms.paths ?? []).filter(p => !p || typeof p.scope !== 'string' || p.scope.trim().length === 0);
+ if (malformed.length > 0) {
+ findings.push({
+ botId: manifest.id,
+ severity: 'error',
+ check: 'permissions',
+ detail: `${malformed.length} path-scope entr(ies) missing a "scope" value in permissions.yaml — the grant is skipped (check for typos like "socpe:")`,
+ });
+ }
+
const dlqDepth = dlqByBot.get(manifest.id) ?? 0;
if (dlqDepth > 0) {
findings.push({
diff --git a/src/bots/registry-factory.ts b/src/bots/registry-factory.ts
index 926c27e7..0cf2a5fb 100644
--- a/src/bots/registry-factory.ts
+++ b/src/bots/registry-factory.ts
@@ -69,7 +69,14 @@ export function createBotCapabilityRegistry(deps: BotRegistryDeps): CapabilityRe
// Reshape the manifest in place (never call save() — that writes the
// global ~/.mercury/permissions.yaml, and bots have no path to it).
const manifest = pm.getManifest();
- const granted = mergeScopeGrants(deps.permissions.paths, parsePersonaAccess(deps.persona ?? ''));
+ // Malformed scope entries (missing/empty `scope` — e.g. a hand-edited
+ // permissions.yaml typo) must never crash every turn: skip + warn.
+ const fileGrants = (deps.permissions.paths ?? []).filter(isValidScopeEntry);
+ const skipped = (deps.permissions.paths?.length ?? 0) - fileGrants.length;
+ if (skipped > 0) {
+ logger.warn({ botId: deps.botId, skipped }, 'Malformed path-scope entries in permissions.yaml (missing "scope") skipped');
+ }
+ const granted = mergeScopeGrants(fileGrants, parsePersonaAccess(deps.persona ?? ''));
// Implicit sandbox grants come LAST and are never user-configurable away:
// the private workspace and the fleet-shared folder are the bot's built-in
// work areas (read/write/execute, no ask, no declaration).
@@ -160,9 +167,15 @@ function normalizeScopeKey(scope: string): string {
* (fail-closed) — but the store always writes a default self scope at
* creation.
*/
+/** A scope entry is usable only with a non-empty string scope. */
+function isValidScopeEntry(p: BotPathScope | undefined): boolean {
+ return !!p && typeof p.scope === 'string' && p.scope.trim().length > 0;
+}
+
function buildBotScopes(paths: BotPathScope[] | undefined, botDir: string): Array<{ path: string; read: boolean; write: boolean; execute?: boolean }> {
const scopes: Array<{ path: string; read: boolean; write: boolean; execute?: boolean }> = [];
for (const p of paths ?? []) {
+ if (!isValidScopeEntry(p)) continue;
const resolved = p.scope === 'self'
? resolve(botDir)
: resolve(p.scope.replace(/^~/, homedir()));