mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-03 22:12:01 +09:00
dream(security): #4618 enforce SecurityPolicy.minTrustLevelFor{Write,Memory,Spawn} in PolicyEngine
PolicyEngine.evaluateMessage() gated federation messages solely via the hardcoded CAPABILITY_GATES table; SecurityPolicy's minTrustLevelForWrite/ Memory/Spawn fields were declared, defaulted, and accepted as constructor overrides but never read. By default this let memory-query/memory-response messages through at TrustLevel.ATTESTED, one level below the policy's own stated minTrustLevelForMemory: TRUSTED. Adds a per-message-type policy-category check layered as an additional restriction on top of CAPABILITY_GATES (never a replacement — a loosened override cannot bypass that hardcoded floor). 10 new discriminating tests; 677/677 full package suite passes; stash-isolated baseline fails the 3 tests this fix targets. Co-Authored-By: RuFlo <ruv@ruv.net> Claude-Session: https://claude.ai/code/session_01ReiDH4ix1BStZRrZwPT81X
This commit is contained in:
2 files changed
+133
No files matched your search
@@ -0,0 +1,93 @@
|
||||
/**
|
||||
* PolicyEngine Tests — SecurityPolicy trust-level thresholds
|
||||
*
|
||||
* `SecurityPolicy.minTrustLevelForWrite/Memory/Spawn` were declared,
|
||||
* defaulted, and accepted as constructor overrides, but `evaluateMessage()`
|
||||
* never read them — the only trust-level gate was the hardcoded
|
||||
* `CAPABILITY_GATES` table in trust-level.ts. By default that table allows
|
||||
* the `query-redacted` operation (used by `memory-query`/`memory-response`)
|
||||
* at TrustLevel.ATTESTED, one level below the policy's own stated
|
||||
* `minTrustLevelForMemory: TrustLevel.TRUSTED` — a live default-config gap,
|
||||
* not dependent on any caller override. These tests pin the expected
|
||||
* behavior once the policy thresholds are actually enforced.
|
||||
*/
|
||||
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { PolicyEngine, type SecurityPolicy } from '../../src/application/policy-engine.js';
|
||||
import { TrustLevel } from '../../src/domain/entities/trust-level.js';
|
||||
|
||||
function allowAllClaims() {
|
||||
return { checkClaim: () => true };
|
||||
}
|
||||
|
||||
describe('PolicyEngine — SecurityPolicy trust-level enforcement', () => {
|
||||
describe('default policy', () => {
|
||||
it('rejects memory-query at ATTESTED even though CAPABILITY_GATES allows query-redacted there', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
const result = engine.evaluateMessage('memory-query', TrustLevel.ATTESTED, 10, 'node-a');
|
||||
expect(result.allowed).toBe(false);
|
||||
expect(result.reason).toMatch(/memory/i);
|
||||
});
|
||||
|
||||
it('allows memory-query at TRUSTED (matches minTrustLevelForMemory default)', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
const result = engine.evaluateMessage('memory-query', TrustLevel.TRUSTED, 10, 'node-b');
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('allows memory-response at TRUSTED, rejects at ATTESTED', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
expect(engine.evaluateMessage('memory-response', TrustLevel.TRUSTED, 10, 'node-c').allowed).toBe(true);
|
||||
expect(engine.evaluateMessage('memory-response', TrustLevel.ATTESTED, 10, 'node-c').allowed).toBe(false);
|
||||
});
|
||||
|
||||
it('still allows task-assignment (write) at ATTESTED — matches minTrustLevelForWrite default', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
const result = engine.evaluateMessage('task-assignment', TrustLevel.ATTESTED, 10, 'node-d');
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('still rejects agent-spawn below PRIVILEGED — matches minTrustLevelForSpawn default', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
const result = engine.evaluateMessage('agent-spawn', TrustLevel.TRUSTED, 10, 'node-e');
|
||||
expect(result.allowed).toBe(false);
|
||||
});
|
||||
|
||||
it('allows agent-spawn at PRIVILEGED', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
const result = engine.evaluateMessage('agent-spawn', TrustLevel.PRIVILEGED, 10, 'node-f');
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('leaves message types with no policy category (e.g. status-broadcast) gated only by CAPABILITY_GATES', () => {
|
||||
const engine = new PolicyEngine(allowAllClaims());
|
||||
const result = engine.evaluateMessage('status-broadcast', TrustLevel.VERIFIED, 10, 'node-g');
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('constructor override actually takes effect', () => {
|
||||
it('honors a tightened minTrustLevelForMemory override (PRIVILEGED) by rejecting TRUSTED', () => {
|
||||
const override: Partial<SecurityPolicy> = { minTrustLevelForMemory: TrustLevel.PRIVILEGED };
|
||||
const engine = new PolicyEngine(allowAllClaims(), override);
|
||||
const result = engine.evaluateMessage('memory-query', TrustLevel.TRUSTED, 10, 'node-h');
|
||||
expect(result.allowed).toBe(false);
|
||||
});
|
||||
|
||||
it('honors a loosened minTrustLevelForMemory override (ATTESTED) by allowing ATTESTED', () => {
|
||||
const override: Partial<SecurityPolicy> = { minTrustLevelForMemory: TrustLevel.ATTESTED };
|
||||
const engine = new PolicyEngine(allowAllClaims(), override);
|
||||
const result = engine.evaluateMessage('memory-query', TrustLevel.ATTESTED, 10, 'node-i');
|
||||
expect(result.allowed).toBe(true);
|
||||
});
|
||||
|
||||
it('does not let a loosened minTrustLevelForSpawn override bypass the hardcoded CAPABILITY_GATES floor (defense in depth)', () => {
|
||||
const override: Partial<SecurityPolicy> = { minTrustLevelForSpawn: TrustLevel.VERIFIED };
|
||||
const engine = new PolicyEngine(allowAllClaims(), override);
|
||||
// CAPABILITY_GATES (trust-level.ts) only grants 'remote-spawn' at PRIVILEGED,
|
||||
// regardless of how permissive the SecurityPolicy override is.
|
||||
const result = engine.evaluateMessage('agent-spawn', TrustLevel.TRUSTED, 10, 'node-j');
|
||||
expect(result.allowed).toBe(false);
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -45,6 +45,33 @@ const DEFAULT_SECURITY_POLICY: SecurityPolicy = {
|
||||
minTrustLevelForSpawn: TrustLevel.PRIVILEGED,
|
||||
};
|
||||
|
||||
// Message categories gated by the per-operation thresholds declared on
|
||||
// SecurityPolicy (minTrustLevelForWrite/Memory/Spawn). Message types not
|
||||
// listed here rely solely on CAPABILITY_GATES (trust-level.ts) — their
|
||||
// gating is not configurable via SecurityPolicy today.
|
||||
type PolicyGatedCategory = 'write' | 'memory' | 'spawn';
|
||||
|
||||
const POLICY_CATEGORY_FOR_MESSAGE_TYPE: Partial<Record<FederationMessageType, PolicyGatedCategory>> = {
|
||||
'task-assignment': 'write',
|
||||
'context-share': 'write',
|
||||
'claim-event': 'write',
|
||||
'memory-query': 'memory',
|
||||
'memory-response': 'memory',
|
||||
'agent-spawn': 'spawn',
|
||||
'agent-handoff': 'spawn',
|
||||
};
|
||||
|
||||
function policyThresholdForCategory(policy: SecurityPolicy, category: PolicyGatedCategory): TrustLevel {
|
||||
switch (category) {
|
||||
case 'write':
|
||||
return policy.minTrustLevelForWrite;
|
||||
case 'memory':
|
||||
return policy.minTrustLevelForMemory;
|
||||
case 'spawn':
|
||||
return policy.minTrustLevelForSpawn;
|
||||
}
|
||||
}
|
||||
|
||||
const CLAIMS_FOR_MESSAGE_TYPE: Record<FederationMessageType, FederationClaimType[]> = {
|
||||
'task-assignment': ['federation:write'],
|
||||
'memory-query': ['federation:read', 'federation:memory'],
|
||||
@@ -153,6 +180,19 @@ export class PolicyEngine {
|
||||
};
|
||||
}
|
||||
|
||||
const policyCategory = POLICY_CATEGORY_FOR_MESSAGE_TYPE[messageType];
|
||||
if (policyCategory) {
|
||||
const requiredLevel = policyThresholdForCategory(this.securityPolicy, policyCategory);
|
||||
if (trustLevel < requiredLevel) {
|
||||
return {
|
||||
allowed: false,
|
||||
reason: `Trust level ${trustLevel} below configured minimum ${requiredLevel} for ${policyCategory} operations`,
|
||||
requiredClaims,
|
||||
trustLevelSufficient: false,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
allowed: true,
|
||||
reason: 'Policy check passed',
|
||||
|
||||
Reference in new issue
Block a user