Skip to content

Commit a0b859b

Browse files
fix(sandbox): redact temporary session credentials in model output (#8503)
* fix(sandbox): redact temporary session credentials in model output * fix(sandbox): unify output handling and avoid response copies * fix(sandbox): scan session output without recursion
1 parent fa32bc1 commit a0b859b

5 files changed

Lines changed: 326 additions & 36 deletions

File tree

‎apps/sim/lib/execution/remote-sandbox/types.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,11 @@ export interface SandboxSessionRequest {
9292
cli?: { path: string; content: string; runtime?: { path: string; content: string } }
9393
/** Extra environment variables present on every execution in the session. */
9494
envs?: Record<string, string>
95+
/**
96+
* Selects ephemeral callback credentials present in the returned JSON for model redaction.
97+
* They expire with the tool lease and do not contribute to machine or exported-file provenance.
98+
*/
99+
outputProvenance?: (value: unknown) => DurableSecretProvenance
95100
/**
96101
* This execution mounts bytes whose secret provenance is unknown, so the machine's input
97102
* history must not stay certified clean even when the caller's own inputs are.

‎apps/sim/lib/function-execution/execute-request.ts‎

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,11 @@ import {
9191
MAX_BLOCK_MOUNTED_FILES,
9292
SANDBOX_OUTPUT_DIR,
9393
} from '@/lib/execution/remote-sandbox/sandbox-paths'
94-
import type { SandboxCollectedFile, SandboxFile } from '@/lib/execution/remote-sandbox/types'
94+
import type {
95+
SandboxCollectedFile,
96+
SandboxFile,
97+
SandboxSessionRequest,
98+
} from '@/lib/execution/remote-sandbox/types'
9599
import { isExecutionResourceLimitError } from '@/lib/execution/resource-errors'
96100
import { MAX_FUNCTION_REFERENCES } from '@/lib/function-execution/limits'
97101
import type { SandboxExportedFile } from '@/lib/function-execution/output'
@@ -1030,6 +1034,7 @@ interface FunctionRouteExecutionContext {
10301034
runtimeFileSecretTraceRegistry?: ResolvedSecretTraceRegistry
10311035
runtimeInputProvenanceUnrecorded?: boolean
10321036
resolvedSecretTraceRegistry?: ResolvedSecretTraceRegistry
1037+
sessionOutputProvenance?: SandboxSessionRequest['outputProvenance']
10331038
}
10341039

10351040
/** Keeps bound file provenance in both ordinary Function results and exported artifact bytes. */
@@ -1276,6 +1281,12 @@ async function functionJsonResponse<T>(
12761281
context: FunctionRouteExecutionContext,
12771282
init?: ResponseInit
12781283
) {
1284+
if (context.sessionOutputProvenance && context.resolvedSecretTraceRegistry) {
1285+
await importDurableSecretProvenance(
1286+
context.resolvedSecretTraceRegistry,
1287+
context.sessionOutputProvenance(body)
1288+
)
1289+
}
12791290
const responseBody = {
12801291
...body,
12811292
largeValueKeys: context.largeValueKeys,
@@ -1543,13 +1554,14 @@ function exportUnchangedNote(sandboxPath?: string): string {
15431554
}
15441555

15451556
function exportFailure(
1557+
context: FunctionRouteExecutionContext,
15461558
error: string,
15471559
status: number,
15481560
stdout: string,
15491561
executionTime: number,
15501562
cost: FunctionExecutionCost | undefined
1551-
): NextResponse {
1552-
return NextResponse.json(
1563+
) {
1564+
return functionJsonResponse(
15531565
{
15541566
success: false,
15551567
error,
@@ -1560,6 +1572,7 @@ function exportFailure(
15601572
...(cost ? { cost } : {}),
15611573
},
15621574
},
1575+
context,
15631576
{ status }
15641577
)
15651578
}
@@ -1649,6 +1662,7 @@ async function maybeExportSandboxFileToWorkspace(args: {
16491662

16501663
if (!outputPath) {
16511664
return exportFailure(
1665+
routeContext,
16521666
'outputSandboxPath requires outputPath. Set outputPath to the destination workspace file, e.g. "files/result.csv".',
16531667
400,
16541668
stdout,
@@ -1662,6 +1676,7 @@ async function maybeExportSandboxFileToWorkspace(args: {
16621676

16631677
if (!resolvedWorkspaceId || routeContext.principal.kind !== 'delegated') {
16641678
return exportFailure(
1679+
routeContext,
16651680
'Workspace context required to save sandbox file to workspace',
16661681
400,
16671682
stdout,
@@ -1672,6 +1687,7 @@ async function maybeExportSandboxFileToWorkspace(args: {
16721687

16731688
if (exportedFileContent === undefined) {
16741689
return exportFailure(
1690+
routeContext,
16751691
`Sandbox file "${outputSandboxPath}" was not found or could not be read`,
16761692
500,
16771693
stdout,
@@ -1695,6 +1711,7 @@ async function maybeExportSandboxFileToWorkspace(args: {
16951711
const outputBytes = Buffer.byteLength(exportedFileContent, isBinary ? 'base64' : 'utf-8')
16961712
if (outputBytes > MAX_SANDBOX_OUTPUT_BYTES) {
16971713
return exportFailure(
1714+
routeContext,
16981715
`Sandbox output files exceed ${MAX_SANDBOX_OUTPUT_BYTES} bytes total`,
16991716
400,
17001717
stdout,
@@ -1779,6 +1796,7 @@ async function maybeExportSandboxFileToWorkspace(args: {
17791796
})
17801797
} catch (error) {
17811798
return exportFailure(
1799+
routeContext,
17821800
getErrorMessage(error, 'Failed to export sandbox file'),
17831801
workspaceFileExportErrorStatus(error),
17841802
stdout,
@@ -1805,6 +1823,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
18051823
if (sandboxFiles.length === 0) return null
18061824
if (sandboxFiles.length > MAX_SANDBOX_OUTPUT_FILES) {
18071825
return exportFailure(
1826+
args.routeContext,
18081827
`Too many sandbox output files requested (${sandboxFiles.length}). Maximum is ${MAX_SANDBOX_OUTPUT_FILES}.`,
18091828
400,
18101829
args.stdout,
@@ -1840,6 +1859,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
18401859
(args.workflowId ? (await getWorkflowById(args.workflowId))?.workspaceId : undefined)
18411860
if (!resolvedWorkspaceId || args.routeContext.principal.kind !== 'delegated') {
18421861
return exportFailure(
1862+
args.routeContext,
18431863
'Workspace context required to save sandbox files to workspace',
18441864
400,
18451865
args.stdout,
@@ -1855,6 +1875,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
18551875
const content = args.exportedFiles?.[sandboxPath]
18561876
if (content === undefined) {
18571877
return exportFailure(
1878+
args.routeContext,
18581879
`Sandbox file "${sandboxPath}" was not found or could not be read`,
18591880
500,
18601881
args.stdout,
@@ -1876,6 +1897,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
18761897
totalOutputBytes += size
18771898
if (totalOutputBytes > MAX_SANDBOX_OUTPUT_BYTES) {
18781899
return exportFailure(
1900+
args.routeContext,
18791901
`Sandbox output files exceed ${MAX_SANDBOX_OUTPUT_BYTES} bytes total`,
18801902
400,
18811903
args.stdout,
@@ -1928,6 +1950,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
19281950
validationPaths = validations.map((validation) => validation.vfsPath)
19291951
} catch (error) {
19301952
return exportFailure(
1953+
args.routeContext,
19311954
getErrorMessage(error, 'Invalid sandbox output destination'),
19321955
workspaceFileExportErrorStatus(error),
19331956
args.stdout,
@@ -1940,6 +1963,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
19401963
)
19411964
if (duplicateDestination) {
19421965
return exportFailure(
1966+
args.routeContext,
19431967
`Duplicate sandbox output destination: ${duplicateDestination}`,
19441968
400,
19451969
args.stdout,
@@ -1997,6 +2021,7 @@ async function maybeExportSandboxFilesToWorkspace(args: {
19972021
}
19982022
} catch (error) {
19992023
return exportFailure(
2024+
args.routeContext,
20002025
getErrorMessage(error, 'Failed to export sandbox files'),
20012026
workspaceFileExportErrorStatus(error),
20022027
args.stdout,
@@ -2145,7 +2170,8 @@ async function collectSandboxOutputFiles(args: {
21452170
// reporting success without them would read as "your script wrote nothing".
21462171
if (!resolvedWorkspaceId || !args.workflowId || !args.executionId) {
21472172
return {
2148-
response: exportFailure(
2173+
response: await exportFailure(
2174+
routeContext,
21492175
'Workspace, workflow, and execution context are required to return files from the sandbox.',
21502176
400,
21512177
args.stdout,
@@ -2176,7 +2202,8 @@ async function collectSandboxOutputFiles(args: {
21762202
) {
21772203
await discardUploadedExecutionFiles(files)
21782204
return {
2179-
response: exportFailure(
2205+
response: await exportFailure(
2206+
routeContext,
21802207
`Sandbox output file "${name}" contains a resolved secret value and was not returned. Write the file without embedding secret values, or export it to a workspace file where its provenance can be recorded.`,
21812208
400,
21822209
args.stdout,
@@ -2487,6 +2514,7 @@ export async function executeFunctionRequest(
24872514
),
24882515
mountedFileSecretProvenanceScanner,
24892516
resolvedSecretTraceRegistry: auth.resolvedSecretTraceRegistry,
2517+
sessionOutputProvenance: admittedSession?.outputProvenance,
24902518
}
24912519

24922520
const lang = isValidCodeLanguage(language) ? language : DEFAULT_CODE_LANGUAGE

0 commit comments

Comments
 (0)