Files
bot/scripts/check-no-raw-window-open.mjs
zeekay 635dffde60 refactor(bot): retire Lit control UI for @hanzo/gui v7 admin SPA
The legacy "openclaw-control-ui" Lit app at ui/ is replaced by
@hanzo/bot-admin (~/work/hanzo/gui/apps/admin-bot), built externally
in the gui workspace and synced into dist/control-ui/ via
scripts/sync-admin-ui.sh. Identical static-serve handler — the bot
already configures gateway.controlUi.basePath, just defaults to
/_/bot now to match the canonical hanzo-binary mount.

Changes:

- scripts/sync-admin-ui.sh — rsyncs the built admin-bot bundle into
  dist/control-ui/. Mirrors tasks/scripts/sync-admin-ui.sh exactly.

- src/gateway/iam-identity.ts (+ test) — pure-trust extractor for
  X-Org-Id / X-User-Id / X-User-Email headers set by hanzoai/gateway
  upstream after JWT validation. attachIamIdentity() runs once per
  HTTP request from server-http.ts handleRequest. The bot does NOT
  validate the JWT itself — that's the gateway's job.

- src/infra/control-ui-assets.ts — sentinel for repo root flipped
  from ui/vite.config.ts to scripts/sync-admin-ui.sh; existing
  resolveControlUiRootSync continues to look for dist/control-ui/
  unchanged. Test updated.

- src/data/moonshot-kimi-k2.ts — moved from the deleted ui/ tree
  (only consumer is scripts/sync-moonshot-docs.ts).

- scripts/ui.js — replaced with a 50-line shim:
    pnpm ui:build → calls scripts/sync-admin-ui.sh
    pnpm ui:dev   → prints the gui-workspace dev pointer
    pnpm ui:install → no-op (admin-bot inherits gui workspace deps)

- Dockerfile — drops the COPY of ui/package.json (ui/ is gone) and
  the RUN pnpm ui:build step (the synced dist/control-ui/ is now
  pulled via the existing COPY . . step).

- vitest.config.ts — drops the four ui/src/ui/ test paths and the
  ui/** coverage exclude.

- src/config/schema.{help,hints}.ts — basePath placeholder /openclaw
  replaced with the canonical /_/bot mount.

Identity flow (Node-runtime exception per HANZO_BINARY.md):
  hanzoai/gateway (JWT validate, strip+set X-* headers)
   → bot.handleRequest()
   → attachIamIdentity(req)
   → downstream handlers via getIamIdentity(req)

Tests: 43 passing across iam-identity (8), control-ui-routing (6),
control-ui.http (18), control-ui-csp (2), control-ui-assets (9).

Repo size: -105,289 / +328 lines.
2026-04-27 19:02:33 -07:00

87 lines
2.4 KiB
JavaScript

#!/usr/bin/env node
import { promises as fs } from "node:fs";
import path from "node:path";
import ts from "typescript";
import {
collectTypeScriptFiles,
resolveRepoRoot,
runAsScript,
toLine,
unwrapExpression,
} from "./lib/ts-guard-utils.mjs";
const repoRoot = resolveRepoRoot(import.meta.url);
const uiSourceDir = path.join(repoRoot, "ui", "src", "ui");
const allowedCallsites = new Set([path.join(uiSourceDir, "open-external-url.ts")]);
function asPropertyAccess(expression) {
if (ts.isPropertyAccessExpression(expression)) {
return expression;
}
if (typeof ts.isPropertyAccessChain === "function" && ts.isPropertyAccessChain(expression)) {
return expression;
}
return null;
}
function isRawWindowOpenCall(expression) {
const propertyAccess = asPropertyAccess(unwrapExpression(expression));
if (!propertyAccess || propertyAccess.name.text !== "open") {
return false;
}
const receiver = unwrapExpression(propertyAccess.expression);
return (
ts.isIdentifier(receiver) && (receiver.text === "window" || receiver.text === "globalThis")
);
}
export function findRawWindowOpenLines(content, fileName = "source.ts") {
const sourceFile = ts.createSourceFile(fileName, content, ts.ScriptTarget.Latest, true);
const lines = [];
const visit = (node) => {
if (ts.isCallExpression(node) && isRawWindowOpenCall(node.expression)) {
lines.push(toLine(sourceFile, node.expression));
}
ts.forEachChild(node, visit);
};
visit(sourceFile);
return lines;
}
export async function main() {
const files = await collectTypeScriptFiles(uiSourceDir, {
extraTestSuffixes: [".browser.test.ts", ".node.test.ts"],
ignoreMissing: true,
});
const violations = [];
for (const filePath of files) {
if (allowedCallsites.has(filePath)) {
continue;
}
const content = await fs.readFile(filePath, "utf8");
for (const line of findRawWindowOpenLines(content, filePath)) {
const relPath = path.relative(repoRoot, filePath);
violations.push(`${relPath}:${line}`);
}
}
if (violations.length === 0) {
return;
}
console.error("Found raw window.open usage outside safe helper:");
for (const violation of violations) {
console.error(`- ${violation}`);
}
console.error("Use openExternalUrlSafe(...) — admin SPA owns its own helper in apps/admin-bot.");
process.exit(1);
}
runAsScript(import.meta.url, main);