diff --git a/src/app/api/runtimes/route.ts b/src/app/api/runtimes/route.ts index 61b9059..e5d5818 100644 --- a/src/app/api/runtimes/route.ts +++ b/src/app/api/runtimes/route.ts @@ -9,7 +9,7 @@ import { deriveRuntimeTrustSummary } from "@/lib/runtime-trust"; import { resolveAccessibleWorkspace } from "@/lib/workspace"; import { normalizeHermesRootUrl } from "@/lib/runtimes/providers/hermes"; import { sealRuntimeAuthToken } from "@/lib/runtime-token-crypto"; -import { toBrowserSafeRuntime } from "@/lib/runtime-api-dto"; +import { toBrowserSafeRuntime, toBrowserSafeRuntimeMetadata } from "@/lib/runtime-api-dto"; export const dynamic = "force-dynamic"; @@ -46,6 +46,7 @@ export async function GET(request: NextRequest) { return NextResponse.json( runtimes.map((runtime) => ({ ...runtime, + metadata: toBrowserSafeRuntimeMetadata(runtime.metadata), capabilitySnapshot: readCapabilitySnapshot(runtime.metadata), trustSummary: deriveRuntimeTrustSummary(runtime), })) diff --git a/src/lib/runtime-api-dto.test.ts b/src/lib/runtime-api-dto.test.ts index 79e862e..d4b2d8a 100644 --- a/src/lib/runtime-api-dto.test.ts +++ b/src/lib/runtime-api-dto.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from "vitest"; -import { toBrowserSafeRuntime } from "./runtime-api-dto"; +import { toBrowserSafeRuntime, toBrowserSafeRuntimeMetadata } from "./runtime-api-dto"; describe("runtime API DTO", () => { it("replaces stored ciphertext with a non-sensitive configured flag", () => { @@ -24,4 +24,34 @@ describe("runtime API DTO", () => { hasAuthToken: false, }); }); + + it("removes device authentication secrets from runtime metadata", () => { + const dto = toBrowserSafeRuntime({ + id: "runtime-3", + authToken: "shared-token-ciphertext", + metadata: { + workspaceId: "workspace-1", + devicePrivateKeyPem: "private-key-ciphertext", + openclawDeviceAuth: { + tokenCiphertext: "device-token-ciphertext", + }, + }, + }); + + expect(dto).toEqual({ + id: "runtime-3", + hasAuthToken: true, + metadata: { workspaceId: "workspace-1" }, + }); + expect(JSON.stringify(dto)).not.toContain("private-key-ciphertext"); + expect(JSON.stringify(dto)).not.toContain("device-token-ciphertext"); + }); + + it("sanitizes metadata selected without the runtime token column", () => { + expect(toBrowserSafeRuntimeMetadata({ + label: "local", + devicePrivateKeyPem: "private-key", + openclawDeviceAuth: { tokenCiphertext: "device-token" }, + })).toEqual({ label: "local" }); + }); }); diff --git a/src/lib/runtime-api-dto.ts b/src/lib/runtime-api-dto.ts index 60a7b1b..edaa748 100644 --- a/src/lib/runtime-api-dto.ts +++ b/src/lib/runtime-api-dto.ts @@ -1,9 +1,18 @@ +import { removeRuntimeAuthSecretsFromMetadata } from "./runtime-device-auth"; + +export function toBrowserSafeRuntimeMetadata(metadata: unknown) { + return removeRuntimeAuthSecretsFromMetadata(metadata); +} + export function toBrowserSafeRuntime< T extends Record & { authToken?: unknown }, >(runtime: T): Omit & { hasAuthToken: boolean } { const { authToken, ...safeRuntime } = runtime; return { ...safeRuntime, + ...(Object.hasOwn(safeRuntime, "metadata") + ? { metadata: toBrowserSafeRuntimeMetadata(safeRuntime.metadata) } + : {}), hasAuthToken: typeof authToken === "string" && authToken.length > 0, - }; + } as Omit & { hasAuthToken: boolean }; }