Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/app/api/runtimes/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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),
}))
Expand Down
32 changes: 31 additions & 1 deletion src/lib/runtime-api-dto.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -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" });
});
});
11 changes: 10 additions & 1 deletion src/lib/runtime-api-dto.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,18 @@
import { removeRuntimeAuthSecretsFromMetadata } from "./runtime-device-auth";

export function toBrowserSafeRuntimeMetadata(metadata: unknown) {
return removeRuntimeAuthSecretsFromMetadata(metadata);
}

export function toBrowserSafeRuntime<
T extends Record<string, unknown> & { authToken?: unknown },
>(runtime: T): Omit<T, "authToken"> & { 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<T, "authToken"> & { hasAuthToken: boolean };
}
Loading