Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 4 additions & 1 deletion packages/utils/third_party/extractZip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,10 @@ class Extractor {
debug('creating symlink', link, dest);
await fs.symlink(link, dest);
} else {
await pipeline(readStream, createWriteStream(dest, { mode: procMode }));
// 'wx' fails when dest exists, including a symlink left by a previous entry.
// Otherwise an archive could place a symlink and then a regular file with the
// same name to write outside of the extraction directory.
await pipeline(readStream, createWriteStream(dest, { mode: procMode, flags: 'wx' }));
}
}

Expand Down
58 changes: 58 additions & 0 deletions tests/library/unit/extract-zip.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) Microsoft Corporation.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import fs from 'fs';
import path from 'path';
import * as yazl from 'yazl';
import { test as it, expect } from '@playwright/test';
import { extractZip } from '../../../packages/utils/third_party/extractZip';

async function writeZip(zipPath: string, entries: { name: string, content: string, mode?: number }[]) {
const zipFile = new yazl.ZipFile();
for (const entry of entries)
zipFile.addBuffer(Buffer.from(entry.content), entry.name, { mode: entry.mode });
zipFile.end();
await new Promise<void>((resolve, reject) => {
zipFile.outputStream.pipe(fs.createWriteStream(zipPath)).on('close', resolve).on('error', reject);
});
}

it('ZP-001 should not write through a symlink that leaves the extraction folder', async ({}, testInfo) => {
const victim = testInfo.outputPath('victim.txt');
await fs.promises.writeFile(victim, 'original');
const zipPath = testInfo.outputPath('evil.zip');
const kSymlinkMode = 0o120777;
await writeZip(zipPath, [
{ name: 'escape', content: victim, mode: kSymlinkMode },
{ name: 'escape', content: 'overwritten' },
]);

await expect(extractZip(zipPath, { dir: testInfo.outputPath('extracted') })).rejects.toThrow('EEXIST');
expect(await fs.promises.readFile(victim, 'utf8')).toBe('original');
});

it('ZP-002 should extract regular files and directories', async ({}, testInfo) => {
const zipPath = testInfo.outputPath('ok.zip');
await writeZip(zipPath, [
{ name: 'index.html', content: 'index' },
{ name: 'nested/file.txt', content: 'nested' },
]);

const dir = testInfo.outputPath('extracted');
await extractZip(zipPath, { dir });
expect(await fs.promises.readFile(path.join(dir, 'index.html'), 'utf8')).toBe('index');
expect(await fs.promises.readFile(path.join(dir, 'nested', 'file.txt'), 'utf8')).toBe('nested');
});
8 changes: 8 additions & 0 deletions tests/registry.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
| `WM` | WebMCP | `library/{webmcp, webmcp-disabled}` |
| `WO` | Workers | `page/workers`<br>`library/{shared-worker, browsercontext-service-worker-policy}` |
| `WS` | WebSocket | `library/{web-socket, route-web-socket}` |
| `ZP` | Zip extraction | `library/unit/extract-zip` |

## Tests

Expand Down Expand Up @@ -5572,3 +5573,10 @@ subsystem, add a row to the Subsystems table. Then update this file:
| `WS-038` | `library/route-web-socket.spec.ts` | should work with baseURL regardless of scheme casing |
| `WS-039` | `library/route-web-socket.spec.ts` | should expose protocols to the route handler |
| `WS-040` | `library/route-web-socket.spec.ts` | should expose protocols on server-side route |

### ZP — Zip extraction

| ID | File | Title |
|---|---|---|
| `ZP-001` | `library/unit/extract-zip.spec.ts` | should not write through a symlink that leaves the extraction folder |
| `ZP-002` | `library/unit/extract-zip.spec.ts` | should extract regular files and directories |
Loading