Files
openclaw-studio/scripts/studio-setup.js
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

87 lines
2.2 KiB
JavaScript

const fs = require("node:fs");
const { execFileSync } = require("node:child_process");
const readline = require("node:readline/promises");
const { resolveStudioSettingsPath, writeJsonFileAtomic } = require("../server/studio-settings");
const DEFAULT_GATEWAY_URL = "ws://127.0.0.1:18789";
const parseArgs = (argv) => {
return {
force: argv.includes("--force"),
};
};
const tryReadGatewayTokenFromOpenclawCli = () => {
try {
const raw = execFileSync("openclaw", ["config", "get", "gateway.auth.token"], {
encoding: "utf8",
stdio: ["ignore", "pipe", "ignore"],
});
const token = String(raw ?? "").trim();
return token || null;
} catch {
return null;
}
};
async function main() {
const args = parseArgs(process.argv.slice(2));
const settingsPath = resolveStudioSettingsPath(process.env);
if (fs.existsSync(settingsPath) && !args.force) {
console.error(
`Studio settings already exist at ${settingsPath}. Re-run with --force to overwrite.`
);
process.exitCode = 1;
return;
}
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout,
});
try {
const urlAnswer = await rl.question(
`Upstream Gateway URL [${DEFAULT_GATEWAY_URL}]: `
);
const gatewayUrl = (urlAnswer || DEFAULT_GATEWAY_URL).trim();
if (!gatewayUrl) {
throw new Error("Gateway URL is required.");
}
const tokenDefault = tryReadGatewayTokenFromOpenclawCli();
const tokenPrompt = tokenDefault
? "Upstream Gateway Token [detected from openclaw]: "
: "Upstream Gateway Token: ";
const tokenAnswer = await rl.question(tokenPrompt);
const token = (tokenAnswer || tokenDefault || "").trim();
if (!token) {
throw new Error(
"Gateway token is required. Provide it, or install/openclaw so it can be auto-detected."
);
}
const next = {
version: 1,
gateway: {
url: gatewayUrl,
token,
},
};
writeJsonFileAtomic(settingsPath, next);
console.info(`Wrote Studio settings to ${settingsPath}.`);
} finally {
rl.close();
}
}
main().catch((err) => {
const msg = err instanceof Error ? err.message : String(err);
console.error(msg);
process.exitCode = 1;
});