Skip to content

Commit b100f95

Browse files
author
Vikhyath Mondreti
committed
fix model provenance issues
1 parent ca0b8e4 commit b100f95

124 files changed

Lines changed: 25533 additions & 964 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎apps/sim/app/api/function/execute/route.test.ts‎

Lines changed: 170 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const {
2727
mockExecuteInIsolatedVM,
2828
mockExecuteShellInSandbox,
2929
mockFetchWorkspaceFileBuffer,
30+
mockEncryptSecret,
3031
mockGetWorkspaceFile,
3132
mockResolveWorkspaceFileReference,
3233
mockUpdateWorkspaceFileContent,
@@ -38,6 +39,10 @@ const {
3839
mockExecuteInIsolatedVM: vi.fn(),
3940
mockExecuteShellInSandbox: vi.fn(),
4041
mockFetchWorkspaceFileBuffer: vi.fn(),
42+
mockEncryptSecret: vi.fn(async (value: string) => ({
43+
encrypted: `encrypted:${value}`,
44+
iv: 'iv',
45+
})),
4146
mockGetWorkspaceFile: vi.fn(),
4247
mockResolveWorkspaceFileReference: vi.fn(),
4348
mockUpdateWorkspaceFileContent: vi.fn(),
@@ -46,6 +51,10 @@ const {
4651
mockWriteWorkspaceFileByPath: vi.fn(),
4752
}))
4853

54+
vi.mock('@/lib/core/security/encryption', () => ({
55+
encryptSecret: mockEncryptSecret,
56+
}))
57+
4958
vi.mock('@/lib/execution/isolated-vm', () => ({
5059
executeInIsolatedVM: mockExecuteInIsolatedVM,
5160
}))
@@ -650,6 +659,125 @@ describe('Function Execute API Route', () => {
650659
])
651660
})
652661

