-
Notifications
You must be signed in to change notification settings - Fork 9.7k
Expand file tree
/
Copy pathtool-tokens.test.ts
More file actions
193 lines (167 loc) · 6.61 KB
/
Copy pathtool-tokens.test.ts
File metadata and controls
193 lines (167 loc) · 6.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
import { afterEach, describe, expect, it, vi } from 'vitest';
import { CHAT_TOOL_ENDPOINTS, CHAT_TOOL_OPERATIONS, ToolTokenRegistry } from '../src/tool-tokens.js';
afterEach(() => {
vi.useRealTimers();
});
describe('run-scoped tool tokens', () => {
it('mints isolated tokens for concurrent runs under the same project', () => {
const registry = new ToolTokenRegistry();
const first = registry.mint({ runId: 'run-1', projectId: 'project-a', nowMs: 1_000 });
const second = registry.mint({ runId: 'run-2', projectId: 'project-a', nowMs: 1_000 });
expect(first.token).not.toBe(second.token);
expect(first.runId).toBe('run-1');
expect(second.runId).toBe('run-2');
expect(first.projectId).toBe('project-a');
expect(second.projectId).toBe('project-a');
expect(registry.activeRunTokenCount('run-1')).toBe(1);
expect(registry.activeRunTokenCount('run-2')).toBe(1);
registry.revokeRun('run-1', 'child_exit');
expect(registry.validate(first.token, { nowMs: 1_001 }).ok).toBe(false);
expect(registry.validate(second.token, { nowMs: 1_001 }).ok).toBe(true);
expect(registry.activeRunTokenCount('run-1')).toBe(0);
expect(registry.activeRunTokenCount('run-2')).toBe(1);
registry.clear();
});
it('binds tokens to endpoint and operation allowlists', () => {
const registry = new ToolTokenRegistry();
const grant = registry.mint({
runId: 'run-allowlist',
projectId: 'project-a',
allowedEndpoints: ['/api/tools/live-artifacts/create'],
allowedOperations: ['live-artifacts:create'],
nowMs: 1_000,
});
expect(registry.validate(grant.token, {
endpoint: '/api/tools/live-artifacts/create',
operation: 'live-artifacts:create',
nowMs: 1_001,
})).toMatchObject({ ok: true });
expect(registry.validate(grant.token, {
endpoint: '/api/tools/live-artifacts/list',
operation: 'live-artifacts:create',
nowMs: 1_001,
})).toMatchObject({ ok: false, code: 'TOOL_ENDPOINT_DENIED' });
expect(registry.validate(grant.token, {
endpoint: '/api/tools/live-artifacts/create',
operation: 'live-artifacts:update',
nowMs: 1_001,
})).toMatchObject({ ok: false, code: 'TOOL_OPERATION_DENIED' });
registry.clear();
});
it('expires and revokes tokens by TTL', () => {
vi.useFakeTimers();
const registry = new ToolTokenRegistry();
const grant = registry.mint({ runId: 'run-ttl', projectId: 'project-a', ttlMs: 10, nowMs: 1_000 });
expect(registry.activeTokenCount()).toBe(1);
vi.advanceTimersByTime(10);
expect(registry.activeTokenCount()).toBe(0);
expect(registry.validate(grant.token)).toMatchObject({ ok: false, code: 'TOOL_TOKEN_INVALID' });
registry.clear();
});
it('reports expiry when validation observes an expired active token', () => {
const registry = new ToolTokenRegistry();
const grant = registry.mint({ runId: 'run-expired', projectId: 'project-a', ttlMs: 10, nowMs: 1_000 });
expect(registry.validate(grant.token, { nowMs: 1_010 })).toMatchObject({ ok: false, code: 'TOOL_TOKEN_EXPIRED' });
expect(registry.activeTokenCount()).toBe(0);
});
it('refreshes an active run token as an inactivity lease without changing its scope', () => {
vi.useFakeTimers();
vi.setSystemTime(1_000);
const registry = new ToolTokenRegistry();
const grant = registry.mint({
runId: 'run-refresh',
projectId: 'project-a',
allowedEndpoints: ['/api/tools/media/generate'],
allowedOperations: ['media:generate'],
ttlMs: 10,
});
vi.advanceTimersByTime(9);
expect(registry.refreshRun('run-refresh')).toBe(1);
vi.advanceTimersByTime(1);
expect(registry.validate(grant.token)).toMatchObject({
ok: true,
grant: {
token: grant.token,
runId: 'run-refresh',
projectId: 'project-a',
allowedEndpoints: ['/api/tools/media/generate'],
allowedOperations: ['media:generate'],
issuedAt: grant.issuedAt,
expiresAt: new Date(1_019).toISOString(),
},
});
vi.advanceTimersByTime(9);
expect(registry.validate(grant.token)).toMatchObject({
ok: false,
code: 'TOOL_TOKEN_INVALID',
});
});
it('does not recreate unknown, expired, or manually revoked tokens during a run refresh', () => {
vi.useFakeTimers();
vi.setSystemTime(1_000);
const registry = new ToolTokenRegistry();
expect(registry.refreshRun('run-unknown')).toBe(0);
const expired = registry.mint({
runId: 'run-expired-refresh',
projectId: 'project-a',
ttlMs: 10,
});
vi.advanceTimersByTime(10);
expect(registry.refreshRun('run-expired-refresh')).toBe(0);
expect(registry.validate(expired.token)).toMatchObject({
ok: false,
code: 'TOOL_TOKEN_INVALID',
});
const revoked = registry.mint({
runId: 'run-revoked-refresh',
projectId: 'project-a',
ttlMs: 10,
});
expect(registry.revokeToken(revoked.token)).toBe(true);
expect(registry.refreshRun('run-revoked-refresh')).toBe(0);
expect(registry.validate(revoked.token)).toMatchObject({
ok: false,
code: 'TOOL_TOKEN_INVALID',
});
expect(registry.activeTokenCount()).toBe(0);
});
it('refreshes only the active run among concurrent runs for the same project', () => {
vi.useFakeTimers();
vi.setSystemTime(1_000);
const registry = new ToolTokenRegistry();
const refreshed = registry.mint({
runId: 'run-refreshed',
projectId: 'project-a',
ttlMs: 10,
});
const alsoRefreshed = registry.mint({
runId: 'run-refreshed',
projectId: 'project-a',
ttlMs: 10,
});
const untouched = registry.mint({
runId: 'run-untouched',
projectId: 'project-a',
ttlMs: 10,
});
vi.advanceTimersByTime(9);
expect(registry.refreshRun('run-refreshed')).toBe(2);
vi.advanceTimersByTime(1);
expect(registry.validate(refreshed.token).ok).toBe(true);
expect(registry.validate(alsoRefreshed.token).ok).toBe(true);
expect(registry.validate(untouched.token)).toMatchObject({
ok: false,
code: 'TOOL_TOKEN_INVALID',
});
expect(registry.activeRunTokenCount('run-refreshed')).toBe(2);
expect(registry.activeRunTokenCount('run-untouched')).toBe(0);
registry.clear();
});
it('uses the chat tool endpoint and operation allowlists by default', () => {
const registry = new ToolTokenRegistry();
const grant = registry.mint({ runId: 'run-defaults', projectId: 'project-a', nowMs: 1_000 });
expect(grant.allowedEndpoints).toEqual([...CHAT_TOOL_ENDPOINTS]);
expect(grant.allowedOperations).toEqual([...CHAT_TOOL_OPERATIONS]);
registry.clear();
});
});