agentshield-sdk 7.0.0

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.
Files changed (84) hide show
  1. package/CHANGELOG.md +191 -0
  2. package/LICENSE +21 -0
  3. package/README.md +975 -0
  4. package/bin/agent-shield.js +680 -0
  5. package/package.json +118 -0
  6. package/src/adaptive.js +330 -0
  7. package/src/agent-protocol.js +998 -0
  8. package/src/alert-tuning.js +480 -0
  9. package/src/allowlist.js +603 -0
  10. package/src/audit-immutable.js +914 -0
  11. package/src/audit-streaming.js +469 -0
  12. package/src/badges.js +196 -0
  13. package/src/behavior-profiling.js +289 -0
  14. package/src/benchmark-harness.js +804 -0
  15. package/src/canary.js +271 -0
  16. package/src/certification.js +563 -0
  17. package/src/circuit-breaker.js +321 -0
  18. package/src/compliance.js +617 -0
  19. package/src/confidence-tuning.js +324 -0
  20. package/src/confused-deputy.js +624 -0
  21. package/src/context-scoring.js +360 -0
  22. package/src/conversation.js +494 -0
  23. package/src/cost-optimizer.js +1024 -0
  24. package/src/ctf.js +462 -0
  25. package/src/detector-core.js +1999 -0
  26. package/src/distributed.js +359 -0
  27. package/src/document-scanner.js +795 -0
  28. package/src/embedding.js +307 -0
  29. package/src/encoding.js +429 -0
  30. package/src/enterprise.js +405 -0
  31. package/src/errors.js +100 -0
  32. package/src/eu-ai-act.js +523 -0
  33. package/src/fuzzer.js +764 -0
  34. package/src/honeypot.js +328 -0
  35. package/src/i18n-patterns.js +523 -0
  36. package/src/index.js +430 -0
  37. package/src/integrations.js +528 -0
  38. package/src/llm-redteam.js +670 -0
  39. package/src/main.js +741 -0
  40. package/src/main.mjs +38 -0
  41. package/src/mcp-bridge.js +542 -0
  42. package/src/mcp-certification.js +846 -0
  43. package/src/mcp-sdk-integration.js +355 -0
  44. package/src/mcp-security-runtime.js +741 -0
  45. package/src/mcp-server.js +740 -0
  46. package/src/middleware.js +208 -0
  47. package/src/model-finetuning.js +884 -0
  48. package/src/model-fingerprint.js +1042 -0
  49. package/src/multi-agent-trust.js +453 -0
  50. package/src/multi-agent.js +404 -0
  51. package/src/multimodal.js +296 -0
  52. package/src/nist-mapping.js +505 -0
  53. package/src/observability.js +330 -0
  54. package/src/openclaw.js +450 -0
  55. package/src/otel.js +544 -0
  56. package/src/owasp-2025.js +483 -0
  57. package/src/pii.js +390 -0
  58. package/src/plugin-marketplace.js +628 -0
  59. package/src/plugin-system.js +349 -0
  60. package/src/policy-dsl.js +775 -0
  61. package/src/policy-extended.js +635 -0
  62. package/src/policy.js +443 -0
  63. package/src/presets.js +409 -0
  64. package/src/production.js +557 -0
  65. package/src/prompt-leakage.js +321 -0
  66. package/src/rag-vulnerability.js +579 -0
  67. package/src/redteam.js +475 -0
  68. package/src/response-handler.js +429 -0
  69. package/src/scanners.js +357 -0
  70. package/src/self-healing.js +363 -0
  71. package/src/semantic.js +339 -0
  72. package/src/shield-score.js +250 -0
  73. package/src/sso-saml.js +897 -0
  74. package/src/stream-scanner.js +806 -0
  75. package/src/testing.js +505 -0
  76. package/src/threat-encyclopedia.js +629 -0
  77. package/src/threat-intel-network.js +1017 -0
  78. package/src/token-analysis.js +467 -0
  79. package/src/tool-guard.js +412 -0
  80. package/src/tool-output-validator.js +354 -0
  81. package/src/utils.js +83 -0
  82. package/src/watermark.js +235 -0
  83. package/src/worker-scanner.js +601 -0
  84. package/types/index.d.ts +2088 -0
