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 <noreply@anthropic.com>

* refactor(cli): name the templates pull entry-type check instead of commenting it

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
James Russo
2026-11-12 01:03:00 +00:00
committed by GitHub
co-authored by Claude Opus 5.5
parent 35cf03cab9
commit ab66e5bd72
2 changed files with 23 additions and 0 deletions
@@ -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<string> {
serve({});
+10
View File
@@ -107,6 +107,8 @@ async function download(url: string, pack: TemplatePack): Promise<Buffer> {
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";