Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions lib/internal/vfs/providers/ziparchive.js
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,24 @@ class ZipProvider extends VirtualProvider {
return false;
}

/**
* Throws when an ancestor of `name` is a file. Missing ancestors are valid:
* ZIP archives represent directories implicitly when they contain entries
* beneath them.
* @param {string} name
* @param {string} syscall
* @param {string} path
*/
#validateParentDirectories(name, syscall, path) {
let slash = StringPrototypeIndexOf(name, '/');
while (slash !== -1) {
if (this.#source.has(StringPrototypeSlice(name, 0, slash))) {
throw createENOTDIR(syscall, path);
}
slash = StringPrototypeIndexOf(name, '/', slash + 1);
}
}

async open(path, flags, mode) {
flags = normalizeFlags(flags);
const name = normalize(path);
Expand All @@ -346,6 +364,7 @@ class ZipProvider extends VirtualProvider {
if (!exists && (flags === 'r' || flags === 'r+')) {
throw createENOENT('open', path);
}
if (!exists) this.#validateParentDirectories(name, 'open', path);
let initial = EMPTY_BUFFER;
if (exists && !isWriteTruncate(flags)) {
initial = await fileEntry.content();
Expand All @@ -369,6 +388,7 @@ class ZipProvider extends VirtualProvider {
if (!exists && (flags === 'r' || flags === 'r+')) {
throw createENOENT('open', path);
}
if (!exists) this.#validateParentDirectories(name, 'open', path);
let initial = EMPTY_BUFFER;
if (exists && !isWriteTruncate(flags)) {
initial = fileEntry.contentSync();
Expand Down Expand Up @@ -450,6 +470,7 @@ class ZipProvider extends VirtualProvider {
async mkdir(path, options) {
if (this.readonly) throw createEROFS('mkdir', path);
const name = normalize(path);
this.#validateParentDirectories(name, 'mkdir', path);
if (await this.exists(path)) {
// `{ recursive: true }` only tolerates an existing *directory*; an
// existing file (or any non-directory) still collides with EEXIST.
Expand All @@ -462,6 +483,7 @@ class ZipProvider extends VirtualProvider {
mkdirSync(path, options) {
if (this.readonly) throw createEROFS('mkdir', path);
const name = normalize(path);
this.#validateParentDirectories(name, 'mkdir', path);
if (this.existsSync(path)) {
// `{ recursive: true }` only tolerates an existing *directory*; an
// existing file (or any non-directory) still collides with EEXIST.
Expand Down Expand Up @@ -531,6 +553,7 @@ class ZipProvider extends VirtualProvider {
const newName = normalize(newPath);
const entry = await this.#getEntry(oldName);
if (entry === null) throw createENOENT('rename', oldPath);
this.#validateParentDirectories(newName, 'rename', newPath);
const content = await entry.content();
await this.#source.add(newName, content, {
mode: entry.mode || undefined,
Expand All @@ -545,6 +568,7 @@ class ZipProvider extends VirtualProvider {
const newName = normalize(newPath);
const entry = this.#getEntrySync(oldName);
if (entry === null) throw createENOENT('rename', oldPath);
this.#validateParentDirectories(newName, 'rename', newPath);
const content = entry.contentSync();
this.#source.addSync(newName, content, {
mode: entry.mode || undefined,
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-vfs-zip-provider.js
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,24 @@ async function buildArchive(entries, comment) {
assert.strictEqual(await archiveVfs.promises.readFile('/new.txt', 'utf8'), 'brand new');
assert.strictEqual(zip.has('new.txt'), true);

// Entries cannot be created beneath a file.
await assert.rejects(
archiveVfs.promises.writeFile('/a.txt/child.txt', 'child'),
{ code: 'ENOTDIR' },
);
await assert.rejects(
archiveVfs.promises.mkdir('/a.txt/child'),
{ code: 'ENOTDIR' },
);
await assert.rejects(
archiveVfs.promises.rename('/new.txt', '/a.txt/renamed.txt'),
{ code: 'ENOTDIR' },
);
assert.strictEqual(zip.has('a.txt/child.txt'), false);
assert.strictEqual(zip.has('a.txt/child/'), false);
assert.strictEqual(zip.has('a.txt/renamed.txt'), false);
assert.strictEqual(zip.has('new.txt'), true);

// Overwriting an existing file.
await archiveVfs.promises.writeFile('/a.txt', 'overwritten');
assert.strictEqual(await archiveVfs.promises.readFile('/a.txt', 'utf8'), 'overwritten');
Expand Down Expand Up @@ -195,6 +213,24 @@ async function buildArchive(entries, comment) {
archiveVfs.appendFileSync('/new.txt', '!');
assert.strictEqual(archiveVfs.readFileSync('/new.txt', 'utf8'), 'brand new!');

// Entries cannot be created beneath a file.
assert.throws(
() => archiveVfs.writeFileSync('/a.txt/child.txt', 'child'),
{ code: 'ENOTDIR' },
);
assert.throws(
() => archiveVfs.mkdirSync('/a.txt/child'),
{ code: 'ENOTDIR' },
);
assert.throws(
() => archiveVfs.renameSync('/new.txt', '/a.txt/renamed.txt'),
{ code: 'ENOTDIR' },
);
assert.strictEqual(zip.has('a.txt/child.txt'), false);
assert.strictEqual(zip.has('a.txt/child/'), false);
assert.strictEqual(zip.has('a.txt/renamed.txt'), false);
assert.strictEqual(zip.has('new.txt'), true);

// mkdir/rmdir.
archiveVfs.mkdirSync('/newdir');
assert.strictEqual(archiveVfs.statSync('/newdir').isDirectory(), true);
Expand Down
Loading