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.
- package/CHANGELOG.md +191 -0
- package/LICENSE +21 -0
- package/README.md +975 -0
- package/bin/agent-shield.js +680 -0
- package/package.json +118 -0
- package/src/adaptive.js +330 -0
- package/src/agent-protocol.js +998 -0
- package/src/alert-tuning.js +480 -0
- package/src/allowlist.js +603 -0
- package/src/audit-immutable.js +914 -0
- package/src/audit-streaming.js +469 -0
- package/src/badges.js +196 -0
- package/src/behavior-profiling.js +289 -0
- package/src/benchmark-harness.js +804 -0
- package/src/canary.js +271 -0
- package/src/certification.js +563 -0
- package/src/circuit-breaker.js +321 -0
- package/src/compliance.js +617 -0
- package/src/confidence-tuning.js +324 -0
- package/src/confused-deputy.js +624 -0
- package/src/context-scoring.js +360 -0
- package/src/conversation.js +494 -0
- package/src/cost-optimizer.js +1024 -0
- package/src/ctf.js +462 -0
- package/src/detector-core.js +1999 -0
- package/src/distributed.js +359 -0
- package/src/document-scanner.js +795 -0
- package/src/embedding.js +307 -0
- package/src/encoding.js +429 -0
- package/src/enterprise.js +405 -0
- package/src/errors.js +100 -0
- package/src/eu-ai-act.js +523 -0
- package/src/fuzzer.js +764 -0
- package/src/honeypot.js +328 -0
- package/src/i18n-patterns.js +523 -0
- package/src/index.js +430 -0
- package/src/integrations.js +528 -0
- package/src/llm-redteam.js +670 -0
- package/src/main.js +741 -0
- package/src/main.mjs +38 -0
- package/src/mcp-bridge.js +542 -0
- package/src/mcp-certification.js +846 -0
- package/src/mcp-sdk-integration.js +355 -0
- package/src/mcp-security-runtime.js +741 -0
- package/src/mcp-server.js +740 -0
- package/src/middleware.js +208 -0
- package/src/model-finetuning.js +884 -0
- package/src/model-fingerprint.js +1042 -0
- package/src/multi-agent-trust.js +453 -0
- package/src/multi-agent.js +404 -0
- package/src/multimodal.js +296 -0
- package/src/nist-mapping.js +505 -0
- package/src/observability.js +330 -0
- package/src/openclaw.js +450 -0
- package/src/otel.js +544 -0
- package/src/owasp-2025.js +483 -0
- package/src/pii.js +390 -0
- package/src/plugin-marketplace.js +628 -0
- package/src/plugin-system.js +349 -0
- package/src/policy-dsl.js +775 -0
- package/src/policy-extended.js +635 -0
- package/src/policy.js +443 -0
- package/src/presets.js +409 -0
- package/src/production.js +557 -0
- package/src/prompt-leakage.js +321 -0
- package/src/rag-vulnerability.js +579 -0
- package/src/redteam.js +475 -0
- package/src/response-handler.js +429 -0
- package/src/scanners.js +357 -0
- package/src/self-healing.js +363 -0
- package/src/semantic.js +339 -0
- package/src/shield-score.js +250 -0
- package/src/sso-saml.js +897 -0
- package/src/stream-scanner.js +806 -0
- package/src/testing.js +505 -0
- package/src/threat-encyclopedia.js +629 -0
- package/src/threat-intel-network.js +1017 -0
- package/src/token-analysis.js +467 -0
- package/src/tool-guard.js +412 -0
- package/src/tool-output-validator.js +354 -0
- package/src/utils.js +83 -0
- package/src/watermark.js +235 -0
- package/src/worker-scanner.js +601 -0
- 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 };
|