refactor(test): snapshot skills install state dir env

This commit is contained in:
Peter Steinberger
2026-02-21 18:38:25 +00:00
parent 884166c7af
commit b44aa5b1f7

View File

@@ -4,6 +4,7 @@ import path from "node:path";
import JSZip from "jszip"; import JSZip from "jszip";
import * as tar from "tar"; import * as tar from "tar";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { captureEnv } from "../test-utils/env.js";
import { setTempStateDir, writeDownloadSkill } from "./skills-install.download-test-utils.js"; import { setTempStateDir, writeDownloadSkill } from "./skills-install.download-test-utils.js";
import { installSkill } from "./skills-install.js"; import { installSkill } from "./skills-install.js";
@@ -11,15 +12,7 @@ const runCommandWithTimeoutMock = vi.fn();
const scanDirectoryWithSummaryMock = vi.fn(); const scanDirectoryWithSummaryMock = vi.fn();
const fetchWithSsrFGuardMock = vi.fn(); const fetchWithSsrFGuardMock = vi.fn();
const originalOpenClawStateDir = process.env.OPENCLAW_STATE_DIR; let envSnapshot: ReturnType<typeof captureEnv>;
afterEach(() => {
if (originalOpenClawStateDir === undefined) {
delete process.env.OPENCLAW_STATE_DIR;
} else {
process.env.OPENCLAW_STATE_DIR = originalOpenClawStateDir;
}
});
vi.mock("../process/exec.js", () => ({ vi.mock("../process/exec.js", () => ({
runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args), runCommandWithTimeout: (...args: unknown[]) => runCommandWithTimeoutMock(...args),
@@ -81,6 +74,7 @@ async function installZipDownloadSkill(params: {
describe("installSkill download extraction safety", () => { describe("installSkill download extraction safety", () => {
beforeEach(() => { beforeEach(() => {
envSnapshot = captureEnv(["OPENCLAW_STATE_DIR"]);
runCommandWithTimeoutMock.mockReset(); runCommandWithTimeoutMock.mockReset();
scanDirectoryWithSummaryMock.mockReset(); scanDirectoryWithSummaryMock.mockReset();
fetchWithSsrFGuardMock.mockReset(); fetchWithSsrFGuardMock.mockReset();
@@ -93,6 +87,10 @@ describe("installSkill download extraction safety", () => {
}); });
}); });
afterEach(() => {
envSnapshot.restore();
});
it("rejects zip slip traversal", async () => { it("rejects zip slip traversal", async () => {
const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-skills-install-")); const workspaceDir = await fs.mkdtemp(path.join(os.tmpdir(), "openclaw-skills-install-"));
try { try {