shield-llm 0.1.1 → 0.2.1

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.
@@ -14762,6 +14762,26 @@ function resolveEnvVar(value) {
14762
14762
  }
14763
14763
 
14764
14764
  // ../../packages/scanner-core/src/http-target-client.ts
14765
+ function encodeRequestBody(body, encoding) {
14766
+ if (encoding === "form") {
14767
+ const params = new URLSearchParams();
14768
+ for (const [k, v] of Object.entries(body)) {
14769
+ params.append(k, typeof v === "string" ? v : JSON.stringify(v));
14770
+ }
14771
+ return {
14772
+ body: params.toString(),
14773
+ contentType: "application/x-www-form-urlencoded"
14774
+ };
14775
+ }
14776
+ if (encoding === "multipart") {
14777
+ const form = new FormData();
14778
+ for (const [k, v] of Object.entries(body)) {
14779
+ form.append(k, typeof v === "string" ? v : JSON.stringify(v));
14780
+ }
14781
+ return { body: form, contentType: null };
14782
+ }
14783
+ return { body: JSON.stringify(body), contentType: "application/json" };
14784
+ }
14765
14785
  var PRIVATE_HOSTNAME_PATTERNS = [
14766
14786
  /^localhost$/i,
14767
14787
  /^127\./,
@@ -14904,12 +14924,21 @@ var HttpTargetClient = class {
14904
14924
  sessionId
14905
14925
  });
14906
14926
  const headers = this.buildHeaders(auth);
