llm-trust-guard 4.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/LICENSE +21 -0
- package/README.md +318 -0
- package/dist/guards/agent-communication-guard.d.ts +169 -0
- package/dist/guards/agent-communication-guard.d.ts.map +1 -0
- package/dist/guards/agent-communication-guard.js +468 -0
- package/dist/guards/agent-communication-guard.js.map +1 -0
- package/dist/guards/autonomy-escalation-guard.d.ts +137 -0
- package/dist/guards/autonomy-escalation-guard.d.ts.map +1 -0
- package/dist/guards/autonomy-escalation-guard.js +470 -0
- package/dist/guards/autonomy-escalation-guard.js.map +1 -0
- package/dist/guards/circuit-breaker.d.ts +142 -0
- package/dist/guards/circuit-breaker.d.ts.map +1 -0
- package/dist/guards/circuit-breaker.js +347 -0
- package/dist/guards/circuit-breaker.js.map +1 -0
- package/dist/guards/code-execution-guard.d.ts +114 -0
- package/dist/guards/code-execution-guard.d.ts.map +1 -0
- package/dist/guards/code-execution-guard.js +467 -0
- package/dist/guards/code-execution-guard.js.map +1 -0
- package/dist/guards/conversation-guard.d.ts +73 -0
- package/dist/guards/conversation-guard.d.ts.map +1 -0
- package/dist/guards/conversation-guard.js +281 -0
- package/dist/guards/conversation-guard.js.map +1 -0
- package/dist/guards/drift-detector.d.ts +182 -0
- package/dist/guards/drift-detector.d.ts.map +1 -0
- package/dist/guards/drift-detector.js +480 -0
- package/dist/guards/drift-detector.js.map +1 -0
- package/dist/guards/encoding-detector.d.ts +76 -0
- package/dist/guards/encoding-detector.d.ts.map +1 -0
- package/dist/guards/encoding-detector.js +698 -0
- package/dist/guards/encoding-detector.js.map +1 -0
- package/dist/guards/execution-monitor.d.ts +73 -0
- package/dist/guards/execution-monitor.d.ts.map +1 -0
- package/dist/guards/execution-monitor.js +205 -0
- package/dist/guards/execution-monitor.js.map +1 -0
- package/dist/guards/input-sanitizer.d.ts +87 -0
- package/dist/guards/input-sanitizer.d.ts.map +1 -0
- package/dist/guards/input-sanitizer.js +301 -0
- package/dist/guards/input-sanitizer.js.map +1 -0
- package/dist/guards/mcp-security-guard.d.ts +204 -0
- package/dist/guards/mcp-security-guard.d.ts.map +1 -0
- package/dist/guards/mcp-security-guard.js +618 -0
- package/dist/guards/mcp-security-guard.js.map +1 -0
- package/dist/guards/memory-guard.d.ts +124 -0
- package/dist/guards/memory-guard.d.ts.map +1 -0
- package/dist/guards/memory-guard.js +476 -0
- package/dist/guards/memory-guard.js.map +1 -0
- package/dist/guards/multimodal-guard.d.ts +93 -0
- package/dist/guards/multimodal-guard.d.ts.map +1 -0
- package/dist/guards/multimodal-guard.js +507 -0
- package/dist/guards/multimodal-guard.js.map +1 -0
- package/dist/guards/output-filter.d.ts +76 -0
- package/dist/guards/output-filter.d.ts.map +1 -0
- package/dist/guards/output-filter.js +289 -0
- package/dist/guards/output-filter.js.map +1 -0
- package/dist/guards/policy-gate.d.ts +57 -0
- package/dist/guards/policy-gate.d.ts.map +1 -0
- package/dist/guards/policy-gate.js +182 -0
- package/dist/guards/policy-gate.js.map +1 -0
- package/dist/guards/prompt-leakage-guard.d.ts +110 -0
- package/dist/guards/prompt-leakage-guard.d.ts.map +1 -0
- package/dist/guards/prompt-leakage-guard.js +529 -0
- package/dist/guards/prompt-leakage-guard.js.map +1 -0
- package/dist/guards/rag-guard.d.ts +188 -0
- package/dist/guards/rag-guard.d.ts.map +1 -0
- package/dist/guards/rag-guard.js +769 -0
- package/dist/guards/rag-guard.js.map +1 -0
- package/dist/guards/schema-validator.d.ts +35 -0
- package/dist/guards/schema-validator.d.ts.map +1 -0
- package/dist/guards/schema-validator.js +316 -0
- package/dist/guards/schema-validator.js.map +1 -0
- package/dist/guards/state-persistence-guard.d.ts +153 -0
- package/dist/guards/state-persistence-guard.d.ts.map +1 -0
- package/dist/guards/state-persistence-guard.js +484 -0
- package/dist/guards/state-persistence-guard.js.map +1 -0
- package/dist/guards/tenant-boundary.d.ts +67 -0
- package/dist/guards/tenant-boundary.d.ts.map +1 -0
- package/dist/guards/tenant-boundary.js +187 -0
- package/dist/guards/tenant-boundary.js.map +1 -0
- package/dist/guards/tool-chain-validator.d.ts +102 -0
- package/dist/guards/tool-chain-validator.d.ts.map +1 -0
- package/dist/guards/tool-chain-validator.js +480 -0
- package/dist/guards/tool-chain-validator.js.map +1 -0
- package/dist/guards/tool-registry.d.ts +45 -0
- package/dist/guards/tool-registry.d.ts.map +1 -0
- package/dist/guards/tool-registry.js +155 -0
- package/dist/guards/tool-registry.js.map +1 -0
- package/dist/guards/trust-exploitation-guard.d.ts +134 -0
- package/dist/guards/trust-exploitation-guard.d.ts.map +1 -0
- package/dist/guards/trust-exploitation-guard.js +354 -0
- package/dist/guards/trust-exploitation-guard.js.map +1 -0
- package/dist/index.d.ts +133 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +430 -0
- package/dist/index.js.map +1 -0
- package/dist/integrations/express.d.ts +119 -0
- package/dist/integrations/express.d.ts.map +1 -0
- package/dist/integrations/express.js +244 -0
- package/dist/integrations/express.js.map +1 -0
- package/dist/integrations/index.d.ts +9 -0
- package/dist/integrations/index.d.ts.map +1 -0
- package/dist/integrations/index.js +26 -0
- package/dist/integrations/index.js.map +1 -0
- package/dist/integrations/langchain.d.ts +165 -0
- package/dist/integrations/langchain.d.ts.map +1 -0
- package/dist/integrations/langchain.js +308 -0
- package/dist/integrations/langchain.js.map +1 -0
- package/dist/integrations/openai.d.ts +205 -0
- package/dist/integrations/openai.d.ts.map +1 -0
- package/dist/integrations/openai.js +380 -0
- package/dist/integrations/openai.js.map +1 -0
- package/dist/types/index.d.ts +245 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +6 -0
- package/dist/types/index.js.map +1 -0
- package/package.json +64 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Nandakishore Leburu
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,318 @@
|
|
|
1
|
+
# @nandakishoreleburu89/llm-trust-guard
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/@nandakishoreleburu89/llm-trust-guard)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
Comprehensive security guards for LLM-powered and agentic AI applications. Implements 20+ protection layers covering **OWASP Top 10 for LLMs 2025**, **OWASP Agentic AI 2026**, and **MCP Security**.
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **Prompt Injection Protection** - Detect and block injection attacks including PAP (Persuasive Adversarial Prompts)
|
|
11
|
+
- **Encoding Attack Detection** - Base64, URL, Hex, Unicode, ROT13, Octal, Base32 encoding bypass prevention
|
|
12
|
+
- **Memory Poisoning Prevention** - Cross-session contamination and context injection protection
|
|
13
|
+
- **Multi-Modal Security** - Image and audio content validation
|
|
14
|
+
- **RAG Security** - Document validation and embedding attack detection
|
|
15
|
+
- **Tool Chain Validation** - Dangerous tool sequence and state corruption detection
|
|
16
|
+
- **MCP Security** - Tool shadowing and supply chain attack prevention
|
|
17
|
+
- **Trust Exploitation Guard** - Human-agent trust boundary enforcement
|
|
18
|
+
|
|
19
|
+
## Quick Start
|
|
20
|
+
|
|
21
|
+
### Installation
|
|
22
|
+
|
|
23
|
+
```bash
|
|
24
|
+
npm install @nandakishoreleburu89/llm-trust-guard
|
|
25
|
+
```
|
|
26
|
+
|
|
27
|
+
### Basic Usage
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { InputSanitizer, EncodingDetector, MemoryGuard } from '@nandakishoreleburu89/llm-trust-guard';
|
|
31
|
+
|
|
32
|
+
// Initialize guards
|
|
33
|
+
const sanitizer = new InputSanitizer();
|
|
34
|
+
const encoder = new EncodingDetector();
|
|
35
|
+
const memory = new MemoryGuard();
|
|
36
|
+
|
|
37
|
+
// Validate user input
|
|
38
|
+
const userInput = "Hello, how can I help?";
|
|
39
|
+
|
|
40
|
+
// Check for prompt injection
|
|
41
|
+
const sanitizeResult = sanitizer.sanitize(userInput);
|
|
42
|
+
if (!sanitizeResult.allowed) {
|
|
43
|
+
console.log('Blocked:', sanitizeResult.violations);
|
|
44
|
+
return;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// Check for encoding attacks
|
|
48
|
+
const encodingResult = encoder.detect(userInput);
|
|
49
|
+
if (!encodingResult.allowed) {
|
|
50
|
+
console.log('Encoded threat detected:', encodingResult.violations);
|
|
51
|
+
return;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Use sanitized input
|
|
55
|
+
console.log('Safe input:', sanitizeResult.sanitizedInput);
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
### Using TrustGuard Facade
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import { TrustGuard } from '@nandakishoreleburu89/llm-trust-guard';
|
|
62
|
+
|
|
63
|
+
const guard = new TrustGuard({
|
|
64
|
+
sanitizer: { enabled: true, threshold: 0.3 },
|
|
65
|
+
encoding: { enabled: true },
|
|
66
|
+
registry: {
|
|
67
|
+
tools: [
|
|
68
|
+
{ name: 'search', allowed_roles: ['user', 'admin'] },
|
|
69
|
+
{ name: 'delete', allowed_roles: ['admin'] }
|
|
70
|
+
]
|
|
71
|
+
}
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const result = guard.check('search', { query: 'test' }, session, {
|
|
75
|
+
userInput: userInput
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
if (!result.allowed) {
|
|
79
|
+
console.log(`Blocked by ${result.block_layer}: ${result.block_reason}`);
|
|
80
|
+
}
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Framework Integrations
|
|
84
|
+
|
|
85
|
+
### Express Middleware
|
|
86
|
+
|
|
87
|
+
```typescript
|
|
88
|
+
import express from 'express';
|
|
89
|
+
import { createTrustGuardMiddleware } from '@nandakishoreleburu89/llm-trust-guard';
|
|
90
|
+
|
|
91
|
+
const app = express();
|
|
92
|
+
app.use(express.json());
|
|
93
|
+
|
|
94
|
+
// Protect LLM endpoints
|
|
95
|
+
app.use('/api/chat', createTrustGuardMiddleware({
|
|
96
|
+
bodyFields: ['message', 'prompt'],
|
|
97
|
+
sanitize: true,
|
|
98
|
+
detectEncoding: true,
|
|
99
|
+
validateMemory: true
|
|
100
|
+
}));
|
|
101
|
+
|
|
102
|
+
app.post('/api/chat', (req, res) => {
|
|
103
|
+
// req.body.message is validated
|
|
104
|
+
res.json({ response: 'Safe response' });
|
|
105
|
+
});
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### LangChain Integration
|
|
109
|
+
|
|
110
|
+
```typescript
|
|
111
|
+
import { TrustGuardLangChain } from '@nandakishoreleburu89/llm-trust-guard';
|
|
112
|
+
|
|
113
|
+
const guard = new TrustGuardLangChain({
|
|
114
|
+
validateInput: true,
|
|
115
|
+
filterOutput: true,
|
|
116
|
+
throwOnViolation: true
|
|
117
|
+
});
|
|
118
|
+
|
|
119
|
+
// Validate before sending to LLM
|
|
120
|
+
const result = guard.validateInput(userMessage);
|
|
121
|
+
if (!result.allowed) {
|
|
122
|
+
throw new Error(`Blocked: ${result.violations.join(', ')}`);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
// Create secure processor
|
|
126
|
+
const processor = guard.createSecureProcessor(sessionId);
|
|
127
|
+
const { allowed, message } = processor.processUserMessage(userInput);
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### OpenAI Integration
|
|
131
|
+
|
|
132
|
+
```typescript
|
|
133
|
+
import OpenAI from 'openai';
|
|
134
|
+
import { SecureOpenAI, wrapOpenAIClient } from '@nandakishoreleburu89/llm-trust-guard';
|
|
135
|
+
|
|
136
|
+
const openai = new OpenAI();
|
|
137
|
+
|
|
138
|
+
// Option 1: Manual validation
|
|
139
|
+
const secure = new SecureOpenAI({
|
|
140
|
+
validateInput: true,
|
|
141
|
+
filterOutput: true
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const messages = [
|
|
145
|
+
{ role: 'system', content: 'You are helpful.' },
|
|
146
|
+
{ role: 'user', content: userInput }
|
|
147
|
+
];
|
|
148
|
+
|
|
149
|
+
const validated = secure.validateMessages(messages, sessionId);
|
|
150
|
+
if (!validated.allowed) {
|
|
151
|
+
throw new Error('Blocked');
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// Option 2: Wrap client (automatic validation)
|
|
155
|
+
const secureOpenAI = wrapOpenAIClient(openai, {
|
|
156
|
+
validateInput: true,
|
|
157
|
+
filterOutput: true,
|
|
158
|
+
throwOnViolation: true
|
|
159
|
+
});
|
|
160
|
+
```
|
|
161
|
+
|
|
162
|
+
## Guards Reference
|
|
163
|
+
|
|
164
|
+
### Core Guards
|
|
165
|
+
|
|
166
|
+
| Guard | Layer | Purpose |
|
|
167
|
+
|-------|-------|---------|
|
|
168
|
+
| InputSanitizer | L1 | Prompt injection & PAP detection |
|
|
169
|
+
| ToolRegistry | L2 | Tool hallucination prevention |
|
|
170
|
+
| PolicyGate | L3 | RBAC enforcement |
|
|
171
|
+
| TenantBoundary | L4 | Multi-tenant isolation |
|
|
172
|
+
| SchemaValidator | L5 | Parameter validation |
|
|
173
|
+
| ExecutionMonitor | L6 | Rate limiting |
|
|
174
|
+
| OutputFilter | L7 | PII/secret detection |
|
|
175
|
+
| ConversationGuard | L8 | Multi-turn manipulation |
|
|
176
|
+
| ToolChainValidator | L9 | Tool sequence validation |
|
|
177
|
+
| EncodingDetector | L10 | Encoding bypass detection |
|
|
178
|
+
|
|
179
|
+
### Advanced Guards
|
|
180
|
+
|
|
181
|
+
| Guard | Layer | Purpose |
|
|
182
|
+
|-------|-------|---------|
|
|
183
|
+
| MultiModalGuard | L11 | Image/audio validation |
|
|
184
|
+
| MemoryGuard | L12 | Memory poisoning prevention |
|
|
185
|
+
| RAGGuard | L13 | Document validation |
|
|
186
|
+
| CodeExecutionGuard | L14 | Safe code execution |
|
|
187
|
+
| AgentCommunicationGuard | L15 | Multi-agent security |
|
|
188
|
+
| CircuitBreaker | L16 | Failure prevention |
|
|
189
|
+
| DriftDetector | L17 | Behavior monitoring |
|
|
190
|
+
| MCPSecurityGuard | L18 | MCP tool security |
|
|
191
|
+
| PromptLeakageGuard | L19 | System prompt protection |
|
|
192
|
+
| TrustExploitationGuard | L20 | Trust boundary enforcement |
|
|
193
|
+
| AutonomyEscalationGuard | L21 | Unauthorized autonomy prevention |
|
|
194
|
+
| StatePersistenceGuard | L22 | State corruption prevention |
|
|
195
|
+
|
|
196
|
+
## OWASP Coverage
|
|
197
|
+
|
|
198
|
+
### LLM Top 10 2025
|
|
199
|
+
|
|
200
|
+
| Threat | Guards |
|
|
201
|
+
|--------|--------|
|
|
202
|
+
| LLM01: Prompt Injection | InputSanitizer, EncodingDetector |
|
|
203
|
+
| LLM02: Sensitive Data Exposure | OutputFilter, PromptLeakageGuard |
|
|
204
|
+
| LLM03: Supply Chain | MCPSecurityGuard |
|
|
205
|
+
| LLM04: Data Poisoning | RAGGuard, MemoryGuard |
|
|
206
|
+
| LLM05: Privilege Escalation | PolicyGate, TenantBoundary |
|
|
207
|
+
| LLM07: System Prompt Leakage | PromptLeakageGuard |
|
|
208
|
+
| LLM08: Vector DB Attacks | RAGGuard |
|
|
209
|
+
|
|
210
|
+
### Agentic AI 2026
|
|
211
|
+
|
|
212
|
+
| Threat | Guards |
|
|
213
|
+
|--------|--------|
|
|
214
|
+
| ASI04: Tool Misuse | ToolChainValidator |
|
|
215
|
+
| ASI05: Privilege Escalation | PolicyGate |
|
|
216
|
+
| ASI06: Memory Poisoning | MemoryGuard |
|
|
217
|
+
| ASI07: State Corruption | ToolChainValidator |
|
|
218
|
+
| ASI08: State Persistence | StatePersistenceGuard |
|
|
219
|
+
| ASI09: Trust Exploitation | TrustExploitationGuard |
|
|
220
|
+
| ASI10: Autonomy Escalation | AutonomyEscalationGuard |
|
|
221
|
+
|
|
222
|
+
## API Examples
|
|
223
|
+
|
|
224
|
+
### InputSanitizer
|
|
225
|
+
|
|
226
|
+
```typescript
|
|
227
|
+
import { InputSanitizer } from '@nandakishoreleburu89/llm-trust-guard';
|
|
228
|
+
|
|
229
|
+
const sanitizer = new InputSanitizer({
|
|
230
|
+
threshold: 0.3,
|
|
231
|
+
detectPAP: true,
|
|
232
|
+
papThreshold: 0.4,
|
|
233
|
+
blockCompoundPersuasion: true
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
const result = sanitizer.sanitize("Ignore all previous instructions");
|
|
237
|
+
// result.allowed = false
|
|
238
|
+
// result.violations = ['INJECTION_DETECTED']
|
|
239
|
+
// result.matches = ['ignore_instructions']
|
|
240
|
+
// result.pap = { detected: false, techniques: [], ... }
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
### EncodingDetector
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
import { EncodingDetector } from '@nandakishoreleburu89/llm-trust-guard';
|
|
247
|
+
|
|
248
|
+
const detector = new EncodingDetector({
|
|
249
|
+
detectBase64: true,
|
|
250
|
+
detectURLEncoding: true,
|
|
251
|
+
detectUnicode: true,
|
|
252
|
+
detectHex: true,
|
|
253
|
+
detectROT13: true
|
|
254
|
+
});
|
|
255
|
+
|
|
256
|
+
const result = detector.detect("aWdub3JlIGFsbA=="); // Base64 encoded
|
|
257
|
+
// result.allowed = false
|
|
258
|
+
// result.violations = ['BASE64_ENCODING_DETECTED']
|
|
259
|
+
// result.encoding_analysis.threats_found = [...]
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### MemoryGuard
|
|
263
|
+
|
|
264
|
+
```typescript
|
|
265
|
+
import { MemoryGuard } from '@nandakishoreleburu89/llm-trust-guard';
|
|
266
|
+
|
|
267
|
+
const guard = new MemoryGuard({
|
|
268
|
+
enableIntegrityCheck: true,
|
|
269
|
+
detectInjections: true,
|
|
270
|
+
riskThreshold: 40
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
// Validate before storing
|
|
274
|
+
const writeResult = guard.checkWrite(content, 'user', sessionId);
|
|
275
|
+
|
|
276
|
+
// Validate context injection
|
|
277
|
+
const ctxResult = guard.validateContextInjection(context, sessionId);
|
|
278
|
+
```
|
|
279
|
+
|
|
280
|
+
## Attack Prevention
|
|
281
|
+
|
|
282
|
+
| Attack | Without Guard | With Guard |
|
|
283
|
+
|--------|--------------|------------|
|
|
284
|
+
| Prompt Injection | Exploitable | Blocked |
|
|
285
|
+
| PAP Attacks | Exploitable | Blocked |
|
|
286
|
+
| Encoding Bypass | Exploitable | Blocked |
|
|
287
|
+
| Memory Poisoning | Exploitable | Blocked |
|
|
288
|
+
| Cross-Tenant Access | Possible | Blocked |
|
|
289
|
+
| Tool Hallucination | Executed | Blocked |
|
|
290
|
+
| Trust Exploitation | Possible | Blocked |
|
|
291
|
+
|
|
292
|
+
## Architecture Principle
|
|
293
|
+
|
|
294
|
+
> **"The LLM proposes. The orchestrator disposes."**
|
|
295
|
+
|
|
296
|
+
LLMs cannot be trusted to enforce security. All security decisions happen in the orchestration layer.
|
|
297
|
+
|
|
298
|
+
## Contributing
|
|
299
|
+
|
|
300
|
+
See [CONTRIBUTING.md](CONTRIBUTING.md) for guidelines.
|
|
301
|
+
|
|
302
|
+
## Security
|
|
303
|
+
|
|
304
|
+
See [SECURITY.md](SECURITY.md) for security policy and reporting vulnerabilities.
|
|
305
|
+
|
|
306
|
+
## Changelog
|
|
307
|
+
|
|
308
|
+
See [CHANGELOG.md](CHANGELOG.md) for version history.
|
|
309
|
+
|
|
310
|
+
## License
|
|
311
|
+
|
|
312
|
+
MIT License - see [LICENSE](LICENSE) for details.
|
|
313
|
+
|
|
314
|
+
## Links
|
|
315
|
+
|
|
316
|
+
- [npm Package](https://www.npmjs.com/package/llm-trust-guard)
|
|
317
|
+
- [OWASP Top 10 for LLMs](https://owasp.org/www-project-top-10-for-large-language-model-applications/)
|
|
318
|
+
- [OWASP Agentic AI](https://owasp.org/www-project-agentic-ai/)
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* AgentCommunicationGuard (L12)
|
|
3
|
+
*
|
|
4
|
+
* Secures communication between agents in multi-agent systems.
|
|
5
|
+
* Prevents impersonation, replay attacks, and message tampering.
|
|
6
|
+
*
|
|
7
|
+
* Threat Model:
|
|
8
|
+
* - ASI07: Insecure Inter-Agent Communication
|
|
9
|
+
* - Agent impersonation attacks
|
|
10
|
+
* - Message replay attacks
|
|
11
|
+
* - Man-in-the-middle attacks
|
|
12
|
+
*
|
|
13
|
+
* Protection Capabilities:
|
|
14
|
+
* - Message authentication (HMAC signing)
|
|
15
|
+
* - Agent identity verification
|
|
16
|
+
* - Replay attack prevention (nonces)
|
|
17
|
+
* - Message encryption (optional)
|
|
18
|
+
* - Channel integrity validation
|
|
19
|
+
*/
|
|
20
|
+
export interface AgentCommunicationGuardConfig {
|
|
21
|
+
/** Secret key for HMAC signing (auto-generated if not provided) */
|
|
22
|
+
signingKey?: string;
|
|
23
|
+
/** Enable message encryption */
|
|
24
|
+
enableEncryption?: boolean;
|
|
25
|
+
/** Encryption key (required if encryption enabled) */
|
|
26
|
+
encryptionKey?: string;
|
|
27
|
+
/** Nonce expiration time in milliseconds */
|
|
28
|
+
nonceExpiration?: number;
|
|
29
|
+
/** Maximum message age in milliseconds */
|
|
30
|
+
maxMessageAge?: number;
|
|
31
|
+
/** Require all messages to be signed */
|
|
32
|
+
requireSignatures?: boolean;
|
|
33
|
+
/** Allowed agent IDs (empty = allow all registered) */
|
|
34
|
+
allowedAgents?: string[];
|
|
35
|
+
/** Enable strict mode (block on any violation) */
|
|
36
|
+
strictMode?: boolean;
|
|
37
|
+
}
|
|
38
|
+
export interface AgentIdentity {
|
|
39
|
+
/** Unique agent identifier */
|
|
40
|
+
agentId: string;
|
|
41
|
+
/** Agent type/role */
|
|
42
|
+
agentType: string;
|
|
43
|
+
/** Agent capabilities/permissions */
|
|
44
|
+
capabilities: string[];
|
|
45
|
+
/** Public key for verification (optional, for asymmetric signing) */
|
|
46
|
+
publicKey?: string;
|
|
47
|
+
/** Registration timestamp */
|
|
48
|
+
registeredAt: number;
|
|
49
|
+
/** Trust score (0-100) */
|
|
50
|
+
trustScore: number;
|
|
51
|
+
/** Metadata */
|
|
52
|
+
metadata?: Record<string, any>;
|
|
53
|
+
}
|
|
54
|
+
export interface AgentMessage {
|
|
55
|
+
/** Message unique identifier */
|
|
56
|
+
messageId: string;
|
|
57
|
+
/** Sender agent ID */
|
|
58
|
+
fromAgent: string;
|
|
59
|
+
/** Recipient agent ID(s) */
|
|
60
|
+
toAgent: string | string[];
|
|
61
|
+
/** Message type */
|
|
62
|
+
type: "request" | "response" | "broadcast" | "event";
|
|
63
|
+
/** Message payload */
|
|
64
|
+
payload: any;
|
|
65
|
+
/** Timestamp */
|
|
66
|
+
timestamp: number;
|
|
67
|
+
/** Nonce for replay prevention */
|
|
68
|
+
nonce: string;
|
|
69
|
+
/** HMAC signature */
|
|
70
|
+
signature?: string;
|
|
71
|
+
/** Encrypted flag */
|
|
72
|
+
encrypted?: boolean;
|
|
73
|
+
/** Reference to parent message (for responses) */
|
|
74
|
+
replyTo?: string;
|
|
75
|
+
/** Time-to-live in milliseconds */
|
|
76
|
+
ttl?: number;
|
|
77
|
+
}
|
|
78
|
+
export interface MessageValidationResult {
|
|
79
|
+
allowed: boolean;
|
|
80
|
+
reason: string;
|
|
81
|
+
violations: string[];
|
|
82
|
+
request_id: string;
|
|
83
|
+
validation: {
|
|
84
|
+
sender_verified: boolean;
|
|
85
|
+
recipient_valid: boolean;
|
|
86
|
+
signature_valid: boolean;
|
|
87
|
+
nonce_valid: boolean;
|
|
88
|
+
timestamp_valid: boolean;
|
|
89
|
+
payload_safe: boolean;
|
|
90
|
+
trust_score: number;
|
|
91
|
+
};
|
|
92
|
+
decrypted_payload?: any;
|
|
93
|
+
recommendations: string[];
|
|
94
|
+
}
|
|
95
|
+
export interface ChannelStatus {
|
|
96
|
+
agentId: string;
|
|
97
|
+
connected: boolean;
|
|
98
|
+
lastSeen: number;
|
|
99
|
+
messageCount: number;
|
|
100
|
+
trustScore: number;
|
|
101
|
+
violations: number;
|
|
102
|
+
}
|
|
103
|
+
export declare class AgentCommunicationGuard {
|
|
104
|
+
private config;
|
|
105
|
+
private signingKey;
|
|
106
|
+
private encryptionKey?;
|
|
107
|
+
private registeredAgents;
|
|
108
|
+
private usedNonces;
|
|
109
|
+
private messageHistory;
|
|
110
|
+
private agentViolations;
|
|
111
|
+
private readonly PAYLOAD_INJECTION_PATTERNS;
|
|
112
|
+
constructor(config?: AgentCommunicationGuardConfig);
|
|
113
|
+
/**
|
|
114
|
+
* Register an agent for communication
|
|
115
|
+
*/
|
|
116
|
+
registerAgent(agentId: string, agentType: string, capabilities: string[], metadata?: Record<string, any>): AgentIdentity;
|
|
117
|
+
/**
|
|
118
|
+
* Unregister an agent
|
|
119
|
+
*/
|
|
120
|
+
unregisterAgent(agentId: string): boolean;
|
|
121
|
+
/**
|
|
122
|
+
* Create a signed message
|
|
123
|
+
*/
|
|
124
|
+
createMessage(fromAgent: string, toAgent: string | string[], type: AgentMessage["type"], payload: any, replyTo?: string, ttl?: number): AgentMessage;
|
|
125
|
+
/**
|
|
126
|
+
* Validate an incoming message
|
|
127
|
+
*/
|
|
128
|
+
validateMessage(message: AgentMessage, receivingAgentId: string, requestId?: string): MessageValidationResult;
|
|
129
|
+
/**
|
|
130
|
+
* Create a response to a message
|
|
131
|
+
*/
|
|
132
|
+
createResponse(originalMessage: AgentMessage, fromAgent: string, payload: any): AgentMessage;
|
|
133
|
+
/**
|
|
134
|
+
* Get channel status for an agent
|
|
135
|
+
*/
|
|
136
|
+
getChannelStatus(agentId: string): ChannelStatus | null;
|
|
137
|
+
/**
|
|
138
|
+
* Get all registered agents
|
|
139
|
+
*/
|
|
140
|
+
getRegisteredAgents(): AgentIdentity[];
|
|
141
|
+
/**
|
|
142
|
+
* Check if agent has capability
|
|
143
|
+
*/
|
|
144
|
+
hasCapability(agentId: string, capability: string): boolean;
|
|
145
|
+
/**
|
|
146
|
+
* Update agent trust score
|
|
147
|
+
*/
|
|
148
|
+
updateTrustScore(agentId: string, delta: number): void;
|
|
149
|
+
/**
|
|
150
|
+
* Reset agent violations
|
|
151
|
+
*/
|
|
152
|
+
resetViolations(agentId: string): void;
|
|
153
|
+
/**
|
|
154
|
+
* Verify message chain (for multi-hop scenarios)
|
|
155
|
+
*/
|
|
156
|
+
verifyMessageChain(messages: AgentMessage[]): {
|
|
157
|
+
valid: boolean;
|
|
158
|
+
broken_at?: number;
|
|
159
|
+
violations: string[];
|
|
160
|
+
};
|
|
161
|
+
private signMessage;
|
|
162
|
+
private encryptPayload;
|
|
163
|
+
private decryptPayload;
|
|
164
|
+
private validatePayload;
|
|
165
|
+
private getObjectDepth;
|
|
166
|
+
private cleanupNonces;
|
|
167
|
+
private generateRecommendations;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=agent-communication-guard.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"agent-communication-guard.d.ts","sourceRoot":"","sources":["../../src/guards/agent-communication-guard.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;GAkBG;AAIH,MAAM,WAAW,6BAA6B;IAC5C,mEAAmE;IACnE,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,gCAAgC;IAChC,gBAAgB,CAAC,EAAE,OAAO,CAAC;IAC3B,sDAAsD;IACtD,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,4CAA4C;IAC5C,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,0CAA0C;IAC1C,aAAa,CAAC,EAAE,MAAM,CAAC;IACvB,wCAAwC;IACxC,iBAAiB,CAAC,EAAE,OAAO,CAAC;IAC5B,uDAAuD;IACvD,aAAa,CAAC,EAAE,MAAM,EAAE,CAAC;IACzB,kDAAkD;IAClD,UAAU,CAAC,EAAE,OAAO,CAAC;CACtB;AAED,MAAM,WAAW,aAAa;IAC5B,8BAA8B;IAC9B,OAAO,EAAE,MAAM,CAAC;IAChB,sBAAsB;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,qCAAqC;IACrC,YAAY,EAAE,MAAM,EAAE,CAAC;IACvB,qEAAqE;IACrE,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,6BAA6B;IAC7B,YAAY,EAAE,MAAM,CAAC;IACrB,0BAA0B;IAC1B,UAAU,EAAE,MAAM,CAAC;IACnB,eAAe;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;CAChC;AAED,MAAM,WAAW,YAAY;IAC3B,gCAAgC;IAChC,SAAS,EAAE,MAAM,CAAC;IAClB,sBAAsB;IACtB,SAAS,EAAE,MAAM,CAAC;IAClB,4BAA4B;IAC5B,OAAO,EAAE,MAAM,GA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