shield-llm 0.6.0 → 0.6.2
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/dist/{chunk-6AYERWP2.js → chunk-3ZVL7ZFC.js} +73 -9
- package/dist/cli.js +40 -3
- package/dist/index.js +1 -1
- package/package.json +1 -1
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { createRequire as __cjs_createRequire } from "module"; import { fileURLToPath as __cjs_fileURLToPath } from "url"; import { dirname as __cjs_dirname } from "path"; const __filename = __cjs_fileURLToPath(import.meta.url); const __dirname = __cjs_dirname(__filename); const require = __cjs_createRequire(import.meta.url);
|
|
2
2
|
|
|
3
3
|
// package.json
|
|
4
|
-
var version = "0.6.
|
|
4
|
+
var version = "0.6.2";
|
|
5
5
|
|
|
6
6
|
// src/config/schema.ts
|
|
7
7
|
import { z as z2 } from "zod";
|
|
@@ -328,6 +328,15 @@ var RATE_LIMIT_PATTERNS = [
|
|
|
328
328
|
pattern: /\b(quota|usage cap)\b.*\b(exceeded|reached|exhausted|hit)\b/i,
|
|
329
329
|
platform: "generic"
|
|
330
330
|
},
|
|
331
|
+
{
|
|
332
|
+
// Credit-based exhaustion (e.g. ChatGot free tier replies "No credits
|
|
333
|
+
// left." for EVERY message once the per-device_id allowance runs out).
|
|
334
|
+
// Without this the scanner reads that as a normal answer and scores an
|
|
335
|
+
// out-of-credits bot as "secure" across the whole suite (false negatives),
|
|
336
|
+
// the same failure mode the soft-throttle classifier exists to prevent.
|
|
337
|
+
pattern: /\b(no|out of|insufficient|zero|0)\s+credits?\b|\bcredits?\s+(have\s+run\s+out|ran\s+out|run\s+out|exhausted|depleted|used\s+up)\b/i,
|
|
338
|
+
platform: "generic"
|
|
339
|
+
},
|
|
331
340
|
{
|
|
332
341
|
pattern: /\blimite de (requ[eê]tes|taux)\b.*\b(attendez|r[eé]essayez|d[eé]pass[eé]e)\b/i,
|
|
333
342
|
platform: "generic"
|
|
@@ -5665,6 +5674,33 @@ async function readBodyWithLimit(response) {
|
|
|
5665
5674
|
result += decoder.decode();
|
|
5666
5675
|
return result;
|
|
5667
5676
|
}
|
|
5677
|
+
function extractSseChunk(parsed) {
|
|
5678
|
+
const p = parsed;
|
|
5679
|
+
if ((p.type === "response.output_text.delta" || p.type === "text-delta") && typeof p.delta === "string") {
|
|
5680
|
+
return p.delta;
|
|
5681
|
+
}
|
|
5682
|
+
const parts = p.candidates?.[0]?.content?.parts;
|
|
5683
|
+
if (Array.isArray(parts)) {
|
|
5684
|
+
const joined = parts.map((part) => typeof part?.text === "string" ? part.text : "").join("");
|
|
5685
|
+
if (joined) return joined;
|
|
5686
|
+
}
|
|
5687
|
+
const candidate = (
|
|
5688
|
+
// {message:"…"} simple wrappers, OR Ollama native {message:{content:"…"}}
|
|
5689
|
+
(typeof p.message === "string" ? p.message : p.message?.content) ?? p.content ?? // {content:"…"}; LangServe /stream AIMessageChunk
|
|
5690
|
+
p.delta?.content ?? // top-level delta object {delta:{content}}
|
|
5691
|
+
p.delta?.text ?? // Anthropic content_block_delta (text_delta)
|
|
5692
|
+
p.delta?.message?.content?.text ?? // Cohere v2 content-delta
|
|
5693
|
+
p.text ?? // {text:"…"}
|
|
5694
|
+
p.choices?.[0]?.delta?.content ?? // OpenAI/Mistral/Ollama-compat chat chunk
|
|
5695
|
+
p.token?.text ?? // HF TGI /generate_stream
|
|
5696
|
+
p.response ?? // Ollama native /api/generate (top-level string)
|
|
5697
|
+
p.data?.message ?? // nested {data:{message}}
|
|
5698
|
+
p.data?.content ?? // nested {data:{content}} (ChatGot)
|
|
5699
|
+
p.data?.chunk?.content ?? // LangChain /stream_events on_chat_model_stream
|
|
5700
|
+
(typeof p.data === "string" ? p.data : void 0)
|
|
5701
|
+
);
|
|
5702
|
+
return typeof candidate === "string" ? candidate : void 0;
|
|
5703
|
+
}
|
|
5668
5704
|
async function collectSSE(response) {
|
|
5669
5705
|
if (!response.body) {
|
|
5670
5706
|
throw new HttpTargetError("SSE response has no body", 0);
|
|
@@ -5691,18 +5727,27 @@ async function collectSSE(response) {
|
|
|
5691
5727
|
const lines = buffer.split("\n");
|
|
5692
5728
|
buffer = lines.pop();
|
|
5693
5729
|
for (const line of lines) {
|
|
5694
|
-
if (!line.startsWith("data:
|
|
5695
|
-
const payload = line.slice(
|
|
5696
|
-
if (payload === "
|
|
5730
|
+
if (!line.startsWith("data:")) continue;
|
|
5731
|
+
const payload = line.slice(5).trim();
|
|
5732
|
+
if (payload === "") continue;
|
|
5733
|
+
if (payload === "[DONE]") {
|
|
5734
|
+
await reader.cancel().catch(() => {
|
|
5735
|
+
});
|
|
5736
|
+
return result;
|
|
5737
|
+
}
|
|
5697
5738
|
try {
|
|
5698
5739
|
const parsed = JSON.parse(payload);
|
|
5699
5740
|
if (typeof parsed === "string") {
|
|
5700
5741
|
result += parsed;
|
|
5701
5742
|
} else {
|
|
5702
|
-
const chunk = parsed
|
|
5703
|
-
|
|
5704
|
-
|
|
5705
|
-
|
|
5743
|
+
const chunk = extractSseChunk(parsed);
|
|
5744
|
+
if (chunk === "[DONE]") {
|
|
5745
|
+
await reader.cancel().catch(() => {
|
|
5746
|
+
});
|
|
5747
|
+
return result;
|
|
5748
|
+
}
|
|
5749
|
+
if (typeof chunk === "string") {
|
|
5750
|
+
result += chunk;
|
|
5706
5751
|
}
|
|
5707
5752
|
}
|
|
5708
5753
|
} catch {
|
|
@@ -5823,6 +5868,11 @@ var Scanner = class {
|
|
|
5823
5868
|
// from a hard quota/ban. Both reset at the start of run().
|
|
5824
5869
|
this.currentDelayMs = 0;
|
|
5825
5870
|
this.throttleFloorStreak = 0;
|
|
5871
|
+
// Pool-global not-before floor (ms epoch). Every send path waits until this
|
|
5872
|
+
// before issuing a turn, so a single observed Retry-After / throttle backoff
|
|
5873
|
+
// quiets the WHOLE worker pool — not just the worker that hit it. 0 = no pause
|
|
5874
|
+
// (the default and the no-throttle hot path, so it's a no-op when idle).
|
|
5875
|
+
this.pauseUntil = 0;
|
|
5826
5876
|
this.targetClient = createTargetClient(
|
|
5827
5877
|
options.target,
|
|
5828
5878
|
options.systemPrompt
|
|
@@ -5900,6 +5950,7 @@ var Scanner = class {
|
|
|
5900
5950
|
let rateLimited = false;
|
|
5901
5951
|
this.currentDelayMs = this.options.delayMs;
|
|
5902
5952
|
this.throttleFloorStreak = 0;
|
|
5953
|
+
this.pauseUntil = 0;
|
|
5903
5954
|
const singleResults = new Array(
|
|
5904
5955
|
attacks.length
|
|
5905
5956
|
);
|
|
@@ -6406,6 +6457,10 @@ var Scanner = class {
|
|
|
6406
6457
|
async sendTurnWithRetry(session, prompt, attackId) {
|
|
6407
6458
|
let attempt = 0;
|
|
6408
6459
|
for (; ; ) {
|
|
6460
|
+
const pause = this.pauseUntil - Date.now();
|
|
6461
|
+
if (pause > 0) {
|
|
6462
|
+
await sleep(pause + Math.floor(Math.random() * PAUSE_JITTER_MS));
|
|
6463
|
+
}
|
|
6409
6464
|
try {
|
|
6410
6465
|
return await session.sendTurn(prompt, {
|
|
6411
6466
|
abortSignal: AbortSignal.timeout(this.options.attackTimeoutMs),
|
|
@@ -6425,7 +6480,10 @@ var Scanner = class {
|
|
|
6425
6480
|
waitMs,
|
|
6426
6481
|
reason: error instanceof RateLimitError ? "rate_limited" : "server_error"
|
|
6427
6482
|
});
|
|
6428
|
-
if (error instanceof RateLimitError)
|
|
6483
|
+
if (error instanceof RateLimitError) {
|
|
6484
|
+
this.aimdDecrease();
|
|
6485
|
+
this.pauseUntil = Math.max(this.pauseUntil, Date.now() + waitMs);
|
|
6486
|
+
}
|
|
6429
6487
|
await sleep(waitMs);
|
|
6430
6488
|
}
|
|
6431
6489
|
}
|
|
@@ -6578,6 +6636,7 @@ var Scanner = class {
|
|
|
6578
6636
|
this.onEvent?.(event);
|
|
6579
6637
|
}
|
|
6580
6638
|
};
|
|
6639
|
+
var PAUSE_JITTER_MS = 250;
|
|
6581
6640
|
function isRetryable(error) {
|
|
6582
6641
|
if (error instanceof RateLimitError) return true;
|
|
6583
6642
|
if (error instanceof HttpTargetError) {
|
|
@@ -6705,6 +6764,11 @@ var ConfigSchema = z2.object({
|
|
|
6705
6764
|
maxRateLimitRetries: z2.number().int().min(0).max(10).optional(),
|
|
6706
6765
|
rateLimitBackoffMs: z2.number().int().min(0).optional(),
|
|
6707
6766
|
rateLimitMaxBackoffMs: z2.number().int().min(0).optional(),
|
|
6767
|
+
// Adaptive-pacing knobs. These existed in ScanOptions but were unreachable —
|
|
6768
|
+
// absent from this schema (stripped by Zod) AND from the scan.ts passthrough —
|
|
6769
|
+
// so they always fell back to the scanner defaults (5000 / 3). Now settable.
|
|
6770
|
+
maxDelayMs: z2.number().int().min(0).optional(),
|
|
6771
|
+
maxFloorThrottles: z2.number().int().min(1).optional(),
|
|
6708
6772
|
thresholds: ThresholdSchema.optional(),
|
|
6709
6773
|
output: OutputSchema.optional()
|
|
6710
6774
|
});
|
package/dist/cli.js
CHANGED
|
@@ -29,7 +29,7 @@ import {
|
|
|
29
29
|
resolveOfflinePosition,
|
|
30
30
|
summarizeAttackErrors,
|
|
31
31
|
version
|
|
32
|
-
} from "./chunk-
|
|
32
|
+
} from "./chunk-3ZVL7ZFC.js";
|
|
33
33
|
|
|
34
34
|
// src/load-env.ts
|
|
35
35
|
try {
|
|
@@ -38,7 +38,7 @@ try {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
// src/cli.ts
|
|
41
|
-
import { Command, CommanderError } from "commander";
|
|
41
|
+
import { Command, CommanderError, InvalidArgumentError } from "commander";
|
|
42
42
|
|
|
43
43
|
// src/credentials.ts
|
|
44
44
|
import {
|
|
@@ -2191,6 +2191,14 @@ async function scanCommand(opts) {
|
|
|
2191
2191
|
...opts.outputFile ? { file: opts.outputFile } : {}
|
|
2192
2192
|
};
|
|
2193
2193
|
}
|
|
2194
|
+
if (opts.concurrency !== void 0)
|
|
2195
|
+
cliOverrides.concurrency = opts.concurrency;
|
|
2196
|
+
if (opts.delay !== void 0) cliOverrides.delayMs = opts.delay;
|
|
2197
|
+
if (opts.maxDelay !== void 0) cliOverrides.maxDelayMs = opts.maxDelay;
|
|
2198
|
+
if (opts.retries !== void 0)
|
|
2199
|
+
cliOverrides.maxRateLimitRetries = opts.retries;
|
|
2200
|
+
if (opts.maxFloorThrottles !== void 0)
|
|
2201
|
+
cliOverrides.maxFloorThrottles = opts.maxFloorThrottles;
|
|
2194
2202
|
const config = loadConfig({
|
|
2195
2203
|
configPath: opts.config,
|
|
2196
2204
|
cliOverrides
|
|
@@ -2303,6 +2311,8 @@ async function scanCommand(opts) {
|
|
|
2303
2311
|
maxRateLimitRetries: config.maxRateLimitRetries,
|
|
2304
2312
|
rateLimitBackoffMs: config.rateLimitBackoffMs,
|
|
2305
2313
|
rateLimitMaxBackoffMs: config.rateLimitMaxBackoffMs,
|
|
2314
|
+
maxDelayMs: config.maxDelayMs,
|
|
2315
|
+
maxFloorThrottles: config.maxFloorThrottles,
|
|
2306
2316
|
checkpoint: checkpointStore,
|
|
2307
2317
|
evaluator
|
|
2308
2318
|
};
|
|
@@ -2612,6 +2622,13 @@ function validateCommand(opts) {
|
|
|
2612
2622
|
}
|
|
2613
2623
|
|
|
2614
2624
|
// src/cli.ts
|
|
2625
|
+
function parseIntArg(value) {
|
|
2626
|
+
const n = Number.parseInt(value, 10);
|
|
2627
|
+
if (Number.isNaN(n)) {
|
|
2628
|
+
throw new InvalidArgumentError("Expected an integer.");
|
|
2629
|
+
}
|
|
2630
|
+
return n;
|
|
2631
|
+
}
|
|
2615
2632
|
function reportFatal(label, err) {
|
|
2616
2633
|
console.error(`
|
|
2617
2634
|
${label}:`);
|
|
@@ -2649,7 +2666,27 @@ program.command("scan").description("Run a security scan against a target chatbo
|
|
|
2649
2666
|
).option(
|
|
2650
2667
|
"--preset <name>",
|
|
2651
2668
|
"Scan preset (dev, quick, owasp, frontier, standard, full)"
|
|
2652
|
-
).option("--system-prompt <text>", "System prompt for target chatbot").option("--system-prompt-file <path>", "Path to system prompt file").option("--crescendo", "Include crescendo (multi-turn) attacks").option(
|
|
2669
|
+
).option("--system-prompt <text>", "System prompt for target chatbot").option("--system-prompt-file <path>", "Path to system prompt file").option("--crescendo", "Include crescendo (multi-turn) attacks").option(
|
|
2670
|
+
"--concurrency <n>",
|
|
2671
|
+
"Max parallel single-turn attacks (AIMD backs off on throttling; default 4, set 1 for sequential)",
|
|
2672
|
+
parseIntArg
|
|
2673
|
+
).option(
|
|
2674
|
+
"--delay <ms>",
|
|
2675
|
+
"Base inter-request delay in ms (the adaptive pacing floor)",
|
|
2676
|
+
parseIntArg
|
|
2677
|
+
).option(
|
|
2678
|
+
"--max-delay <ms>",
|
|
2679
|
+
"Ceiling for the adaptive inter-request delay in ms (default 5000)",
|
|
2680
|
+
parseIntArg
|
|
2681
|
+
).option(
|
|
2682
|
+
"--retries <n>",
|
|
2683
|
+
"Max retries on a 429 / gateway 5xx before giving up an attack (default 3)",
|
|
2684
|
+
parseIntArg
|
|
2685
|
+
).option(
|
|
2686
|
+
"--max-floor-throttles <n>",
|
|
2687
|
+
"Consecutive throttles at the pacing floor before hard-stopping (default 3)",
|
|
2688
|
+
parseIntArg
|
|
2689
|
+
).option("--min-score <n>", "Minimum score threshold", parseFloat).option("--fail-on-critical", "Fail if any CRITICAL vulnerability found").option("-o, --output <format>", "Output format (json, markdown, sarif, pdf)").option("--output-file <path>", "Write report to file").option(
|
|
2653
2690
|
"--write-json <path>",
|
|
2654
2691
|
"Always write a parseable JSON copy here, regardless of --output (useful for CI to extract score/grade alongside SARIF)"
|
|
2655
2692
|
).option(
|
package/dist/index.js
CHANGED