@tpsdev-ai/agent 0.5.3 → 0.6.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/dist/bin.js +47 -2
- package/dist/bin.js.map +1 -1
- package/dist/config.d.ts +2 -1
- package/dist/config.d.ts.map +1 -1
- package/dist/config.js +8 -0
- package/dist/config.js.map +1 -1
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -0
- package/dist/index.js.map +1 -1
- package/dist/io/flair.d.ts +43 -0
- package/dist/io/flair.d.ts.map +1 -0
- package/dist/io/flair.js +143 -0
- package/dist/io/flair.js.map +1 -0
- package/dist/io/mail.d.ts +24 -2
- package/dist/io/mail.d.ts.map +1 -1
- package/dist/io/mail.js +168 -15
- package/dist/io/mail.js.map +1 -1
- package/dist/lib/signEnvelope.d.ts +67 -0
- package/dist/lib/signEnvelope.d.ts.map +1 -0
- package/dist/lib/signEnvelope.js +244 -0
- package/dist/lib/signEnvelope.js.map +1 -0
- package/dist/llm/provider.d.ts +21 -1
- package/dist/llm/provider.d.ts.map +1 -1
- package/dist/llm/provider.js +422 -23
- package/dist/llm/provider.js.map +1 -1
- package/dist/runtime/agent.d.ts +2 -0
- package/dist/runtime/agent.d.ts.map +1 -1
- package/dist/runtime/agent.js +50 -3
- package/dist/runtime/agent.js.map +1 -1
- package/dist/runtime/event-loop.d.ts +44 -2
- package/dist/runtime/event-loop.d.ts.map +1 -1
- package/dist/runtime/event-loop.js +478 -56
- package/dist/runtime/event-loop.js.map +1 -1
- package/dist/runtime/types.d.ts +27 -1
- package/dist/runtime/types.d.ts.map +1 -1
- package/dist/telemetry/events.d.ts +57 -0
- package/dist/telemetry/events.d.ts.map +1 -0
- package/dist/telemetry/events.js +37 -0
- package/dist/telemetry/events.js.map +1 -0
- package/dist/tools/edit.d.ts.map +1 -1
- package/dist/tools/edit.js +94 -16
- package/dist/tools/edit.js.map +1 -1
- package/package.json +10 -2
- package/src/bin.ts +56 -3
- package/src/config.ts +10 -1
- package/src/index.ts +8 -0
- package/src/io/flair.ts +183 -0
- package/src/io/mail.ts +184 -19
- package/src/lib/signEnvelope.ts +332 -0
- package/src/llm/provider.ts +454 -22
- package/src/runtime/agent.ts +55 -3
- package/src/runtime/event-loop.ts +523 -44
- package/src/runtime/types.ts +32 -1
- package/src/telemetry/events.ts +101 -0
- package/src/tools/edit.ts +108 -17
- package/test/bin-launch-id.test.ts +104 -0
- package/test/core-loop.test.ts +240 -0
- package/test/events.test.ts +33 -0
- package/test/flair-context.test.ts +102 -0
- package/test/governance.test.ts +103 -2
- package/test/io.test.ts +8 -8
- package/test/provider.test.ts +471 -0
- package/test/security/mail-trust.test.ts +266 -0
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Security regression tests for mail trust and capability scoping.
|
|
3
|
+
* Each test maps to a finding in SECURITY.md.
|
|
4
|
+
*/
|
|
5
|
+
import { describe, test, expect, mock, beforeEach } from "bun:test";
|
|
6
|
+
import { EventLoop } from "../../src/runtime/event-loop.js";
|
|
7
|
+
import type { AgentConfig, LLMMessage, ToolSpec, CompletionResponse } from "../../src/runtime/types.js";
|
|
8
|
+
import type { MemoryStore } from "../../src/io/memory.js";
|
|
9
|
+
import type { ContextManager } from "../../src/io/context.js";
|
|
10
|
+
import type { ProviderManager } from "../../src/llm/provider.js";
|
|
11
|
+
import { ToolRegistry } from "../../src/tools/registry.js";
|
|
12
|
+
|
|
13
|
+
function makeConfig(overrides: Partial<AgentConfig> = {}): AgentConfig {
|
|
14
|
+
return {
|
|
15
|
+
name: "test-agent",
|
|
16
|
+
agentId: "test",
|
|
17
|
+
workspace: "/tmp/test-workspace",
|
|
18
|
+
provider: "anthropic",
|
|
19
|
+
model: "test",
|
|
20
|
+
maxToolTurns: 5,
|
|
21
|
+
...overrides,
|
|
22
|
+
} as AgentConfig;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function makeMemory(): MemoryStore {
|
|
26
|
+
return { append: mock(() => Promise.resolve()) } as any;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function makeContext(): ContextManager {
|
|
30
|
+
return {} as any;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* Build a provider mock that captures what tools were passed to it.
|
|
35
|
+
*/
|
|
36
|
+
function makeProvider(capturedTools: ToolSpec[][]): ProviderManager {
|
|
37
|
+
return {
|
|
38
|
+
complete: mock((req: any) => {
|
|
39
|
+
capturedTools.push([...req.tools]);
|
|
40
|
+
return Promise.resolve({
|
|
41
|
+
content: "Done.",
|
|
42
|
+
toolCalls: undefined,
|
|
43
|
+
inputTokens: 10,
|
|
44
|
+
outputTokens: 5,
|
|
45
|
+
} as CompletionResponse);
|
|
46
|
+
}),
|
|
47
|
+
} as any;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function makeToolRegistry(): ToolRegistry {
|
|
51
|
+
const reg = new ToolRegistry();
|
|
52
|
+
reg.register({
|
|
53
|
+
name: "read",
|
|
54
|
+
description: "Read a file",
|
|
55
|
+
input_schema: { path: { type: "string" } },
|
|
56
|
+
execute: async () => ({ content: "file contents" }),
|
|
57
|
+
});
|
|
58
|
+
reg.register({
|
|
59
|
+
name: "write",
|
|
60
|
+
description: "Write a file",
|
|
61
|
+
input_schema: { path: { type: "string" }, content: { type: "string" } },
|
|
62
|
+
execute: async () => ({ content: "ok" }),
|
|
63
|
+
});
|
|
64
|
+
reg.register({
|
|
65
|
+
name: "edit",
|
|
66
|
+
description: "Edit a file",
|
|
67
|
+
input_schema: { path: { type: "string" }, old_string: { type: "string" }, new_string: { type: "string" } },
|
|
68
|
+
execute: async () => ({ content: "ok" }),
|
|
69
|
+
});
|
|
70
|
+
reg.register({
|
|
71
|
+
name: "exec",
|
|
72
|
+
description: "Execute a command",
|
|
73
|
+
input_schema: { command: { type: "string" } },
|
|
74
|
+
execute: async () => ({ content: "output" }),
|
|
75
|
+
});
|
|
76
|
+
reg.register({
|
|
77
|
+
name: "mail",
|
|
78
|
+
description: "Send mail",
|
|
79
|
+
input_schema: { to: { type: "string" }, body: { type: "string" } },
|
|
80
|
+
execute: async () => ({ content: "sent" }),
|
|
81
|
+
});
|
|
82
|
+
return reg;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
describe("S43-A: internal mail drops exec", () => {
|
|
86
|
+
test("user trust gets exec", async () => {
|
|
87
|
+
const captured: ToolSpec[][] = [];
|
|
88
|
+
const loop = new EventLoop({
|
|
89
|
+
config: makeConfig(),
|
|
90
|
+
memory: makeMemory(),
|
|
91
|
+
context: makeContext(),
|
|
92
|
+
provider: makeProvider(captured),
|
|
93
|
+
tools: makeToolRegistry(),
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
await loop.runOnce("hello"); // runOnce uses trust=user
|
|
97
|
+
expect(captured.length).toBeGreaterThan(0);
|
|
98
|
+
const toolNames = captured[0].map((t) => t.name);
|
|
99
|
+
expect(toolNames).toContain("exec");
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
test("internal trust does NOT get exec", async () => {
|
|
103
|
+
const captured: ToolSpec[][] = [];
|
|
104
|
+
const loop = new EventLoop({
|
|
105
|
+
config: makeConfig(),
|
|
106
|
+
memory: makeMemory(),
|
|
107
|
+
context: makeContext(),
|
|
108
|
+
provider: makeProvider(captured),
|
|
109
|
+
tools: makeToolRegistry(),
|
|
110
|
+
});
|
|
111
|
+
|
|
112
|
+
// Simulate internal mail
|
|
113
|
+
const mail = {
|
|
114
|
+
body: "do something",
|
|
115
|
+
headers: { "X-TPS-Trust": "internal", "X-TPS-Sender": "agent-coder" },
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
// Access private processMail via run() with injected inbox
|
|
119
|
+
let callCount = 0;
|
|
120
|
+
await loop.run(async () => {
|
|
121
|
+
if (callCount++ === 0) return [mail as any];
|
|
122
|
+
await loop.stop();
|
|
123
|
+
return [];
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
expect(captured.length).toBeGreaterThan(0);
|
|
127
|
+
const toolNames = captured[0].map((t) => t.name);
|
|
128
|
+
expect(toolNames).not.toContain("exec");
|
|
129
|
+
expect(toolNames).toContain("read");
|
|
130
|
+
expect(toolNames).toContain("write");
|
|
131
|
+
expect(toolNames).toContain("mail");
|
|
132
|
+
});
|
|
133
|
+
|
|
134
|
+
test("external trust does NOT get exec", async () => {
|
|
135
|
+
const captured: ToolSpec[][] = [];
|
|
136
|
+
const loop = new EventLoop({
|
|
137
|
+
config: makeConfig(),
|
|
138
|
+
memory: makeMemory(),
|
|
139
|
+
context: makeContext(),
|
|
140
|
+
provider: makeProvider(captured),
|
|
141
|
+
tools: makeToolRegistry(),
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
const mail = {
|
|
145
|
+
body: "do something",
|
|
146
|
+
headers: { "X-TPS-Trust": "external", "X-TPS-Sender": "unknown" },
|
|
147
|
+
};
|
|
148
|
+
|
|
149
|
+
let callCount = 0;
|
|
150
|
+
await loop.run(async () => {
|
|
151
|
+
if (callCount++ === 0) return [mail as any];
|
|
152
|
+
await loop.stop();
|
|
153
|
+
return [];
|
|
154
|
+
});
|
|
155
|
+
|
|
156
|
+
expect(captured.length).toBeGreaterThan(0);
|
|
157
|
+
const toolNames = captured[0].map((t) => t.name);
|
|
158
|
+
expect(toolNames).not.toContain("exec");
|
|
159
|
+
});
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
describe("S43-D: scratch path traversal", () => {
|
|
163
|
+
test("external mail write to scratch/file.txt is allowed", async () => {
|
|
164
|
+
const memory = makeMemory();
|
|
165
|
+
const tools = makeToolRegistry();
|
|
166
|
+
const writeResults: string[] = [];
|
|
167
|
+
|
|
168
|
+
// Override write tool to capture calls
|
|
169
|
+
tools.register({
|
|
170
|
+
name: "write",
|
|
171
|
+
description: "Write a file",
|
|
172
|
+
input_schema: { path: { type: "string" }, content: { type: "string" } },
|
|
173
|
+
execute: async (input: any) => {
|
|
174
|
+
writeResults.push(input.path);
|
|
175
|
+
return { content: "ok" };
|
|
176
|
+
},
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
const provider = {
|
|
180
|
+
complete: mock(async (req: any) => {
|
|
181
|
+
// First call: request a write to scratch/file.txt
|
|
182
|
+
if ((provider.complete as any).mock.calls.length <= 1) {
|
|
183
|
+
return {
|
|
184
|
+
content: "",
|
|
185
|
+
toolCalls: [{ id: "1", name: "write", input: { path: "scratch/file.txt", content: "hello" } }],
|
|
186
|
+
inputTokens: 10,
|
|
187
|
+
outputTokens: 5,
|
|
188
|
+
};
|
|
189
|
+
}
|
|
190
|
+
return { content: "Done.", toolCalls: undefined, inputTokens: 10, outputTokens: 5 };
|
|
191
|
+
}),
|
|
192
|
+
} as any;
|
|
193
|
+
|
|
194
|
+
const loop = new EventLoop({
|
|
195
|
+
config: makeConfig(),
|
|
196
|
+
memory,
|
|
197
|
+
context: makeContext(),
|
|
198
|
+
provider,
|
|
199
|
+
tools,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const mail = {
|
|
203
|
+
body: "write a file",
|
|
204
|
+
headers: { "X-TPS-Trust": "external", "X-TPS-Sender": "outsider" },
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
let callCount = 0;
|
|
208
|
+
await loop.run(async () => {
|
|
209
|
+
if (callCount++ === 0) return [mail as any];
|
|
210
|
+
await loop.stop();
|
|
211
|
+
return [];
|
|
212
|
+
});
|
|
213
|
+
|
|
214
|
+
expect(writeResults).toContain("scratch/file.txt");
|
|
215
|
+
});
|
|
216
|
+
|
|
217
|
+
test("external mail write to scratch/../../etc/passwd is BLOCKED", async () => {
|
|
218
|
+
const memory = makeMemory();
|
|
219
|
+
const memoryAppendCalls: any[] = [];
|
|
220
|
+
(memory.append as any).mockImplementation((entry: any) => {
|
|
221
|
+
memoryAppendCalls.push(entry);
|
|
222
|
+
return Promise.resolve();
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
const provider = {
|
|
226
|
+
complete: mock(async (req: any) => {
|
|
227
|
+
if ((provider.complete as any).mock.calls.length <= 1) {
|
|
228
|
+
return {
|
|
229
|
+
content: "",
|
|
230
|
+
toolCalls: [{ id: "1", name: "write", input: { path: "scratch/../../etc/passwd", content: "pwned" } }],
|
|
231
|
+
inputTokens: 10,
|
|
232
|
+
outputTokens: 5,
|
|
233
|
+
};
|
|
234
|
+
}
|
|
235
|
+
return { content: "Done.", toolCalls: undefined, inputTokens: 10, outputTokens: 5 };
|
|
236
|
+
}),
|
|
237
|
+
} as any;
|
|
238
|
+
|
|
239
|
+
const loop = new EventLoop({
|
|
240
|
+
config: makeConfig(),
|
|
241
|
+
memory,
|
|
242
|
+
context: makeContext(),
|
|
243
|
+
provider,
|
|
244
|
+
tools: makeToolRegistry(),
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
const mail = {
|
|
248
|
+
body: "write a file",
|
|
249
|
+
headers: { "X-TPS-Trust": "external", "X-TPS-Sender": "attacker" },
|
|
250
|
+
};
|
|
251
|
+
|
|
252
|
+
let callCount = 0;
|
|
253
|
+
await loop.run(async () => {
|
|
254
|
+
if (callCount++ === 0) return [mail as any];
|
|
255
|
+
await loop.stop();
|
|
256
|
+
return [];
|
|
257
|
+
});
|
|
258
|
+
|
|
259
|
+
// Should have logged a permission denied error
|
|
260
|
+
const denials = memoryAppendCalls.filter(
|
|
261
|
+
(e: any) => e.type === "tool_result" && e.data?.result?.isError,
|
|
262
|
+
);
|
|
263
|
+
expect(denials.length).toBeGreaterThan(0);
|
|
264
|
+
expect(denials[0].data.result.content).toContain("Permission denied");
|
|
265
|
+
});
|
|
266
|
+
});
|