@aeondave/pi-persona-mind 0.9.7 → 0.9.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +137 -137
- package/CHANGELOG.md +287 -282
- package/LICENSE +24 -24
- package/README.md +1 -1
- package/docs/DESIGN.md +605 -605
- package/docs/MEMORY-RESEARCH.md +163 -163
- package/package.json +11 -2
- package/src/core/auto-optimize.ts +323 -323
- package/src/core/backlog.ts +228 -228
- package/src/core/blocked.ts +38 -38
- package/src/core/capture.ts +155 -155
- package/src/core/context-budget.ts +74 -74
- package/src/core/handoff-store.ts +111 -111
- package/src/core/handoff.ts +70 -70
- package/src/core/host-compat.ts +16 -16
- package/src/core/ids.ts +59 -59
- package/src/core/inject.ts +247 -247
- package/src/core/knowledge-review.ts +107 -107
- package/src/core/memory-read.ts +34 -34
- package/src/core/memory.ts +321 -321
- package/src/core/migrate.ts +713 -713
- package/src/core/mind-settings.ts +54 -54
- package/src/core/optimize-history.ts +163 -163
- package/src/core/optimize-service.ts +189 -189
- package/src/core/optimize.ts +127 -127
- package/src/core/retrieval.ts +164 -164
- package/src/core/scanner.ts +146 -146
- package/src/core/scope.ts +201 -201
- package/src/core/service.ts +720 -720
- package/src/core/session-memory.ts +71 -71
- package/src/core/store.ts +1002 -1002
- package/src/extension/activity.ts +79 -79
- package/src/extension/auto-optimize-session.ts +143 -143
- package/src/extension/auto-optimize.ts +565 -565
- package/src/extension/handoff.ts +23 -23
- package/src/extension/knowledge-review.ts +229 -229
- package/src/extension/memory-inspector.ts +82 -82
- package/src/extension/mind-actions.ts +38 -38
- package/src/extension/mind-worker.ts +32 -32
- package/src/extension/persona-dependency.ts +76 -76
- package/src/extension/reference-anchor.ts +48 -48
- package/src/extension/review-evidence.ts +222 -222
- package/src/extension/worker-memory-anchor.ts +28 -28
- package/src/session-memory.ts +127 -127
- package/src/tools/backlog.ts +126 -126
- package/src/tools/memory-handoff.ts +30 -30
- package/src/tools/memory-history.ts +46 -46
- package/src/tools/memory.ts +6 -4
- package/src/ui/card-frame.ts +161 -161
- package/src/ui/inspector-frame.ts +44 -44
- package/src/ui/memory-inspector.ts +464 -464
- package/src/ui/mind-result-card.ts +48 -48
- package/src/ui/presentation.ts +87 -87
- package/src/ui/session-note-card.ts +58 -58
package/src/core/scanner.ts
CHANGED
|
@@ -1,146 +1,146 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Content scanner — gates every write into the mind, and re-runs on load before injection.
|
|
3
|
-
*
|
|
4
|
-
* A memory is injected back into the supervisor's system prompt, and a sub-agent (or another
|
|
5
|
-
* process) may be the one writing it, so stored text is an untrusted escalation surface. The
|
|
6
|
-
* scanner rejects, in tiers, the classes that are genuinely dangerous as PERSISTED, RE-INJECTED
|
|
7
|
-
* facts:
|
|
8
|
-
*
|
|
9
|
-
* - "all" (fires in every scope): the attacks on the supervisor itself — prompt-injection /
|
|
10
|
-
* role-hijack, deception directives ("don't tell the user"), and invisible unicode
|
|
11
|
-
* used to smuggle them. These are never a legitimate fact to store.
|
|
12
|
-
* - "context" (the default): adds credential/secret leakage — blocking a raw key from being
|
|
13
|
-
* persisted into a shared, re-injected store.
|
|
14
|
-
* - "strict" (opt-in): adds high-false-positive offensive-security vocabulary. OFF by default so
|
|
15
|
-
* a custom security persona can retain legitimate findings like any other persona.
|
|
16
|
-
*
|
|
17
|
-
* Pattern-based, no LLM: cheap, deterministic, cross-OS. Injection patterns are filler-tolerant so
|
|
18
|
-
* padding between the verb and its target cannot bypass them, and are matched against a
|
|
19
|
-
* whitespace-collapsed copy too so a newline placed inside a phrase (which the render path rejoins)
|
|
20
|
-
* cannot slip past the scan.
|
|
21
|
-
*/
|
|
22
|
-
|
|
23
|
-
export type ScanScope = "all" | "context" | "strict";
|
|
24
|
-
const SCOPE_RANK: Record<ScanScope, number> = { all: 0, context: 1, strict: 2 };
|
|
25
|
-
|
|
26
|
-
export interface ScanResult {
|
|
27
|
-
ok: boolean;
|
|
28
|
-
reason?: string;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
interface Rule {
|
|
32
|
-
re: RegExp;
|
|
33
|
-
reason: string;
|
|
34
|
-
minScope: ScanScope;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
// Invisible / format-control characters: zero-width spaces & joiners (U+200B-200D), bidi marks and
|
|
38
|
-
// overrides (U+200E-200F, U+202A-202E), word joiner & invisible math ops (U+2060-2064), bidi
|
|
39
|
-
// isolates (U+2066-2069), BOM/ZWNBSP (U+FEFF), supplementary variation selectors
|
|
40
|
-
// (U+E0100-E01EF), and the Unicode Tags block (U+E0000-E007F) — the "Trojan Source" hiding set.
|
|
41
|
-
// Basic VS15/VS16 are intentionally not blanket-banned: ordinary emoji such as ⚠️ use them.
|
|
42
|
-
const INVISIBLE = /[----\u{e0000}-\u{e007f}\u{e0100}-\u{e01ef}]/u;
|
|
43
|
-
/** A basic variation selector attached to ASCII is not normal typography and is a common byte-
|
|
44
|
-
* smuggling shape; the same selector after an emoji/symbol remains legitimate presentation data. */
|
|
45
|
-
const ASCII_VARIATION_SELECTOR = /[\u0000-\u007f][\ufe00-\ufe0f]/u;
|
|
46
|
-
|
|
47
|
-
// NUL and the remaining non-whitespace C0/C1 controls can terminate strings or alter terminal /
|
|
48
|
-
// transport framing. Tabs and line breaks are ordinary prose formatting and stay allowed.
|
|
49
|
-
const UNSAFE_CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/u;
|
|
50
|
-
|
|
51
|
-
// Override/system-prompt phrases are matched as word sequences below instead of as a regex with a
|
|
52
|
-
// greedy filler gap. That makes the scan linear even when an untrusted document contains megabytes
|
|
53
|
-
// of near-matching filler, and avoids the old fixed 120-character bypass.
|
|
54
|
-
const OVERRIDE_START = new Set(["ignore", "disregard", "forget", "override", "ignora", "ignorare", "dimentica", "dimenticare", "sovrascrivi", "sovrascrivere"]);
|
|
55
|
-
const OVERRIDE_TARGET = new Set([
|
|
56
|
-
"previous", "prior", "above", "earlier", "all", "the", "these", "those", "your",
|
|
57
|
-
"precedente", "precedenti", "sopra", "anteriore", "anteriori", "tutto", "tutti", "tutta", "tutte", "il", "lo", "la", "i", "gli", "le", "questo", "questa", "questi", "queste", "tuo", "tua", "tuoi", "tue",
|
|
58
|
-
]);
|
|
59
|
-
const OVERRIDE_OBJECT = new Set([
|
|
60
|
-
"instruction", "instructions", "directive", "directives", "prompt", "prompts", "message", "messages", "rule", "rules",
|
|
61
|
-
"istruzione", "istruzioni", "direttiva", "direttive", "messaggio", "messaggi", "regola", "regole",
|
|
62
|
-
]);
|
|
63
|
-
const SYSTEM_TARGET = new Set(["disregard", "ignore", "override", "ignora", "ignorare", "sovrascrivi", "sovrascrivere"]);
|
|
64
|
-
const SYSTEM_WORD = new Set(["system", "sistema"]);
|
|
65
|
-
const PROMPT_WORD = new Set(["prompt"]);
|
|
66
|
-
const DECEPTION_START = new Set(["never", "dont", "mai"]);
|
|
67
|
-
const DECEPTION_VERB = new Set([
|
|
68
|
-
"tell", "inform", "mention", "reveal", "show", "disclose", "notify",
|
|
69
|
-
"dire", "informa", "informare", "menziona", "menzionare", "rivela", "rivelare", "mostra", "mostrare", "notifica", "notificare",
|
|
70
|
-
]);
|
|
71
|
-
const USER_TARGET = new Set(["user", "utente", "allutente"]);
|
|
72
|
-
|
|
73
|
-
function hasWordSequence(text: string, groups: readonly ReadonlySet<string>[]): boolean {
|
|
74
|
-
// Splitting at a full stop/newline preserves the old rule's sentence boundary. The caller passes a
|
|
75
|
-
// whitespace-collapsed copy, so a newline inserted inside a phrase is still detected.
|
|
76
|
-
for (const sentence of text.split(/[.\n]/)) {
|
|
77
|
-
const words = sentence.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [];
|
|
78
|
-
let group = 0;
|
|
79
|
-
for (const word of words) {
|
|
80
|
-
if (!groups[group]?.has(word)) continue;
|
|
81
|
-
group++;
|
|
82
|
-
if (group === groups.length) return true;
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
return false;
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
const RULES: Rule[] = [
|
|
89
|
-
// ── attacks on the supervisor itself — always flagged ───────────────────────
|
|
90
|
-
{ re: /\byou\s+are\s+now\s+(?:a\b|an\b|the\b|no\s+longer\b)/i, reason: "possible role-hijack: identity reassignment", minScope: "all" },
|
|
91
|
-
{ re: /^\s*(?:please\s+)?act\s+as\s+(?:the\s+)?system\b/i, reason: "possible role-hijack: system impersonation", minScope: "all" },
|
|
92
|
-
{ re: /\b(?:new|updated)\s+(?:system\s+)?(?:instructions?|directives?)\s*:/i, reason: "possible prompt-injection: injected directives", minScope: "all" },
|
|
93
|
-
// ── credential / secret leakage — blocked by default (context) ──────────────
|
|
94
|
-
{ re: /\bsk-ant-[A-Za-z0-9_-]{16,}/, reason: "possible secret: Anthropic API key", minScope: "context" },
|
|
95
|
-
{ re: /\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}/, reason: "possible secret: OpenAI API key", minScope: "context" },
|
|
96
|
-
{ re: /\bAKIA[0-9A-Z]{16}\b/, reason: "possible secret: AWS access key id", minScope: "context" },
|
|
97
|
-
{ re: /\bgh[pousr]_[A-Za-z0-9]{20,}\b/, reason: "possible secret: GitHub token", minScope: "context" },
|
|
98
|
-
{ re: /\bxox[baprs]-[A-Za-z0-9-]{10,}/, reason: "possible secret: Slack token", minScope: "context" },
|
|
99
|
-
{ re: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/, reason: "possible secret: private key block", minScope: "context" },
|
|
100
|
-
{ re: /\bBearer\s+[A-Za-z0-9._-]{16,}/, reason: "possible secret: bearer token / credential", minScope: "context" },
|
|
101
|
-
{ re: /\b(?:password|passwd|pwd|secret|api[_-]?key|token|access[_-]?token)\s*[=:]\s*\S{8,}/i, reason: "possible secret: inline credential assignment", minScope: "context" },
|
|
102
|
-
// ── offensive-security vocabulary — opt-in only, independent of persona name ──
|
|
103
|
-
{ re: /\b(?:cobalt\s+strike|sliver|havoc|mythic|meterpreter)\b[^.\n]{0,30}\b(?:beacon|implant|payload|c2)\b/i, reason: "offensive-security tooling (strict scope)", minScope: "strict" },
|
|
104
|
-
{ re: /\bauthorized_keys\b[^.\n]{0,40}\b(?:append|echo|>>|backdoor|persist)/i, reason: "SSH-key persistence (strict scope)", minScope: "strict" },
|
|
105
|
-
];
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Scan a candidate memory/backlog text at the given scope (default "context"). Returns
|
|
109
|
-
* `{ ok: true }` when safe to store/inject, or `{ ok: false, reason }` naming the first problem
|
|
110
|
-
* found. Empty/whitespace is treated as OK here — non-emptiness is the caller's concern.
|
|
111
|
-
*/
|
|
112
|
-
export function scanContent(text: string, scope: ScanScope = "context"): ScanResult {
|
|
113
|
-
if (INVISIBLE.test(text)) return { ok: false, reason: "invisible unicode / hidden characters detected" };
|
|
114
|
-
if (ASCII_VARIATION_SELECTOR.test(text)) return { ok: false, reason: "suspicious variation selector attached to ASCII" };
|
|
115
|
-
if (UNSAFE_CONTROL.test(text)) return { ok: false, reason: "unsafe control character detected" };
|
|
116
|
-
// Fold compatibility-width glyphs before matching: a model reads full-width Latin as the same
|
|
117
|
-
// instruction, so the trust boundary must as well. The render path also collapses whitespace;
|
|
118
|
-
// scan that form so a newline-split phrase cannot re-join into an unchecked instruction.
|
|
119
|
-
const normalized = text.normalize("NFKC");
|
|
120
|
-
const collapsed = normalized.replace(/\s+/g, " ");
|
|
121
|
-
if (hasWordSequence(collapsed, [OVERRIDE_START, OVERRIDE_TARGET, OVERRIDE_OBJECT])) {
|
|
122
|
-
return { ok: false, reason: "possible prompt-injection: override instruction" };
|
|
123
|
-
}
|
|
124
|
-
if (
|
|
125
|
-
hasWordSequence(collapsed, [SYSTEM_TARGET, SYSTEM_WORD, PROMPT_WORD]) ||
|
|
126
|
-
hasWordSequence(collapsed, [SYSTEM_TARGET, PROMPT_WORD, SYSTEM_WORD])
|
|
127
|
-
) {
|
|
128
|
-
return { ok: false, reason: "possible prompt-injection: target the system prompt" };
|
|
129
|
-
}
|
|
130
|
-
// Keep this filler-unbounded like the override checks. Apostrophes are removed before tokenizing
|
|
131
|
-
// so `don't` and `don’t` both become the single sentinel `dont`.
|
|
132
|
-
const deceptionText = collapsed.replace(/['’]/g, "");
|
|
133
|
-
if (
|
|
134
|
-
hasWordSequence(deceptionText, [DECEPTION_START, DECEPTION_VERB, USER_TARGET]) ||
|
|
135
|
-
hasWordSequence(deceptionText, [new Set(["do"]), new Set(["not"]), DECEPTION_VERB, USER_TARGET]) ||
|
|
136
|
-
hasWordSequence(deceptionText, [new Set(["non"]), DECEPTION_VERB, USER_TARGET])
|
|
137
|
-
) {
|
|
138
|
-
return { ok: false, reason: "possible deception: directive to hide/conceal from the user" };
|
|
139
|
-
}
|
|
140
|
-
const limit = SCOPE_RANK[scope];
|
|
141
|
-
for (const rule of RULES) {
|
|
142
|
-
if (SCOPE_RANK[rule.minScope] > limit) continue;
|
|
143
|
-
if (rule.re.test(normalized) || (collapsed !== normalized && rule.re.test(collapsed))) return { ok: false, reason: rule.reason };
|
|
144
|
-
}
|
|
145
|
-
return { ok: true };
|
|
146
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* Content scanner — gates every write into the mind, and re-runs on load before injection.
|
|
3
|
+
*
|
|
4
|
+
* A memory is injected back into the supervisor's system prompt, and a sub-agent (or another
|
|
5
|
+
* process) may be the one writing it, so stored text is an untrusted escalation surface. The
|
|
6
|
+
* scanner rejects, in tiers, the classes that are genuinely dangerous as PERSISTED, RE-INJECTED
|
|
7
|
+
* facts:
|
|
8
|
+
*
|
|
9
|
+
* - "all" (fires in every scope): the attacks on the supervisor itself — prompt-injection /
|
|
10
|
+
* role-hijack, deception directives ("don't tell the user"), and invisible unicode
|
|
11
|
+
* used to smuggle them. These are never a legitimate fact to store.
|
|
12
|
+
* - "context" (the default): adds credential/secret leakage — blocking a raw key from being
|
|
13
|
+
* persisted into a shared, re-injected store.
|
|
14
|
+
* - "strict" (opt-in): adds high-false-positive offensive-security vocabulary. OFF by default so
|
|
15
|
+
* a custom security persona can retain legitimate findings like any other persona.
|
|
16
|
+
*
|
|
17
|
+
* Pattern-based, no LLM: cheap, deterministic, cross-OS. Injection patterns are filler-tolerant so
|
|
18
|
+
* padding between the verb and its target cannot bypass them, and are matched against a
|
|
19
|
+
* whitespace-collapsed copy too so a newline placed inside a phrase (which the render path rejoins)
|
|
20
|
+
* cannot slip past the scan.
|
|
21
|
+
*/
|
|
22
|
+
|
|
23
|
+
export type ScanScope = "all" | "context" | "strict";
|
|
24
|
+
const SCOPE_RANK: Record<ScanScope, number> = { all: 0, context: 1, strict: 2 };
|
|
25
|
+
|
|
26
|
+
export interface ScanResult {
|
|
27
|
+
ok: boolean;
|
|
28
|
+
reason?: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface Rule {
|
|
32
|
+
re: RegExp;
|
|
33
|
+
reason: string;
|
|
34
|
+
minScope: ScanScope;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Invisible / format-control characters: zero-width spaces & joiners (U+200B-200D), bidi marks and
|
|
38
|
+
// overrides (U+200E-200F, U+202A-202E), word joiner & invisible math ops (U+2060-2064), bidi
|
|
39
|
+
// isolates (U+2066-2069), BOM/ZWNBSP (U+FEFF), supplementary variation selectors
|
|
40
|
+
// (U+E0100-E01EF), and the Unicode Tags block (U+E0000-E007F) — the "Trojan Source" hiding set.
|
|
41
|
+
// Basic VS15/VS16 are intentionally not blanket-banned: ordinary emoji such as ⚠️ use them.
|
|
42
|
+
const INVISIBLE = /[----\u{e0000}-\u{e007f}\u{e0100}-\u{e01ef}]/u;
|
|
43
|
+
/** A basic variation selector attached to ASCII is not normal typography and is a common byte-
|
|
44
|
+
* smuggling shape; the same selector after an emoji/symbol remains legitimate presentation data. */
|
|
45
|
+
const ASCII_VARIATION_SELECTOR = /[\u0000-\u007f][\ufe00-\ufe0f]/u;
|
|
46
|
+
|
|
47
|
+
// NUL and the remaining non-whitespace C0/C1 controls can terminate strings or alter terminal /
|
|
48
|
+
// transport framing. Tabs and line breaks are ordinary prose formatting and stay allowed.
|
|
49
|
+
const UNSAFE_CONTROL = /[\u0000-\u0008\u000b\u000c\u000e-\u001f\u007f-\u009f]/u;
|
|
50
|
+
|
|
51
|
+
// Override/system-prompt phrases are matched as word sequences below instead of as a regex with a
|
|
52
|
+
// greedy filler gap. That makes the scan linear even when an untrusted document contains megabytes
|
|
53
|
+
// of near-matching filler, and avoids the old fixed 120-character bypass.
|
|
54
|
+
const OVERRIDE_START = new Set(["ignore", "disregard", "forget", "override", "ignora", "ignorare", "dimentica", "dimenticare", "sovrascrivi", "sovrascrivere"]);
|
|
55
|
+
const OVERRIDE_TARGET = new Set([
|
|
56
|
+
"previous", "prior", "above", "earlier", "all", "the", "these", "those", "your",
|
|
57
|
+
"precedente", "precedenti", "sopra", "anteriore", "anteriori", "tutto", "tutti", "tutta", "tutte", "il", "lo", "la", "i", "gli", "le", "questo", "questa", "questi", "queste", "tuo", "tua", "tuoi", "tue",
|
|
58
|
+
]);
|
|
59
|
+
const OVERRIDE_OBJECT = new Set([
|
|
60
|
+
"instruction", "instructions", "directive", "directives", "prompt", "prompts", "message", "messages", "rule", "rules",
|
|
61
|
+
"istruzione", "istruzioni", "direttiva", "direttive", "messaggio", "messaggi", "regola", "regole",
|
|
62
|
+
]);
|
|
63
|
+
const SYSTEM_TARGET = new Set(["disregard", "ignore", "override", "ignora", "ignorare", "sovrascrivi", "sovrascrivere"]);
|
|
64
|
+
const SYSTEM_WORD = new Set(["system", "sistema"]);
|
|
65
|
+
const PROMPT_WORD = new Set(["prompt"]);
|
|
66
|
+
const DECEPTION_START = new Set(["never", "dont", "mai"]);
|
|
67
|
+
const DECEPTION_VERB = new Set([
|
|
68
|
+
"tell", "inform", "mention", "reveal", "show", "disclose", "notify",
|
|
69
|
+
"dire", "informa", "informare", "menziona", "menzionare", "rivela", "rivelare", "mostra", "mostrare", "notifica", "notificare",
|
|
70
|
+
]);
|
|
71
|
+
const USER_TARGET = new Set(["user", "utente", "allutente"]);
|
|
72
|
+
|
|
73
|
+
function hasWordSequence(text: string, groups: readonly ReadonlySet<string>[]): boolean {
|
|
74
|
+
// Splitting at a full stop/newline preserves the old rule's sentence boundary. The caller passes a
|
|
75
|
+
// whitespace-collapsed copy, so a newline inserted inside a phrase is still detected.
|
|
76
|
+
for (const sentence of text.split(/[.\n]/)) {
|
|
77
|
+
const words = sentence.toLowerCase().match(/[\p{L}\p{N}]+/gu) ?? [];
|
|
78
|
+
let group = 0;
|
|
79
|
+
for (const word of words) {
|
|
80
|
+
if (!groups[group]?.has(word)) continue;
|
|
81
|
+
group++;
|
|
82
|
+
if (group === groups.length) return true;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const RULES: Rule[] = [
|
|
89
|
+
// ── attacks on the supervisor itself — always flagged ───────────────────────
|
|
90
|
+
{ re: /\byou\s+are\s+now\s+(?:a\b|an\b|the\b|no\s+longer\b)/i, reason: "possible role-hijack: identity reassignment", minScope: "all" },
|
|
91
|
+
{ re: /^\s*(?:please\s+)?act\s+as\s+(?:the\s+)?system\b/i, reason: "possible role-hijack: system impersonation", minScope: "all" },
|
|
92
|
+
{ re: /\b(?:new|updated)\s+(?:system\s+)?(?:instructions?|directives?)\s*:/i, reason: "possible prompt-injection: injected directives", minScope: "all" },
|
|
93
|
+
// ── credential / secret leakage — blocked by default (context) ──────────────
|
|
94
|
+
{ re: /\bsk-ant-[A-Za-z0-9_-]{16,}/, reason: "possible secret: Anthropic API key", minScope: "context" },
|
|
95
|
+
{ re: /\bsk-(?:proj-)?[A-Za-z0-9_-]{20,}/, reason: "possible secret: OpenAI API key", minScope: "context" },
|
|
96
|
+
{ re: /\bAKIA[0-9A-Z]{16}\b/, reason: "possible secret: AWS access key id", minScope: "context" },
|
|
97
|
+
{ re: /\bgh[pousr]_[A-Za-z0-9]{20,}\b/, reason: "possible secret: GitHub token", minScope: "context" },
|
|
98
|
+
{ re: /\bxox[baprs]-[A-Za-z0-9-]{10,}/, reason: "possible secret: Slack token", minScope: "context" },
|
|
99
|
+
{ re: /-----BEGIN [A-Z0-9 ]*PRIVATE KEY-----/, reason: "possible secret: private key block", minScope: "context" },
|
|
100
|
+
{ re: /\bBearer\s+[A-Za-z0-9._-]{16,}/, reason: "possible secret: bearer token / credential", minScope: "context" },
|
|
101
|
+
{ re: /\b(?:password|passwd|pwd|secret|api[_-]?key|token|access[_-]?token)\s*[=:]\s*\S{8,}/i, reason: "possible secret: inline credential assignment", minScope: "context" },
|
|
102
|
+
// ── offensive-security vocabulary — opt-in only, independent of persona name ──
|
|
103
|
+
{ re: /\b(?:cobalt\s+strike|sliver|havoc|mythic|meterpreter)\b[^.\n]{0,30}\b(?:beacon|implant|payload|c2)\b/i, reason: "offensive-security tooling (strict scope)", minScope: "strict" },
|
|
104
|
+
{ re: /\bauthorized_keys\b[^.\n]{0,40}\b(?:append|echo|>>|backdoor|persist)/i, reason: "SSH-key persistence (strict scope)", minScope: "strict" },
|
|
105
|
+
];
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
* Scan a candidate memory/backlog text at the given scope (default "context"). Returns
|
|
109
|
+
* `{ ok: true }` when safe to store/inject, or `{ ok: false, reason }` naming the first problem
|
|
110
|
+
* found. Empty/whitespace is treated as OK here — non-emptiness is the caller's concern.
|
|
111
|
+
*/
|
|
112
|
+
export function scanContent(text: string, scope: ScanScope = "context"): ScanResult {
|
|
113
|
+
if (INVISIBLE.test(text)) return { ok: false, reason: "invisible unicode / hidden characters detected" };
|
|
114
|
+
if (ASCII_VARIATION_SELECTOR.test(text)) return { ok: false, reason: "suspicious variation selector attached to ASCII" };
|
|
115
|
+
if (UNSAFE_CONTROL.test(text)) return { ok: false, reason: "unsafe control character detected" };
|
|
116
|
+
// Fold compatibility-width glyphs before matching: a model reads full-width Latin as the same
|
|
117
|
+
// instruction, so the trust boundary must as well. The render path also collapses whitespace;
|
|
118
|
+
// scan that form so a newline-split phrase cannot re-join into an unchecked instruction.
|
|
119
|
+
const normalized = text.normalize("NFKC");
|
|
120
|
+
const collapsed = normalized.replace(/\s+/g, " ");
|
|
121
|
+
if (hasWordSequence(collapsed, [OVERRIDE_START, OVERRIDE_TARGET, OVERRIDE_OBJECT])) {
|
|
122
|
+
return { ok: false, reason: "possible prompt-injection: override instruction" };
|
|
123
|
+
}
|
|
124
|
+
if (
|
|
125
|
+
hasWordSequence(collapsed, [SYSTEM_TARGET, SYSTEM_WORD, PROMPT_WORD]) ||
|
|
126
|
+
hasWordSequence(collapsed, [SYSTEM_TARGET, PROMPT_WORD, SYSTEM_WORD])
|
|
127
|
+
) {
|
|
128
|
+
return { ok: false, reason: "possible prompt-injection: target the system prompt" };
|
|
129
|
+
}
|
|
130
|
+
// Keep this filler-unbounded like the override checks. Apostrophes are removed before tokenizing
|
|
131
|
+
// so `don't` and `don’t` both become the single sentinel `dont`.
|
|
132
|
+
const deceptionText = collapsed.replace(/['’]/g, "");
|
|
133
|
+
if (
|
|
134
|
+
hasWordSequence(deceptionText, [DECEPTION_START, DECEPTION_VERB, USER_TARGET]) ||
|
|
135
|
+
hasWordSequence(deceptionText, [new Set(["do"]), new Set(["not"]), DECEPTION_VERB, USER_TARGET]) ||
|
|
136
|
+
hasWordSequence(deceptionText, [new Set(["non"]), DECEPTION_VERB, USER_TARGET])
|
|
137
|
+
) {
|
|
138
|
+
return { ok: false, reason: "possible deception: directive to hide/conceal from the user" };
|
|
139
|
+
}
|
|
140
|
+
const limit = SCOPE_RANK[scope];
|
|
141
|
+
for (const rule of RULES) {
|
|
142
|
+
if (SCOPE_RANK[rule.minScope] > limit) continue;
|
|
143
|
+
if (rule.re.test(normalized) || (collapsed !== normalized && rule.re.test(collapsed))) return { ok: false, reason: rule.reason };
|
|
144
|
+
}
|
|
145
|
+
return { ok: true };
|
|
146
|
+
}
|