-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathpermissions.e2e.test.ts
More file actions
671 lines (579 loc) · 25 KB
/
permissions.e2e.test.ts
File metadata and controls
671 lines (579 loc) · 25 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
*--------------------------------------------------------------------------------------------*/
import { realpathSync } from "fs";
import { mkdir, readFile, writeFile } from "fs/promises";
import { join } from "path";
import { describe, expect, it } from "vitest";
import { z } from "zod";
import type {
PermissionRequest,
PermissionRequestResult,
ToolResultObject,
} from "../../src/index.js";
import { approveAll, defineTool } from "../../src/index.js";
import { createSdkTestContext } from "./harness/sdkTestContext.js";
import { getFinalAssistantMessage, getNextEventOfType } from "./harness/sdkTestHelper.js";
describe("Permission callbacks", async () => {
const { copilotClient: client, workDir } = await createSdkTestContext();
it("should invoke permission handler for write operations", async () => {
const permissionRequests: PermissionRequest[] = [];
const session = await client.createSession({
onPermissionRequest: (request, invocation) => {
permissionRequests.push(request);
expect(invocation.sessionId).toBe(session.sessionId);
// Approve the permission
const result: PermissionRequestResult = { kind: "approve-once" };
return result;
},
});
await writeFile(join(workDir, "test.txt"), "original content");
await session.sendAndWait({
prompt: "Edit test.txt and replace 'original' with 'modified'",
});
// Should have received at least one permission request
expect(permissionRequests.length).toBeGreaterThan(0);
// Should include write permission request
const writeRequests = permissionRequests.filter((req) => req.kind === "write");
expect(writeRequests.length).toBeGreaterThan(0);
await session.disconnect();
});
it("should deny permission when handler returns denied", async () => {
const session = await client.createSession({
onPermissionRequest: () => {
return { kind: "reject" };
},
});
// Regression check for https://github.com/github/copilot-sdk/issues/1194:
// the reject decision must round-trip through the CLI with its discriminator
// intact so the agent surfaces the user-rejected error to the model. The
// CLI emits a kind-specific error message ("The user rejected this tool call.")
// for the reject decision, which lets us assert the decision was honored
// — not merely that the operation didn't happen.
let userRejectedToolCall = false;
session.on((event) => {
if (
event.type === "tool.execution_complete" &&
!event.data.success &&
event.data.error?.message.toLowerCase().includes("user rejected")
) {
userRejectedToolCall = true;
}
});
const originalContent = "protected content";
const testFile = join(workDir, "protected.txt");
await writeFile(testFile, originalContent);
await session.sendAndWait({
prompt: "Edit protected.txt and replace 'protected' with 'hacked'.",
});
expect(userRejectedToolCall).toBe(true);
// Verify the file was NOT modified
const content = await readFile(testFile, "utf-8");
expect(content).toBe(originalContent);
await session.disconnect();
});
it("should deny tool operations when handler explicitly denies", async () => {
let permissionDenied = false;
const session = await client.createSession({
onPermissionRequest: () => ({
kind: "user-not-available",
}),
});
session.on((event) => {
if (
event.type === "tool.execution_complete" &&
!event.data.success &&
event.data.error?.message.includes("Permission denied")
) {
permissionDenied = true;
}
});
await session.sendAndWait({ prompt: "Run 'node --version'" });
expect(permissionDenied).toBe(true);
await session.disconnect();
});
it("should deny tool operations when handler explicitly denies after resume", async () => {
const session1 = await client.createSession({ onPermissionRequest: approveAll });
const sessionId = session1.sessionId;
await session1.sendAndWait({ prompt: "What is 1+1?" });
const session2 = await client.resumeSession(sessionId, {
onPermissionRequest: () => ({
kind: "user-not-available",
}),
});
let permissionDenied = false;
session2.on((event) => {
if (
event.type === "tool.execution_complete" &&
!event.data.success &&
event.data.error?.message.includes("Permission denied")
) {
permissionDenied = true;
}
});
await session2.sendAndWait({ prompt: "Run 'node --version'" });
expect(permissionDenied).toBe(true);
await session2.disconnect();
});
it("should work with approve-all permission handler", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const message = await session.sendAndWait({
prompt: "What is 2+2?",
});
expect(message?.data.content).toContain("4");
await session.disconnect();
});
it("should handle async permission handler", async () => {
const permissionRequests: PermissionRequest[] = [];
const session = await client.createSession({
onPermissionRequest: async (request, _invocation) => {
permissionRequests.push(request);
await Promise.resolve();
return { kind: "approve-once" };
},
});
await session.sendAndWait({
prompt: "Run 'echo test' and tell me what happens",
});
expect(permissionRequests.length).toBeGreaterThan(0);
await session.disconnect();
});
it("should resume session with permission handler", async () => {
const permissionRequests: PermissionRequest[] = [];
// Create initial session
const session1 = await client.createSession({ onPermissionRequest: approveAll });
const sessionId = session1.sessionId;
await session1.sendAndWait({ prompt: "What is 1+1?" });
// Resume with permission handler
const session2 = await client.resumeSession(sessionId, {
onPermissionRequest: (request) => {
permissionRequests.push(request);
return { kind: "approve-once" };
},
});
await session2.sendAndWait({
prompt: "Run 'echo resumed' for me",
});
// Should have permission requests from resumed session
expect(permissionRequests.length).toBeGreaterThan(0);
await session2.disconnect();
});
it("should handle permission handler errors gracefully", async () => {
const session = await client.createSession({
onPermissionRequest: () => {
throw new Error("Handler error");
},
});
const message = await session.sendAndWait({
prompt: "Run 'echo test'. If you can't, say 'failed'.",
});
// Should handle the error and deny permission
expect(message?.data.content?.toLowerCase()).toMatch(/fail|cannot|unable|permission/);
await session.disconnect();
});
it("should receive toolCallId in permission requests", async () => {
let receivedToolCallId = false;
const session = await client.createSession({
onPermissionRequest: (request) => {
if (request.toolCallId) {
receivedToolCallId = true;
expect(typeof request.toolCallId).toBe("string");
expect(request.toolCallId.length).toBeGreaterThan(0);
}
return { kind: "approve-once" };
},
});
await session.sendAndWait({
prompt: "Run 'echo test'",
});
expect(receivedToolCallId).toBe(true);
await session.disconnect();
});
it("should wait for slow permission handler", async () => {
let handlerStartedResolve: () => void;
let releaseHandler: () => void;
let targetToolCallId: string | undefined;
const handlerStarted = new Promise<void>((resolve) => {
let resolved = false;
handlerStartedResolve = () => {
if (!resolved) {
resolved = true;
resolve();
}
};
});
const handlerGate = new Promise<void>((resolve) => {
releaseHandler = resolve;
});
let permissionCount = 0;
const lifecycle: Array<{ phase: string; toolCallId?: string }> = [];
const session = await client.createSession({
onPermissionRequest: async (
request: PermissionRequest
): Promise<PermissionRequestResult> => {
permissionCount++;
targetToolCallId = request.toolCallId;
lifecycle.push({ phase: "permission-start", toolCallId: request.toolCallId });
handlerStartedResolve!();
await handlerGate;
lifecycle.push({ phase: "permission-complete", toolCallId: request.toolCallId });
return { kind: "approve-once" };
},
});
session.on((event) => {
if (event.type === "tool.execution_start") {
lifecycle.push({ phase: "tool-start", toolCallId: event.data.toolCallId });
} else if (event.type === "tool.execution_complete") {
lifecycle.push({ phase: "tool-complete", toolCallId: event.data.toolCallId });
}
});
const sessionDone = getFinalAssistantMessage(session);
void session.send({ prompt: "Run 'echo slow_handler_test'" });
// Wait for permission handler to be invoked
await handlerStarted;
expect(
lifecycle.some(
(entry) =>
entry.phase === "tool-complete" &&
(!targetToolCallId || entry.toolCallId === targetToolCallId)
)
).toBe(false);
// Handler is blocked — release it now
releaseHandler!();
const answer = await sessionDone;
expect(answer.data.content).toContain("slow_handler_test");
expect(permissionCount).toBe(1);
const permissionCompleteIndex = lifecycle.findIndex(
(entry) =>
entry.phase === "permission-complete" &&
(!targetToolCallId || entry.toolCallId === targetToolCallId)
);
const toolCompleteIndex = lifecycle.findIndex(
(entry) =>
entry.phase === "tool-complete" &&
(!targetToolCallId || entry.toolCallId === targetToolCallId)
);
expect(permissionCompleteIndex).toBeGreaterThanOrEqual(0);
expect(toolCompleteIndex).toBeGreaterThanOrEqual(0);
expect(permissionCompleteIndex).toBeLessThan(toolCompleteIndex);
await session.disconnect();
});
it("should handle concurrent permission requests from parallel tools", async () => {
let resolveFirst: (() => void) | undefined;
let resolveSecond: (() => void) | undefined;
const firstArrived = new Promise<void>((r) => (resolveFirst = r));
const secondArrived = new Promise<void>((r) => (resolveSecond = r));
let requestCount = 0;
let firstToolCalled = false;
let secondToolCalled = false;
const permissionRequests: Array<PermissionRequest & { toolName?: string }> = [];
const toolCompletions: string[] = [];
const session = await client.createSession({
tools: [
defineTool("first_permission_tool", {
description: "First concurrent permission test tool",
parameters: z.object({}),
handler: async (): Promise<ToolResultObject> => {
firstToolCalled = true;
return {
textResultForLlm:
"first_permission_tool completed after permission approval",
resultType: "rejected",
};
},
}),
defineTool("second_permission_tool", {
description: "Second concurrent permission test tool",
parameters: z.object({}),
handler: async (): Promise<ToolResultObject> => {
secondToolCalled = true;
return {
textResultForLlm:
"second_permission_tool completed after permission approval",
resultType: "rejected",
};
},
}),
],
availableTools: ["first_permission_tool", "second_permission_tool"],
onPermissionRequest: async (
request: PermissionRequest
): Promise<PermissionRequestResult> => {
permissionRequests.push(request as PermissionRequest & { toolName?: string });
requestCount++;
if (requestCount === 1) resolveFirst?.();
if (requestCount === 2) resolveSecond?.();
// Wait until both have arrived before approving
await Promise.all([firstArrived, secondArrived]);
return { kind: "approve-once" };
},
});
session.on((event) => {
if (event.type === "tool.execution_complete" && event.data.error?.message) {
toolCompletions.push(event.data.error.message);
}
});
const idle = getNextEventOfType(session, "session.idle");
await session.send({
prompt: "Call both first_permission_tool and second_permission_tool in the same turn. Do not call any other tools.",
});
await Promise.all([firstArrived, secondArrived]);
await idle;
expect(requestCount).toBe(2);
expect(
permissionRequests.some((request) => request.toolName === "first_permission_tool")
).toBe(true);
expect(
permissionRequests.some((request) => request.toolName === "second_permission_tool")
).toBe(true);
expect(firstToolCalled).toBe(true);
expect(secondToolCalled).toBe(true);
expect(
toolCompletions.some((message) =>
message.includes("first_permission_tool completed after permission approval")
)
).toBe(true);
expect(
toolCompletions.some((message) =>
message.includes("second_permission_tool completed after permission approval")
)
).toBe(true);
await session.disconnect();
});
it("should deny permission with noresult kind", async () => {
// With no-result, the TypeScript SDK does not send any response to the CLI's permission
// request, leaving the tool execution pending. We verify the permission handler fires.
let resolvePermissionCalled!: () => void;
const permissionCalled = new Promise<void>((resolve) => {
resolvePermissionCalled = resolve;
});
const session = await client.createSession({
onPermissionRequest: (_request: PermissionRequest): PermissionRequestResult => {
resolvePermissionCalled();
return { kind: "no-result" };
},
});
void session.send({ prompt: "Run 'node --version'" });
await permissionCalled;
await session.disconnect();
});
it("should short circuit permission handler when set approve all enabled", async () => {
let handlerCalled = false;
const session = await client.createSession({
onPermissionRequest: (_request: PermissionRequest): PermissionRequestResult => {
handlerCalled = true;
return { kind: "approve-once" };
},
});
// Enable approve-all server-side short circuit
await session.rpc.permissions.setApproveAll({ enabled: true });
try {
const answer = await session.sendAndWait({
prompt: "Run 'echo test' and tell me what happens",
});
expect(handlerCalled).toBe(false);
expect(answer?.data.content).toContain("test");
} finally {
await session.rpc.permissions.setApproveAll({ enabled: false });
}
await session.disconnect();
});
it("should configure and update permission paths", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const configuredAllowedDirectory = await createUniqueWorkDirectory(
workDir,
"configured-allowed"
);
const addedAllowedDirectory = await createUniqueWorkDirectory(workDir, "added-allowed");
const newPrimaryDirectory = await createUniqueWorkDirectory(workDir, "new-primary");
try {
const configureResult = await session.rpc.permissions.configure({
approveAllToolPermissionRequests: false,
approveAllReadPermissionRequests: true,
rules: {
approved: [{ kind: "read", argument: null }],
denied: [{ kind: "write", argument: null }],
},
paths: {
workspacePath: workDir,
additionalDirectories: [configuredAllowedDirectory],
includeTempDirectory: false,
unrestricted: false,
},
urls: {
initialAllowed: ["https://example.invalid/permissions-configure"],
unrestricted: false,
},
});
expect(configureResult.success).toBe(true);
const configuredList = await session.rpc.permissions.paths.list();
expectPathEqual(configuredList.primary, workDir);
expect(configuredList.directories.some((p) => pathsEqual(p, workDir))).toBe(true);
expect(
configuredList.directories.some((p) => pathsEqual(p, configuredAllowedDirectory))
).toBe(true);
expect(
(await session.rpc.permissions.paths.add({ path: addedAllowedDirectory })).success
).toBe(true);
expect(
(
await session.rpc.permissions.paths.isPathWithinAllowedDirectories({
path: join(addedAllowedDirectory, "child.txt"),
})
).allowed
).toBe(true);
expect(
(await session.rpc.permissions.paths.updatePrimary({ path: newPrimaryDirectory }))
.success
).toBe(true);
const updatedList = await session.rpc.permissions.paths.list();
expectPathEqual(updatedList.primary, newPrimaryDirectory);
expect(updatedList.directories.some((p) => pathsEqual(p, newPrimaryDirectory))).toBe(
true
);
expect(
(
await session.rpc.permissions.paths.isPathWithinWorkspace({
path: join(newPrimaryDirectory, "child.txt"),
})
).allowed
).toBe(true);
} finally {
await session.disconnect();
}
});
it("should invoke permission state rpc apis", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
try {
expect((await session.rpc.permissions.pendingRequests()).items).toEqual([]);
expect((await session.rpc.permissions.setRequired({ required: true })).success).toBe(
true
);
expect((await session.rpc.permissions.setRequired({ required: false })).success).toBe(
true
);
expect(
(
await session.rpc.permissions.notifyPromptShown({
message: "Permission prompt shown from Node SDK E2E",
})
).success
).toBe(true);
const rule = {
kind: "commands",
argument: `node-permission-e2e-${Date.now()}`,
};
expect(
(
await session.rpc.permissions.modifyRules({
scope: "session",
add: [rule],
})
).success
).toBe(true);
expect(
(
await session.rpc.permissions.modifyRules({
scope: "session",
remove: [rule],
})
).success
).toBe(true);
expect(
(await session.rpc.permissions.urls.setUnrestrictedMode({ unrestricted: true }))
.success
).toBe(true);
expect(
(await session.rpc.permissions.urls.setUnrestrictedMode({ unrestricted: false }))
.success
).toBe(true);
} finally {
await session.disconnect();
}
});
it("should invoke permission location and folder trust rpc apis", async () => {
const session = await client.createSession({ onPermissionRequest: approveAll });
const locationDirectory = await createUniqueWorkDirectory(workDir, "permission-location");
const trustedDirectory = await createUniqueWorkDirectory(workDir, "folder-trust");
const commandIdentifier = `node-permission-location-${Date.now()}`;
try {
const resolved = await session.rpc.permissions.locations.resolve({
workingDirectory: locationDirectory,
});
expect(resolved.locationType).toBe("dir");
expectPathEqual(resolved.locationKey, locationDirectory);
expect(
(
await session.rpc.permissions.locations.addToolApproval({
locationKey: resolved.locationKey,
approval: {
kind: "commands",
commandIdentifiers: [commandIdentifier],
},
})
).success
).toBe(true);
const applied = await session.rpc.permissions.locations.apply({
workingDirectory: locationDirectory,
});
expect(applied.locationType).toBe(resolved.locationType);
expectPathEqual(applied.locationKey, resolved.locationKey);
expect(applied.appliedRuleCount).toBeGreaterThanOrEqual(1);
expect(
applied.appliedRules.some(
(rule) => rule.kind === "shell" && rule.argument === commandIdentifier
)
).toBe(true);
expect(
(
await session.rpc.permissions.folderTrust.isTrusted({
path: trustedDirectory,
})
).trusted
).toBe(false);
expect(
(
await session.rpc.permissions.folderTrust.addTrusted({
path: trustedDirectory,
})
).success
).toBe(true);
expect(
(
await session.rpc.permissions.folderTrust.isTrusted({
path: trustedDirectory,
})
).trusted
).toBe(true);
} finally {
await session.disconnect();
}
});
});
async function createUniqueWorkDirectory(baseDir: string, prefix: string): Promise<string> {
const directory = join(
baseDir,
`${prefix}-${Date.now()}-${Math.random().toString(36).slice(2)}`
);
await mkdir(directory, { recursive: true });
return directory;
}
function expectPathEqual(actual: string, expected: string): void {
expect(pathsEqual(actual, expected), `Expected path '${actual}' to equal '${expected}'.`).toBe(
true
);
}
function pathsEqual(left: string, right: string): boolean {
return normalizePath(left) === normalizePath(right);
}
function normalizePath(value: string): string {
const trimmed = value.replace(/[\\/]+$/g, "");
try {
return realpathSync
.native(trimmed)
.replace(/[\\/]+$/g, "")
.toLowerCase();
} catch {
return trimmed.toLowerCase();
}
}