mirror of
https://github.com/grp06/openclaw-studio.git
synced 2026-08-14 08:52:03 +00:00
Centralize OpenClaw agent id validation in a new module (src/lib/agents/agentIds.ts) and route every gateway, cron, ssh, and intent path through it. Tighten the safe-id regex to match the gateway's 64-char normalization and reserve "main" from UI creation. Add symlink-aware boundary checks and move rollback to trash/restoreAgentStateLocally and the SSH equivalent so a failed move never leaves the filesystem half-migrated, and restore refuses symlinks that escape stateDir. Validate session keys (hasMalformedAgentSessionKey, sessionKeyBelongsToAgent) and cron job fields before trusting gateway output, and compare cron agent ids case-insensitively. Refactor applyGatewayConfigPatch and exec-approvals retry to fetch the snapshot inside the retry callback, eliminating a stale-baseHash race. Harden the control-plane adapter: stop() now waits on in-flight start, times out hung sockets, and ignores stale ws event handlers via a connection epoch. Close a WebSocket upgrade auth bypass in server/index.js by routing upgrades through accessGate.allowUpgrade, make access-gate cookie values URL-safe and stop reconstructing redirect URLs from Host headers, and apply the access gate to all non-token requests rather than only /api/. Read media via realpath + boundary re-check, enforce MAX_MEDIA_BYTES on remote SSH responses, and whitelist response MIME types. Clean up SSE streams on client abort. Normalize localhost gateway URLs in studio-settings so token hints only apply when the draft URL matches, and write settings atomically. Make the Playwright port configurable (PLAYWRIGHT_PORT, default 3100) to avoid colliding with the running dev server, and ignore .worktrees in eslint. Add unit tests for the new agentIds module, gateway connect profile, disconnect-like errors, local gateway, and studio settings store, and extend existing tests to cover the new validation, rollback, retry, and normalization paths.
191 lines
6.0 KiB
TypeScript
191 lines
6.0 KiB
TypeScript
import { describe, expect, it, vi } from "vitest";
|
|
|
|
import { upsertAgentExecApprovalsPolicyViaRuntime } from "@/lib/controlplane/exec-approvals";
|
|
import { ControlPlaneGatewayError } from "@/lib/controlplane/openclaw-adapter";
|
|
import type { ControlPlaneRuntime } from "@/lib/controlplane/runtime";
|
|
|
|
describe("control-plane exec approvals policy upsert", () => {
|
|
it("rejects unsafe agent ids before reading approval policy", async () => {
|
|
const runtime = {
|
|
callGateway: vi.fn(),
|
|
} as unknown as ControlPlaneRuntime;
|
|
|
|
await expect(
|
|
upsertAgentExecApprovalsPolicyViaRuntime({
|
|
runtime,
|
|
agentId: "../agent-1",
|
|
role: "autonomous",
|
|
})
|
|
).rejects.toThrow("Invalid agentId: ../agent-1");
|
|
|
|
expect(runtime.callGateway).not.toHaveBeenCalled();
|
|
});
|
|
|
|
it("requires a base hash unless the exec approvals file is known missing", async () => {
|
|
const runtime = {
|
|
callGateway: vi.fn(async (method: string) => {
|
|
if (method === "exec.approvals.get") {
|
|
return {
|
|
path: "/tmp/approvals.json",
|
|
file: { version: 1, agents: {} },
|
|
};
|
|
}
|
|
throw new Error(`unexpected method: ${method}`);
|
|
}),
|
|
} as unknown as ControlPlaneRuntime;
|
|
|
|
await expect(
|
|
upsertAgentExecApprovalsPolicyViaRuntime({
|
|
runtime,
|
|
agentId: "agent-1",
|
|
role: "autonomous",
|
|
})
|
|
).rejects.toThrow("Exec approvals hash unavailable; re-run exec.approvals.get.");
|
|
});
|
|
|
|
it("rebuilds retry payload from latest snapshot on stale base-hash conflicts", async () => {
|
|
let getCount = 0;
|
|
let setCount = 0;
|
|
|
|
const runtime = {
|
|
callGateway: vi.fn(async (method: string, params: unknown) => {
|
|
if (method === "exec.approvals.get") {
|
|
getCount += 1;
|
|
if (getCount === 1) {
|
|
return {
|
|
path: "/tmp/approvals.json",
|
|
exists: true,
|
|
hash: "hash-1",
|
|
file: {
|
|
version: 1,
|
|
agents: {
|
|
"agent-1": {
|
|
security: "allowlist",
|
|
ask: "always",
|
|
allowlist: [{ pattern: "/bin/old" }],
|
|
},
|
|
"agent-2": {
|
|
security: "allowlist",
|
|
ask: "always",
|
|
allowlist: [{ pattern: "/bin/shared" }],
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
return {
|
|
path: "/tmp/approvals.json",
|
|
exists: true,
|
|
hash: "hash-2",
|
|
file: {
|
|
version: 1,
|
|
agents: {
|
|
"agent-1": {
|
|
security: "allowlist",
|
|
ask: "always",
|
|
allowlist: [{ pattern: "/bin/new" }],
|
|
},
|
|
"agent-2": {
|
|
security: "full",
|
|
ask: "off",
|
|
allowlist: [{ pattern: "/bin/shared" }, { pattern: "/bin/extra" }],
|
|
},
|
|
"agent-3": {
|
|
security: "allowlist",
|
|
ask: "always",
|
|
allowlist: [{ pattern: "/bin/third" }],
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
|
|
if (method === "exec.approvals.set") {
|
|
setCount += 1;
|
|
const payload = params as {
|
|
baseHash?: string;
|
|
file?: { agents?: Record<string, unknown> };
|
|
};
|
|
if (setCount === 1) {
|
|
expect(payload.baseHash).toBe("hash-1");
|
|
throw new ControlPlaneGatewayError({
|
|
code: "INVALID_REQUEST",
|
|
message: "exec approvals changed since last load; re-run exec.approvals.get and retry",
|
|
});
|
|
}
|
|
expect(payload.baseHash).toBe("hash-2");
|
|
expect(payload.file?.agents?.["agent-1"]).toBeUndefined();
|
|
expect(payload.file?.agents?.["agent-2"]).toEqual({
|
|
security: "full",
|
|
ask: "off",
|
|
allowlist: [{ pattern: "/bin/shared" }, { pattern: "/bin/extra" }],
|
|
});
|
|
expect(payload.file?.agents?.["agent-3"]).toEqual({
|
|
security: "allowlist",
|
|
ask: "always",
|
|
allowlist: [{ pattern: "/bin/third" }],
|
|
});
|
|
return { ok: true };
|
|
}
|
|
|
|
throw new Error(`unexpected method: ${method}`);
|
|
}),
|
|
} as unknown as ControlPlaneRuntime;
|
|
|
|
await upsertAgentExecApprovalsPolicyViaRuntime({
|
|
runtime,
|
|
agentId: "agent-1",
|
|
role: "conservative",
|
|
});
|
|
|
|
expect(setCount).toBe(2);
|
|
});
|
|
|
|
it("retries for reload-and-retry INVALID_REQUEST messages from openclaw node host", async () => {
|
|
let setCount = 0;
|
|
const runtime = {
|
|
callGateway: vi.fn(async (method: string, params: unknown) => {
|
|
if (method === "exec.approvals.get") {
|
|
return {
|
|
path: "/tmp/approvals.json",
|
|
exists: true,
|
|
hash: setCount === 0 ? "hash-1" : "hash-2",
|
|
file: {
|
|
version: 1,
|
|
agents: {
|
|
"agent-1": {
|
|
security: "allowlist",
|
|
ask: "always",
|
|
allowlist: [{ pattern: "/bin/tool" }],
|
|
},
|
|
},
|
|
},
|
|
};
|
|
}
|
|
if (method === "exec.approvals.set") {
|
|
setCount += 1;
|
|
const payload = params as { baseHash?: string };
|
|
if (setCount === 1) {
|
|
expect(payload.baseHash).toBe("hash-1");
|
|
throw new ControlPlaneGatewayError({
|
|
code: "INVALID_REQUEST",
|
|
message: "INVALID_REQUEST: exec approvals base hash required; reload and retry",
|
|
});
|
|
}
|
|
expect(payload.baseHash).toBe("hash-2");
|
|
return { ok: true };
|
|
}
|
|
throw new Error(`unexpected method: ${method}`);
|
|
}),
|
|
} as unknown as ControlPlaneRuntime;
|
|
|
|
await upsertAgentExecApprovalsPolicyViaRuntime({
|
|
runtime,
|
|
agentId: "agent-1",
|
|
role: "autonomous",
|
|
});
|
|
|
|
expect(setCount).toBe(2);
|
|
});
|
|
});
|