🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
fix(sandbox): redact tool payloads from runtime logs (#16308)
Ben Bacheme392c6659b72
diff --git a/packages/in-app-agent-sandbox-runtime/src/logging.ts b/packages/in-app-agent-sandbox-runtime/src/logging.tsnew file mode 100644index 0000000000..637ce0ec02--- /dev/null+++ b/packages/in-app-agent-sandbox-runtime/src/logging.ts@@ -0,0 +1,64 @@+import type { SandboxOperation } from "./contracts.js";++type BashResult = {+ stdout: string;+ stderr: string;+ exitCode: number;+ startedAt: string;+ completedAt: string;+};++export function summarizeReadResult(path: string, content: string | null) {+ return {+ path,+ found: content !== null,+ contentBytes: content === null ? 0 : Buffer.byteLength(content, "utf8"),+ };+}++export function summarizeBashResult(result: BashResult) {+ return {+ exitCode: result.exitCode,+ startedAt: result.startedAt,+ completedAt: result.completedAt,+ stdoutBytes: Buffer.byteLength(result.stdout, "utf8"),+ stderrBytes: Buffer.byteLength(result.stderr, "utf8"),+ };+}++export function summarizeOperation(body: SandboxOperation) {+ if (body.operation === "read") {+ return { operation: body.operation, path: body.path };+ }++ if (body.operation === "write") {+ return {+ operation: body.operation,+ path: body.path,+ contentBytes: Buffer.byteLength(body.content, "utf8"),+ };+ }++ if (body.operation === "edit") {+ return {+ operation: body.operation,+ path: body.path,+ oldTextLength: body.oldText.length,+ newTextLength: body.newText.length,+ };+ }++ if (body.operation === "bash") {+ return {+ operation: body.operation,+ timeoutMs: body.timeoutMs ?? null,+ commandBytes: Buffer.byteLength(body.command, "utf8"),+ };+ }++ return assertUnreachable(body);+}++function assertUnreachable(_value: never): never {+ throw new Error("Unhandled sandbox operation");+}diff --git a/packages/in-app-agent-sandbox-runtime/src/server.ts b/packages/in-app-agent-sandbox-runtime/src/server.tsindex f8d6dc9944..4f65cde48a 100644--- a/packages/in-app-agent-sandbox-runtime/src/server.ts+++ b/packages/in-app-agent-sandbox-runtime/src/server.ts@@ -14,9 +14,13 @@ import {type EditSandboxOperation,type ReadSandboxOperation,type SandboxFile,- type SandboxOperation,type WriteSandboxOperation,} from "./contracts.js";+import {+ summarizeBashResult,+ summarizeOperation,+ summarizeReadResult,+} from "./logging.js";const SERVER_PORT = 5000;const WORKSPACE_ROOT = "/workspace";@@ -176,7 +180,7 @@ async function readOperation(body: ReadSandboxOperation, requestId: string) {logSandboxServer("read.complete", {requestId,path: body.path,- result,+ result: summarizeReadResult(filePath, content),});return { result };}@@ -240,7 +244,7 @@ async function bashOperation(body: BashSandboxOperation, requestId: string) {const result = await runCommand(body.command, body.timeoutMs, requestId);logSandboxServer("bash.complete", {requestId,- result,+ result: summarizeBashResult(result),});return { result };}@@ -319,7 +323,7 @@ function runCommand(logSandboxServer("bash.start", {requestId,pid: child.pid ?? null,- command,+ commandBytes: Buffer.byteLength(command, "utf8"),timeoutMs: timeoutMs ?? null,});@@ -414,32 +418,6 @@ function logSandboxServer(event: string, details?: Record<string, unknown>) {console.log(`[sandbox] ${new Date().toISOString()} ${event}${payload}`);}-function summarizeOperation(body: SandboxOperation) {- switch (body.operation) {- case "read":- return { operation: body.operation, path: body.path };- case "write":- return {- operation: body.operation,- path: body.path,- contentBytes: Buffer.byteLength(body.content, "utf8"),- };- case "edit":- return {- operation: body.operation,- path: body.path,- oldTextLength: body.oldText.length,- newTextLength: body.newText.length,- };- case "bash":- return {- operation: body.operation,- timeoutMs: body.timeoutMs ?? null,- command: body.command,- };- }-}-function resolveSandboxPath(requestPath: string) {const candidate = path.isAbsolute(requestPath)? requestPathdiff --git a/packages/in-app-agent-sandbox-runtime/tests/sandbox.test.ts b/packages/in-app-agent-sandbox-runtime/tests/sandbox.test.tsindex 1655af6075..49f40ca4fe 100644--- a/packages/in-app-agent-sandbox-runtime/tests/sandbox.test.ts+++ b/packages/in-app-agent-sandbox-runtime/tests/sandbox.test.ts@@ -177,6 +177,93 @@ describe("sandbox runtime docker container", () => {}),});+ const sensitiveFileContent = "customer-read-content-must-not-be-logged";+ await requestJson(baseUrl, "/sandbox", {+ method: "POST",+ body: JSON.stringify({+ operation: "write",+ path: "sensitive.txt",+ content: sensitiveFileContent,+ }),+ });+ await requestJson(baseUrl, "/sandbox", {+ method: "POST",+ body: JSON.stringify({+ operation: "read",+ path: "sensitive.txt",+ }),+ });++ const sensitiveOldText = "customer-edit-old-text-must-not-be-logged";+ const sensitiveNewText = "customer-edit-new-text-must-not-be-logged";+ await requestJson(baseUrl, "/sandbox", {+ method: "POST",+ body: JSON.stringify({+ operation: "write",+ path: "edit-sensitive.txt",+ content: sensitiveOldText,+ }),+ });+ await requestJson(baseUrl, "/sandbox", {+ method: "POST",+ body: JSON.stringify({+ operation: "edit",+ path: "edit-sensitive.txt",+ oldText: sensitiveOldText,+ newText: sensitiveNewText,+ }),+ });++ const sensitiveToolCallContent =+ "customer-tool-call-content-must-not-be-logged";+ await requestJson(baseUrl, "/sandbox", {+ method: "POST",+ body: JSON.stringify({+ operation: "bash",+ command: "true",+ toolCallFiles: [+ {+ path: "/workspace/tool_calls/sensitive.txt",+ content: sensitiveToolCallContent,+ },+ ],+ }),+ });++ const sensitiveCommand =+ "printf customer-stdout-must-not-be-logged; printf customer-stderr-must-not-be-logged >&2 # customer-command-must-not-be-logged";+ await requestJson(baseUrl, "/sandbox", {+ method: "POST",+ body: JSON.stringify({+ operation: "bash",+ command: sensitiveCommand,+ }),+ });++ const logs = await getContainerLogs(container!);+ expect(logs).not.toContain(sensitiveFileContent);+ expect(logs).not.toContain(sensitiveOldText);+ expect(logs).not.toContain(sensitiveNewText);+ expect(logs).not.toContain(sensitiveToolCallContent);+ expect(logs).not.toContain("customer-command-must-not-be-logged");+ expect(logs).not.toContain("customer-stdout-must-not-be-logged");+ expect(logs).not.toContain("customer-stderr-must-not-be-logged");+ expect(logs).toContain(+ `"contentBytes":${Buffer.byteLength(sensitiveFileContent, "utf8")}`,+ );+ expect(logs).toContain(+ `"oldTextLength":${sensitiveOldText.length},"newTextLength":${sensitiveNewText.length}`,+ );+ expect(logs).toContain(+ `"commandBytes":${Buffer.byteLength(sensitiveCommand, "utf8")}`,+ );+ expect(logs).toContain(+ `"stdoutBytes":${Buffer.byteLength("customer-stdout-must-not-be-logged", "utf8")}`,+ );+ expect(logs).toContain(+ `"stderrBytes":${Buffer.byteLength("customer-stderr-must-not-be-logged", "utf8")}`,+ );+const escaped = await requestJson(baseUrl, "/sandbox", {method: "POST",body: JSON.stringify({@@ -278,12 +365,7 @@ async function waitForHealth(baseUrl: string, container: Docker.Container) {await new Promise((resolve) => setTimeout(resolve, 250));}- const logs = await container- .logs({ stdout: true, stderr: true, tail: 50 })- .catch(() => Buffer.from(""));- const logText = Buffer.isBuffer(logs)- ? logs.toString("utf8")- : await readStreamToString(logs);+ const logText = await getContainerLogs(container);throw new Error([@@ -296,6 +378,15 @@ async function waitForHealth(baseUrl: string, container: Docker.Container) {);}+async function getContainerLogs(container: Docker.Container) {+ const logs = await container+ .logs({ stdout: true, stderr: true, tail: 50 })+ .catch(() => Buffer.from(""));+ return Buffer.isBuffer(logs)+ ? logs.toString("utf8")+ : await readStreamToString(logs);+}+function readStreamToString(stream: NodeJS.ReadableStream) {return new Promise<string>((resolve, reject) => {const chunks: Buffer[] = [];