mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-08 17:38:27 +00:00
chore: Enable "curly" rule to avoid single-statement if confusion/errors.
This commit is contained in:
@@ -40,7 +40,9 @@ function resolveControlUiRoot(): string | null {
|
||||
path.resolve(process.cwd(), "dist", "control-ui"),
|
||||
].filter((dir): dir is string => Boolean(dir));
|
||||
for (const dir of candidates) {
|
||||
if (fs.existsSync(path.join(dir, "index.html"))) return dir;
|
||||
if (fs.existsSync(path.join(dir, "index.html"))) {
|
||||
return dir;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
@@ -103,8 +105,12 @@ export function handleControlUiAvatarRequest(
|
||||
opts: { basePath?: string; resolveAvatar: (agentId: string) => ControlUiAvatarResolution },
|
||||
): boolean {
|
||||
const urlRaw = req.url;
|
||||
if (!urlRaw) return false;
|
||||
if (req.method !== "GET" && req.method !== "HEAD") return false;
|
||||
if (!urlRaw) {
|
||||
return false;
|
||||
}
|
||||
if (req.method !== "GET" && req.method !== "HEAD") {
|
||||
return false;
|
||||
}
|
||||
|
||||
const url = new URL(urlRaw, "http://localhost");
|
||||
const basePath = normalizeControlUiBasePath(opts.basePath);
|
||||
@@ -112,7 +118,9 @@ export function handleControlUiAvatarRequest(
|
||||
const pathWithBase = basePath
|
||||
? `${basePath}${CONTROL_UI_AVATAR_PREFIX}/`
|
||||
: `${CONTROL_UI_AVATAR_PREFIX}/`;
|
||||
if (!pathname.startsWith(pathWithBase)) return false;
|
||||
if (!pathname.startsWith(pathWithBase)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const agentIdParts = pathname.slice(pathWithBase.length).split("/").filter(Boolean);
|
||||
const agentId = agentIdParts[0] ?? "";
|
||||
@@ -185,7 +193,9 @@ function injectControlUiConfig(html: string, opts: ControlUiInjectionOpts): stri
|
||||
)};` +
|
||||
`</script>`;
|
||||
// Check if already injected
|
||||
if (html.includes("__OPENCLAW_ASSISTANT_NAME__")) return html;
|
||||
if (html.includes("__OPENCLAW_ASSISTANT_NAME__")) {
|
||||
return html;
|
||||
}
|
||||
const headClose = html.indexOf("</head>");
|
||||
if (headClose !== -1) {
|
||||
return `${html.slice(0, headClose)}${script}${html.slice(headClose)}`;
|
||||
@@ -227,10 +237,16 @@ function serveIndexHtml(res: ServerResponse, indexPath: string, opts: ServeIndex
|
||||
}
|
||||
|
||||
function isSafeRelativePath(relPath: string) {
|
||||
if (!relPath) return false;
|
||||
if (!relPath) {
|
||||
return false;
|
||||
}
|
||||
const normalized = path.posix.normalize(relPath);
|
||||
if (normalized.startsWith("../") || normalized === "..") return false;
|
||||
if (normalized.includes("\0")) return false;
|
||||
if (normalized.startsWith("../") || normalized === "..") {
|
||||
return false;
|
||||
}
|
||||
if (normalized.includes("\0")) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -240,7 +256,9 @@ export function handleControlUiHttpRequest(
|
||||
opts?: ControlUiRequestOptions,
|
||||
): boolean {
|
||||
const urlRaw = req.url;
|
||||
if (!urlRaw) return false;
|
||||
if (!urlRaw) {
|
||||
return false;
|
||||
}
|
||||
if (req.method !== "GET" && req.method !== "HEAD") {
|
||||
res.statusCode = 405;
|
||||
res.setHeader("Content-Type", "text/plain; charset=utf-8");
|
||||
@@ -266,7 +284,9 @@ export function handleControlUiHttpRequest(
|
||||
res.end();
|
||||
return true;
|
||||
}
|
||||
if (!pathname.startsWith(`${basePath}/`)) return false;
|
||||
if (!pathname.startsWith(`${basePath}/`)) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
const root = resolveControlUiRoot();
|
||||
@@ -282,9 +302,13 @@ export function handleControlUiHttpRequest(
|
||||
const uiPath =
|
||||
basePath && pathname.startsWith(`${basePath}/`) ? pathname.slice(basePath.length) : pathname;
|
||||
const rel = (() => {
|
||||
if (uiPath === ROOT_PREFIX) return "";
|
||||
if (uiPath === ROOT_PREFIX) {
|
||||
return "";
|
||||
}
|
||||
const assetsIndex = uiPath.indexOf("/assets/");
|
||||
if (assetsIndex >= 0) return uiPath.slice(assetsIndex + 1);
|
||||
if (assetsIndex >= 0) {
|
||||
return uiPath.slice(assetsIndex + 1);
|
||||
}
|
||||
return uiPath.slice(1);
|
||||
})();
|
||||
const requested = rel && !rel.endsWith("/") ? rel : `${rel}index.html`;
|
||||
|
||||
Reference in New Issue
Block a user