14927
+ const { body: encodedBody, contentType } = encodeRequestBody(
14928
+ body,
14929
+ this.config.request.encoding
14930
+ );
14931
+ if (contentType === null) {
14932
+ delete headers["Content-Type"];
14933
+ } else {
14934
+ headers["Content-Type"] = contentType;
14935
+ }
14907
14936
  return fetchWithTimeout(
14908
14937
  this.config.url,
14909
14938
  {
14910
14939
  method: this.config.request.method ?? "POST",
14911
14940
  headers,
14912
- body: JSON.stringify(body),
14941
+ body: encodedBody,
14913
14942
  signal: options?.abortSignal
14914
14943
  },
14915
14944
  this.config.timeoutMs ?? 3e4,
@@ -15246,8 +15275,11 @@ async function collectSSE(response) {
15246
15275
  if (typeof parsed === "string") {
15247
15276
  result += parsed;
15248
15277
  } else {
15249
- const chunk = parsed.message ?? parsed.content ?? parsed.delta?.content ?? parsed.text ?? parsed.choices?.[0]?.delta?.content ?? parsed.data;
15250
- if (chunk != null) result += String(chunk);
15278
+ const chunk = parsed.message ?? parsed.content ?? parsed.delta?.content ?? parsed.text ?? parsed.choices?.[0]?.delta?.content ?? parsed.data?.message ?? // nested: {"data":{"message":"…"}} (e.g. ChatGot)
15279
+ parsed.data?.content ?? parsed.data;
15280
+ if (chunk != null) {
15281
+ result += typeof chunk === "string" ? chunk : JSON.stringify(chunk);
15282
+ }
15251
15283
  }
15252
15284
  } catch {
15253
15285
  result += payload;
@@ -23324,7 +23356,12 @@ var AuthConfigSchema = z29.discriminatedUnion("type", [
23324
23356
  var RequestTemplateSchema = z29.object({
23325
23357
  method: z29.enum(["POST", "PUT", "PATCH"]).optional(),
23326
23358
  body: z29.record(z29.unknown()),
23327
- headers: z29.record(z29.string()).optional()
23359
+ headers: z29.record(z29.string()).optional(),
23360
+ // How to encode the outgoing body. Default "json" (JSON.stringify +
23361
+ // application/json). "form" = application/x-www-form-urlencoded, "multipart"
23362
+ // = multipart/form-data (Content-Type is left to fetch so the boundary is
23363
+ // set). Real chatbots often expect form encodings (e.g. FastAPI `Form(...)`).
23364
+ encoding: z29.enum(["json", "form", "multipart"]).optional()
23328
23365
  });
23329
23366
  var ResponseTemplateSchema = z29.object({
23330
23367
  field: z29.string().min(1),
@@ -24270,6 +24307,7 @@ export {
24270
24307
  deserializeAttack,
24271
24308
  deserializeMultiTurnAttack,
24272
24309
  calculateComplianceScore,
24310
+ createTargetClient,
24273
24311
  Scanner,
24274
24312
  ConfigSchema,
24275
24313
  loadConfig,
package/dist/cli.js CHANGED
@@ -6,6 +6,7 @@ import {
6
6
  analyzeJudgeHealth,
7
7
  buildTargetConfig,
8
8
  calculateComplianceScore,
9
+ createTargetClient,
9
10
  deserializeAttack,
10
11
  deserializeMultiTurnAttack,
11
12
  evaluateThresholds,
@@ -16,7 +17,7 @@ import {
16
17
  generatePdf,
17
18
  loadConfig,
18
19
  loadSystemPrompt
19
- } from "./chunk-XTU2RLZM.js";
20
+ } from "./chunk-LH6W4P2C.js";
20
21
  import "./chunk-7D5WVZW5.js";
21
22
 
22
23
  // src/cli.ts
@@ -1086,23 +1087,41 @@ async function scanCommand(opts) {
1086
1087
  }
1087
1088
  const systemPrompt = loadSystemPrompt(config);
1088
1089
  const presetForScan = config.preset ?? "standard";
1089
- const includeCrescendo = config.includeCrescendo ?? config.preset === "full";
1090
- const fetched = await fetchAttacks({
1091
- apiUrl: credentials.apiUrl,
1092
- apiKey: credentials.apiKey,
1093
- preset: presetForScan,
1094
- includeMultiTurn: includeCrescendo
1095
- });
1096
- const multiTurnNote = fetched.multiTurnAttacks.length > 0 ? ` + ${fetched.multiTurnAttacks.length} multi-turn` : "";
1097
- info(
1098
- `Loaded ${fetched.attacks.length} attacks${multiTurnNote} from ${fetched.source} (catalog ${fetched.catalogVersion})`
1099
- );
1090
+ const customOnly = Boolean(opts.customOnly);
1091
+ const includeCrescendo = customOnly ? false : config.includeCrescendo ?? config.preset === "full";
1092
+ let attacks = [];
1093
+ let multiTurnAttacks = [];
1094
+ if (customOnly) {
1095
+ if (!config.customTests || config.customTests.length === 0) {
1096
+ console.error(
1097
+ "--custom-only needs at least one custom test (config.customTests or --load-custom-tests)."
1098
+ );
1099
+ setExitCode(2);
1100
+ return;
1101
+ }
1102
+ info(
1103
+ `Custom-only mode: ${config.customTests.length} custom test(s), no preset attacks sent.`
1104
+ );
1105
+ } else {
1106
+ const fetched = await fetchAttacks({
1107
+ apiUrl: credentials.apiUrl,
1108
+ apiKey: credentials.apiKey,
1109
+ preset: presetForScan,
1110
+ includeMultiTurn: includeCrescendo
1111
+ });
1112
+ attacks = fetched.attacks;
1113
+ multiTurnAttacks = fetched.multiTurnAttacks;
1114
+ const multiTurnNote = fetched.multiTurnAttacks.length > 0 ? ` + ${fetched.multiTurnAttacks.length} multi-turn` : "";
1115
+ info(
1116
+ `Loaded ${fetched.attacks.length} attacks${multiTurnNote} from ${fetched.source} (catalog ${fetched.catalogVersion})`
1117
+ );
1118
+ }
1100
1119
  const scanOptions = {
1101
1120
  target: buildTargetConfig(config),
1102
1121
  systemPrompt,
1103
1122
  preset: presetForScan,
1104
- attacks: fetched.attacks,
1105
- multiTurnAttacks: fetched.multiTurnAttacks,
1123
+ attacks,
1124
+ multiTurnAttacks,
1106
1125
  includeCrescendo,
1107
1126
  customTests: config.customTests,
1108
1127
  concurrency: config.concurrency,
@@ -1213,6 +1232,50 @@ async function scanCommand(opts) {
1213
1232
  }
1214
1233
  }
1215
1234
 
1235
+ // src/commands/test-connection.ts
1236
+ var DEFAULT_MESSAGE = "Hello! This is a Shield LLM connectivity test \u2014 please reply in one short sentence.";
1237
+ async function testConnectionCommand(opts) {
1238
+ let config;
1239
+ try {
1240
+ config = loadConfig({ configPath: opts.config });
1241
+ } catch (e) {
1242
+ console.error(
1243
+ e instanceof ConfigError ? e.message : `Failed to load config: ${String(e)}`
1244
+ );
1245
+ setExitCode(2);
1246
+ return;
1247
+ }
1248
+ const target = buildTargetConfig(config);
1249
+ const systemPrompt = loadSystemPrompt(config);
1250
+ const message = opts.message ?? DEFAULT_MESSAGE;
1251
+ const where = target.mode === "endpoint" ? target.url : `${target.provider}/${target.model ?? "default"}`;
1252
+ console.log(`Target: ${where}`);
1253
+ if (target.mode === "endpoint" && target.request.encoding) {
1254
+ console.log(`Encoding: ${target.request.encoding}`);
1255
+ }
1256
+ console.log(`Sending benign test message: "${message}"
1257
+ `);
1258
+ try {
1259
+ const client = createTargetClient(target, systemPrompt);
1260
+ const start = Date.now();
1261
+ const reply = await client.sendMessage(message);
1262
+ const ms = Date.now() - start;
1263
+ console.log(`\u2713 Connection OK (${ms}ms). Chatbot replied:
1264
+ `);
1265
+ console.log(reply.trim().slice(0, 2e3) || "(empty response)");
1266
+ console.log(
1267
+ "\nConfig looks good \u2014 the target is reachable and the response parses. You can run a scan against it."
1268
+ );
1269
+ } catch (e) {
1270
+ const msg = e instanceof Error ? e.message : String(e);
1271
+ console.error(`\u2716 Connection failed: ${msg}`);
1272
+ console.error(
1273
+ "\nCheck: endpoint URL, request.encoding (json/form/multipart), auth, and response.field."
1274
+ );
1275
+ setExitCode(3);
1276
+ }
1277
+ }
1278
+
1216
1279
  // src/commands/tests.ts
1217
1280
  async function testsCommand(opts) {
1218
1281
  let localTests = [];
@@ -1344,7 +1407,7 @@ process.on("uncaughtException", (err) => {
1344
1407
  });
1345
1408
  var program = new Command();
1346
1409
  program.exitOverride();
1347
- program.name("shield-llm").description("AI chatbot security scanner \u2014 automated red teaming for LLMs").version("0.1.1");
1410
+ program.name("shield-llm").description("AI chatbot security scanner \u2014 automated red teaming for LLMs").version("0.2.1");
1348
1411
  program.command("scan").description("Run a security scan against a target chatbot").option("-c, --config <path>", "Path to shield.config.json").option("-p, --provider <name>", "Target provider (openai, mistral)").option("-m, --model <name>", "Target model name").option("-e, --endpoint <url>", "Target chatbot HTTP endpoint URL").option("--auth <type>", "Auth type: bearer, api-key, oauth2").option("--token <value>", "Bearer token or API key value").option("--auth-header <name>", "Custom auth header name (for api-key auth)").option(
1349
1412
  "--response-field <path>",
1350
1413
  "Dot-notation path to response text in JSON (default: response)"
@@ -1363,6 +1426,9 @@ program.command("scan").description("Run a security scan against a target chatbo
1363
1426
  ).option(
1364
1427
  "--load-custom-tests",
1365
1428
  "Fetch custom tests from your Shield LLM dashboard"
1429
+ ).option(
1430
+ "--custom-only",
1431
+ "Run ONLY custom tests \u2014 send no preset attack payloads to the target"
1366
1432
  ).option("--eu-ai-act", "Show EU AI Act compliance assessment after scan").option(
1367
1433
  "--compliance-threshold <n>",
1368
1434
  "Minimum compliance score (0-100)",
@@ -1379,6 +1445,12 @@ program.command("tests").description("List custom tests (local config and/or rem
1379
1445
  "Backend API URL (overrides SHIELD_API_URL env var)"
1380
1446
  ).action(testsCommand);
1381
1447
  program.command("validate").description("Validate a shield.config.json file").option("-c, --config <path>", "Path to config file").action(validateCommand);
1448
+ program.command("test-connection").description(
1449
+ "Send one benign message to the configured chatbot and print its reply (config plumbing check, no scan)"
1450
+ ).option("-c, --config <path>", "Path to config file").option(
1451
+ "-m, --message <text>",
1452
+ "Benign message to send (default: a greeting)"
1453
+ ).action(testConnectionCommand);
1382
1454
  program.command("report <scanId>").description("Fetch a past scan report from your dashboard").option("-o, --output <format>", "Output format (json, markdown)", "json").option("--output-file <path>", "Write report to file").option("--api-url <url>", "Backend API URL override").action(reportCommand);
1383
1455
  program.command("login").description("Authenticate with your Shield LLM API key").requiredOption("--key <api-key>", "Your API key (sk_shield_...)").option(
1384
1456
  "--api-url <url>",
package/dist/index.js CHANGED
@@ -9,7 +9,7 @@ import {
9
9
  generatePdf,
10
10
  loadConfig,
11
11
  loadSystemPrompt
12
- } from "./chunk-XTU2RLZM.js";
12
+ } from "./chunk-LH6W4P2C.js";
13
13
  import "./chunk-7D5WVZW5.js";
14
14
  export {
15
15
  ConfigError,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "shield-llm",
3
- "version": "0.1.1",
3
+ "version": "0.2.1",
4
4
  "description": "AI chatbot security scanner — automated red teaming for LLMs",
5
5
  "type": "module",
6
6
  "publishConfig": {