662+
it('atomically classifies text exports and acknowledges the durable v2 capability', async () => {
663+
envFlagsMock.isRemoteSandboxEnabled = true
664+
mockExecuteInSandbox.mockResolvedValueOnce({
665+
result: 'done',
666+
stdout: '',
667+
sandboxId: 'sandbox-123',
668+
exportedFiles: { '/home/user/secret.txt': 'Bearer secret-value' },
669+
})
670+
671+
const response = await POST(
672+
createMockRequest(
673+
'POST',
674+
{
675+
code: 'print("done")',
676+
language: 'python',
677+
workspaceId: 'workspace-1',
678+
envVars: { API_KEY: 'secret-value' },
679+
outputs: {
680+
files: [
681+
{
682+
path: 'files/secret.txt',
683+
sandboxPath: '/home/user/secret.txt',
684+
mimeType: 'text/plain',
685+
},
686+
],
687+
},
688+
},
689+
{
690+
'x-sim-request-private-tool-metadata': 'resolved-secret-names-durable-files-v2',
691+
}
692+
)
693+
)
694+
const data = await response.json()
695+
696+
expect(response.status).toBe(200)
697+
expect(response.headers.get('x-sim-private-tool-metadata')).toBe(
698+
'resolved-secret-names-durable-files-v2'
699+
)
700+
expect(data).not.toHaveProperty('__resolvedSecretFileNames')
701+
expect(mockWriteWorkspaceFileByPath).toHaveBeenCalledWith(
702+
expect.objectContaining({
703+
secretProvenance: {
704+
status: 'exact',
705+
entries: [{ name: 'API_KEY', encryptedValue: 'encrypted:secret-value' }],
706+
},
707+
})
708+
)
709+
})
710+
711+
it('projects resolved secrets out of persisted Function export paths', async () => {
712+
envFlagsMock.isRemoteSandboxEnabled = true
713+
mockExecuteInSandbox.mockResolvedValueOnce({
714+
result: 'done',
715+
stdout: '',
716+
sandboxId: 'sandbox-123',
717+
exportedFiles: { '/home/user/report.txt': 'safe content' },
718+
})
719+
720+
const response = await POST(
721+
createMockRequest('POST', {
722+
code: 'print("done")',
723+
language: 'python',
724+
workspaceId: 'workspace-1',
725+
envVars: { API_KEY: 'secret-value' },
726+
outputs: {
727+
files: [
728+
{
729+
path: 'files/report-secret-value.txt',
730+
sandboxPath: '/home/user/report.txt',
731+
mimeType: 'text/plain',
732+
},
733+
],
734+
},
735+
})
736+
)
737+
const data = await response.json()
738+
739+
expect(response.status).toBe(200)
740+
expect(mockWriteWorkspaceFileByPath).toHaveBeenCalledWith(
741+
expect.objectContaining({
742+
target: expect.objectContaining({ path: 'files/report-{{API_KEY}}.txt' }),
743+
})
744+
)
745+
expect(JSON.stringify(data)).not.toContain('secret-value')
746+
})
747+
748+
it('marks binary exports unknown without failing the Function execution', async () => {
749+
envFlagsMock.isRemoteSandboxEnabled = true
750+
mockExecuteInSandbox.mockResolvedValueOnce({
751+
result: 'done',
752+
stdout: '',
753+
sandboxId: 'sandbox-123',
754+
exportedFiles: { '/home/user/archive.zip': 'UEsDBA==' },
755+
})
756+
757+
const response = await POST(
758+
createMockRequest('POST', {
759+
code: 'print("done")',
760+
language: 'python',
761+
workspaceId: 'workspace-1',
762+
envVars: { API_KEY: 'secret-value' },
763+
outputs: {
764+
files: [
765+
{
766+
path: 'files/archive.zip',
767+
sandboxPath: '/home/user/archive.zip',
768+
mimeType: 'application/zip',
769+
},
770+
],
771+
},
772+
})
773+
)
774+
775+
expect(response.status).toBe(200)
776+
expect(mockWriteWorkspaceFileByPath).toHaveBeenCalledWith(
777+
expect.objectContaining({ secretProvenance: { status: 'unknown' } })
778+
)
779+
})
780+
653781
it('rejects one oversized sandbox output before creating a workspace file buffer', async () => {
654782
envFlagsMock.isRemoteSandboxEnabled = true
655783
mockExecuteInSandbox.mockResolvedValueOnce({
@@ -1420,7 +1548,8 @@ describe('Function Execute API Route', () => {
14201548
})
14211549

14221550
describe('Template Variable Resolution', () => {
1423-
it.concurrent('should resolve environment variables with {{var_name}} syntax', async () => {
1551+
it('should resolve environment variables with {{var_name}} syntax', async () => {
1552+
mockExecuteInIsolatedVM.mockResolvedValueOnce({ result: 'secret-key-123', stdout: '' })
14241553
const req = createMockRequest(
14251554
'POST',
14261555
{
@@ -1520,6 +1649,10 @@ describe('Function Execute API Route', () => {
15201649

15211650
it('compiles legacy bare and quoted Custom Tool placeholders into opaque VM bindings', async () => {
15221651
const secret = 'quote" slash\\ newline\n{{OTHER}} true 123'
1652+
mockExecuteInIsolatedVM.mockResolvedValueOnce({
1653+
result: [secret, secret, `Bearer ${secret}`],
1654+
stdout: '',
1655+
})
15231656
const response = await POST(
15241657
createMockRequest(
15251658
'POST',
@@ -1709,6 +1842,11 @@ describe('Function Execute API Route', () => {
17091842
it('routes quoted shell heredocs through private sandbox input files', async () => {
17101843
envFlagsMock.isRemoteSandboxEnabled = true
17111844
const secret = 'shell"\\\n{{OTHER}}'
1845+
mockExecuteShellInSandbox.mockResolvedValueOnce({
1846+
result: null,
1847+
stdout: `Bearer ${secret}\n$UNRELATED \`touch /tmp/nope\``,
1848+
sandboxId: 'test-shell-sandbox-id',
1849+
})
17121850

17131851
const response = await POST(
17141852
createMockRequest(
@@ -1765,7 +1903,11 @@ describe('Function Execute API Route', () => {
17651903
expect(mockExecuteInSandbox).not.toHaveBeenCalled()
17661904
})
17671905

1768-
it('reports successful references and exact secret values returned through the environment map', async () => {
1906+
it('reports exact secret values returned through placeholders and the environment map', async () => {
1907+
mockExecuteInIsolatedVM.mockResolvedValueOnce({
1908+
result: 'secret-valueother-secret',
1909+
stdout: '',
1910+
})
17691911
const envResponse = await POST(
17701912
createMockRequest(
17711913
'POST',
@@ -1830,6 +1972,11 @@ describe('Function Execute API Route', () => {
18301972

18311973
it('reports shell substitutions and exact secret output from direct environment access', async () => {
18321974
envFlagsMock.isRemoteSandboxEnabled = true
1975+
mockExecuteShellInSandbox.mockResolvedValueOnce({
1976+
result: null,
1977+
stdout: 'secret-value',
1978+
sandboxId: 'test-shell-sandbox-id',
1979+
})
18331980

18341981
const referencedResponse = await POST(
18351982
createMockRequest(
@@ -1911,11 +2058,13 @@ describe('Function Execute API Route', () => {
19112058
)
19122059

19132060
expect(response.status).toBe(200)
1914-
expect((await response.json()).__resolvedSecretNames).toEqual(['OVERSIZED_SECRET'])
2061+
expect((await response.json()).__resolvedSecretNames).toBeUndefined()
2062+
expect(response.headers.get('x-sim-private-tool-metadata')).toBeNull()
19152063
expect(mockExecuteInIsolatedVM).toHaveBeenCalled()
19162064
})
19172065

19182066
it('reports only substitutions allowed by the Function secret scope', async () => {
2067+
mockExecuteInIsolatedVM.mockResolvedValueOnce({ result: 'allowed-secret', stdout: '' })
19192068
const response = await POST(
19202069
createMockRequest(
19212070
'POST',
@@ -1935,6 +2084,7 @@ describe('Function Execute API Route', () => {
19352084
})
19362085

19372086
it('resolves a selected __proto__ secret as an own environment key', async () => {
2087+
mockExecuteInIsolatedVM.mockResolvedValueOnce({ result: 'secret-value', stdout: '' })
19382088
const response = await POST(
19392089
createMockRequest(
19402090
'POST',
@@ -1954,6 +2104,23 @@ describe('Function Execute API Route', () => {
19542104
expect((await response.json()).__resolvedSecretNames).toEqual(['__proto__'])
19552105
})
19562106

2107+
it('does not activate a referenced secret that does not cross the Function result', async () => {
2108+
mockExecuteInIsolatedVM.mockResolvedValueOnce({ result: 'safe-result', stdout: '' })
2109+
2110+
const response = await POST(
2111+
createMockRequest(
2112+
'POST',
2113+
{
2114+
code: 'const key = {{API_KEY}}; return "safe-result"',
2115+
envVars: { API_KEY: 'secret-value' },
2116+
},
2117+
{ 'x-sim-request-private-tool-metadata': 'resolved-secret-names-v1' }
2118+
)
2119+
)
2120+
2121+
expect((await response.json()).__resolvedSecretNames).toEqual([])
2122+
})
2123+
19572124
it.concurrent('should resolve tag variables with <tag_name> syntax', async () => {
19582125
const req = createMockRequest('POST', {
19592126
code: 'return <email>',

0 commit comments

Comments
 (0)