@@ -0,0 +1,208 @@
1
+ 'use strict';
2
+
3
+ /**
4
+ * Agent Shield Middleware
5
+ *
6
+ * Plug-and-play middleware for common agent frameworks.
7
+ * Wraps agent input/output pipelines with automatic threat scanning.
8
+ */
9
+
10
+ const { AgentShield } = require('./index');
11
+
12
+ /**
13
+ * Creates an Express/Connect-style middleware that scans request bodies
14
+ * for AI-specific threats before they reach your agent endpoint.
15
+ *
16
+ * @param {object} [config] - AgentShield configuration.
17
+ * @returns {Function} Express middleware function.
18
+ *
19
+ * @example
20
+ * const express = require('express');
21
+ * const { expressMiddleware } = require('agent-shield/src/middleware');
22
+ *
23
+ * const app = express();
24
+ * app.use(express.json());
25
+ * app.use(expressMiddleware({ blockOnThreat: true, blockThreshold: 'high' }));
26
+ *
27
+ * app.post('/agent', (req, res) => {
28
+ * // req.agentShield contains scan results
29
+ * if (req.agentShield.blocked) {
30
+ * return res.status(400).json({ error: 'Input blocked for safety' });
31
+ * }
32
+ * // ... process the agent request
33
+ * });
34
+ */
35
+ const expressMiddleware = (config = {}) => {
36
+ const shield = new AgentShield({ blockOnThreat: true, ...config });
37
+
38
+ return (req, res, next) => {
39
+ if (!req.body) {
40
+ req.agentShield = { status: 'safe', threats: [], blocked: false };
41
+ return next();
42
+ }
43
+
44
+ // Extract text from common request body shapes
45
+ const text = extractTextFromBody(req.body);
46
+
47
+ if (!text) {
48
+ req.agentShield = { status: 'safe', threats: [], blocked: false };
49
+ return next();
50
+ }
51
+
52
+ const result = shield.scanInput(text, { source: 'http_request' });
53
+ req.agentShield = result;
54
+
55
+ if (result.blocked) {
56
+ return res.status(400).json({
57
+ error: 'Input blocked by Agent Shield',
58
+ status: result.status,
59
+ threats: result.threats.map(t => ({
60
+ severity: t.severity,
61
+ description: t.description
62
+ }))
63
+ });
64
+ }
65
+
66
+ next();
67
+ };
68
+ };
69
+
70
+ /**
71
+ * Creates a wrapper function that scans input/output around any async function.
72
+ * Works with any agent framework — just wrap your agent's main function.
73
+ *
74
+ * @param {Function} agentFn - The agent function to wrap. Should accept (input) and return output.
75
+ * @param {object} [config] - AgentShield configuration.
76
+ * @returns {Function} Wrapped function with the same signature.
77
+ *
78
+ * @example
79
+ * const { wrapAgent } = require('agent-shield/src/middleware');
80
+ *
81
+ * async function myAgent(input) {
82
+ * const response = await callLLM(input);
83
+ * return response;
84
+ * }
85
+ *
86
+ * const protectedAgent = wrapAgent(myAgent, {
87
+ * blockOnThreat: true,
88
+ * logging: true
89
+ * });
90
+ *
91
+ * // Use it the same way
92
+ * const result = await protectedAgent('Hello, how are you?');
93
+ */
94
+ const wrapAgent = (agentFn, config = {}) => {
95
+ const shield = new AgentShield({ blockOnThreat: true, ...config });
96
+
97
+ return async (input, ...rest) => {
98
+ // Scan input
99
+ const inputText = typeof input === 'string' ? input : JSON.stringify(input);
100
+ const inputResult = shield.scanInput(inputText, { source: 'agent_input' });
101
+
102
+ if (inputResult.blocked) {
103
+ return {
104
+ blocked: true,
105
+ reason: 'Input blocked by Agent Shield',
106
+ threats: inputResult.threats,
107
+ output: null
108
+ };
109
+ }
110
+
111
+ // Run the agent
112
+ const output = await agentFn(input, ...rest);
113
+
114
+ // Scan output
115
+ const outputText = typeof output === 'string' ? output : JSON.stringify(output);
116
+ const outputResult = shield.scanOutput(outputText, { source: 'agent_output' });
117
+
118
+ if (outputResult.blocked) {
119
+ return {
120
+ blocked: true,
121
+ reason: 'Output blocked by Agent Shield',
122
+ threats: outputResult.threats,
123
+ output: null
124
+ };
125
+ }
126
+
127
+ return {
128
+ blocked: false,
129
+ threats: [...inputResult.threats, ...outputResult.threats],
130
+ output
131
+ };
132
+ };
133
+ };
134
+
135
+ /**
136
+ * Creates a tool-call interceptor that scans tool calls before execution.
137
+ *
138
+ * @param {object} tools - Map of tool name -> tool function.
139
+ * @param {object} [config] - AgentShield configuration.
140
+ * @returns {object} Map of tool name -> wrapped tool function.
141
+ *
142
+ * @example
143
+ * const { shieldTools } = require('agent-shield/src/middleware');
144
+ *
145
+ * const tools = {
146
+ * bash: async (args) => exec(args.command),
147
+ * readFile: async (args) => fs.readFile(args.path, 'utf-8'),
148
+ * };
149
+ *
150
+ * const protectedTools = shieldTools(tools, {
151
+ * blockOnThreat: true,
152
+ * logging: true
153
+ * });
154
+ *
155
+ * // Use protectedTools in your agent — dangerous calls get blocked
156
+ */
157
+ const shieldTools = (tools, config = {}) => {
158
+ const shield = new AgentShield({ blockOnThreat: true, ...config });
159
+ const wrapped = {};
160
+
161
+ for (const [name, fn] of Object.entries(tools)) {
162
+ wrapped[name] = async (args, ...rest) => {
163
+ const result = shield.scanToolCall(name, args);
164
+
165
+ if (result.blocked) {
166
+ const error = new Error(
167
+ `[Agent Shield] Tool call "${name}" blocked: ${result.threats.map(t => t.description).join('; ')}`
168
+ );
169
+ error.agentShield = result;
170
+ throw error;
171
+ }
172
+
173
+ return fn(args, ...rest);
174
+ };
175
+ }
176
+
177
+ return wrapped;
178
+ };
179
+
180
+ /**
181
+ * Extracts scannable text from common request body formats.
182
+ * @param {object} body
183
+ * @returns {string|null}
184
+ */
185
+ const extractTextFromBody = (body) => {
186
+ if (!body || (typeof body !== 'object' && typeof body !== 'string')) return null;
187
+ if (typeof body === 'string') return body;
188
+
189
+ // OpenAI-style messages array
190
+ if (body.messages && Array.isArray(body.messages)) {
191
+ return body.messages
192
+ .map(m => typeof m.content === 'string' ? m.content : JSON.stringify(m.content))
193
+ .join('\n');
194
+ }
195
+
196
+ // Single message/prompt field
197
+ if (body.message) return typeof body.message === 'string' ? body.message : JSON.stringify(body.message);
198
+ if (body.prompt) return typeof body.prompt === 'string' ? body.prompt : JSON.stringify(body.prompt);
199
+ if (body.input) return typeof body.input === 'string' ? body.input : JSON.stringify(body.input);
200
+ if (body.query) return typeof body.query === 'string' ? body.query : JSON.stringify(body.query);
201
+ if (body.text) return typeof body.text === 'string' ? body.text : JSON.stringify(body.text);
202
+
203
+ // Fallback: stringify the whole body
204
+ const str = JSON.stringify(body);
205
+ return str.length > 20 ? str : null;
206
+ };
207
+
208
+ module.exports = { expressMiddleware, wrapAgent, shieldTools, extractTextFromBody };