Files
openclaw-studio/tests/unit/agentStateExecutor.test.ts
T
George Pickett 732b994120 Harden agent id validation, session keys, agent-state rollback, and gateway/auth reliability
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.
2026-06-22 10:06:22 -07:00

71 lines
2.5 KiB
TypeScript

import { beforeEach, describe, expect, it, vi } from "vitest";
import { runSshJson } from "@/lib/ssh/gateway-host";
import {
restoreAgentStateOverSsh,
trashAgentStateOverSsh,
} from "@/lib/ssh/agent-state";
vi.mock("@/lib/ssh/gateway-host", () => ({
runSshJson: vi.fn(),
}));
describe("agent state ssh executor", () => {
const mockedRunSshJson = vi.mocked(runSshJson);
beforeEach(() => {
mockedRunSshJson.mockReset();
});
it("trashes agent state via ssh", () => {
mockedRunSshJson.mockReturnValueOnce({ trashDir: "/tmp/trash", moved: [] });
const result = trashAgentStateOverSsh({ sshTarget: "me@host", agentId: "my-agent" });
expect(result).toEqual({ trashDir: "/tmp/trash", moved: [] });
expect(runSshJson).toHaveBeenCalledTimes(1);
expect(runSshJson).toHaveBeenCalledWith(
expect.objectContaining({
sshTarget: "me@host",
argv: ["bash", "-s", "--", "my-agent"],
label: "trash agent state (my-agent)",
input: expect.stringContaining('python3 - "$1"'),
})
);
const call = mockedRunSshJson.mock.calls[0]?.[0];
expect(call?.input).toContain("workspace-{agent_id}");
expect(call?.input).toContain("rollback_moves");
expect(call?.input).toContain("Rollback also failed");
});
it("restores agent state via ssh", () => {
mockedRunSshJson.mockReturnValueOnce({ restored: [] });
const result = restoreAgentStateOverSsh({
sshTarget: "me@host",
agentId: "my-agent",
trashDir: "/tmp/trash",
});
expect(result).toEqual({ restored: [] });
expect(runSshJson).toHaveBeenCalledTimes(1);
expect(runSshJson).toHaveBeenCalledWith(
expect.objectContaining({
sshTarget: "me@host",
argv: ["bash", "-s", "--", "my-agent", "/tmp/trash"],
label: "restore agent state (my-agent)",
input: expect.stringContaining('python3 - "$1" "$2"'),
})
);
const call = mockedRunSshJson.mock.calls[0]?.[0];
expect(call?.input).toContain("Refusing to restore source outside trashDir");
expect(call?.input).toContain("Refusing to restore symlink outside stateDir");
expect(call?.input).toContain("resolve_restored_symlink_target(dest, os.readlink(src))");
expect(call?.input).toContain('trash_root = base / "trash" / "studio-delete-agent"');
expect(call?.input).toContain("trashDir is not under {trash_root}");
expect(call?.input).not.toMatch(/^\t/m);
expect(call?.input).toContain("rollback_moves");
expect(call?.input).toContain("Rollback also failed");
});
});