Skip to content

Commit 0f45e3b

Browse files
committed
fix(host-kit): keep extracted directories owner-accessible
An archive could declare a directory without owner read access (e.g. 0o300). Recursive mkdir applied that mode to implied parents too, so cleanup's recursive rm failed with EACCES: a failed extraction reported the cleanup error instead of its own and leaked the temp tree, and a successful one leaked it after install. Entry modes now come from one rule shared by zip and tar: declared permission bits only, at least owner rwx for directories and rw for files. Implied parents are created with the default mode. Adds adversarial extraction tests pinning link, special-entry, escaping-path, PAX override, duplicate, case-collision, and set-id handling, none of which were covered.
1 parent cc5e356 commit 0f45e3b

6 files changed

Lines changed: 391 additions & 14 deletions

File tree

‎packages/host-kit/src/internal/archive-extraction-tar.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as tar from 'tar-stream';
66
import {
77
ArchiveBudget,
88
archiveError,
9+
createExtractedDirectory,
10+
extractedEntryMode,
911
normalizeArchiveEntryName,
1012
reserveArchiveManifest,
1113
resolveArchiveOutputPath,
@@ -120,11 +122,6 @@ function readTarKind(type: tar.Headers['type']): 'directory' | 'file' {
120122
throw archiveError('ARCHIVE_UNSAFE_ENTRY', 'Archive contains a link or special entry');
121123
}
122124

123-
function safeMode(mode: number | undefined, kind: 'directory' | 'file'): number {
124-
const fallback = kind === 'directory' ? 0o755 : 0o644;
125-
return (mode ?? fallback) & 0o777;
126-
}
127-
128125
function manifestEntryFromTarHeader(header: tar.Headers): ArchiveManifestEntry | undefined {
129126
if (isRootDirectoryMarker(header.name, header.type)) return undefined;
130127
const name = normalizeArchiveEntryName(header.name);
@@ -133,7 +130,7 @@ function manifestEntryFromTarHeader(header: tar.Headers): ArchiveManifestEntry |
133130
if (!Number.isSafeInteger(size) || size < 0) {
134131
throw archiveError('ARCHIVE_INVALID_ENTRY', 'Archive entry has an invalid size');
135132
}
136-
return { name, kind, size, mode: safeMode(header.mode, kind) };
133+
return { name, kind, size, mode: extractedEntryMode(kind, header.mode) };
137134
}
138135

139136
async function writeTarEntry(
@@ -144,7 +141,7 @@ async function writeTarEntry(
144141
): Promise<void> {
145142
const outputPath = resolveArchiveOutputPath(outputRoot, manifestEntry.name);
146143
if (manifestEntry.kind === 'directory') {
147-
await fs.mkdir(outputPath, { recursive: true, mode: manifestEntry.mode });
144+
await createExtractedDirectory(outputPath, manifestEntry.mode);
148145
await drainTarEntry(entry);
149146
return;
150147
}

‎packages/host-kit/src/internal/archive-extraction-zip.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import * as yauzl from 'yauzl';
55
import {
66
ArchiveBudget,
77
archiveError,
8+
createExtractedDirectory,
9+
extractedEntryMode,
810
normalizeArchiveEntryName,
911
reserveArchiveManifest,
1012
resolveArchiveOutputPath,
@@ -59,7 +61,7 @@ export async function extractZipArchive(options: ZipOptions): Promise<void> {
5961
}
6062
const outputPath = resolveArchiveOutputPath(options.outputRoot, expected.name);
6163
if (expected.kind === 'directory') {
62-
await fs.mkdir(outputPath, { recursive: true, mode: expected.mode });
64+
await createExtractedDirectory(outputPath, expected.mode);
6365
} else {
6466
await fs.mkdir(path.dirname(outputPath), { recursive: true });
6567
const source = await openEntryStream(zipFile, entry);
@@ -152,13 +154,12 @@ function toManifestEntry(entry: yauzl.Entry): ArchiveManifestEntry {
152154
assertZipEntryIsReadable(entry);
153155
const name = normalizeArchiveEntryName(entry.fileName);
154156
const unixMode = (entry.externalFileAttributes >>> 16) & 0xffff;
155-
const directory = isZipDirectory(entry.fileName, unixMode);
156-
const fallback = directory ? 0o755 : 0o644;
157+
const kind = isZipDirectory(entry.fileName, unixMode) ? 'directory' : 'file';
157158
return {
158159
name,
159-
kind: directory ? 'directory' : 'file',
160-
size: directory ? 0 : entry.uncompressedSize,
161-
mode: unixMode & 0o777 || fallback,
160+
kind,
161+
size: kind === 'directory' ? 0 : entry.uncompressedSize,
162+
mode: extractedEntryMode(kind, unixMode),
162163
};
163164
}
164165

‎packages/host-kit/src/internal/archive-extraction.fixtures.ts‎

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { promises as fs } from 'node:fs';
22

33
import path from 'node:path';
4-
import { gzipSync } from 'node:zlib';
4+
import { crc32, gzipSync } from 'node:zlib';
55
import * as tar from 'tar-stream';
66
import { runCmdSync } from './exec.ts';
77
import { mkdtempForTest } from './tmp-dir.fixtures.ts';
@@ -35,3 +35,78 @@ export async function createZipWithEncryptedSecondEntry(archivePath: string): Pr
3535
runCmdSync('zip', ['-q', archivePath, 'first.txt'], { cwd: staging });
3636
runCmdSync('zip', ['-q', '-P', 'secret', archivePath, 'second.txt'], { cwd: staging });
3737
}
38+
39+
/** One stored zip entry; `mode` is the full Unix `st_mode`, file-type bits included. */
40+
export type ZipFixtureEntry = { name: string; mode: number; data?: string };
41+
42+
/**
43+
* Writes a stored (uncompressed) zip whose names and Unix attributes are exactly as given, which
44+
* `zip` itself refuses to produce for links, special files, and escaping names.
45+
*/
46+
export async function writeZipFixture(
47+
archivePath: string,
48+
entries: readonly ZipFixtureEntry[],
49+
): Promise<void> {
50+
const localRecords: Buffer[] = [];
51+
const centralRecords: Buffer[] = [];
52+
let offset = 0;
53+
for (const entry of entries) {
54+
const name = Buffer.from(entry.name, 'utf8');
55+
const data = Buffer.from(entry.data ?? '', 'utf8');
56+
const checksum = crc32(data);
57+
const local = Buffer.alloc(30);
58+
local.writeUInt32LE(0x04034b50, 0);
59+
local.writeUInt16LE(20, 4);
60+
local.writeUInt16LE(0x0800, 6);
61+
local.writeUInt32LE(checksum, 14);
62+
local.writeUInt32LE(data.length, 18);
63+
local.writeUInt32LE(data.length, 22);
64+
local.writeUInt16LE(name.length, 26);
65+
const central = Buffer.alloc(46);
66+
central.writeUInt32LE(0x02014b50, 0);
67+
central.writeUInt16LE((3 << 8) | 20, 4);
68+
central.writeUInt16LE(20, 6);
69+
central.writeUInt16LE(0x0800, 8);
70+
central.writeUInt32LE(checksum, 16);
71+
central.writeUInt32LE(data.length, 20);
72+
central.writeUInt32LE(data.length, 24);
73+
central.writeUInt16LE(name.length, 28);
74+
central.writeUInt32LE((entry.mode << 16) >>> 0, 38);
75+
central.writeUInt32LE(offset, 42);
76+
localRecords.push(local, name, data);
77+
centralRecords.push(central, name);
78+
offset += local.length + name.length + data.length;
79+
}
80+
const centralDirectory = Buffer.concat(centralRecords);
81+
const end = Buffer.alloc(22);
82+
end.writeUInt32LE(0x06054b50, 0);
83+
end.writeUInt16LE(entries.length, 8);
84+
end.writeUInt16LE(entries.length, 10);
85+
end.writeUInt32LE(centralDirectory.length, 12);
86+
end.writeUInt32LE(offset, 16);
87+
await fs.writeFile(archivePath, Buffer.concat([...localRecords, centralDirectory, end]));
88+
}
89+
90+
/** `pax` records are written as a PAX extended header; tar-stream's typings omit the field. */
91+
export type TarFixtureEntry = {
92+
header: tar.Headers;
93+
pax?: Record<string, string>;
94+
data?: string;
95+
};
96+
97+
export async function writeTarFixture(
98+
archivePath: string,
99+
entries: readonly TarFixtureEntry[],
100+
): Promise<void> {
101+
const pack = tar.pack();
102+
for (const entry of entries) {
103+
const header: tar.Headers & { pax?: Record<string, string> } = entry.pax
104+
? { ...entry.header, pax: entry.pax }
105+
: entry.header;
106+
pack.entry(header, Buffer.from(entry.data ?? ''));
107+
}
108+
pack.finalize();
109+
const chunks: Buffer[] = [];
110+
for await (const chunk of pack) chunks.push(Buffer.from(chunk));
111+
await fs.writeFile(archivePath, Buffer.concat(chunks));
112+
}

0 commit comments

Comments
 (0)