-
Notifications
You must be signed in to change notification settings - Fork 3.9k
fix(sandbox): withhold workbench certification after an unprovenanced file mount #8456
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
217f28a
fix(sandbox): withhold workbench certification after an unprovenanced…
waleedlatif1 41818fb
test(sandbox): certify workbench history against real Redis and close…
waleedlatif1 f050a04
test(sandbox): restore only the env the certification suite changed
waleedlatif1 cfce8b3
test(sandbox): close the shared Redis client after the certification …
waleedlatif1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
apps/sim/lib/execution/remote-sandbox/session-input-certification.integration.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| /** | ||
| * The persistent workbench's input history is what lets a scratch file reach the model. This runs | ||
| * the real code boundary against the real history script in a disposable Redis, so a mount the | ||
| * caller could not classify has to leave the machine uncertified. Only the sandbox provider is a | ||
| * stand-in: it hands back an existing machine that executes nothing. | ||
| * | ||
| * Set TEST_REDIS_URL to an isolated local Redis service. | ||
| */ | ||
| import { createHash } from 'node:crypto' | ||
| import { | ||
| remoteSandboxProviderMock, | ||
| remoteSandboxProviderMockFns, | ||
| } from '@sim/testing/mocks/remote-sandbox-provider.mock' | ||
| import { generateShortId } from '@sim/utils/id' | ||
| import { afterAll, describe, expect, it, vi } from 'vitest' | ||
|
|
||
| const { redisUrl, inheritedRedisUrl, mockFindSessionSandbox } = await vi.hoisted(async () => { | ||
| const { readTestRedisUrl } = await import('@sim/db/testing/test-infrastructure') | ||
| const url = readTestRedisUrl() | ||
| const inheritedRedisUrl = process.env.REDIS_URL | ||
| /** The real Redis module reads this at import. */ | ||
| if (url) process.env.REDIS_URL = url | ||
| return { redisUrl: url, inheritedRedisUrl, mockFindSessionSandbox: vi.fn() } | ||
| }) | ||
|
|
||
| vi.mock('@/lib/execution/remote-sandbox/provider', () => remoteSandboxProviderMock) | ||
| vi.mock('@/lib/execution/remote-sandbox/resolve', () => ({ | ||
| resolveWorkspaceSandbox: async () => null, | ||
| provisionRuntimeDependencies: async () => {}, | ||
| repairMissingSandboxImage: async () => null, | ||
| RUNTIME_INSTALL_TIMEOUT_MS: 60_000, | ||
| })) | ||
|
|
||
| import { closeRedisConnection, getRedisClient } from '@/lib/core/config/redis' | ||
| import { CodeLanguage } from '@/lib/execution/languages' | ||
| import { | ||
| executeInSandbox, | ||
| executeShellInSandbox, | ||
| SIM_RESULT_PREFIX, | ||
| } from '@/lib/execution/remote-sandbox' | ||
| import { observeSandboxSessionInputs } from '@/lib/execution/remote-sandbox/execution-observer' | ||
| import { | ||
| initializeSessionFileProvenance, | ||
| isSessionFileProvenanceClean, | ||
| } from '@/lib/execution/remote-sandbox/session-file-provenance' | ||
| import type { SandboxHandle, SandboxProvider } from '@/lib/execution/remote-sandbox/types' | ||
|
|
||
| remoteSandboxProviderMockFns.mockResolveProvider.mockImplementation( | ||
| () => | ||
| ({ | ||
| id: 'e2b', | ||
| dependencyStrategy: 'prebuilt', | ||
| resolveLifetimeMs: (ms: number) => ms, | ||
| create: async () => { | ||
| throw new Error('The certification fixture only reuses an existing machine') | ||
| }, | ||
| findSessionSandbox: mockFindSessionSandbox, | ||
| }) satisfies SandboxProvider | ||
| ) | ||
|
|
||
| function machine(sandboxId: string): SandboxHandle { | ||
| return { | ||
| sandboxId, | ||
| runCode: async () => ({ text: `${SIM_RESULT_PREFIX}{"ok":true}`, stdout: '', stderr: '' }), | ||
| runCommand: async () => ({ stdout: '', stderr: '', exitCode: 0 }), | ||
| extendLifetime: async () => {}, | ||
| getFileSize: async () => 0, | ||
| readFile: async () => '', | ||
| readFileWithLimit: async () => ({ content: '', byteLength: 0 }), | ||
| writeFile: async () => {}, | ||
| removeFile: async () => {}, | ||
| listFiles: async () => [], | ||
| kill: async () => {}, | ||
| } | ||
| } | ||
|
|
||
| /** Machine-history keys this suite created, so cleanup never touches another suite's state. */ | ||
| const createdKeys: string[] = [] | ||
|
|
||
| afterAll(async () => { | ||
| if (redisUrl && createdKeys.length) { | ||
| const redis = getRedisClient() | ||
| if (redis) await redis.del(...createdKeys) | ||
| } | ||
| if (redisUrl) await closeRedisConnection() | ||
| // Only restore what the hoisted setup changed; assigning undefined would store the string "undefined". | ||
| if (!redisUrl) return | ||
| if (inheritedRedisUrl === undefined) Reflect.deleteProperty(process.env, 'REDIS_URL') | ||
| else process.env.REDIS_URL = inheritedRedisUrl | ||
| }) | ||
|
|
||
| describe.skipIf(!redisUrl)('workbench certification at the code boundary', () => { | ||
| it.each([ | ||
| ['code', false], | ||
| ['code', true], | ||
| ['shell', false], | ||
| ['shell', true], | ||
| ] as const)('%s with unprovenanced mounts %s', async (kind, unprovenanced) => { | ||
| const sandboxId = `machine-${generateShortId(12)}` | ||
| const key = `certification-${generateShortId(12)}` | ||
| const identity = { providerId: 'e2b', sandboxId } as const | ||
| createdKeys.push( | ||
| `mothership:workbench-provenance:v1:${createHash('sha256') | ||
| .update(JSON.stringify([key, identity.providerId, sandboxId])) | ||
| .digest('hex')}` | ||
| ) | ||
| mockFindSessionSandbox.mockResolvedValue(machine(sandboxId)) | ||
| await initializeSessionFileProvenance(key, identity) | ||
| expect(await isSessionFileProvenanceClean(key, identity)).toBe(true) | ||
|
|
||
| const request = { | ||
| code: 'print(1)', | ||
| language: CodeLanguage.Python, | ||
| timeoutMs: 30_000, | ||
| session: { key, ...(unprovenanced ? { unprovenancedInputs: true } : {}) }, | ||
| } | ||
| await observeSandboxSessionInputs( | ||
| () => true, | ||
| () => | ||
| kind === 'code' | ||
| ? executeInSandbox(request) | ||
| : executeShellInSandbox({ ...request, envs: {} }) | ||
| ) | ||
| expect(await isSessionFileProvenanceClean(key, identity)).toBe(!unprovenanced) | ||
| }) | ||
| }) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.