@agent-beam/beam 0.2.27 → 0.2.29
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -14
- package/Skills/agentbeam-app-monitoring/SKILL.md +28 -7
- package/dist/cli.js +43 -4
- package/dist/cli.js.map +1 -1
- package/dist/client.js +103 -81
- package/dist/client.js.map +1 -1
- package/dist/core.d.ts +2 -13
- package/dist/core.js +2 -25
- package/dist/core.js.map +1 -1
- package/dist/monitor-config.d.ts +39 -0
- package/dist/monitor-config.js +134 -0
- package/dist/monitor-config.js.map +1 -0
- package/dist/monitor-llm.d.ts +50 -7
- package/dist/monitor-llm.js +227 -31
- package/dist/monitor-llm.js.map +1 -1
- package/dist/monitor-pipeline.d.ts +101 -0
- package/dist/monitor-pipeline.js +291 -0
- package/dist/monitor-pipeline.js.map +1 -0
- package/dist/monitor-rules.d.ts +9 -5
- package/dist/monitor-rules.js +107 -13
- package/dist/monitor-rules.js.map +1 -1
- package/dist/monitor.d.ts +32 -39
- package/dist/monitor.js +200 -181
- package/dist/monitor.js.map +1 -1
- package/dist/prompt-attacks.d.ts +29 -0
- package/dist/prompt-attacks.js +237 -0
- package/dist/prompt-attacks.js.map +1 -0
- package/dist/rule-catalog.d.ts +13 -0
- package/dist/rule-catalog.js +56 -0
- package/dist/rule-catalog.js.map +1 -0
- package/dist/sdk.d.ts +88 -0
- package/dist/sdk.js +298 -0
- package/dist/sdk.js.map +1 -0
- package/package.json +11 -2
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import type { MonitorFinding, MonitorLevel, MonitorSeverity } from "./monitor-client.js";
|
|
2
|
+
import type { CustomDetector } from "./data-detectors.js";
|
|
3
|
+
import { type LineSource, type MonitorRule } from "./monitor-rules.js";
|
|
4
|
+
/** A short, stable fingerprint (two seeded 53-bit hashes); not cryptographic, only used for dedupe ids. */
|
|
5
|
+
export declare function fingerprint(text: string): string;
|
|
6
|
+
/** light: critical only · standard: high and up · strict: medium and up (same thresholds as the server). */
|
|
7
|
+
export declare const meetsLevel: (severity: MonitorSeverity, level: MonitorLevel) => boolean;
|
|
8
|
+
export declare const DEDUPE_WINDOW_MS: Record<MonitorLevel, number>;
|
|
9
|
+
export declare const BRUTE_FORCE_THRESHOLD: Record<MonitorLevel, number>;
|
|
10
|
+
export declare const LLM_FLOOD_THRESHOLD: Record<MonitorLevel, number>;
|
|
11
|
+
export declare const MAX_NEW_FINDINGS_PER_10_MIN = 200;
|
|
12
|
+
export type SendOutcome = "sent" | "retry" | "drop";
|
|
13
|
+
export interface IngestOptions {
|
|
14
|
+
/** What the text is. Default: worked out from the line (an access-log line or an app log line). */
|
|
15
|
+
source?: LineSource;
|
|
16
|
+
/** Who sent it (client address or user id): groups repeats and counts floods. Default: the line's client address. */
|
|
17
|
+
client?: string;
|
|
18
|
+
/** Rules for this text only, like the SDK's canary check. */
|
|
19
|
+
rules?: MonitorRule[];
|
|
20
|
+
/** False sends the rule, count and sender without the text excerpt. */
|
|
21
|
+
excerpt?: boolean;
|
|
22
|
+
}
|
|
23
|
+
export interface PipelineOptions {
|
|
24
|
+
/** The active level, or null while capture is off (not enabled, or the allowance is spent). */
|
|
25
|
+
level: () => MonitorLevel | null;
|
|
26
|
+
customDetectors?: () => CustomDetector[];
|
|
27
|
+
now?: () => number;
|
|
28
|
+
/** Detection time allowed per minute; past it, lines are skipped (and counted) until the next minute. */
|
|
29
|
+
cpuBudgetMsPerMinute?: number;
|
|
30
|
+
/** Or a budget shared with other work (the SDK's prompt and output checks). */
|
|
31
|
+
budget?: CpuBudget;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* A hard cap on detection time per minute, so monitoring can never take more than a fixed share of
|
|
35
|
+
* a CPU, whatever the traffic or input. Work over the cap is skipped and counted, not queued.
|
|
36
|
+
*/
|
|
37
|
+
export declare class CpuBudget {
|
|
38
|
+
private readonly msPerMinute;
|
|
39
|
+
private minute;
|
|
40
|
+
private spent;
|
|
41
|
+
private skippedSince;
|
|
42
|
+
constructor(msPerMinute: number);
|
|
43
|
+
/** Runs `work` if this minute's budget isn't spent; otherwise skips it and returns undefined. */
|
|
44
|
+
run<T>(work: () => T): T | undefined;
|
|
45
|
+
/** How many were skipped since the last call (for a diagnostic). */
|
|
46
|
+
takeSkipped(): number;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Events per key over the last few minutes, in one-minute buckets: a fixed few numbers per key
|
|
50
|
+
* however busy the key is, and at most `maxKeys` keys (the least recently active go first).
|
|
51
|
+
*/
|
|
52
|
+
export declare class WindowCounter {
|
|
53
|
+
private readonly windowMinutes;
|
|
54
|
+
private readonly maxKeys;
|
|
55
|
+
private keys;
|
|
56
|
+
constructor(windowMinutes: number, maxKeys?: number);
|
|
57
|
+
get size(): number;
|
|
58
|
+
/** Records one event and returns the count over the window, this one included. */
|
|
59
|
+
add(key: string, now: number): number;
|
|
60
|
+
/** Drops keys with nothing in the window. */
|
|
61
|
+
prune(now: number): void;
|
|
62
|
+
}
|
|
63
|
+
export declare class MonitorPipeline {
|
|
64
|
+
private readonly opts;
|
|
65
|
+
private seen;
|
|
66
|
+
private queue;
|
|
67
|
+
private queued;
|
|
68
|
+
private authFailures;
|
|
69
|
+
private llmCalls;
|
|
70
|
+
private floodBucket;
|
|
71
|
+
private floodCount;
|
|
72
|
+
private suppressed;
|
|
73
|
+
private maintainedMinute;
|
|
74
|
+
readonly budget: CpuBudget | null;
|
|
75
|
+
constructor(opts: PipelineOptions);
|
|
76
|
+
private now;
|
|
77
|
+
get pending(): MonitorFinding[];
|
|
78
|
+
/** Sizes of everything the pipeline keeps, for tests and diagnostics: all of them are bounded. */
|
|
79
|
+
get footprint(): {
|
|
80
|
+
seen: number;
|
|
81
|
+
queue: number;
|
|
82
|
+
queued: number;
|
|
83
|
+
authFailures: number;
|
|
84
|
+
llmCalls: number;
|
|
85
|
+
};
|
|
86
|
+
/** Once a minute: forget dedupe entries and counters that can no longer matter. */
|
|
87
|
+
private maintain;
|
|
88
|
+
/**
|
|
89
|
+
* Matches one line and queues any findings at or above the level. Returns the rule ids raised.
|
|
90
|
+
* Over the CPU budget, the line is skipped (see budget.takeSkipped()).
|
|
91
|
+
*/
|
|
92
|
+
ingestLine(line: string, file?: string, options?: IngestOptions): string[];
|
|
93
|
+
private ingest;
|
|
94
|
+
/** Queues a finding produced elsewhere (dependency checks) under its own stable id, once. */
|
|
95
|
+
addFinding(finding: MonitorFinding): boolean;
|
|
96
|
+
private raise;
|
|
97
|
+
private emitSuppressed;
|
|
98
|
+
private enqueue;
|
|
99
|
+
/** Sends queued findings in batches; a "retry" outcome keeps the batch for the next flush. */
|
|
100
|
+
flush(send: (batch: MonitorFinding[]) => Promise<SendOutcome>): Promise<void>;
|
|
101
|
+
}
|
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
import { clientIp, isAuthFailure, isLlmRequest, lineSource, matchLine, redactLine } from "./monitor-rules.js";
|
|
2
|
+
/** A short, stable fingerprint (two seeded 53-bit hashes); not cryptographic, only used for dedupe ids. */
|
|
3
|
+
export function fingerprint(text) {
|
|
4
|
+
const h = (seed) => {
|
|
5
|
+
let h1 = 0xdeadbeef ^ seed, h2 = 0x41c6ce57 ^ seed;
|
|
6
|
+
for (let i = 0; i < text.length; i++) {
|
|
7
|
+
const ch = text.charCodeAt(i);
|
|
8
|
+
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
9
|
+
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
10
|
+
}
|
|
11
|
+
h1 = Math.imul(h1 ^ (h1 >>> 16), 2246822507) ^ Math.imul(h2 ^ (h2 >>> 13), 3266489909);
|
|
12
|
+
h2 = Math.imul(h2 ^ (h2 >>> 16), 2246822507) ^ Math.imul(h1 ^ (h1 >>> 13), 3266489909);
|
|
13
|
+
return (4294967296 * (2097151 & h2) + (h1 >>> 0)).toString(16).padStart(14, "0");
|
|
14
|
+
};
|
|
15
|
+
return `${h(1)}${h(2)}`.slice(0, 24);
|
|
16
|
+
}
|
|
17
|
+
const RANK = { critical: 3, high: 2, medium: 1, low: 0 };
|
|
18
|
+
const MIN_RANK = { light: 3, standard: 2, strict: 1 };
|
|
19
|
+
/** light: critical only · standard: high and up · strict: medium and up (same thresholds as the server). */
|
|
20
|
+
export const meetsLevel = (severity, level) => RANK[severity] >= MIN_RANK[level];
|
|
21
|
+
// One finding absorbs repeats (its `count` goes up) for this long before a new one is raised.
|
|
22
|
+
export const DEDUPE_WINDOW_MS = { light: 60 * 60_000, standard: 15 * 60_000, strict: 5 * 60_000 };
|
|
23
|
+
// Failed sign-ins from one address within five minutes before it counts as brute forcing.
|
|
24
|
+
export const BRUTE_FORCE_THRESHOLD = { light: 50, standard: 20, strict: 10 };
|
|
25
|
+
const BRUTE_FORCE_WINDOW_MS = 5 * 60_000;
|
|
26
|
+
// Paid model calls from one sender within five minutes before it counts as abuse: scripted use
|
|
27
|
+
// that runs up the app's LLM bill ("denial of wallet") or scrapes the model through the app.
|
|
28
|
+
export const LLM_FLOOD_THRESHOLD = { light: 1500, standard: 300, strict: 120 };
|
|
29
|
+
const LLM_FLOOD_WINDOW_MS = 5 * 60_000;
|
|
30
|
+
// A scan or a log loop can match thousands of distinct lines; past this many new findings in ten
|
|
31
|
+
// minutes the rest are folded into one summary finding so a flood can't spend the month's allowance.
|
|
32
|
+
export const MAX_NEW_FINDINGS_PER_10_MIN = 200;
|
|
33
|
+
const FLOOD_WINDOW_MS = 10 * 60_000;
|
|
34
|
+
const MAX_QUEUE = 1000;
|
|
35
|
+
const BATCH_SIZE = 100;
|
|
36
|
+
/** Groups lines that differ only in ids, numbers, addresses or timestamps. */
|
|
37
|
+
function normalizeForGrouping(line) {
|
|
38
|
+
return line.toLowerCase()
|
|
39
|
+
.replace(/\b(?:\d{1,3}\.){3}\d{1,3}\b/g, "<ip>")
|
|
40
|
+
.replace(/\b[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\b/g, "<id>")
|
|
41
|
+
.replace(/\b[0-9a-f]{16,}\b/g, "<hex>")
|
|
42
|
+
.replace(/\d+/g, "0")
|
|
43
|
+
.replace(/\s+/g, " ")
|
|
44
|
+
.slice(0, 300);
|
|
45
|
+
}
|
|
46
|
+
const clock = () => (globalThis.performance?.now?.() ?? Date.now());
|
|
47
|
+
/**
|
|
48
|
+
* A hard cap on detection time per minute, so monitoring can never take more than a fixed share of
|
|
49
|
+
* a CPU, whatever the traffic or input. Work over the cap is skipped and counted, not queued.
|
|
50
|
+
*/
|
|
51
|
+
export class CpuBudget {
|
|
52
|
+
msPerMinute;
|
|
53
|
+
minute = -1;
|
|
54
|
+
spent = 0;
|
|
55
|
+
skippedSince = 0;
|
|
56
|
+
constructor(msPerMinute) {
|
|
57
|
+
this.msPerMinute = msPerMinute;
|
|
58
|
+
}
|
|
59
|
+
/** Runs `work` if this minute's budget isn't spent; otherwise skips it and returns undefined. */
|
|
60
|
+
run(work) {
|
|
61
|
+
const minute = Math.floor(Date.now() / 60_000);
|
|
62
|
+
if (minute !== this.minute) {
|
|
63
|
+
this.minute = minute;
|
|
64
|
+
this.spent = 0;
|
|
65
|
+
}
|
|
66
|
+
if (this.spent >= this.msPerMinute) {
|
|
67
|
+
this.skippedSince++;
|
|
68
|
+
return undefined;
|
|
69
|
+
}
|
|
70
|
+
const start = clock();
|
|
71
|
+
try {
|
|
72
|
+
return work();
|
|
73
|
+
}
|
|
74
|
+
finally {
|
|
75
|
+
this.spent += clock() - start;
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
/** How many were skipped since the last call (for a diagnostic). */
|
|
79
|
+
takeSkipped() { const n = this.skippedSince; this.skippedSince = 0; return n; }
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Events per key over the last few minutes, in one-minute buckets: a fixed few numbers per key
|
|
83
|
+
* however busy the key is, and at most `maxKeys` keys (the least recently active go first).
|
|
84
|
+
*/
|
|
85
|
+
export class WindowCounter {
|
|
86
|
+
windowMinutes;
|
|
87
|
+
maxKeys;
|
|
88
|
+
keys = new Map();
|
|
89
|
+
constructor(windowMinutes, maxKeys = 10_000) {
|
|
90
|
+
this.windowMinutes = windowMinutes;
|
|
91
|
+
this.maxKeys = maxKeys;
|
|
92
|
+
}
|
|
93
|
+
get size() { return this.keys.size; }
|
|
94
|
+
/** Records one event and returns the count over the window, this one included. */
|
|
95
|
+
add(key, now) {
|
|
96
|
+
const minute = Math.floor(now / 60_000);
|
|
97
|
+
let entry = this.keys.get(key);
|
|
98
|
+
if (entry)
|
|
99
|
+
this.keys.delete(key); // re-inserted below: Map order is then least recently active first
|
|
100
|
+
else
|
|
101
|
+
entry = { minutes: new Array(this.windowMinutes).fill(-1), counts: new Array(this.windowMinutes).fill(0) };
|
|
102
|
+
const slot = minute % this.windowMinutes;
|
|
103
|
+
if (entry.minutes[slot] !== minute) {
|
|
104
|
+
entry.minutes[slot] = minute;
|
|
105
|
+
entry.counts[slot] = 0;
|
|
106
|
+
}
|
|
107
|
+
entry.counts[slot]++;
|
|
108
|
+
this.keys.set(key, entry);
|
|
109
|
+
if (this.keys.size > this.maxKeys)
|
|
110
|
+
this.keys.delete(this.keys.keys().next().value);
|
|
111
|
+
let total = 0;
|
|
112
|
+
for (let i = 0; i < this.windowMinutes; i++)
|
|
113
|
+
if (minute - entry.minutes[i] < this.windowMinutes)
|
|
114
|
+
total += entry.counts[i];
|
|
115
|
+
return total;
|
|
116
|
+
}
|
|
117
|
+
/** Drops keys with nothing in the window. */
|
|
118
|
+
prune(now) {
|
|
119
|
+
const minute = Math.floor(now / 60_000);
|
|
120
|
+
for (const [key, entry] of this.keys)
|
|
121
|
+
if (Math.max(...entry.minutes) <= minute - this.windowMinutes)
|
|
122
|
+
this.keys.delete(key);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export class MonitorPipeline {
|
|
126
|
+
opts;
|
|
127
|
+
seen = new Map();
|
|
128
|
+
queue = [];
|
|
129
|
+
queued = new Set();
|
|
130
|
+
authFailures = new WindowCounter(BRUTE_FORCE_WINDOW_MS / 60_000);
|
|
131
|
+
llmCalls = new WindowCounter(LLM_FLOOD_WINDOW_MS / 60_000);
|
|
132
|
+
floodBucket = -1;
|
|
133
|
+
floodCount = 0;
|
|
134
|
+
suppressed = null;
|
|
135
|
+
maintainedMinute = -1;
|
|
136
|
+
budget;
|
|
137
|
+
constructor(opts) {
|
|
138
|
+
this.opts = opts;
|
|
139
|
+
this.budget = opts.budget ?? (opts.cpuBudgetMsPerMinute ? new CpuBudget(opts.cpuBudgetMsPerMinute) : null);
|
|
140
|
+
}
|
|
141
|
+
now() { return this.opts.now?.() ?? Date.now(); }
|
|
142
|
+
get pending() { return [...this.queue]; }
|
|
143
|
+
/** Sizes of everything the pipeline keeps, for tests and diagnostics: all of them are bounded. */
|
|
144
|
+
get footprint() {
|
|
145
|
+
return { seen: this.seen.size, queue: this.queue.length, queued: this.queued.size, authFailures: this.authFailures.size, llmCalls: this.llmCalls.size };
|
|
146
|
+
}
|
|
147
|
+
/** Once a minute: forget dedupe entries and counters that can no longer matter. */
|
|
148
|
+
maintain(now) {
|
|
149
|
+
const minute = Math.floor(now / 60_000);
|
|
150
|
+
if (minute === this.maintainedMinute)
|
|
151
|
+
return;
|
|
152
|
+
this.maintainedMinute = minute;
|
|
153
|
+
const cutoff = now - 2 * DEDUPE_WINDOW_MS.light;
|
|
154
|
+
for (const [fp, entry] of this.seen)
|
|
155
|
+
if (entry.at < cutoff)
|
|
156
|
+
this.seen.delete(fp);
|
|
157
|
+
this.authFailures.prune(now);
|
|
158
|
+
this.llmCalls.prune(now);
|
|
159
|
+
if (this.queued.size > 5000)
|
|
160
|
+
this.queued.clear(); // dependency ids; a repeat is deduped by the server too
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Matches one line and queues any findings at or above the level. Returns the rule ids raised.
|
|
164
|
+
* Over the CPU budget, the line is skipped (see budget.takeSkipped()).
|
|
165
|
+
*/
|
|
166
|
+
ingestLine(line, file, options = {}) {
|
|
167
|
+
if (!this.budget)
|
|
168
|
+
return this.ingest(line, file, options);
|
|
169
|
+
return this.budget.run(() => this.ingest(line, file, options)) ?? [];
|
|
170
|
+
}
|
|
171
|
+
ingest(line, file, options) {
|
|
172
|
+
const level = this.opts.level();
|
|
173
|
+
if (!level || !line.trim())
|
|
174
|
+
return [];
|
|
175
|
+
const custom = this.opts.customDetectors?.() ?? [];
|
|
176
|
+
const source = options.source ?? lineSource(line);
|
|
177
|
+
// Prompts and model output travel as "log" findings, which every workspace version accepts.
|
|
178
|
+
const wire = source === "http" ? "http" : "log";
|
|
179
|
+
const ip = options.client ? redactLine(options.client, custom).slice(0, 80) : source === "http" ? clientIp(line) : undefined;
|
|
180
|
+
this.maintain(this.now());
|
|
181
|
+
const excerpt = options.excerpt === false ? "(the app withheld the text)" : redactLine(line, custom);
|
|
182
|
+
const raised = [];
|
|
183
|
+
const seenRules = new Set();
|
|
184
|
+
for (const rule of matchLine(line, source, options.rules)) {
|
|
185
|
+
if (!meetsLevel(rule.severity, level) || seenRules.has(rule.id))
|
|
186
|
+
continue;
|
|
187
|
+
seenRules.add(rule.id);
|
|
188
|
+
// Traffic groups by client address (one scan = one finding); app logs group by line shape.
|
|
189
|
+
this.raise(level, ip ?? normalizeForGrouping(line), {
|
|
190
|
+
ruleId: rule.id, title: ip ? `${rule.title} from ${ip}` : rule.title, severity: rule.severity,
|
|
191
|
+
category: rule.category, source: wire, file, evidence: options.excerpt === false ? `Matched "${rule.title}"; the app withheld the text.` : excerpt,
|
|
192
|
+
});
|
|
193
|
+
raised.push(rule.id);
|
|
194
|
+
}
|
|
195
|
+
if (isAuthFailure(line, source) && meetsLevel("high", level)) {
|
|
196
|
+
const key = clientIp(line) ?? "unknown address";
|
|
197
|
+
const recent = this.authFailures.add(key, this.now());
|
|
198
|
+
if (recent >= BRUTE_FORCE_THRESHOLD[level]) {
|
|
199
|
+
this.raise(level, key, {
|
|
200
|
+
ruleId: "app.auth_bruteforce", title: `Repeated failed sign-ins from ${key}`, severity: "high", category: "recon",
|
|
201
|
+
source: wire, file, evidence: `${recent} failures in 5 minutes. Latest: ${redactLine(line, custom)}`.slice(0, 600),
|
|
202
|
+
});
|
|
203
|
+
raised.push("app.auth_bruteforce");
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
// Only a known sender counts: behind a proxy that hides addresses, all traffic would look like one client.
|
|
207
|
+
if (ip && meetsLevel("high", level) && (source === "prompt" || (source === "http" && isLlmRequest(line)))) {
|
|
208
|
+
const recent = this.llmCalls.add(`${source}\0${ip}`, this.now());
|
|
209
|
+
if (recent >= LLM_FLOOD_THRESHOLD[level]) {
|
|
210
|
+
this.raise(level, ip, {
|
|
211
|
+
ruleId: "llm.denial_of_wallet", title: `Unusual volume of LLM requests from ${ip}`, severity: "high", category: "llm",
|
|
212
|
+
source: wire, file, evidence: `${recent} ${source === "prompt" ? "prompts" : "requests to LLM endpoints"} in 5 minutes. Scripted use runs up the model bill. Latest: ${excerpt}`.slice(0, 600),
|
|
213
|
+
});
|
|
214
|
+
raised.push("llm.denial_of_wallet");
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
return raised;
|
|
218
|
+
}
|
|
219
|
+
/** Queues a finding produced elsewhere (dependency checks) under its own stable id, once. */
|
|
220
|
+
addFinding(finding) {
|
|
221
|
+
const level = this.opts.level();
|
|
222
|
+
if (!level || !meetsLevel(finding.severity, level) || this.queued.has(finding.id))
|
|
223
|
+
return false;
|
|
224
|
+
this.maintain(this.now());
|
|
225
|
+
this.queued.add(finding.id);
|
|
226
|
+
this.enqueue(finding);
|
|
227
|
+
return true;
|
|
228
|
+
}
|
|
229
|
+
raise(level, groupKey, partial) {
|
|
230
|
+
const now = this.now();
|
|
231
|
+
const bucket = Math.floor(now / DEDUPE_WINDOW_MS[level]);
|
|
232
|
+
const fp = fingerprint(`${partial.ruleId}\0${groupKey}`);
|
|
233
|
+
const existing = this.seen.get(fp);
|
|
234
|
+
if (existing && existing.bucket === bucket) {
|
|
235
|
+
existing.finding.count++; // still queued: the batch carries the new count
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
const flood = Math.floor(now / FLOOD_WINDOW_MS);
|
|
239
|
+
if (flood !== this.floodBucket) {
|
|
240
|
+
this.emitSuppressed();
|
|
241
|
+
this.floodBucket = flood;
|
|
242
|
+
this.floodCount = 0;
|
|
243
|
+
}
|
|
244
|
+
if (this.floodCount >= MAX_NEW_FINDINGS_PER_10_MIN) {
|
|
245
|
+
const worst = this.suppressed && RANK[this.suppressed.severity] >= RANK[partial.severity] ? this.suppressed.severity : partial.severity;
|
|
246
|
+
this.suppressed = { count: (this.suppressed?.count ?? 0) + 1, severity: worst };
|
|
247
|
+
return;
|
|
248
|
+
}
|
|
249
|
+
this.floodCount++;
|
|
250
|
+
const finding = { id: `${fp}-${bucket}`, ...partial, count: 1, firstSeen: new Date(now).toISOString() };
|
|
251
|
+
this.seen.set(fp, { bucket, finding, at: now });
|
|
252
|
+
this.enqueue(finding);
|
|
253
|
+
}
|
|
254
|
+
emitSuppressed() {
|
|
255
|
+
if (!this.suppressed)
|
|
256
|
+
return;
|
|
257
|
+
const { count, severity } = this.suppressed;
|
|
258
|
+
this.suppressed = null;
|
|
259
|
+
this.enqueue({
|
|
260
|
+
id: `flood-${this.floodBucket}`, ruleId: "beam.monitor.flood", title: "Too many findings to send individually", severity,
|
|
261
|
+
category: "integrity", source: "log", count,
|
|
262
|
+
evidence: `${count} more findings within 10 minutes were not sent one by one, to protect the monthly allowance. Check this server's logs.`,
|
|
263
|
+
firstSeen: new Date(this.floodBucket * FLOOD_WINDOW_MS).toISOString(),
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
enqueue(finding) {
|
|
267
|
+
if (this.queue.length >= MAX_QUEUE) {
|
|
268
|
+
// Full (usually: the workspace is unreachable). Make room by dropping the least severe, oldest.
|
|
269
|
+
let drop = 0;
|
|
270
|
+
for (let i = 1; i < this.queue.length; i++)
|
|
271
|
+
if (RANK[this.queue[i].severity] < RANK[this.queue[drop].severity])
|
|
272
|
+
drop = i;
|
|
273
|
+
this.queue.splice(drop, 1);
|
|
274
|
+
}
|
|
275
|
+
this.queue.push(finding);
|
|
276
|
+
}
|
|
277
|
+
/** Sends queued findings in batches; a "retry" outcome keeps the batch for the next flush. */
|
|
278
|
+
async flush(send) {
|
|
279
|
+
if (this.suppressed && Math.floor(this.now() / FLOOD_WINDOW_MS) !== this.floodBucket)
|
|
280
|
+
this.emitSuppressed();
|
|
281
|
+
while (this.queue.length) {
|
|
282
|
+
const batch = this.queue.splice(0, BATCH_SIZE);
|
|
283
|
+
if ((await send(batch)) === "retry") {
|
|
284
|
+
this.queue.unshift(...batch);
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
this.maintain(this.now());
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
//# sourceMappingURL=monitor-pipeline.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"monitor-pipeline.js","sourceRoot":"","sources":["../src/monitor-pipeline.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,YAAY,EAAE,UAAU,EAAE,SAAS,EAAE,UAAU,EAAqC,MAAM,oBAAoB,CAAC;AAEjJ,2GAA2G;AAC3G,MAAM,UAAU,WAAW,CAAC,IAAY;IACtC,MAAM,CAAC,GAAG,CAAC,IAAY,EAAE,EAAE;QACzB,IAAI,EAAE,GAAG,UAAU,GAAG,IAAI,EAAE,EAAE,GAAG,UAAU,GAAG,IAAI,CAAC;QACnD,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,EAAE,EAAE,CAAC;YACrC,MAAM,EAAE,GAAG,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC;YAC9B,EAAE,GAAG,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,EAAE,UAAU,CAAC,CAAC;YACpC,EAA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E,IAAI,IAAI,CAAC,UAAU,IAAI,IAAI,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,EAAE,GAAG,eAAe,CAAC,KAAK,IAAI,CAAC,WAAW;YAAE,IAAI,CAAC,cAAc,EAAE,CAAC;QAC5G,OAAO,IAAI,CAAC,KAAK,CAAC,MAAM,EAAE,CAAC;YACzB,MAAM,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC,EAAE,UAAU,CAAC,CAAC;YAC/C,IAAI,CAAC,MAAM,IAAI,CAAC,KAAK,CAAC,CAAC,KAAK,OAAO,EAAE,CAAC;gBACpC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,KAAK,CAAC,CAAC;gBAC7B,MAAM;YACR,CAAC;QACH,CAAC;QACD,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,CAAC;IAC5B,CAAC;CACF"}
|
package/dist/monitor-rules.d.ts
CHANGED
|
@@ -1,13 +1,14 @@
|
|
|
1
|
-
import { type RuleCategory } from "./
|
|
1
|
+
import { type RuleCategory } from "./rule-catalog.js";
|
|
2
2
|
import type { CustomDetector } from "./data-detectors.js";
|
|
3
3
|
import type { MonitorSeverity } from "./monitor-client.js";
|
|
4
|
-
|
|
4
|
+
/** log/http: lines from log files and traffic. prompt/output: text going into, or coming out of, an LLM (SDK). */
|
|
5
|
+
export type LineSource = "log" | "http" | "prompt" | "output";
|
|
5
6
|
export interface MonitorRule {
|
|
6
7
|
id: string;
|
|
7
8
|
title: string;
|
|
8
9
|
severity: MonitorSeverity;
|
|
9
|
-
category: RuleCategory | "exploit";
|
|
10
|
-
/**
|
|
10
|
+
category: RuleCategory | "exploit" | "llm";
|
|
11
|
+
/** Where the rule applies; omitted means log and http lines (never prompts or model output). */
|
|
11
12
|
sources?: LineSource[];
|
|
12
13
|
/** `decoded` is URL-decoded (twice, for double encoding); `raw` is the line as written. */
|
|
13
14
|
match: (decoded: string, raw: string) => boolean;
|
|
@@ -16,7 +17,8 @@ export declare const monitorRules: MonitorRule[];
|
|
|
16
17
|
export declare function lineSource(line: string): LineSource;
|
|
17
18
|
/** URL-decodes up to twice so `%252e%252e%252f` (double-encoded `../`) is matched too. */
|
|
18
19
|
export declare function decodeForMatch(line: string): string;
|
|
19
|
-
|
|
20
|
+
/** The rules a line matches. Prompts and model output aren't URL-decoded: they aren't URLs. */
|
|
21
|
+
export declare function matchLine(line: string, source?: LineSource, extra?: MonitorRule[]): MonitorRule[];
|
|
20
22
|
/**
|
|
21
23
|
* Redaction for evidence that leaves the machine. Unlike the agent-hook redaction it keeps query
|
|
22
24
|
* strings -- for an access log the query usually *is* the attack -- but removes credentials,
|
|
@@ -28,5 +30,7 @@ export declare function redactLine(line: string, custom?: CustomDetector[]): str
|
|
|
28
30
|
* nginx/Apache/Caddy common log format, otherwise the first standalone IPv4 address.
|
|
29
31
|
*/
|
|
30
32
|
export declare function clientIp(line: string): string | undefined;
|
|
33
|
+
/** A POST to what looks like an LLM endpoint (chat, completions, generate…), each one a paid model call. */
|
|
34
|
+
export declare function isLlmRequest(line: string): boolean;
|
|
31
35
|
/** A failed sign-in: a 401/403 on an auth path in an access log, or an app's own "login failed" line. */
|
|
32
36
|
export declare function isAuthFailure(line: string, source: LineSource): boolean;
|
package/dist/monitor-rules.js
CHANGED
|
@@ -2,11 +2,49 @@
|
|
|
2
2
|
// HTTP access logs. Separate from the agent-hook catalog in core.ts on purpose -- application logs
|
|
3
3
|
// are full of words like "env", "token" and "production" that would drown those rules -- but a few
|
|
4
4
|
// built-in rules that mean the same thing anywhere (reverse shells, piped remote code, cloud
|
|
5
|
-
// metadata access
|
|
6
|
-
|
|
5
|
+
// metadata access) are reused as-is. Attacks on LLM features (prompt injection and friends) come
|
|
6
|
+
// from prompt-attacks.ts. Heuristic, like everything else in Beam.
|
|
7
|
+
import { builtinRulesById } from "./rule-catalog.js";
|
|
8
|
+
import { promptAttack, revealHidden } from "./prompt-attacks.js";
|
|
7
9
|
// Trailing boundary for shell commands, so `| id=5` in a log line isn't read as running `id`.
|
|
8
10
|
const END = String.raw `(?=$|[\s;'"&|)\x60])`;
|
|
9
11
|
const commandInjection = new RegExp(String.raw `(?:[;|\x60]|\$\(|&&)\s*(?:id|whoami|uname\s+-a|cat\s+/etc/(?:passwd|shadow)|(?:curl|wget)\s+\S+|nc\s+-\w|bash\s+-[ic]|sh\s+-c|ping\s+-c\s*\d+)${END}`, "i");
|
|
12
|
+
// A URL-like parameter pointing the server at itself, its network or its cloud metadata, or a
|
|
13
|
+
// scheme only an attacker wants fetched. LLM agents with browsing or fetch tools are a common way
|
|
14
|
+
// in. Parameters are split and checked one by one: as one regex this backtracked quadratically on
|
|
15
|
+
// long hostile query strings, and it runs inside customer apps.
|
|
16
|
+
const SSRF_NAME = /url|uri|link|src|dest|target|redirect|callback|webhook|feed|host|endpoint|proxy|image|fetch|next|return/i;
|
|
17
|
+
const SSRF_SCHEME = /^(?:gopher|dict|ldap|tftp|jar|netdoc):\/\//i;
|
|
18
|
+
const SSRF_INTERNAL = /^(?:(?:https?|ftp):\/\/(?:localhost|127\.\d+\.\d+\.\d+|0\.0\.0\.0|0x7f\w*|2130706433|\[::(?:1|ffff:[^\]]{0,40})?\]|10\.\d+\.\d+\.\d+|192\.168\.|172\.(?:1[6-9]|2\d|3[01])\.|169\.254\.|100\.100\.100\.200|metadata\.google\.internal)|file:\/)/i;
|
|
19
|
+
function ssrf(s) {
|
|
20
|
+
const query = s.indexOf("?");
|
|
21
|
+
if (query < 0)
|
|
22
|
+
return false;
|
|
23
|
+
let seen = 0;
|
|
24
|
+
for (const param of s.slice(query + 1).split(/[&?\s"]/)) {
|
|
25
|
+
const eq = param.indexOf("=");
|
|
26
|
+
if (eq < 0)
|
|
27
|
+
continue;
|
|
28
|
+
if (++seen > 100)
|
|
29
|
+
break;
|
|
30
|
+
const name = param.slice(0, eq);
|
|
31
|
+
const value = param.slice(eq + 1);
|
|
32
|
+
if (SSRF_SCHEME.test(value) || (name.length <= 64 && SSRF_NAME.test(name) && SSRF_INTERNAL.test(value)))
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
return false;
|
|
36
|
+
}
|
|
37
|
+
// Credentials, by format. LLM provider keys are worth the most to an attacker right now.
|
|
38
|
+
const SECRET = /-----BEGIN [A-Z ]*PRIVATE KEY-----|\b(?:sk-(?:proj-|ant-|or-v1-)?[A-Za-z0-9_-]{20,}|gh[pousr]_[A-Za-z0-9_]{30,}|github_pat_[A-Za-z0-9_]{20,}|AKIA[A-Z0-9]{16}|xox[baprs]-[\w-]{10,}|AIza[0-9A-Za-z_-]{35}|gsk_[A-Za-z0-9]{40,}|hf_[A-Za-z0-9]{30,}|r8_[A-Za-z0-9]{30,}|xai-[A-Za-z0-9]{40,}|pplx-[A-Za-z0-9]{40,}|beam_app_[\w-]{20,})\b|\bauthorization:\s*bearer\s+[A-Za-z0-9._~+/-]{20,}/i;
|
|
39
|
+
const XSS = /<script[\s>]|javascript:\s*[a-z(]|<(?:img|svg|body|iframe)\b[^>]{0,200}\son(?:error|load)\s*=|\bdocument\.cookie\b/i;
|
|
40
|
+
const EVERYWHERE = ["log", "http", "prompt", "output"];
|
|
41
|
+
const SENSITIVE_FILE = /\/(?:\.env(?:\.[\w-]+)?|\.git\/(?:config|HEAD)|\.aws\/credentials|\.ssh\/id_\w+|wp-config\.php(?:\.bak)?|\.htpasswd|server-status|actuator\/(?:env|heapdump)|\.DS_Store)(?=[\s?"'#]|$)/i;
|
|
42
|
+
const ACCESS_STATUS = /"[A-Z]+ \S+ HTTP\/[\d.]+" (\d{3})\b|"(?:status|httpStatus|status_code|statusCode|response_status)"\s*:\s*"?(\d{3})\b/;
|
|
43
|
+
/** The response status in an access-log line was 2xx. */
|
|
44
|
+
function isSuccess(line) {
|
|
45
|
+
const m = line.match(ACCESS_STATUS);
|
|
46
|
+
return (m?.[1] ?? m?.[2] ?? "").startsWith("2");
|
|
47
|
+
}
|
|
10
48
|
const appRules = [
|
|
11
49
|
{ id: "web.jndi_injection", title: "JNDI lookup (Log4Shell-style) injection", severity: "critical", category: "exploit",
|
|
12
50
|
match: s => /\$\{\s*jndi\s*:|\$\{\s*\$\{\s*(?:::-|lower:|upper:|env:)/i.test(s) },
|
|
@@ -22,20 +60,61 @@ const appRules = [
|
|
|
22
60
|
match: s => /\/(?:c99|r57|wso|b374k|webshell|cmd|shell)\.(?:php|jsp|aspx?)\b|[?&](?:cmd|exec|command)=(?:whoami|id|ls|uname|cat)\b/i.test(s) },
|
|
23
61
|
{ id: "web.deserialization", title: "Serialized object payload", severity: "high", category: "exploit",
|
|
24
62
|
match: s => /\brO0AB[A-Za-z0-9+/=]{8,}|\bO:\d+:"[A-Za-z_\\]+":\d+:\{/.test(s) },
|
|
63
|
+
{ id: "web.ssrf", title: "Server-side request forgery attempt", severity: "high", category: "exploit", sources: ["http"],
|
|
64
|
+
match: s => ssrf(s) },
|
|
25
65
|
{ id: "app.secret_in_log", title: "Credential written to logs", severity: "high", category: "secrets",
|
|
26
|
-
match: (_s, raw) =>
|
|
66
|
+
match: (_s, raw) => SECRET.test(raw) },
|
|
27
67
|
{ id: "web.xss", title: "Cross-site scripting attempt", severity: "medium", category: "exploit",
|
|
28
|
-
match: s =>
|
|
68
|
+
match: s => XSS.test(s) },
|
|
29
69
|
{ id: "web.prototype_pollution", title: "Prototype pollution attempt", severity: "medium", category: "exploit",
|
|
30
70
|
match: s => /\b__proto__\b\s*[\]["'=:.]|constructor\s*\[\s*["']?prototype/i.test(s) },
|
|
71
|
+
// Bots ask every site for /.env all day, so a probe alone is medium. One that got a 2xx back
|
|
72
|
+
// may have handed over the app's keys: high. (An app that answers 200 to every path, like a
|
|
73
|
+
// single-page app's catch-all, can trip the second.)
|
|
31
74
|
{ id: "web.sensitive_file_probe", title: "Probe for secrets or config files", severity: "medium", category: "recon", sources: ["http"],
|
|
32
|
-
match: s =>
|
|
75
|
+
match: s => SENSITIVE_FILE.test(s) },
|
|
76
|
+
{ id: "web.secret_file_exposed", title: "Secret or config file served to a client", severity: "high", category: "secrets", sources: ["http"],
|
|
77
|
+
match: (s, raw) => SENSITIVE_FILE.test(s) && isSuccess(raw) },
|
|
33
78
|
{ id: "web.scanner", title: "Known attack tool", severity: "medium", category: "recon", sources: ["http"],
|
|
34
79
|
match: s => /\b(?:sqlmap|nikto|nuclei|masscan|zgrab|acunetix|nessus|wpscan|dirbuster|gobuster|feroxbuster|ffuf|hydra|openvas|commix|xsstrike)\b/i.test(s) },
|
|
35
80
|
];
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
81
|
+
// Attacks on the app's LLM features. Prompt-side rules also read traffic and logs, because
|
|
82
|
+
// prompts arrive in query strings and apps log them; forged chat markers only count where a
|
|
83
|
+
// client wrote them (a model server's own logs are full of its template).
|
|
84
|
+
const llmRules = [
|
|
85
|
+
{ id: "llm.prompt_injection", title: "Prompt injection attempt", severity: "high", category: "llm", sources: EVERYWHERE,
|
|
86
|
+
match: s => promptAttack.override(s) },
|
|
87
|
+
{ id: "llm.reviewer_manipulation", title: "Attempt to sway an AI reviewer", severity: "high", category: "llm", sources: EVERYWHERE,
|
|
88
|
+
match: s => promptAttack.reviewer(s) },
|
|
89
|
+
{ id: "llm.hidden_instructions", title: "Hidden instructions in invisible Unicode", severity: "high", category: "llm", sources: EVERYWHERE,
|
|
90
|
+
match: s => promptAttack.hidden(s) },
|
|
91
|
+
{ id: "llm.chat_template_injection", title: "Forged system or chat-template markers", severity: "high", category: "llm", sources: ["http", "prompt"],
|
|
92
|
+
match: s => promptAttack.chatTemplate(s) },
|
|
93
|
+
{ id: "llm.secret_extraction", title: "Attempt to extract API keys or secrets", severity: "high", category: "llm", sources: ["log"],
|
|
94
|
+
match: s => promptAttack.secretRequest(s) },
|
|
95
|
+
{ id: "llm.secret_extraction", title: "Attempt to extract API keys or secrets", severity: "high", category: "llm", sources: ["http", "prompt"],
|
|
96
|
+
match: s => promptAttack.secretRequest(s) || promptAttack.secretCommand(s) },
|
|
97
|
+
{ id: "llm.jailbreak", title: "Jailbreak attempt", severity: "medium", category: "llm", sources: ["log", "http", "prompt"],
|
|
98
|
+
match: s => promptAttack.jailbreak(s) },
|
|
99
|
+
{ id: "llm.prompt_extraction", title: "System prompt or data extraction attempt", severity: "medium", category: "llm", sources: ["log", "http", "prompt"],
|
|
100
|
+
match: s => promptAttack.extraction(s) },
|
|
101
|
+
{ id: "llm.output_exfiltration", title: "Data exfiltration link in model output", severity: "high", category: "llm", sources: ["log"],
|
|
102
|
+
match: (_s, raw) => promptAttack.markdownExfil(raw) },
|
|
103
|
+
{ id: "llm.output_exfiltration", title: "Data exfiltration link in model output", severity: "high", category: "llm", sources: ["output"],
|
|
104
|
+
match: (_s, raw) => promptAttack.htmlExfil(raw) },
|
|
105
|
+
{ id: "llm.output_secret", title: "Credential in model output", severity: "high", category: "llm", sources: ["output"],
|
|
106
|
+
match: (_s, raw) => SECRET.test(raw) || promptAttack.envSecret(raw) },
|
|
107
|
+
{ id: "llm.output_script", title: "Script or active HTML in model output", severity: "medium", category: "llm", sources: ["output"],
|
|
108
|
+
match: (_s, raw) => XSS.test(raw) },
|
|
109
|
+
];
|
|
110
|
+
const sharedRules = builtinRulesById(["network.reverse-shell", "execution.remote", "execution.obfuscated", "network.metadata"])
|
|
111
|
+
.map(r => ({
|
|
112
|
+
id: r.id, title: r.title, severity: r.severity === "info" ? "medium" : r.severity, category: r.category, match: (s) => r.match(s),
|
|
113
|
+
// An agent asked to fetch the metadata endpoint is the same attack as a request for it.
|
|
114
|
+
...(r.id === "network.metadata" ? { sources: EVERYWHERE } : {}),
|
|
115
|
+
}));
|
|
116
|
+
export const monitorRules = [...appRules, ...llmRules, ...sharedRules];
|
|
117
|
+
const CLASSIC = ["log", "http"];
|
|
39
118
|
const HTTP_LINE = /"(?:GET|POST|PUT|DELETE|PATCH|HEAD|OPTIONS|CONNECT|TRACE) \S+ HTTP\/[\d.]+"|^\S+ \S+ \S+ \[[^\]]+\] "|"(?:method|http_method|request_method)"\s*:\s*"[A-Z]+"/;
|
|
40
119
|
export function lineSource(line) {
|
|
41
120
|
return HTTP_LINE.test(line) ? "http" : "log";
|
|
@@ -59,10 +138,11 @@ export function decodeForMatch(line) {
|
|
|
59
138
|
}
|
|
60
139
|
return s;
|
|
61
140
|
}
|
|
62
|
-
|
|
63
|
-
|
|
141
|
+
/** The rules a line matches. Prompts and model output aren't URL-decoded: they aren't URLs. */
|
|
142
|
+
export function matchLine(line, source = lineSource(line), extra = []) {
|
|
64
143
|
const raw = line.length > 16_384 ? line.slice(0, 16_384) : line;
|
|
65
|
-
|
|
144
|
+
const decoded = source === "prompt" || source === "output" ? raw : decodeForMatch(line);
|
|
145
|
+
return [...monitorRules, ...extra].filter(r => (r.sources ?? CLASSIC).includes(source) && r.match(decoded, raw));
|
|
66
146
|
}
|
|
67
147
|
/**
|
|
68
148
|
* Redaction for evidence that leaves the machine. Unlike the agent-hook redaction it keeps query
|
|
@@ -70,9 +150,10 @@ export function matchLine(line, source = lineSource(line)) {
|
|
|
70
150
|
* bearer tokens, URL userinfo, email addresses and the policy's custom detector matches.
|
|
71
151
|
*/
|
|
72
152
|
export function redactLine(line, custom = []) {
|
|
73
|
-
|
|
153
|
+
// Hidden Unicode text is made readable first, so it is redacted like the rest and a reviewer can see it.
|
|
154
|
+
let out = revealHidden(line.slice(0, 4000))
|
|
74
155
|
.replace(/-----BEGIN [^-]*PRIVATE KEY-----[\s\S]*?(?:-----END [^-]*PRIVATE KEY-----|$)/g, "[REDACTED PRIVATE KEY]")
|
|
75
|
-
.replace(/\b(?:sk-(?:proj-|ant-)?[A-Za-z0-9_-]{12,}|gh[pousr]_[A-Za-z0-9_]{16,}|github_pat_[A-Za-z0-9_]{16,}|AKIA[A-Z0-9]{16}|xox[baprs]-[\w-]{10,}|AIza[0-9A-Za-z_-]{35}|beam_app_[\w-]{20,})\b/g, "[REDACTED TOKEN]")
|
|
156
|
+
.replace(/\b(?:sk-(?:proj-|ant-|or-v1-)?[A-Za-z0-9_-]{12,}|gh[pousr]_[A-Za-z0-9_]{16,}|github_pat_[A-Za-z0-9_]{16,}|AKIA[A-Z0-9]{16}|xox[baprs]-[\w-]{10,}|AIza[0-9A-Za-z_-]{35}|gsk_[A-Za-z0-9]{20,}|hf_[A-Za-z0-9]{20,}|r8_[A-Za-z0-9]{20,}|xai-[A-Za-z0-9]{20,}|pplx-[A-Za-z0-9]{20,}|beam_app_[\w-]{20,})\b/g, "[REDACTED TOKEN]")
|
|
76
157
|
.replace(/(bearer\s+)[A-Za-z0-9._~+/-]{8,}/gi, "$1[REDACTED]")
|
|
77
158
|
.replace(/(\b(?:[\w-]*(?:api[_-]?key|token|secret|password|passwd|pwd|authorization|session|cookie)[\w-]*)["']?\s*[:=]\s*)["']?[^\s,"';&}\]]+/gi, "$1[REDACTED]")
|
|
78
159
|
.replace(/(https?:\/\/)[^\s/@]+:[^\s/@]+@/gi, "$1[REDACTED]@")
|
|
@@ -99,8 +180,21 @@ export function clientIp(line) {
|
|
|
99
180
|
}
|
|
100
181
|
const AUTH_PATH = /\/(?:login|signin|sign-in|auth|oauth|token|session|api\/v\d+\/auth|wp-login\.php|admin)\b/i;
|
|
101
182
|
const AUTH_FAIL_TEXT = /\b(?:login|authentication|auth|sign[- ]?in)\s+(?:failed|failure|denied)|\binvalid\s+(?:password|credentials|username or password|api key|token)|\bfailed\s+password\b|\bincorrect\s+password\b|\bunauthori[sz]ed\b/i;
|
|
183
|
+
const REQUEST_LINE = /"([A-Z]+) (\S+) HTTP\/[\d.]+"/;
|
|
184
|
+
const JSON_METHOD = /"(?:method|http_method|request_method)"\s*:\s*"([A-Z]+)"/;
|
|
185
|
+
const JSON_PATH = /"(?:path|uri|url|request_uri|requestPath)"\s*:\s*"([^"]+)"/;
|
|
186
|
+
const LLM_PATH = /\/(?:chat|completions?|generate|llm|ai|assistant|agents?|ask|prompt|inference|copilot)(?:[/?#]|$)|\/v1\/(?:messages|responses|embeddings)(?:[/?#]|$)/i;
|
|
187
|
+
/** A POST to what looks like an LLM endpoint (chat, completions, generate…), each one a paid model call. */
|
|
188
|
+
export function isLlmRequest(line) {
|
|
189
|
+
const request = line.match(REQUEST_LINE);
|
|
190
|
+
const method = request?.[1] ?? line.match(JSON_METHOD)?.[1];
|
|
191
|
+
const path = request?.[2] ?? line.match(JSON_PATH)?.[1];
|
|
192
|
+
return method === "POST" && Boolean(path && LLM_PATH.test(path.split("?")[0]));
|
|
193
|
+
}
|
|
102
194
|
/** A failed sign-in: a 401/403 on an auth path in an access log, or an app's own "login failed" line. */
|
|
103
195
|
export function isAuthFailure(line, source) {
|
|
196
|
+
if (source === "prompt" || source === "output")
|
|
197
|
+
return false; // a chat message saying "login failed" isn't one
|
|
104
198
|
if (source === "http")
|
|
105
199
|
return (/"\s(?:401|403)\s/.test(line) || /"(?:status|httpStatus|status_code|statusCode|response_status)"\s*:\s*"?(?:401|403)\b/.test(line)) && AUTH_PATH.test(line);
|
|
106
200
|
return AUTH_FAIL_TEXT.test(line);
|