designpaca/packages/core/test/safety.test.ts
Yun Chan eecd3f03ff fix(core,cli): harden install engine and argument parsing
- Reject unknown flags with exit 2 and never write on --dry-run, even in a TTY
- Treat files already matching the new content as up to date after an interrupted update
- Remove unmodified orphan files from older versions, keep and report modified ones and shared paths
- Degrade malformed manifests instead of blocking installs; readIfExists only maps ENOENT/ENOTDIR to null
- Block downgrades from older CLIs (semver), honor uninstall --scope, dedupe targets, fix next-step output and table width
- Deeper package verification, LF normalization for .mjs, MIT LICENSE
2026-10-09 18:18:49 +09:00

361 lines
17 KiB
TypeScript

import assert from "node:assert/strict";
import fs from "node:fs/promises";
import os from "node:os";
import path from "node:path";
import { after, test } from "node:test";
import {
assertNoLinksBelow,
backup,
readIfExists,
readOrNull,
removeFileAndPrune,
sha256,
writeAtomic,
} from "../src/fsx.ts";
import { applyPlan, envForRecord, planForRecord, planInstall, removeInstall } from "../src/installer.ts";
import { inspectDrift, readManifest, writeManifest } from "../src/manifest.ts";
import type { InstallRecord, SkillSource } from "../src/types.ts";
// 보안 감사(2026-10-01) 재현 시나리오를 회귀 테스트로 고정한다. 모든 쓰기는 임시 디렉터리 안에서만 한다.
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), "designpaca-safety-"));
after(async () => {
await fs.rm(tmp, { recursive: true, force: true });
});
const skill: SkillSource = {
version: "9.9.9",
skillMd: ["---", "name: designpaca", 'description: "테스트용"', "---", "", "# 본문", ""].join("\n"),
files: new Map([["references/tokens.md", "# 토큰\n"]]),
};
async function fresh(name: string): Promise<{ home: string; cwd: string; outside: string }> {
const dir = path.join(tmp, name);
const home = path.join(dir, "home");
const cwd = path.join(dir, "proj");
const outside = path.join(dir, "outside");
for (const d of [home, cwd, outside]) await fs.mkdir(d, { recursive: true });
return { home, cwd, outside };
}
async function listAll(dir: string): Promise<string[]> {
try {
return (await fs.readdir(dir, { recursive: true })).map(String).sort();
} catch {
return [];
}
}
/** 디렉터리 링크 — Windows 는 관리자 권한 없이 만들 수 있는 정션을 쓴다 */
async function linkDir(target: string, link: string): Promise<void> {
await fs.symlink(target, link, process.platform === "win32" ? "junction" : "dir");
}
/** 파일 심링크는 Windows 에서 권한이 없으면 만들 수 없다 — 그때는 false (해당 검증만 생략) */
async function tryLinkFile(target: string, link: string): Promise<boolean> {
try {
await fs.symlink(target, link, "file");
return true;
} catch {
return false;
}
}
test("writeAtomic: 임시 파일을 남기지 않고, 예측 가능한 <p>.<pid>.tmp 를 쓰지 않는다", async () => {
const { outside, cwd } = await fresh("atomic");
const victim = path.join(outside, "bashrc");
await fs.writeFile(victim, "원래 내용\n", "utf8");
const p = path.join(cwd, "AGENTS.md");
// 예전 구현이 쓰던 이름에 링크를 심어둬도 따라가지 않는다
await tryLinkFile(victim, `${p}.${process.pid}.tmp`);
await writeAtomic(p, "새 내용\n");
assert.equal(await fs.readFile(p, "utf8"), "새 내용\n");
assert.equal(await fs.readFile(victim, "utf8"), "원래 내용\n");
const leftovers = (await fs.readdir(cwd)).filter((n) => n.endsWith(".tmp") && !n.includes(String(process.pid)));
assert.deepEqual(leftovers, []);
});
test("backup: 기존 .orig 를 덮지 않고 .orig.1 로 회전한다", async () => {
const { cwd } = await fresh("backup");
const p = path.join(cwd, "SKILL.md");
await fs.writeFile(p, "첫 수정\n", "utf8");
assert.equal(await backup(p), `${p}.orig`);
await fs.writeFile(p, "두 번째 수정\n", "utf8");
assert.equal(await backup(p), `${p}.orig.1`);
assert.equal(await fs.readFile(`${p}.orig`, "utf8"), "첫 수정\n");
assert.equal(await fs.readFile(`${p}.orig.1`, "utf8"), "두 번째 수정\n");
});
test("removeFileAndPrune: 기준 밖 파일은 지우지 않는다 (접두사가 같은 형제 디렉터리 포함)", async () => {
const { cwd } = await fresh("prune");
const root = path.join(cwd, "a", "b");
const sibling = path.join(cwd, "a", "bc");
await fs.mkdir(root, { recursive: true });
await fs.mkdir(sibling, { recursive: true });
const victim = path.join(sibling, "important.txt");
await fs.writeFile(victim, "x", "utf8");
await assert.rejects(() => removeFileAndPrune(victim, root));
await fs.access(victim);
});
test("assertNoLinksBelow: 기준 아래의 정션·심링크를 거부한다", async () => {
const { cwd, outside } = await fresh("links");
await linkDir(outside, path.join(cwd, ".claude"));
await assert.rejects(() => assertNoLinksBelow(cwd, path.join(cwd, ".claude", "skills", "x", "SKILL.md")));
await assert.rejects(() => assertNoLinksBelow(cwd, path.join(cwd, "..", "escape.md")));
await assertNoLinksBelow(cwd, path.join(cwd, ".cursor", "skills", "x", "SKILL.md"));
});
test("project 설치는 정션으로 연결된 .claude 를 따라 저장소 밖에 쓰지 않는다", async () => {
const { home, cwd, outside } = await fresh("junction-install");
await linkDir(outside, path.join(cwd, ".claude"));
const plan = await planInstall("claude-code", "project", skill, { home, cwd });
await assert.rejects(() => applyPlan(plan, skill.version, { env: { home } }), /링크/);
assert.deepEqual(await listAll(outside), []);
assert.equal((await readManifest(home)).installs.length, 0);
});
test("심링크인 AGENTS.md 에는 주입하지 않는다 (링크 대상 내용을 저장소로 끌어오지 않는다)", async () => {
const { home, cwd, outside } = await fresh("symlink-agents");
const secret = path.join(outside, "secret.env");
await fs.writeFile(secret, "SECRET=abc\n", "utf8");
if (!(await tryLinkFile(secret, path.join(cwd, "AGENTS.md")))) return; // 파일 심링크 권한 없음
const plan = await planInstall("agents-md", "project", skill, { home, cwd });
await assert.rejects(() => applyPlan(plan, skill.version, { env: { home } }), /링크/);
assert.equal(await fs.readFile(secret, "utf8"), "SECRET=abc\n");
assert.ok((await fs.lstat(path.join(cwd, "AGENTS.md"))).isSymbolicLink());
// 계획 전체를 먼저 검사하므로 .designpaca/ 도 만들어지지 않는다
await assert.rejects(() => fs.access(path.join(cwd, ".designpaca")));
});
test("설치 기록 없이 이미 있는 다른 내용의 파일은 덮기 전에 .orig 로 남긴다", async () => {
const { home, cwd } = await fresh("no-record");
const root = path.join(cwd, ".claude", "skills", "designpaca");
await fs.mkdir(root, { recursive: true });
await fs.writeFile(path.join(root, "SKILL.md"), "user own skill\n", "utf8");
const plan = await planInstall("claude-code", "project", skill, { home, cwd });
const res = await applyPlan(plan, skill.version, { env: { home } });
assert.deepEqual(res.backedUp, [path.join(root, "SKILL.md.orig")]);
assert.equal(await fs.readFile(path.join(root, "SKILL.md.orig"), "utf8"), "user own skill\n");
await removeInstall(res.record, { env: { home } });
});
test("envForRecord: 기록 root 에서 프로젝트·홈을 역산하고, 어댑터가 만들 수 없는 root 는 거부한다", async () => {
const { home, cwd } = await fresh("env-record");
const plan = await planInstall("agents-md", "project", skill, { home, cwd });
const rec: InstallRecord = {
target: "agents-md",
scope: "project",
root: plan.root,
version: "0.0.1",
installedAt: "",
files: [],
};
assert.deepEqual(await envForRecord(rec), { cwd });
const userPlan = await planInstall("claude-code", "user", skill, { home, cwd });
assert.deepEqual(await envForRecord({ ...rec, target: "claude-code", scope: "user", root: userPlan.root }), {
home,
});
await assert.rejects(() => envForRecord({ ...rec, root: path.join(cwd, "somewhere") }));
const blocked = await planForRecord({ ...rec, root: path.join(cwd, "somewhere") }, skill);
assert.ok(blocked.blocked);
// 기록 root 그대로 다시 계획된다 — cwd 가 어디든 상관없다
const again = await planForRecord(rec, skill);
assert.equal(again.root, plan.root);
});
test("변조된 매니페스트로 uninstall 이 root 밖 파일을 지우지 않는다", async () => {
const { home, cwd, outside } = await fresh("tampered");
const victim = path.join(outside, "sub", "important.txt");
await fs.mkdir(path.dirname(victim), { recursive: true });
await fs.writeFile(victim, "중요\n", "utf8");
// (1) 어댑터가 만들 수 없는 root
const bogus: InstallRecord = {
target: "claude-code",
scope: "project",
root: outside,
version: "0.0.1",
installedAt: "",
files: [{ path: victim, sha256: sha256("중요\n") }],
};
// (2) 정상 root 지만 파일 경로가 root 밖 / 주입 문서가 아닌 마커 파일
const plan = await planInstall("claude-code", "project", skill, { home, cwd });
const sneaky: InstallRecord = {
...bogus,
root: plan.root,
files: [
{ path: victim, sha256: sha256("중요\n") },
{ path: path.join(outside, "x.md"), sha256: "", marker: "designpaca" },
],
};
await writeManifest({ schema: 1, installs: [bogus, sneaky] }, home);
for (const rec of [bogus, sneaky]) {
const res = await removeInstall(rec, { env: { home }, force: true });
assert.equal(res.removed.length, 0);
assert.equal(res.refused.length, rec.files.length);
}
await fs.access(victim);
// 거부한 파일은 기록에 남긴다(조용히 사라지지 않는다)
assert.equal((await readManifest(home)).installs.length, 2);
});
test("손상된 매니페스트는 덮기 전에 .corrupt-* 로 보존한다", async () => {
const { home } = await fresh("corrupt");
const file = path.join(home, ".designpaca", "manifest.json");
await fs.mkdir(path.dirname(file), { recursive: true });
await fs.writeFile(file, "{ 깨진 json", "utf8");
assert.equal((await readManifest(home)).installs.length, 0);
await writeManifest({ schema: 1, installs: [] }, home);
const kept = (await fs.readdir(path.dirname(file))).filter((n) => n.startsWith("manifest.json.corrupt-"));
assert.equal(kept.length, 1);
});
// ---- 감사(2026-10) 회귀: 중단된 update·고아 파일·읽기 실패·손상 매니페스트·공유 root ----
function withFiles(version: string, files: [string, string][]): SkillSource {
return { ...skill, version, files: new Map(files) };
}
test("중단된 update: 디스크가 이미 새 내용이면 드리프트로 보지 않는다", async () => {
const { home, cwd } = await fresh("interrupted");
const env = { home, cwd };
const v1 = withFiles("1.0.0", [["references/a.md", "v1\n"]]);
const v2 = withFiles("2.0.0", [["references/a.md", "v2\n"]]);
const plan1 = await planInstall("claude-code", "project", v1, env);
await applyPlan(plan1, "1.0.0", { env });
// update 중간 크래시 모사: 파일만 v2 로 쓰이고 매니페스트는 v1 그대로
const a = path.join(plan1.root, "references", "a.md");
await fs.writeFile(a, "v2\n", "utf8");
const plan2 = await planInstall("claude-code", "project", v2, env);
const res = await applyPlan(plan2, "2.0.0", { env });
assert.deepEqual(res.skipped, []);
assert.equal(res.backedUp.length, 0);
const rec = (await readManifest(home)).installs[0]!;
assert.equal((await inspectDrift(rec)).modified.length, 0);
});
test("사용자 편집이 새 버전과 다르면 여전히 건너뛴다", async () => {
const { home, cwd } = await fresh("still-dirty");
const env = { home, cwd };
const v1 = withFiles("1.0.0", [["references/a.md", "v1\n"]]);
const v2 = withFiles("2.0.0", [["references/a.md", "v2\n"]]);
const plan1 = await planInstall("claude-code", "project", v1, env);
await applyPlan(plan1, "1.0.0", { env });
const a = path.join(plan1.root, "references", "a.md");
await fs.writeFile(a, "내가 고침\n", "utf8");
const res = await applyPlan(await planInstall("claude-code", "project", v2, env), "2.0.0", { env });
assert.deepEqual(res.skipped, [a]);
assert.equal(await fs.readFile(a, "utf8"), "내가 고침\n");
// 기록은 설치 당시 해시를 유지한다
const kept = (await readManifest(home)).installs[0]!.files.find((f) => f.path === a)!;
assert.equal(kept.sha256, sha256("v1\n"));
});
test("새 버전에서 빠진 파일은 지우고, 사용자가 고친 것은 보존한다", async () => {
const { home, cwd } = await fresh("orphans");
const env = { home, cwd };
const v1 = withFiles("1.0.0", [
["references/keep.md", "k\n"],
["references/old.md", "o\n"],
["references/edited.md", "e\n"],
]);
const v2 = withFiles("2.0.0", [["references/keep.md", "k\n"]]);
const plan1 = await planInstall("claude-code", "project", v1, env);
await applyPlan(plan1, "1.0.0", { env });
const old = path.join(plan1.root, "references", "old.md");
const edited = path.join(plan1.root, "references", "edited.md");
await fs.writeFile(edited, "내 수정\n", "utf8");
const res = await applyPlan(await planInstall("claude-code", "project", v2, env), "2.0.0", { env });
assert.deepEqual(res.removedStale, [old]);
assert.deepEqual(res.keptStale, [edited]);
await assert.rejects(fs.access(old));
assert.equal(await fs.readFile(edited, "utf8"), "내 수정\n");
const files = (await readManifest(home)).installs[0]!.files;
assert.ok(
files.some((f) => f.path === edited && f.sha256 === sha256("e\n")),
"보존한 파일은 원래 해시로 기록에 남는다",
);
assert.ok(!files.some((f) => f.path === old));
});
test("readIfExists: 없는 파일만 null, 그 밖의 읽기 실패는 던진다", async () => {
const { cwd } = await fresh("readerr");
assert.equal(await readIfExists(path.join(cwd, "없음.md")), null);
assert.equal(await readIfExists(path.join(cwd, "없음.md", "아래")), null);
// 디렉터리를 파일처럼 읽으면 EISDIR — 예전에는 null("없음")로 삼켰다
await assert.rejects(readIfExists(cwd));
assert.equal(await readOrNull(cwd), null);
});
test("읽을 수 없는 AGENTS.md 는 덮지 않고 설치를 멈춘다 (POSIX 일반 사용자)", async () => {
// AGENTS.md 규칙 1 때문에 test.skip 을 쓰지 않는다 — 환경이 안 맞으면 본문을 건너뛴다
if (process.platform === "win32" || process.getuid?.() === 0) return;
const { home, cwd } = await fresh("unreadable");
const doc = path.join(cwd, "AGENTS.md");
await fs.writeFile(doc, "# 내 지침\n", "utf8");
await fs.chmod(doc, 0o000);
try {
const plan = await planInstall("agents-md", "project", skill, { home, cwd });
await assert.rejects(applyPlan(plan, skill.version, { env: { home, cwd } }));
} finally {
await fs.chmod(doc, 0o644);
}
assert.equal(await fs.readFile(doc, "utf8"), "# 내 지침\n");
});
test("형태가 깨진 매니페스트 레코드는 빈 매니페스트로 강등하고 원본을 보존한다", async () => {
const { home, cwd } = await fresh("shape");
const file = path.join(home, ".designpaca", "manifest.json");
await fs.mkdir(path.dirname(file), { recursive: true });
const broken = [
{ schema: 1, installs: [null] },
{ schema: 1, installs: [{ target: "codex", scope: "user", root: "/x" }] },
];
for (const bad of broken) {
await fs.writeFile(file, JSON.stringify(bad), "utf8");
assert.equal((await readManifest(home)).installs.length, 0);
const plan = await planInstall("claude-code", "project", skill, { home, cwd });
await applyPlan(plan, skill.version, { env: { home, cwd } });
const kept = (await fs.readdir(path.dirname(file))).filter((n) => n.startsWith("manifest.json.corrupt-"));
assert.ok(kept.length >= 1, "원본이 .corrupt-* 로 남아야 한다");
await removeInstall((await readManifest(home)).installs[0]!, { env: { home }, force: true });
}
// 유효한 레코드는 그대로 읽힌다
const ok: InstallRecord = {
target: "claude-code",
scope: "user",
root: path.join(home, "r"),
version: "1",
installedAt: "x",
files: [{ path: path.join(home, "r", "a"), sha256: "h" }],
};
await writeManifest({ schema: 1, installs: [ok] }, home);
assert.equal((await readManifest(home)).installs.length, 1);
});
test("project 범위에서 agents 와 antigravity 가 root 를 공유해도 한쪽 제거가 다른 쪽을 깨지 않는다", async () => {
const { home, cwd } = await fresh("shared-root");
const env = { home, cwd };
const pa = await planInstall("agents", "project", skill, env);
const pg = await planInstall("antigravity", "project", skill, env);
assert.equal(pa.root, pg.root);
const ra = await applyPlan(pa, skill.version, { env });
const rg = await applyPlan(pg, skill.version, { env });
const first = await removeInstall(ra.record, { env });
assert.equal(first.removed.length, 0);
assert.equal(first.sharedKept.length, ra.record.files.length);
await fs.access(path.join(pa.root, "SKILL.md"));
const left = (await readManifest(home)).installs;
assert.equal(left.length, 1);
assert.equal((await inspectDrift(left[0]!)).files.every((f) => f.status === "ok"), true);
const last = await removeInstall(rg.record, { env });
assert.equal(last.sharedKept.length, 0);
await assert.rejects(fs.access(path.join(pa.root, "SKILL.md")));
});