mirror of
https://github.com/ruvnet/ruflo.git
synced 2026-12-03 22:12:01 +09:00
feat(aidefence): select MCP pattern packs with RUFLO_AIDEFENCE_PACKS (#4785)
Unset keeps the current options exactly. true/false or a comma list of pack names is passed to createAIDefence in every load and retry branch; any other value is refused with a clear error. Closes #4785 Co-Authored-By: RuFlo <ruv@ruv.net>
This commit is contained in:
1 parent
dc79625daa
commit
95ec6e44e8
2 files changed
+107
-3
No files matched your search
@@ -0,0 +1,66 @@
|
||||
/**
|
||||
* #4785: the MCP server's shared defender takes its pattern-pack selection
|
||||
* from RUFLO_AIDEFENCE_PACKS. Unset keeps today's options exactly.
|
||||
*/
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const createAIDefence = vi.fn((_options: Record<string, unknown>) => ({
|
||||
detect: async () => ({ safe: true, threats: [], piiFound: false, detectionTimeMs: 0, inputHash: 'h' }),
|
||||
getStats: async () => ({}),
|
||||
}));
|
||||
vi.mock('@claude-flow/aidefence', () => ({ createAIDefence }));
|
||||
|
||||
const previous = process.env.RUFLO_AIDEFENCE_PACKS;
|
||||
|
||||
async function scanWith(value: string | undefined) {
|
||||
vi.resetModules();
|
||||
createAIDefence.mockClear();
|
||||
if (value === undefined) delete process.env.RUFLO_AIDEFENCE_PACKS;
|
||||
else process.env.RUFLO_AIDEFENCE_PACKS = value;
|
||||
const { securityTools } = await import('../src/mcp-tools/security-tools.js');
|
||||
const scan = securityTools.find(tool => tool.name === 'aidefence_scan')!;
|
||||
return scan.handler({ input: 'hello' }) as Promise<Record<string, unknown>>;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
if (previous === undefined) delete process.env.RUFLO_AIDEFENCE_PACKS;
|
||||
else process.env.RUFLO_AIDEFENCE_PACKS = previous;
|
||||
});
|
||||
|
||||
describe('aidefencePackSelection (#4785)', () => {
|
||||
it('parses the accepted forms and refuses everything else', async () => {
|
||||
const { aidefencePackSelection } = await import('../src/mcp-tools/security-tools.js');
|
||||
expect(aidefencePackSelection(undefined)).toBeUndefined();
|
||||
expect(aidefencePackSelection(' ')).toBeUndefined();
|
||||
expect(aidefencePackSelection('false')).toBe(false);
|
||||
expect(aidefencePackSelection('TRUE')).toBe(true);
|
||||
expect(aidefencePackSelection('core, exfil_url,core')).toEqual(['core', 'exfil_url']);
|
||||
expect(() => aidefencePackSelection('no')).toThrow(/Invalid RUFLO_AIDEFENCE_PACKS/);
|
||||
expect(() => aidefencePackSelection('core,bogus')).toThrow(/Invalid RUFLO_AIDEFENCE_PACKS/);
|
||||
expect(() => aidefencePackSelection(',')).toThrow(/Invalid RUFLO_AIDEFENCE_PACKS/);
|
||||
});
|
||||
});
|
||||
|
||||
describe('shared defender options (#4785)', () => {
|
||||
it('omits packs when the variable is unset', async () => {
|
||||
await scanWith(undefined);
|
||||
expect(createAIDefence).toHaveBeenCalledTimes(1);
|
||||
expect(createAIDefence.mock.calls[0][0]).toEqual({ enableLearning: true });
|
||||
});
|
||||
|
||||
it('passes packs:false for built-ins-only mode', async () => {
|
||||
await scanWith('false');
|
||||
expect(createAIDefence.mock.calls[0][0]).toEqual({ enableLearning: true, packs: false });
|
||||
});
|
||||
|
||||
it('passes a named pack list', async () => {
|
||||
await scanWith('core,tool_invocation');
|
||||
expect(createAIDefence.mock.calls[0][0]).toEqual({ enableLearning: true, packs: ['core', 'tool_invocation'] });
|
||||
});
|
||||
|
||||
it('refuses an invalid value instead of creating a defender', async () => {
|
||||
const result = await scanWith('maybe');
|
||||
expect(createAIDefence).not.toHaveBeenCalled();
|
||||
expect(JSON.stringify(result)).toMatch(/Invalid RUFLO_AIDEFENCE_PACKS/);
|
||||
});
|
||||
});
|
||||
@@ -43,6 +43,44 @@ const wrapperStats = {
|
||||
quickScanThreats: 0,
|
||||
};
|
||||
|
||||
const KNOWN_PACKS = new Set([
|
||||
'core',
|
||||
'tool_invocation',
|
||||
'exfil_url',
|
||||
'encoded_instruction',
|
||||
'slack_markup_forgery',
|
||||
'instruction_override_i18n',
|
||||
]);
|
||||
|
||||
/**
|
||||
* Pattern-pack selection for the shared defender (#4785, ADR-517), read once
|
||||
* per process from RUFLO_AIDEFENCE_PACKS:
|
||||
* unset/empty current default (option omitted)
|
||||
* true|false all enabled packs | built-in patterns only
|
||||
* a,b,c exactly the named packs
|
||||
* Anything else is refused rather than silently falling back.
|
||||
*/
|
||||
export function aidefencePackSelection(raw = process.env.RUFLO_AIDEFENCE_PACKS): boolean | string[] | undefined {
|
||||
const value = raw?.trim();
|
||||
if (!value) return undefined;
|
||||
const lower = value.toLowerCase();
|
||||
if (lower === 'true') return true;
|
||||
if (lower === 'false') return false;
|
||||
const names = value.split(',').map(name => name.trim()).filter(Boolean);
|
||||
const unknown = names.filter(name => !KNOWN_PACKS.has(name));
|
||||
if (names.length === 0 || unknown.length > 0) {
|
||||
throw new Error(
|
||||
`Invalid RUFLO_AIDEFENCE_PACKS=${JSON.stringify(value)}: use true, false, or a comma list of ${[...KNOWN_PACKS].join(', ')}`,
|
||||
);
|
||||
}
|
||||
return [...new Set(names)];
|
||||
}
|
||||
|
||||
function aidefenceOptions(): { enableLearning: true; packs?: boolean | string[] } {
|
||||
const packs = aidefencePackSelection();
|
||||
return packs === undefined ? { enableLearning: true } : { enableLearning: true, packs };
|
||||
}
|
||||
|
||||
/**
|
||||
* Get or create AIDefence instance (throws if unavailable)
|
||||
*/
|
||||
@@ -56,7 +94,7 @@ async function getAIDefence(): Promise<AIDefenceInstance> {
|
||||
// First attempt - try to load via dynamic import (ESM)
|
||||
try {
|
||||
const aidefence = await import(packageName);
|
||||
const instance = aidefence.createAIDefence({ enableLearning: true });
|
||||
const instance = aidefence.createAIDefence(aidefenceOptions());
|
||||
if (!instance) {
|
||||
throw new Error('createAIDefence returned null');
|
||||
}
|
||||
@@ -96,7 +134,7 @@ async function getAIDefence(): Promise<AIDefenceInstance> {
|
||||
// the module cache is stable across the await boundary.
|
||||
try {
|
||||
const aidefence = await import(packageName);
|
||||
const instance = aidefence.createAIDefence({ enableLearning: true });
|
||||
const instance = aidefence.createAIDefence(aidefenceOptions());
|
||||
if (instance) {
|
||||
aidefenceInstance = instance;
|
||||
console.error(`[claude-flow] ${packageName} loaded after install (resolver path)`);
|
||||
@@ -110,7 +148,7 @@ async function getAIDefence(): Promise<AIDefenceInstance> {
|
||||
const modulePath = require.resolve(packageName);
|
||||
const cacheBust = `?t=${Date.now()}`;
|
||||
const aidefence = await import(`file://${modulePath}${cacheBust}`);
|
||||
const instance = aidefence.createAIDefence({ enableLearning: true });
|
||||
const instance = aidefence.createAIDefence(aidefenceOptions());
|
||||
if (!instance) {
|
||||
throw new Error('createAIDefence returned null after install');
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user