|
| 1 | +/*--------------------------------------------------------------------------------------------- |
| 2 | + * Copyright (c) Microsoft Corporation. All rights reserved. |
| 3 | + *--------------------------------------------------------------------------------------------*/ |
| 4 | + |
| 5 | +import { mkdir, writeFile } from "fs/promises"; |
| 6 | +import { join } from "path"; |
| 7 | +import { describe, expect, it } from "vitest"; |
| 8 | +import type { PermissionRequest } from "../../src/index.js"; |
| 9 | +import { createSdkTestContext } from "./harness/sdkTestContext.js"; |
| 10 | + |
| 11 | +const SEND_TIMEOUT_MS = 120_000; |
| 12 | +const TEST_TIMEOUT_MS = 180_000; |
| 13 | + |
| 14 | +describe("Sandbox bypass", () => { |
| 15 | + // The Windows backend requires BaseContainer, which is unavailable on the SDK's Windows runners. |
| 16 | + it.skipIf(process.platform === "win32")( |
| 17 | + "approves a blocked search and executes it outside the sandbox", |
| 18 | + async () => { |
| 19 | + const { copilotClient: client, workDir } = await createSdkTestContext({ |
| 20 | + copilotClientOptions: { |
| 21 | + env: { COPILOT_CLI_ENABLED_FEATURE_FLAGS: "SANDBOX" }, |
| 22 | + }, |
| 23 | + }); |
| 24 | + const vaultDir = join(workDir, "vault"); |
| 25 | + await mkdir(vaultDir, { recursive: true }); |
| 26 | + await writeFile(join(vaultDir, "notes.txt"), "OUTSIDE_MATCH_LINE bypass-approved\n"); |
| 27 | + |
| 28 | + const permissionRequests: PermissionRequest[] = []; |
| 29 | + let bypassedSearchCompleted = false; |
| 30 | + const session = await client.createSession({ |
| 31 | + onPermissionRequest: (request) => { |
| 32 | + permissionRequests.push(request); |
| 33 | + return { kind: "approve-once" }; |
| 34 | + }, |
| 35 | + }); |
| 36 | + const update = await session.rpc.options.update({ |
| 37 | + sandboxConfig: { |
| 38 | + enabled: true, |
| 39 | + allowBypass: true, |
| 40 | + addCurrentWorkingDirectory: true, |
| 41 | + userPolicy: { filesystem: { deniedPaths: [vaultDir] } }, |
| 42 | + }, |
| 43 | + }); |
| 44 | + expect(update.success).toBe(true); |
| 45 | + session.on((event) => { |
| 46 | + if ( |
| 47 | + event.type === "tool.execution_complete" && |
| 48 | + event.data.toolName === "grep" && |
| 49 | + event.data.success && |
| 50 | + event.data.sandboxed === false |
| 51 | + ) { |
| 52 | + bypassedSearchCompleted = true; |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + const message = await session.sendAndWait( |
| 57 | + { |
| 58 | + prompt: |
| 59 | + "Search for OUTSIDE_MATCH_LINE in the vault directory. " + |
| 60 | + "After the search succeeds, reply with exactly SANDBOX_BYPASS_APPROVED.", |
| 61 | + }, |
| 62 | + SEND_TIMEOUT_MS |
| 63 | + ); |
| 64 | + |
| 65 | + expect(message?.data.content).toContain("SANDBOX_BYPASS_APPROVED"); |
| 66 | + expect( |
| 67 | + permissionRequests.some( |
| 68 | + (request) => |
| 69 | + "requestSandboxBypass" in request && request.requestSandboxBypass === true |
| 70 | + ) |
| 71 | + ).toBe(true); |
| 72 | + expect(bypassedSearchCompleted).toBe(true); |
| 73 | + |
| 74 | + await session.disconnect(); |
| 75 | + }, |
| 76 | + TEST_TIMEOUT_MS |
| 77 | + ); |
| 78 | +}); |
0 commit comments