Files
waggle-os/packages/core/src/injection-scanner.ts
shumeiko.it ad2ca875cb feat(core): give Waggle its own database, apart from memory (P1.5)
Waggle kept its own tables in the memory database. Before the memory
engine can be replaced by mem0 they need a home of their own.

- add WaggleDB (<dataDir>/waggle.db). Automations, notifications, held
  actions, skill hashes, thumbs feedback, cost tracking, the profile card
  and awareness items live there. Their stores take a WaggleDB, so passing
  a memory database is a type error.
- the profile card and awareness items are now per scope ('personal' or
  'workspace:<id>') in one table each, instead of one table per .mind file
- on the first start, copy these tables from personal.mind and every
  workspace.mind (importLegacyMindData). The legacy files are only read,
  each scope is copied once and a table only while its target is empty.
- copy the logger, injection scanner, external-memory ingress guard,
  WorkspaceManager and the embedders from @waggle/hive-mind-core into
  @waggle/core, which now re-exports its own copies
- point the server, agent orchestrator and CLI at waggle.db; update the
  tests that seeded these tables in a memory database

ADR-0007 records the layout and the licensing question about the copied
files.
2026-10-23 14:31:28 +02:00

86 lines
3.2 KiB
TypeScript

/**
* Prompt-injection scanner — pattern-based detection for role-override,
* prompt-extraction, and instruction-injection attempts.
*
* Historically lived in `@waggle/agent`. Moved to `@waggle/core` so the
* harvest pipeline (which ingests untrusted external conversation exports)
* can call it without a cross-package dependency. The `@waggle/agent`
* version is now a thin re-export for backward compatibility.
*
* Three pattern sets:
* - ROLE_OVERRIDE: "ignore previous instructions", "you are now…", memory-wipe attempts
* - PROMPT_EXTRACTION: "print your system prompt", "reveal your instructions"
* - INSTRUCTION_INJECTION: fake "IMPORTANT:", "SYSTEM:", "[INST]" authority markers
*/
export interface ScanResult {
safe: boolean;
score: number;
flags: string[];
}
const ROLE_OVERRIDE_PATTERNS = [
/ignore\s+(all\s+)?previous\s+(instructions|prompts|rules)/i,
/you\s+are\s+now\s+/i,
/new\s+instructions?\s*:/i,
/forget\s+(everything|all|your)\s+(you|instructions|rules)/i,
/override\s+(your|the|all)\s+(instructions|rules|prompt)/i,
/disregard\s+(your|the|all|previous)\s+(instructions|rules|prompt)/i,
/ignoriere\s+alle/i,
/ignora\s+todas/i,
/ignorez\s+toutes/i,
// F8: Memory wipe — "forget everything", "erase all context", etc.
/(?:forget|erase|clear|wipe|reset)\s+(?:everything|conversation|memory|context|history|all)/i,
// F8: Role override — "from now on you are", "pretend you are", "your new role is"
/(?:from\s+now\s+on\s+you\s+are|act\s+as\s+if\s+you\s+are|pretend\s+you\s+are|your\s+new\s+role\s+is)/i,
];
const PROMPT_EXTRACTION_PATTERNS = [
/print\s+(your|the)\s+system\s+prompt/i,
/show\s+(me\s+)?(your|the)\s+(system\s+)?prompt/i,
/what\s+(is|are)\s+your\s+(system\s+)?(prompt|instructions|rules)/i,
/repeat\s+(your|the)\s+(system\s+)?(prompt|instructions)/i,
/output\s+(your|the)\s+(system\s+)?prompt\s+verbatim/i,
// F8: Instruction disclosure — "reveal your instructions", "disclose your prompt", etc.
/(?:reveal|disclose|show|display|print|output|dump)\s+(?:your\s+)?(?:instructions|system\s+prompt|rules|guidelines|configuration|training)/i,
];
const INSTRUCTION_INJECTION_PATTERNS = [
/IMPORTANT\s*:\s*(ignore|disregard|forget|override)/i,
/SYSTEM\s*:\s*/i,
/\[INST\]/i,
/<<SYS>>/i,
/\bASSISTANT\s*:\s*/i,
/BEGIN\s+NEW\s+INSTRUCTIONS/i,
// F8: Authority claims — fake system/admin messages
/(?:system\s+message|admin\s+message|admin\s+override|elevated\s+privileges|root\s+access|operator\s+mode|maintenance\s+mode|debug\s+mode)/i,
];
export function scanForInjection(
text: string,
context: 'user_input' | 'tool_output' = 'user_input'
): ScanResult {
const flags: string[] = [];
let score = 0;
for (const pattern of ROLE_OVERRIDE_PATTERNS) {
if (pattern.test(text)) { flags.push('role_override'); score += 0.5; break; }
}
for (const pattern of PROMPT_EXTRACTION_PATTERNS) {
if (pattern.test(text)) { flags.push('prompt_extraction'); score += 0.4; break; }
}
for (const pattern of INSTRUCTION_INJECTION_PATTERNS) {
if (pattern.test(text)) {
flags.push('instruction_injection');
score += context === 'tool_output' ? 0.6 : 0.3;
break;
}
}
score = Math.min(score, 1.0);
return { safe: score < 0.3, score, flags };
}