|
| 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 | +const TEST_NAME = "approves a blocked search and executes it outside the sandbox"; |
| 14 | + |
| 15 | +describe("Sandbox bypass", async () => { |
| 16 | + if (process.platform !== "darwin") { |
| 17 | + // SDK runners provide a sandbox backend only on macOS (no bwrap/BaseContainer elsewhere). |
| 18 | + it.skip(TEST_NAME, () => undefined); |
| 19 | + return; |
| 20 | + } |
| 21 | + |
| 22 | + const { copilotClient: client, workDir } = await createSdkTestContext({ |
| 23 | + copilotClientOptions: { |
| 24 | + env: { COPILOT_CLI_ENABLED_FEATURE_FLAGS: "SANDBOX" }, |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + it( |
| 29 | + TEST_NAME, |
| 30 | + async () => { |
| 31 | + const vaultDir = join(workDir, "vault"); |
| 32 | + await mkdir(vaultDir, { recursive: true }); |
| 33 | + await writeFile(join(vaultDir, "notes.txt"), "OUTSIDE_MATCH_LINE bypass-approved\n"); |
| 34 | + |
| 35 | + const permissionRequests: PermissionRequest[] = []; |
| 36 | + let bypassedSearchCompleted = false; |
| 37 | + const session = await client.createSession({ |
| 38 | + onPermissionRequest: (request) => { |
| 39 | + permissionRequests.push(request); |
| 40 | + return { kind: "approve-once" }; |
| 41 | + }, |
| 42 | + }); |
| 43 | + const update = await session.rpc.options.update({ |
| 44 | + sandboxConfig: { |
| 45 | + enabled: true, |
| 46 | + allowBypass: true, |
| 47 | + addCurrentWorkingDirectory: true, |
| 48 | + userPolicy: { filesystem: { deniedPaths: [vaultDir] } }, |
| 49 | + }, |
| 50 | + }); |
| 51 | + expect(update.success).toBe(true); |
| 52 | + let grepToolCallId: string | undefined; |
| 53 | + session.on((event) => { |
| 54 | + if (event.type === "tool.execution_start" && event.data.toolName === "grep") { |
| 55 | + grepToolCallId = event.data.toolCallId; |
| 56 | + } else if ( |
| 57 | + event.type === "tool.execution_complete" && |
| 58 | + event.data.toolCallId === grepToolCallId && |
| 59 | + event.data.success && |
| 60 | + event.data.result?.content.includes("OUTSIDE_MATCH_LINE bypass-approved") |
| 61 | + ) { |
| 62 | + bypassedSearchCompleted = true; |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + const message = await session.sendAndWait( |
| 67 | + { |
| 68 | + prompt: |
| 69 | + "Search for OUTSIDE_MATCH_LINE in the vault directory. " + |
| 70 | + "After the search succeeds, reply with exactly SANDBOX_BYPASS_APPROVED.", |
| 71 | + }, |
| 72 | + SEND_TIMEOUT_MS |
| 73 | + ); |
| 74 | + |
| 75 | + expect(message?.data.content).toContain("SANDBOX_BYPASS_APPROVED"); |
| 76 | + expect( |
| 77 | + permissionRequests.some( |
| 78 | + (request) => |
| 79 | + "requestSandboxBypass" in request && request.requestSandboxBypass === true |
| 80 | + ) |
| 81 | + ).toBe(true); |
| 82 | + expect(bypassedSearchCompleted).toBe(true); |
| 83 | + |
| 84 | + await session.disconnect(); |
| 85 | + }, |
| 86 | + TEST_TIMEOUT_MS |
| 87 | + ); |
| 88 | +}); |
0 commit comments