Files
bot/src/agents/skills-install.download.test.ts
zeekay acaeda400a Eradicate OpenClaw — resolve merge conflicts and finish the rebrand
Three coordinated things:

1. Resolve the committed upstream/main merge conflicts. 94 files were
   sitting with raw <<<<<<< HEAD ... >>>>>>> upstream/main blocks. Every
   conflict had HanzoBot/Bot on HEAD and OpenClaw on upstream. Resolved
   by keeping HEAD everywhere — 540 conflict blocks.

2. Rewrite remaining OpenClaw text refs.
     OPENCLAW   → HANZO_BOT
     OpenClaw   → HanzoBot          (PascalCase type identifiers)
     openclaw   → bot               (codebase convention — botToken,
                                    botUsername etc.; avoids invalid
                                    hyphenated JS identifiers)
     openclaw[_]/openclaw[A-Z] → bot[_]/bot[A-Z]   (compound)
     ai.openclaw.x → ai.hanzo.bot.x  (JVM package path)

3. Delete or rename openclaw-named file paths (116 of them). Dead
   duplicates with a Bot-named canonical from the partial migration:
   deleted. Otherwise renamed.
     ai/openclaw/**           → deleted (ai/hanzo/bot/** canonical)
     Sources/OpenClaw*/       → deleted (Sources/Bot* canonical)
     Tests/OpenClawIPCTests/  → deleted (Tests/BotIPCTests canonical)
     OpenClawKit/             → deleted (BotKit canonical)
     openclaw-tools.*.ts      → deleted (bot-tools.*.ts canonical)
     openclaw-root.ts etc.    → deleted (bot-root.ts canonical)
     types.openclaw.ts        → deleted (types.bot.ts canonical)
     extensions/*/openclaw.plugin.json → renamed hanzo-bot.plugin.json
     docs/start/openclaw.md   → renamed hanzo-bot.md (+ zh-CN)
     docs/assets/openclaw-*.png, whatsapp-openclaw*.jpg → deleted
     scripts/.../openclaw-*   → deleted (bot/hanzo-bot parallels)
     openclaw.mjs             → deleted (hanzo-bot.mjs is package.bin)

Then patched 65 broken JS/TS import strings where hanzo-bot had ended
up inside an import path ('./types.hanzo-bot.js' → './types.bot.js'
etc., since the on-disk filename uses the brand-neutral bot- prefix).

Pre-existing lint debt cleaned up to get oxlint --type-aware to 0/0:
removed the dead i18n test that referenced a path no longer existing;
defined the missing DIDConfig and WalletConfig types in types.base.ts
(they were imported but never declared); dropped unused imports;
collapsed redundant type assertion and a tautological meta.bot lookup
that the openclaw→bot rename made redundant.

The a2ui.bundle.js generated artifact is kept at its pre-rebrand
contents — it gets regenerated by 'pnpm canvas:a2ui:bundle' from
sources I cannot rebuild in this commit; the source side is clean.

Verified: 0 occurrences of openclaw (case-insensitive) in tree source
content, 0 file or directory paths with openclaw in the name; oxlint
--type-aware src test reports 0/0.
2026-05-11 17:56:29 -07:00

388 lines
13 KiB
TypeScript

import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from "vitest";
import { installDownloadSpec } from "./skills-install-download.js";
import { setTempStateDir } from "./skills-install.download-test-utils.js";
import {
fetchWithSsrFGuardMock,
hasBinaryMock,
runCommandWithTimeoutMock,
} from "./skills-install.test-mocks.js";
import { resolveSkillToolsRootDir } from "./skills/tools-dir.js";
import type { SkillEntry, SkillInstallSpec } from "./skills/types.js";
vi.mock("../process/exec.js", () => ({
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
}));
vi.mock("../infra/net/fetch-guard.js", () => ({
fetchWithSsrFGuard: (...args: unknown[]) => fetchWithSsrFGuardMock(...args),
}));
vi.mock("./skills.js", async (importOriginal) => ({
...(await importOriginal<typeof import("./skills.js")>()),
hasBinary: (bin: string) => hasBinaryMock(bin),
}));
async function fileExists(filePath: string): Promise<boolean> {
try {
await fs.stat(filePath);
return true;
} catch {
return false;
}
}
const SAFE_ZIP_BUFFER = Buffer.from(
"UEsDBAoAAAAAAMOJVlysKpPYAgAAAAIAAAAJAAAAaGVsbG8udHh0aGlQSwECFAAKAAAAAADDiVZcrCqT2AIAAAACAAAACQAAAAAAAAAAAAAAAAAAAAAAaGVsbG8udHh0UEsFBgAAAAABAAEANwAAACkAAAAAAA==",
"base64",
);
const STRIP_COMPONENTS_ZIP_BUFFER = Buffer.from(
"UEsDBAoAAAAAAMOJVlwAAAAAAAAAAAAAAAAIAAAAcGFja2FnZS9QSwMECgAAAAAAw4lWXKwqk9gCAAAAAgAAABEAAABwYWNrYWdlL2hlbGxvLnR4dGhpUEsBAhQACgAAAAAAw4lWXAAAAAAAAAAAAAAAAAgAAAAAAAAAAAAQAAAAAAAAAHBhY2thZ2UvUEsBAhQACgAAAAAAw4lWXKwqk9gCAAAAAgAAABEAAAAAAAAAAAAAAAAAJgAAAHBhY2thZ2UvaGVsbG8udHh0UEsFBgAAAAACAAIAdQAAAFcAAAAAAA==",
"base64",
);
const ZIP_SLIP_BUFFER = Buffer.from(
"UEsDBAoAAAAAAMOJVlwAAAAAAAAAAAAAAAADAAAALi4vUEsDBAoAAAAAAMOJVlwAAAAAAAAAAAAAAAARAAAALi4vb3V0c2lkZS13cml0ZS9QSwMECgAAAAAAw4lWXD3iZKoEAAAABAAAABoAAAAuLi9vdXRzaWRlLXdyaXRlL3B3bmVkLnR4dHB3bmRQSwECFAAKAAAAAADDiVZcAAAAAAAAAAAAAAAAAwAAAAAAAAAAABAAAAAAAAAALi4vUEsBAhQACgAAAAAAw4lWXAAAAAAAAAAAAAAAABEAAAAAAAAAAAAQAAAAIQAAAC4uL291dHNpZGUtd3JpdGUvUEsBAhQACgAAAAAAw4lWXD3iZKoEAAAABAAAABoAAAAAAAAAAAAAAAAAUAAAAC4uL291dHNpZGUtd3JpdGUvcHduZWQudHh0UEsFBgAAAAADAAMAuAAAAIwAAAAAAA==",
"base64",
);
const TAR_GZ_TRAVERSAL_BUFFER = Buffer.from(
// Prebuilt archive containing ../outside-write/pwned.txt.
"H4sIAK4xm2kAA+2VvU7DMBDH3UoIUWaYLXbcS5PYZegQEKhBRUBbIT4GZBpXCqJNSFySlSdgZed1eCgcUvFRaMsQgVD9k05nW3eWz8nfR0g1GMnY98RmEvlSVMllmAyFR2QqUUEAALUsnHlG7VcPtXwO+djEhm1YlJpAbYrBYAYDhKGoA8xiFEseqaPEUvihkGJanArr92fsk5eC3/x/YWl9GZUROuA9fNjBp3hMtoZWlNWU3SrL5k8/29LpdtvjYZbxqGx1IqT0vr7WCwaEh+GNIGEU3IkhH/YEKpXRxv3FQznsPxdQpGYaZFL/RzxtCu6JqFrYOzBX/wZ81n8NmEERTosocB4Lrn8T8ED6A9EwmHp0Wd1idQK2ZVIAm1ZshlvuttPeabonuyTlUkbkO7k2nGPXcYO9q+tkPzmPk4q1hTsqqXU2K+mDxit/fQ+Lyhf9F9795+tf/WoT/Z8yi+n+/xuoz+1p8Wk0Gs3i8QJSs3VlABAAAA==",
"base64",
);
function buildEntry(name: string): SkillEntry {
const skillDir = path.join(workspaceDir, "skills", name);
return {
skill: {
name,
description: `${name} test skill`,
source: "bot-workspace",
filePath: path.join(skillDir, "SKILL.md"),
baseDir: skillDir,
disableModelInvocation: false,
},
frontmatter: {},
};
}
function buildDownloadSpec(params: {
url: string;
archive: "tar.gz" | "tar.bz2" | "zip";
targetDir: string;
stripComponents?: number;
}): SkillInstallSpec {
return {
kind: "download",
id: "dl",
url: params.url,
archive: params.archive,
extract: true,
targetDir: params.targetDir,
...(typeof params.stripComponents === "number"
? { stripComponents: params.stripComponents }
: {}),
};
}
async function installDownloadSkill(params: {
name: string;
url: string;
archive: "tar.gz" | "tar.bz2" | "zip";
targetDir: string;
stripComponents?: number;
}) {
return installDownloadSpec({
entry: buildEntry(params.name),
spec: buildDownloadSpec(params),
timeoutMs: 30_000,
});
}
function mockArchiveResponse(buffer: Uint8Array): void {
const blobPart = Uint8Array.from(buffer);
fetchWithSsrFGuardMock.mockResolvedValue({
response: new Response(new Blob([blobPart]), { status: 200 }),
release: async () => undefined,
});
}
function runCommandResult(params?: Partial<Record<"code" | "stdout" | "stderr", string | number>>) {
return {
code: 0,
stdout: "",
stderr: "",
signal: null,
killed: false,
...params,
};
}
function mockTarExtractionFlow(params: {
listOutput: string;
verboseListOutput: string;
extract: "ok" | "reject";
}) {
runCommandWithTimeoutMock.mockImplementation(async (...argv: unknown[]) => {
const cmd = (argv[0] ?? []) as string[];
if (cmd[0] === "tar" && cmd[1] === "tf") {
return runCommandResult({ stdout: params.listOutput });
}
if (cmd[0] === "tar" && cmd[1] === "tvf") {
return runCommandResult({ stdout: params.verboseListOutput });
}
if (cmd[0] === "tar" && cmd[1] === "xf") {
if (params.extract === "reject") {
throw new Error("should not extract");
}
return runCommandResult({ stdout: "ok" });
}
return runCommandResult();
});
}
let workspaceDir = "";
let stateDir = "";
beforeAll(async () => {
workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "bot-skills-install-"));
stateDir = setTempStateDir(workspaceDir);
});
afterAll(async () => {
if (workspaceDir) {
await fs.rm(workspaceDir, { recursive: true, force: true }).catch(() => undefined);
workspaceDir = "";
stateDir = "";
}
});
beforeEach(() => {
runCommandWithTimeoutMock.mockReset();
runCommandWithTimeoutMock.mockResolvedValue(runCommandResult());
fetchWithSsrFGuardMock.mockReset();
hasBinaryMock.mockReset();
hasBinaryMock.mockReturnValue(true);
});
describe("installDownloadSpec extraction safety", () => {
it("rejects archive traversal writes outside targetDir", async () => {
for (const testCase of [
{
label: "zip-slip",
name: "zip-slip",
url: "https://example.invalid/evil.zip",
archive: "zip" as const,
buffer: ZIP_SLIP_BUFFER,
},
{
label: "tar-slip",
name: "tar-slip",
url: "https://example.invalid/evil",
archive: "tar.gz" as const,
buffer: TAR_GZ_TRAVERSAL_BUFFER,
},
]) {
const entry = buildEntry(testCase.name);
const targetDir = path.join(resolveSkillToolsRootDir(entry), "target");
const outsideWritePath = path.join(workspaceDir, "outside-write", "pwned.txt");
mockArchiveResponse(new Uint8Array(testCase.buffer));
const result = await installDownloadSkill({
...testCase,
targetDir,
});
expect(result.ok, testCase.label).toBe(false);
expect(await fileExists(outsideWritePath), testCase.label).toBe(false);
}
});
it("extracts zip with stripComponents safely", async () => {
const entry = buildEntry("zip-good");
const targetDir = path.join(resolveSkillToolsRootDir(entry), "target");
mockArchiveResponse(new Uint8Array(STRIP_COMPONENTS_ZIP_BUFFER));
const result = await installDownloadSkill({
name: "zip-good",
url: "https://example.invalid/good.zip",
archive: "zip",
stripComponents: 1,
targetDir,
});
expect(result.ok).toBe(true);
expect(await fs.readFile(path.join(targetDir, "hello.txt"), "utf-8")).toBe("hi");
});
it("rejects targetDir escapes outside the per-skill tools root", async () => {
mockArchiveResponse(new Uint8Array(SAFE_ZIP_BUFFER));
const beforeFetchCalls = fetchWithSsrFGuardMock.mock.calls.length;
const result = await installDownloadSkill({
name: "relative-traversal",
url: "https://example.invalid/good.zip",
archive: "zip",
targetDir: "../outside",
});
expect(result.ok).toBe(false);
expect(result.stderr).toContain("Refusing to install outside the skill tools directory");
expect(fetchWithSsrFGuardMock.mock.calls.length).toBe(beforeFetchCalls);
expect(stateDir.length).toBeGreaterThan(0);
});
it("allows relative targetDir inside the per-skill tools root", async () => {
mockArchiveResponse(new Uint8Array(SAFE_ZIP_BUFFER));
const entry = buildEntry("relative-targetdir");
const result = await installDownloadSkill({
name: "relative-targetdir",
url: "https://example.invalid/good.zip",
archive: "zip",
targetDir: "runtime",
});
expect(result.ok).toBe(true);
expect(
await fs.readFile(
path.join(resolveSkillToolsRootDir(entry), "runtime", "hello.txt"),
"utf-8",
),
).toBe("hi");
});
});
describe("installDownloadSpec extraction safety (tar.bz2)", () => {
it("handles tar.bz2 extraction safety edge-cases", async () => {
for (const testCase of [
{
label: "rejects archives containing symlinks",
name: "tbz2-symlink",
url: "https://example.invalid/evil.tbz2",
listOutput: "link\n",
verboseListOutput: "lrwxr-xr-x 0 0 0 0 Jan 1 00:00 link -> ../outside\n",
extract: "reject" as const,
expectedOk: false,
expectedExtract: false,
expectedStderrSubstring: "link",
},
{
label: "rejects archives containing FIFO entries",
name: "tbz2-fifo",
url: "https://example.invalid/evil.tbz2",
listOutput: "evil-fifo\n",
verboseListOutput: "prw-r--r-- 0 0 0 0 Jan 1 00:00 evil-fifo\n",
extract: "reject" as const,
expectedOk: false,
expectedExtract: false,
expectedStderrSubstring: "link",
},
{
label: "rejects oversized extracted entries",
name: "tbz2-oversized",
url: "https://example.invalid/oversized.tbz2",
listOutput: "big.bin\n",
verboseListOutput: "-rw-r--r-- 0 0 0 314572800 Jan 1 00:00 big.bin\n",
extract: "reject" as const,
expectedOk: false,
expectedExtract: false,
expectedStderrSubstring: "archive entry extracted size exceeds limit",
},
{
label: "extracts safe archives with stripComponents",
name: "tbz2-ok",
url: "https://example.invalid/good.tbz2",
listOutput: "package/hello.txt\n",
verboseListOutput: "-rw-r--r-- 0 0 0 0 Jan 1 00:00 package/hello.txt\n",
stripComponents: 1,
extract: "ok" as const,
expectedOk: true,
expectedExtract: true,
},
{
label: "rejects stripComponents escapes",
name: "tbz2-strip-escape",
url: "https://example.invalid/evil.tbz2",
listOutput: "a/../b.txt\n",
verboseListOutput: "-rw-r--r-- 0 0 0 0 Jan 1 00:00 a/../b.txt\n",
stripComponents: 1,
extract: "reject" as const,
expectedOk: false,
expectedExtract: false,
},
]) {
const entry = buildEntry(testCase.name);
const targetDir = path.join(resolveSkillToolsRootDir(entry), "target");
const commandCallCount = runCommandWithTimeoutMock.mock.calls.length;
mockArchiveResponse(new Uint8Array([1, 2, 3]));
mockTarExtractionFlow({
listOutput: testCase.listOutput,
verboseListOutput: testCase.verboseListOutput,
extract: testCase.extract,
});
const result = await installDownloadSkill({
name: testCase.name,
url: testCase.url,
archive: "tar.bz2",
stripComponents: testCase.stripComponents,
targetDir,
});
expect(result.ok, testCase.label).toBe(testCase.expectedOk);
const extractionAttempted = runCommandWithTimeoutMock.mock.calls
.slice(commandCallCount)
.some((call) => (call[0] as string[])[1] === "xf");
expect(extractionAttempted, testCase.label).toBe(testCase.expectedExtract);
if (typeof testCase.expectedStderrSubstring === "string") {
expect(result.stderr.toLowerCase(), testCase.label).toContain(
testCase.expectedStderrSubstring,
);
}
}
});
it("rejects tar.bz2 archives that change after preflight", async () => {
const entry = buildEntry("tbz2-preflight-change");
const targetDir = path.join(resolveSkillToolsRootDir(entry), "target");
const commandCallCount = runCommandWithTimeoutMock.mock.calls.length;
mockArchiveResponse(new Uint8Array([1, 2, 3]));
runCommandWithTimeoutMock.mockImplementation(async (...argv: unknown[]) => {
const cmd = (argv[0] ?? []) as string[];
if (cmd[0] === "tar" && cmd[1] === "tf") {
return runCommandResult({ stdout: "package/hello.txt\n" });
}
if (cmd[0] === "tar" && cmd[1] === "tvf") {
const archivePath = String(cmd[2] ?? "");
if (archivePath) {
await fs.appendFile(archivePath, "mutated");
}
return runCommandResult({ stdout: "-rw-r--r-- 0 0 0 0 Jan 1 00:00 package/hello.txt\n" });
}
if (cmd[0] === "tar" && cmd[1] === "xf") {
throw new Error("should not extract");
}
return runCommandResult();
});
const result = await installDownloadSkill({
name: "tbz2-preflight-change",
url: "https://example.invalid/change.tbz2",
archive: "tar.bz2",
targetDir,
});
expect(result.ok).toBe(false);
expect(result.stderr).toContain("changed during safety preflight");
const extractionAttempted = runCommandWithTimeoutMock.mock.calls
.slice(commandCallCount)
.some((call) => (call[0] as string[])[1] === "xf");
expect(extractionAttempted).toBe(false);
});
});