diff --git a/packages/cli/src/commands/templates.test.ts b/packages/cli/src/commands/templates.test.ts index a09f865669..898fbe5728 100644 --- a/packages/cli/src/commands/templates.test.ts +++ b/packages/cli/src/commands/templates.test.ts @@ -193,6 +193,19 @@ describe("pullTemplate", () => { expect(existsSync(join(dir, "templates", "sample"))).toBe(false); }); + it("refuses an archive holding a symlink and leaves no partial folder", async () => { + const src = mkdtempSync(join(dir, "linked-")); + writeFileSync(join(src, "SKILL.md"), "x"); + symlinkSync("/tmp", join(src, "shared")); + const out = join(dir, "linked.tar.gz"); + execFileSync("tar", ["-czf", out, "-C", src, "SKILL.md", "shared"]); + const linked = readFileSync(out); + pin(linked); + serve({}, linked); + await expect(pullTemplate("sample", dir)).rejects.toThrow("link or special file"); + expect(existsSync(join(dir, "templates", "sample"))).toBe(false); + }); + /** A part folder exactly as a pull leaves it mid-unpack, made to belong to `pid`. */ async function partLeftBy(pid: number, suffix: string): Promise { serve({}); diff --git a/packages/cli/src/commands/templates.ts b/packages/cli/src/commands/templates.ts index c8d68b9363..33432aff5a 100644 --- a/packages/cli/src/commands/templates.ts +++ b/packages/cli/src/commands/templates.ts @@ -107,6 +107,8 @@ async function download(url: string, pack: TemplatePack): Promise { return data; } +const PLAIN_FILE_OR_FOLDER_LISTING = /^[-d]/; + function assertSafeEntries(data: Buffer): void { const names = execFileSync("tar", ["-tzf", "-"], { input: data, @@ -116,6 +118,14 @@ function assertSafeEntries(data: Buffer): void { for (const name of names.split("\n")) if (name.startsWith("/") || name.split("/").includes("..")) throw new PullRefused(`The template holds an unsafe path (${name}); nothing was unpacked.`); + const kinds = execFileSync("tar", ["-tvzf", "-"], { + input: data, + encoding: "utf8", + maxBuffer: 1 << 26, + }); + for (const line of kinds.split("\n")) + if (line && !PLAIN_FILE_OR_FOLDER_LISTING.test(line)) + throw new PullRefused("The template holds a link or special file; nothing was unpacked."); } const PULLED_HASH = ".template-sha256";