🌊 The original agent harness. Deploy intelligent multi-player swarms, coordinate autonomous workflows, and build conversational AI systems. Features adaptive memory, self-learning intelligence, federation, vector RAG integration, and native Claude Code / Codex / Hermes and many more Integrated
TypeScript agentic-aiagentic-frameworkagentic-workflowagentsai-agentsai-assistantai-skillsautonomous-agentsclaudecodexdsh-pluginharnessmcp-servermulti-agentmulti-agent-systemsnpmskillsswarmswarm-intelligencetypescript
fix(mods): secret screen judges values, not names — fewer false positives, vendor keys, URL credentials, env assignments, 200 KB scan (ADR-445)
ruv0b5eb1c109e8
diff --git a/plugins/ruflo-adr/hooks/screen.ts b/plugins/ruflo-adr/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-adr/hooks/screen.ts+++ b/plugins/ruflo-adr/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-agent/hooks/screen.ts b/plugins/ruflo-agent/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-agent/hooks/screen.ts+++ b/plugins/ruflo-agent/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-agentdb/README.md b/plugins/ruflo-agentdb/README.mdindex 27cc37ec2c..7d1de9f8bd 100644--- a/plugins/ruflo-agentdb/README.md+++ b/plugins/ruflo-agentdb/README.md@@ -211,7 +211,7 @@ A function-hook mod ships beside the skills. Needs a Claude Code with mods (2.1.| Piece | Default | What it does ||---|---|---|-| **Secret guard** | on | Refuses a memory write (`agentdb_hierarchical-store`, `agentdb_pattern-store`, `agentdb_batch`, `agentdb_causal-edge`, `memory_store`, `hooks_remember`) that holds a private key, cloud/GitHub/Slack token, bearer token, JWT or key-like assignment. The secret is never echoed. |+| **Secret guard** | on | Refuses a memory write (`agentdb_hierarchical-store`, `agentdb_pattern-store`, `agentdb_batch`, `agentdb_causal-edge`, `memory_store`, `hooks_remember`) that holds a private key, cloud/GitHub/Slack token, bearer token, JWT or key-like assignment. The secret is never echoed. The shared screen (`hooks/screen.ts`, copied to every mod by `scripts/sync-mod-screen.mjs`) judges an assignment by its value: calls, env references, identifier paths, placeholders, UUIDs, secret-manager paths and hyphenated names are not secrets; a literal needs two character classes (one a digit or symbol) and at least 2.5 bits of entropy per character. Vendor keys (Stripe, npm, HuggingFace, SendGrid, Twilio, Slack webhooks) and `scheme://user:pass@host` URLs are matched by shape; input is scanned in one pass up to 200 KB (head and tail beyond that). || **Recall into prompts** | **off** | Attaches the best 1–5 memories to each prompt as framed, per-prompt context (the prompt cache is not disturbed). Read through the already-connected AgentDB (or ruvector) tool: no CLI, no network. Skipped for slash commands, `!` lines and short prompts; gives up after `recallDeadlineMs` (800); cached 10 minutes. || **Untrusted memory** | always | A retrieved memory with a secret or an instruction-to-the-model phrase is dropped; the rest are control-character-stripped, capped (5 items, 400 chars each, 1500 total) and framed as data. || **`/agentdb-mod`** | — | `status`, `recall <text>`, `scan <text>`, `recent`; answered locally, no model call. |@@ -220,7 +220,7 @@ A function-hook mod ships beside the skills. Needs a Claude Code with mods (2.1.Options (`userConfig`): `recall` off|on, `recallLimit` 1–5, `recallDeadlineMs` 200–3000, `guard` on|off, `source` auto|agentdb|ruvector|none.```bash-claude plugin test plugins/ruflo-agentdb # 26 tests: screening, recall, reader fallback, guard, /agentdb-mod, deadline, cache+claude plugin test plugins/ruflo-agentdb # 66 tests: screening (incl. secret-screen quality), recall, reader fallback, guard, /agentdb-mod, deadline, cachenode plugins/ruflo-agentdb/scripts/bench.mjs # per-call cost of the pure paths (tens of µs)```diff --git a/plugins/ruflo-agentdb/hooks/screen.ts b/plugins/ruflo-agentdb/hooks/screen.tsindex f75f792820..170d85f804 100644--- a/plugins/ruflo-agentdb/hooks/screen.ts+++ b/plugins/ruflo-agentdb/hooks/screen.ts@@ -16,10 +16,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -35,11 +42,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-agentdb/scripts/bench.mjs b/plugins/ruflo-agentdb/scripts/bench.mjsindex b05cc9374d..b5d2c33f65 100644--- a/plugins/ruflo-agentdb/scripts/bench.mjs+++ b/plugins/ruflo-agentdb/scripts/bench.mjs@@ -53,6 +53,8 @@ function time(name, fn, n) {const rows = [time('scan 2 KB text', () => m.scan(note.repeat(16)), 20000),time('scan 20 KB adversarial (ReDoS probe)', () => m.scan('ignore all previous curl wget Bearer secret= '.repeat(450)), 300),+ time('scan 20 KB of assignments + URLs (screen-quality probe)', () => m.scan('token= password: a://b:c@h api_key = "x1" Bearer secret_key=Ab1 '.repeat(330)), 300),+ time('scan 200 KB clean text (cap, one pass)', () => m.scan(note.repeat(1600)), 20),time('guard verdict (memory write, clean)', () => m.verdict('mcp__x__agentdb_hierarchical-store', write), 20000),time('guard verdict (other tool, skipped)', () => m.verdict('Bash', { command: 'ls -la' }), 200000),time('parse + screen + frame (5 results)', () => m.frame(m.screen(m.parse(result, 'agentdb', Date.now()), 3).items), 10000),diff --git a/plugins/ruflo-agentdb/tests/screen.test.ts b/plugins/ruflo-agentdb/tests/screen.test.tsindex c8caf88b55..72e2c665b7 100644--- a/plugins/ruflo-agentdb/tests/screen.test.ts+++ b/plugins/ruflo-agentdb/tests/screen.test.ts@@ -40,6 +40,105 @@ describe('screen', () => {})})+// Secret-shaped values are assembled at run time so no literal sits in the source.+const rnd = (n: number, seed = 'q7Zk3Xp9Lm2Vb8Nc4Rt6Yw1Hs5Df0Jg') => seed.repeat(Math.ceil(n / seed.length)).slice(0, n)+const hex = (n: number) => '3fa9c01be7d4852c6a1f'.repeat(4).slice(0, n)++describe('screen quality: false positives', () => {+ const clean = [+ 'const token = getTokenFromHeader(request)',+ 'token=getAuthenticationTokenFromRequestHeaders()',+ 'const key = process.env.ANTHROPIC_API_KEY_FOR_THIS_SERVICE',+ 'apiKey = process.env.ANTHROPIC_API_KEY',+ 'secret: my-k8s-secret-name-for-database',+ 'token: <uuid>',+ 'token: 123e4567-e89b-12d3-a456-426614174000',+ 'Authorization: Bearer YOUR_TOKEN_GOES_HERE_PLACEHOLDER',+ 'use sk-learn-pipeline-stage-preprocessing-step-one for the preprocessing stage',+ 'password: stored-in-1password-vault-item',+ 'secret: projects/p/secrets/s/versions/1',+ 'password: ${DB_PASSWORD}',+ 'token: opts.token,',+ 'password: "required"',+ 'DATABASE_PASSWORD=$DB_PASSWORD',+ 'postgres://user:password@localhost:5432/app',+ 'postgres://app:${DB_PASSWORD}@db/app',+ 'mysql://root:<password>@localhost/test',+ 'Run the tests, then update the token handling docs.',+ ]+ for (const text of clean) {+ test('does not flag: ' + text, () => {+ expect(scan(text).secrets).toEqual([])+ })+ }+})++describe('screen quality: false negatives', () => {+ const pw = 'Tr0ub4dor&3xKcd9876'+ const found: readonly (readonly [string, string, string])[] = [+ ['password with & in quotes', 'password: "' + pw + '"', 'key assignment'],+ ['password with @ ! #', "const password = 'p@ss!w0rd#2024xY'", 'key assignment'],+ ['JSON secret', '{"client_secret": "' + rnd(32) + '"}', 'key assignment'],+ ['AWS_SECRET_ACCESS_KEY', 'AWS_SECRET_ACCESS_KEY=' + rnd(40, 'wJalrXUtnFEMI7K7MDENGbPxRfiCY'), 'key assignment'],+ ['GITHUB_TOKEN env', 'GITHUB_TOKEN=' + rnd(30), 'key assignment'],+ ['DATABASE_PASSWORD env', 'DATABASE_PASSWORD=' + 'hunter2hunter2', 'key assignment'],+ ['URL query token', 'GET /v1/items?access_token=' + rnd(24) + '&page=2', 'key assignment'],+ ['stripe live key', 'sk_' + 'live_' + rnd(24), 'stripe key'],+ ['stripe restricted key', 'rk_' + 'live_' + rnd(24), 'stripe key'],+ ['npm token', 'npm_' + rnd(36), 'npm token'],+ ['huggingface token', 'hf_' + rnd(34), 'huggingface token'],+ ['sendgrid key', 'SG.' + rnd(22) + '.' + rnd(43, 'aB3dE5gH7jK9mN1pQ'), 'sendgrid key'],+ ['twilio key', 'SK' + hex(32), 'twilio key'],+ ['slack webhook', 'https://hooks.slack.com/services/T0' + 'ABCDEFGH/B0' + 'ABCDEFGH/' + rnd(24), 'slack webhook'],+ ['db url with credentials', 'postgres://app_user:' + 's3cr3tPw' + '[email protected]:5432/app', 'database url with credentials'],+ ['bearer token', 'Authorization: Bearer ' + rnd(40), 'bearer token'],+ ['openai style key', 'sk-' + rnd(48, 'aB3dE5gH7jK9mN1pQ2'), 'anthropic or openai key'],+ ]+ for (const [label, text, rule] of found) {+ test('flags ' + label, () => {+ const f = scan(text)+ expect(f.secrets).toContain(rule)+ expect(JSON.stringify(f)).not.toContain(rnd(24))+ })+ }++ test('a secret far past the old 20k cap is still found, in one long string', () => {+ const filler = 'lorem ipsum dolor sit amet '.repeat(5000)+ expect(filler.length).toBeGreaterThan(100_000)+ expect(hasSecret(filler.slice(0, 60_000) + 'password=' + pw + ' ' + filler.slice(0, 60_000))).toBe(true)+ expect(hasSecret(filler.slice(0, 60_000) + ' ' + filler.slice(0, 60_000))).toBe(false)+ })++ test('input beyond the scan cap keeps head and tail', () => {+ const big = 'a '.repeat(150_000)+ expect(hasSecret('npm_' + rnd(36) + ' ' + big)).toBe(true)+ expect(hasSecret(big + ' npm_' + rnd(36))).toBe(true)+ })+})++describe('screen quality: cost', () => {+ const adversarial = (n: number) => 'ignore all previous curl wget Bearer secret= token: password=a://b:c@ '.repeat(n)++ test('stays linear: 10x the input costs about 10x, not 100x', () => {+ const time = (text: string) => {+ scan(text)+ const t = performance.now()+ for (let i = 0; i < 3; i++) scan(text)+ return performance.now() - t+ }+ const small = time(adversarial(300))+ const large = time(adversarial(3000))+ expect(large / Math.max(small, 1)).toBeLessThan(40)+ })++ test('a 20 KB adversarial input finishes well inside a prompt budget', () => {+ const text = adversarial(280).slice(0, 20_000)+ const t = performance.now()+ scan(text)+ expect(performance.now() - t).toBeLessThan(50)+ })+})+describe('recall', () => {test('worthRecalling skips slash, shell and short prompts', () => {expect(worthRecalling('/clear')).toBe(false)diff --git a/plugins/ruflo-agntcy/hooks/screen.ts b/plugins/ruflo-agntcy/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-agntcy/hooks/screen.ts+++ b/plugins/ruflo-agntcy/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-ai-team/hooks/screen.ts b/plugins/ruflo-ai-team/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-ai-team/hooks/screen.ts+++ b/plugins/ruflo-ai-team/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-aidefence/hooks/screen.ts b/plugins/ruflo-aidefence/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-aidefence/hooks/screen.ts+++ b/plugins/ruflo-aidefence/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-arena/hooks/screen.ts b/plugins/ruflo-arena/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-arena/hooks/screen.ts+++ b/plugins/ruflo-arena/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-autopilot/hooks/screen.ts b/plugins/ruflo-autopilot/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-autopilot/hooks/screen.ts+++ b/plugins/ruflo-autopilot/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-bbs-federation/hooks/screen.ts b/plugins/ruflo-bbs-federation/hooks/screen.tsindex 2eb643ec3a..aa16d4f4f4 100644--- a/plugins/ruflo-bbs-federation/hooks/screen.ts+++ b/plugins/ruflo-bbs-federation/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000++/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-browser/hooks/screen.ts b/plugins/ruflo-browser/hooks/screen.tsindex d56a5160a1..82577449c9 100644--- a/plugins/ruflo-browser/hooks/screen.ts+++ b/plugins/ruflo-browser/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000++/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-business-pods/hooks/screen.ts b/plugins/ruflo-business-pods/hooks/screen.tsindex 7a00619578..53d058fc1e 100644--- a/plugins/ruflo-business-pods/hooks/screen.ts+++ b/plugins/ruflo-business-pods/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000++/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-chatgpt-federation/hooks/screen.ts b/plugins/ruflo-chatgpt-federation/hooks/screen.tsindex 97d3b30dae..daae6df822 100644--- a/plugins/ruflo-chatgpt-federation/hooks/screen.ts+++ b/plugins/ruflo-chatgpt-federation/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000++/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-daa/hooks/screen.ts b/plugins/ruflo-daa/hooks/screen.tsindex c7122184d6..2f07bcdbed 100644--- a/plugins/ruflo-daa/hooks/screen.ts+++ b/plugins/ruflo-daa/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-ddd/hooks/screen.ts b/plugins/ruflo-ddd/hooks/screen.tsindex c7122184d6..2f07bcdbed 100644--- a/plugins/ruflo-ddd/hooks/screen.ts+++ b/plugins/ruflo-ddd/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-deepseek-harness/hooks/screen.ts b/plugins/ruflo-deepseek-harness/hooks/screen.tsindex c7122184d6..2f07bcdbed 100644--- a/plugins/ruflo-deepseek-harness/hooks/screen.ts+++ b/plugins/ruflo-deepseek-harness/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-docs/hooks/screen.ts b/plugins/ruflo-docs/hooks/screen.tsindex c7122184d6..2f07bcdbed 100644--- a/plugins/ruflo-docs/hooks/screen.ts+++ b/plugins/ruflo-docs/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-federation/hooks/screen.ts b/plugins/ruflo-federation/hooks/screen.tsindex c7122184d6..2f07bcdbed 100644--- a/plugins/ruflo-federation/hooks/screen.ts+++ b/plugins/ruflo-federation/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-goals/hooks/screen.ts b/plugins/ruflo-goals/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-goals/hooks/screen.ts+++ b/plugins/ruflo-goals/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-graph-intelligence/hooks/screen.ts b/plugins/ruflo-graph-intelligence/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-graph-intelligence/hooks/screen.ts+++ b/plugins/ruflo-graph-intelligence/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-intelligence/hooks/screen.ts b/plugins/ruflo-intelligence/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-intelligence/hooks/screen.ts+++ b/plugins/ruflo-intelligence/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-iot-cognitum/hooks/screen.ts b/plugins/ruflo-iot-cognitum/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-iot-cognitum/hooks/screen.ts+++ b/plugins/ruflo-iot-cognitum/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-jujutsu/hooks/screen.ts b/plugins/ruflo-jujutsu/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-jujutsu/hooks/screen.ts+++ b/plugins/ruflo-jujutsu/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-knowledge-graph/hooks/screen.ts b/plugins/ruflo-knowledge-graph/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-knowledge-graph/hooks/screen.ts+++ b/plugins/ruflo-knowledge-graph/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-loop-workers/hooks/screen.ts b/plugins/ruflo-loop-workers/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-loop-workers/hooks/screen.ts+++ b/plugins/ruflo-loop-workers/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-market-data/hooks/screen.ts b/plugins/ruflo-market-data/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-market-data/hooks/screen.ts+++ b/plugins/ruflo-market-data/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-metaharness/hooks/screen.ts b/plugins/ruflo-metaharness/hooks/screen.tsindex 44d44964a2..daa743e23c 100644--- a/plugins/ruflo-metaharness/hooks/screen.ts+++ b/plugins/ruflo-metaharness/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-migrations/hooks/screen.ts b/plugins/ruflo-migrations/hooks/screen.tsindex ac5c8585fa..d18268021b 100644--- a/plugins/ruflo-migrations/hooks/screen.ts+++ b/plugins/ruflo-migrations/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-music/hooks/screen.ts b/plugins/ruflo-music/hooks/screen.tsindex 708ff4af27..d2ee44e167 100644--- a/plugins/ruflo-music/hooks/screen.ts+++ b/plugins/ruflo-music/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-neural-trader/hooks/screen.ts b/plugins/ruflo-neural-trader/hooks/screen.tsindex 26363d951e..f543dfb133 100644--- a/plugins/ruflo-neural-trader/hooks/screen.ts+++ b/plugins/ruflo-neural-trader/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-observability/hooks/screen.ts b/plugins/ruflo-observability/hooks/screen.tsindex 7e29007f11..cee50e7951 100644--- a/plugins/ruflo-observability/hooks/screen.ts+++ b/plugins/ruflo-observability/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-rag-memory/hooks/screen.ts b/plugins/ruflo-rag-memory/hooks/screen.tsindex 23355464bc..1662eed60c 100644--- a/plugins/ruflo-rag-memory/hooks/screen.ts+++ b/plugins/ruflo-rag-memory/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-ruvector/hooks/screen.ts b/plugins/ruflo-ruvector/hooks/screen.tsindex aba3641659..a862aa3811 100644--- a/plugins/ruflo-ruvector/hooks/screen.ts+++ b/plugins/ruflo-ruvector/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-ruvllm/hooks/screen.ts b/plugins/ruflo-ruvllm/hooks/screen.tsindex 270241a1ca..f8a0cbba85 100644--- a/plugins/ruflo-ruvllm/hooks/screen.ts+++ b/plugins/ruflo-ruvllm/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-rvf/hooks/screen.ts b/plugins/ruflo-rvf/hooks/screen.tsindex b34f61d860..5be98c0c10 100644--- a/plugins/ruflo-rvf/hooks/screen.ts+++ b/plugins/ruflo-rvf/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-security-audit/hooks/screen.ts b/plugins/ruflo-security-audit/hooks/screen.tsindex fac26a9fbc..87f8461382 100644--- a/plugins/ruflo-security-audit/hooks/screen.ts+++ b/plugins/ruflo-security-audit/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-sparc/hooks/screen.ts b/plugins/ruflo-sparc/hooks/screen.tsindex d36ced2b33..fca7544d2d 100644--- a/plugins/ruflo-sparc/hooks/screen.ts+++ b/plugins/ruflo-sparc/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-workflows/hooks/screen.ts b/plugins/ruflo-workflows/hooks/screen.tsindex 9cccc515c7..d3d01c6d14 100644--- a/plugins/ruflo-workflows/hooks/screen.ts+++ b/plugins/ruflo-workflows/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }diff --git a/plugins/ruflo-x-gateway/hooks/screen.ts b/plugins/ruflo-x-gateway/hooks/screen.tsindex 734858376d..e8471fa2d4 100644--- a/plugins/ruflo-x-gateway/hooks/screen.ts+++ b/plugins/ruflo-x-gateway/hooks/screen.ts@@ -12,10 +12,17 @@ export const COMMON_SECRETS: Rules = [['aws access key', /\b(?:AKIA|ASIA)[0-9A-Z]{16}\b/],['github token', /\b(?:gh[pousr]_[A-Za-z0-9]{30,}|github_pat_[A-Za-z0-9_]{40,})\b/],['slack token', /\bxox[abprs]-[A-Za-z0-9-]{10,}/],+ ['slack webhook', /\bhooks\.slack\.com\/services\/T[A-Z0-9]{6,}\/B[A-Z0-9]{6,}\/[A-Za-z0-9]{16,}/],['google api key', /\bAIza[0-9A-Za-z_-]{35}\b/],- ['anthropic or openai key', /\bsk-(?:ant-|proj-)?[A-Za-z0-9_-]{24,}/],+ ['anthropic or openai key', /\bsk-(?:(?:ant|proj|svcacct|admin)-[A-Za-z0-9_-]{20,}|(?=[A-Za-z]{0,40}\d)[A-Za-z0-9]{32,})/],+ ['stripe key', /\b[rs]k_live_[A-Za-z0-9]{16,}/],+ ['npm token', /\bnpm_[A-Za-z0-9]{36}\b/],+ ['huggingface token', /\bhf_[A-Za-z0-9]{30,}\b/],+ ['sendgrid key', /\bSG\.[A-Za-z0-9_-]{16,}\.[A-Za-z0-9_-]{16,}/],+ ['twilio key', /\bSK[0-9a-f]{32}\b/],['jwt', /\beyJ[A-Za-z0-9_-]{8,}\.eyJ[A-Za-z0-9_-]{8,}\.[A-Za-z0-9_-]{8,}/],- ['bearer token', /\bBearer\s+[A-Za-z0-9._~+/=-]{24,}/],+ ['bearer token', /\bBearer\s+([A-Za-z0-9._~+/=-]{24,})/],+ ['database url with credentials', /\b[a-z][a-z0-9+.-]{1,20}:\/\/[^\s:@/]+:[^\s@/]{3,}@[^\s/]+/i],]export const INJECTION: Rules = [@@ -31,11 +38,90 @@ export const INJECTION: Rules = [// C0/C1 controls (keeping tab and newline), DEL, zero-width and bidi override characters; built with escapes, never raw.const INVISIBLE = new RegExp('[\\u0000-\\u0008\\u000b-\\u001f\\u007f-\\u009f\\u200b-\\u200f\\u2028-\\u202e\\u2060-\\u2064\\ufeff]', 'g')-/** Input capped at 20k characters with invisible characters removed, so none can hide a secret or a phrase. */-export const bare = (text: string) => (text.length > 20_000 ? text.slice(0, 20_000) : text).replace(INVISIBLE, '')+/** Longest input scanned in one pass; a longer one keeps its head and tail halves. One regex pass per rule, so cost stays linear. */+const MAX_SCAN = 200_000-/** Names of the rules that match `text` (already `bare`d). */-export const names = (rules: Rules, text: string) => rules.filter(([, re]) => re.test(text)).map(([name]) => name)+/** Input bounded to MAX_SCAN characters with invisible characters removed, so none can hide a secret or a phrase. */+export const bare = (text: string) =>+ (text.length > MAX_SCAN ? text.slice(0, MAX_SCAN / 2) + '\n' + text.slice(-MAX_SCAN / 2) : text).replace(INVISIBLE, '')++// A value is a secret CANDIDATE only when it is not a reference (env var, call, identifier path, placeholder, secret-manager path) and its+// shape is random enough: at least two character classes, one of them a digit or symbol, and Shannon entropy of at least 2.5 bits per character.+const PLACEHOLDER = /placeholder|your[-_ ]|example|changeme|change[-_]?me|redacted|dummy|replace[-_]?me|insert[-_]|\*{3,}|x{5,}|\.{3}|^(?:none|null|undefined|true|false)$/i+const REFERENCE =+ /^(?:\$(?:\{[^}]*\}|\(|[A-Za-z_]\w*$)|%[^%]*%$|<[^>]*>$|\{\{|process\.env|os\.environ|env[.[]|import\.meta|System\.getenv|secrets?\.|vault:|op:\/\/|ref\+|arn:|projects\/[^/]+\/secrets\/|gcp:|kms:|aws:|file:)/i+const CALL = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*\(|^[A-Za-z_$][\w$]*\[/+const IDENT_PATH = /^[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)+$/+const UUID = /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i+const NAME_LIKE = /^[a-z][a-z0-9]*(?:[-_./][a-z0-9]+){2,}$/++function entropy(v: string): number {+ const counts = new Map<string, number>()+ for (const ch of v) counts.set(ch, (counts.get(ch) ?? 0) + 1)+ let h = 0+ for (const n of counts.values()) h -= (n / v.length) * Math.log2(n / v.length)+ return h+}++/** True when `v` is a name, call, path or placeholder rather than a literal credential. */+function isReference(v: string): boolean {+ if (PLACEHOLDER.test(v) || REFERENCE.test(v) || CALL.test(v) || IDENT_PATH.test(v)) return true+ return NAME_LIKE.test(v) && v.replace(/\D/g, '').length / v.length < 0.15+}++export function plausibleSecret(v: string): boolean {+ if (v.length < 8 || v.length > 256 || /\s/.test(v) || UUID.test(v) || isReference(v)) return false+ const symbol = /[^A-Za-z0-9]/.test(v)+ const digit = /\d/.test(v)+ const classes = [/[a-z]/.test(v), /[A-Z]/.test(v), digit, symbol].filter(Boolean).length+ return classes >= 2 && (digit || symbol) && entropy(v) >= 2.5+}++const KEY_NAME = /(?:api[_-]?key|secret|token|passw(?:or)?d|passwd|pwd|credential|private[_-]?key)s?[A-Za-z0-9_-]{0,40}["']?\s*[:=]\s*/gi+const QUOTED = /(["'\x60])((?:(?!\1)[^\n]){1,256})\1/y+const BARE_VALUE = /[^\s"'\x60,;]{1,256}/y+const QUERY_VALUE = /[^\s"'\x60,;&]{1,256}/y++/** A secret-named key assigned a literal value: env style, JSON, YAML, code. Values that are calls, references or placeholders do not count. */+function assignmentSecret(text: string): boolean {+ for (const m of text.matchAll(KEY_NAME)) {+ const at = m.index + m[0].length+ let back = m.index+ while (back > 0 && m.index - back < 64 && /[A-Za-z0-9_.-]/.test(text.charAt(back - 1))) back--+ const re = /["'\x60]/.test(text.charAt(at)) ? QUOTED : /[?&]/.test(text.charAt(back - 1)) ? QUERY_VALUE : BARE_VALUE+ re.lastIndex = at+ const hit = re.exec(text)+ if (hit && plausibleSecret(hit[2] ?? hit[0])) return true+ }+ return false+}++/** A password in a URL's userinfo that is not a placeholder such as user:password or ${DB_PASSWORD}. */+function urlCredential(url: string): boolean {+ const pass = /^[^:]+:\/\/[^\s:@/]+:([^\s@/]+)@/.exec(url)?.[1]+ if (!pass || /\$\{|\{\{|%\(|%s/.test(pass)) return false+ return !/^(?:password|passwd|pass|pwd|secret|changeme|dbpassword|db_password|\$\w*|<.*>|\{.*\}|\*+|x+)$/i.test(pass) && !PLACEHOLDER.test(pass)+}++const CHECKS: Readonly<Record<string, (m: RegExpMatchArray) => boolean>> = {+ 'bearer token': m => !isReference(m[1] ?? ''),+ 'database url with credentials': m => urlCredential(m[0]),+ 'database url with password': m => urlCredential(m[0]),+}+const globals = new WeakMap<RegExp, RegExp>()++function matches(name: string, re: RegExp, text: string): boolean {+ if (name === 'key assignment') return assignmentSecret(text)+ const check = CHECKS[name]+ if (!check) return re.test(text)+ let g = globals.get(re)+ if (!g) globals.set(re, (g = new RegExp(re.source, re.flags.includes('g') ? re.flags : re.flags + 'g')))+ for (const m of text.matchAll(g)) if (check(m)) return true+ return false+}++/** Names of the rules that match `text` (already bare'd). A rule named 'key assignment' is judged by assignmentSecret, whatever its regex. */+export const names = (rules: Rules, text: string) => rules.filter(([name, re]) => matches(name, re, text)).map(([name]) => name)export type Findings = { readonly secrets: readonly string[]; readonly injection: readonly string[] }