From ab66e5bd72ad16b36a37dd1ee50d23cc21b41bb0 Mon Sep 17 00:00:00 2001 From: James Russo Date: Thu, 12 Nov 2026 01:03:00 +0000 Subject: [PATCH] fix(cli): templates pull unpacks only plain files and folders (#7576) * fix(cli): templates pull unpacks only plain files and folders `templates pull` already refuses entries that climb out of the target folder. It now also reads the archive's long listing and refuses any entry that isn't a plain file or folder (a symlink, hard link or device), the same rule Desktop applies when it unpacks example films and templates. Co-Authored-By: Claude Opus 5.5 * refactor(cli): name the templates pull entry-type check instead of commenting it Co-Authored-By: Claude Opus 5.5 --------- Co-authored-by: Claude Opus 5.5 --- packages/cli/src/commands/templates.test.ts | 13 +++++++++++++ packages/cli/src/commands/templates.ts | 10 ++++++++++ 2 files changed, 23 insertions(+) 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";