From b18fe7974909315b78fe8eb72f90c2bc7f5f475a Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 4 Nov 2026 15:21:55 -0800 Subject: [PATCH 1/3] fix(zip): reject symlink entries and never write through a symlink A zip archive could contain a symlink entry followed by a regular file with the same name. The parent directory check passed for both, and the file write followed the symlink out of the extraction directory. Symlink entries are now rejected unless the caller passes allowSymlinks. Only browser archives need them (macOS framework bundles); report zips opened with show-report never do. With allowSymlinks, the extractor still refuses to write through an existing symlink. --- .../server/registry/oopDownloadBrowserMain.ts | 2 +- packages/utils/third_party/extractZip.ts | 10 ++++ tests/library/unit/extract-zip.spec.ts | 59 +++++++++++++++++++ tests/registry.md | 8 +++ 4 files changed, 78 insertions(+), 1 deletion(-) create mode 100644 tests/library/unit/extract-zip.spec.ts diff --git a/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts b/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts index e7a302b33820a..0b2e788cedf69 100644 --- a/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts +++ b/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts @@ -120,7 +120,7 @@ async function main(options: DownloadParams) { log(`removing existing browser directory if any`); await removeFolders([options.browserDirectory]); log(`extracting archive`); - await extractZip(options.zipPath, { dir: options.browserDirectory }); + await extractZip(options.zipPath, { dir: options.browserDirectory, allowSymlinks: true }); if (options.executablePath) { log(`fixing permissions at ${options.executablePath}`); await fs.promises.chmod(options.executablePath, 0o755); diff --git a/packages/utils/third_party/extractZip.ts b/packages/utils/third_party/extractZip.ts index 1dc0d27253042..0dd6d044155aa 100644 --- a/packages/utils/third_party/extractZip.ts +++ b/packages/utils/third_party/extractZip.ts @@ -46,6 +46,8 @@ export interface Options { defaultDirMode?: number; /** File Mode (permissions), defaults to `0o644` */ defaultFileMode?: number; + /** Allow symlink entries. Off by default, because they can point outside of `dir`. */ + allowSymlinks?: boolean; /** * If present, will be called with (entry, zipfile), * entry is every entry from the zip file forwarded @@ -169,10 +171,18 @@ class Extractor { if (isDir) return; + // Never write through a symlink. An archive can place a symlink entry and then + // a regular file with the same name to escape the extraction directory. + const existing = await fs.lstat(dest).catch(() => undefined); + if (existing?.isSymbolicLink()) + throw new Error(`Refusing to overwrite symlink "${dest}" while processing file ${entry.fileName}`); + debug('opening read stream', dest); const readStream = await promisify(this.zipfile.openReadStream.bind(this.zipfile))(entry); if (symlink) { + if (!this.opts.allowSymlinks) + throw new Error(`Symlink entry "${entry.fileName}" is not allowed`); const link = await getStream(readStream); debug('creating symlink', link, dest); await fs.symlink(link, dest); diff --git a/tests/library/unit/extract-zip.spec.ts b/tests/library/unit/extract-zip.spec.ts new file mode 100644 index 0000000000000..c86b2782edb10 --- /dev/null +++ b/tests/library/unit/extract-zip.spec.ts @@ -0,0 +1,59 @@ +/** + * 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((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('Symlink entry "escape" is not allowed'); + await expect(extractZip(zipPath, { dir: testInfo.outputPath('extracted-with-symlinks'), allowSymlinks: true })).rejects.toThrow('Refusing to overwrite symlink'); + 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'); +}); diff --git a/tests/registry.md b/tests/registry.md index 2ea0103160cf4..db11e4c467a55 100644 --- a/tests/registry.md +++ b/tests/registry.md @@ -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`
`library/{shared-worker, browsercontext-service-worker-policy}` | | `WS` | WebSocket | `library/{web-socket, route-web-socket}` | +| `ZP` | Zip extraction | `library/unit/extract-zip` | ## Tests @@ -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 | From 50b9a9c3f89b5b70557b7ddf814d615b7ea6ae57 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 4 Nov 2026 16:04:01 -0800 Subject: [PATCH 2/3] fix(zip): open extracted files with the wx flag instead of checking for symlinks O_EXCL fails when the destination exists, including a symlink, so the kernel enforces the check. Symlink entries are allowed again; the browser archives on macOS need them. --- .../src/server/registry/oopDownloadBrowserMain.ts | 2 +- packages/utils/third_party/extractZip.ts | 15 ++++----------- tests/library/unit/extract-zip.spec.ts | 3 +-- 3 files changed, 6 insertions(+), 14 deletions(-) diff --git a/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts b/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts index 0b2e788cedf69..e7a302b33820a 100644 --- a/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts +++ b/packages/playwright-core/src/server/registry/oopDownloadBrowserMain.ts @@ -120,7 +120,7 @@ async function main(options: DownloadParams) { log(`removing existing browser directory if any`); await removeFolders([options.browserDirectory]); log(`extracting archive`); - await extractZip(options.zipPath, { dir: options.browserDirectory, allowSymlinks: true }); + await extractZip(options.zipPath, { dir: options.browserDirectory }); if (options.executablePath) { log(`fixing permissions at ${options.executablePath}`); await fs.promises.chmod(options.executablePath, 0o755); diff --git a/packages/utils/third_party/extractZip.ts b/packages/utils/third_party/extractZip.ts index 0dd6d044155aa..3508e2f6bcc27 100644 --- a/packages/utils/third_party/extractZip.ts +++ b/packages/utils/third_party/extractZip.ts @@ -46,8 +46,6 @@ export interface Options { defaultDirMode?: number; /** File Mode (permissions), defaults to `0o644` */ defaultFileMode?: number; - /** Allow symlink entries. Off by default, because they can point outside of `dir`. */ - allowSymlinks?: boolean; /** * If present, will be called with (entry, zipfile), * entry is every entry from the zip file forwarded @@ -171,23 +169,18 @@ class Extractor { if (isDir) return; - // Never write through a symlink. An archive can place a symlink entry and then - // a regular file with the same name to escape the extraction directory. - const existing = await fs.lstat(dest).catch(() => undefined); - if (existing?.isSymbolicLink()) - throw new Error(`Refusing to overwrite symlink "${dest}" while processing file ${entry.fileName}`); - debug('opening read stream', dest); const readStream = await promisify(this.zipfile.openReadStream.bind(this.zipfile))(entry); if (symlink) { - if (!this.opts.allowSymlinks) - throw new Error(`Symlink entry "${entry.fileName}" is not allowed`); const link = await getStream(readStream); 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' })); } } diff --git a/tests/library/unit/extract-zip.spec.ts b/tests/library/unit/extract-zip.spec.ts index c86b2782edb10..7a7dec072222e 100644 --- a/tests/library/unit/extract-zip.spec.ts +++ b/tests/library/unit/extract-zip.spec.ts @@ -40,8 +40,7 @@ it('ZP-001 should not write through a symlink that leaves the extraction folder' { name: 'escape', content: 'overwritten' }, ]); - await expect(extractZip(zipPath, { dir: testInfo.outputPath('extracted') })).rejects.toThrow('Symlink entry "escape" is not allowed'); - await expect(extractZip(zipPath, { dir: testInfo.outputPath('extracted-with-symlinks'), allowSymlinks: true })).rejects.toThrow('Refusing to overwrite symlink'); + await expect(extractZip(zipPath, { dir: testInfo.outputPath('extracted') })).rejects.toThrow('EEXIST'); expect(await fs.promises.readFile(victim, 'utf8')).toBe('original'); }); From f01ab1d58453c31b6709d482f5c5318bb61dda04 Mon Sep 17 00:00:00 2001 From: Yury Semikhatsky Date: Wed, 4 Nov 2026 17:09:40 -0800 Subject: [PATCH 3/3] fix(blob): add each attachment file to the report zip once Attachments that share a path and content type map to the same zip entry. The reporter added the entry once per attachment, and the extractor now refuses to overwrite an existing file. Drop the extract-zip unit tests. --- packages/playwright/src/reporters/blob.ts | 4 ++ tests/library/unit/extract-zip.spec.ts | 58 ----------------------- tests/registry.md | 8 ---- 3 files changed, 4 insertions(+), 66 deletions(-) delete mode 100644 tests/library/unit/extract-zip.spec.ts diff --git a/packages/playwright/src/reporters/blob.ts b/packages/playwright/src/reporters/blob.ts index a1411271a62c7..660a101b69639 100644 --- a/packages/playwright/src/reporters/blob.ts +++ b/packages/playwright/src/reporters/blob.ts @@ -84,9 +84,13 @@ export class BlobReporter extends TeleReporterEmitter { zipFinishPromise.resolve(undefined); }).on('error', error => zipFinishPromise.reject(error)); + const addedEntries = new Set(); for (const { originalPath, zipEntryPath } of this._attachments) { + if (addedEntries.has(zipEntryPath)) + continue; if (!fs.statSync(originalPath, { throwIfNoEntry: false })?.isFile()) continue; + addedEntries.add(zipEntryPath); zipFile.addFile(originalPath, zipEntryPath); } diff --git a/tests/library/unit/extract-zip.spec.ts b/tests/library/unit/extract-zip.spec.ts deleted file mode 100644 index 7a7dec072222e..0000000000000 --- a/tests/library/unit/extract-zip.spec.ts +++ /dev/null @@ -1,58 +0,0 @@ -/** - * 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((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'); -}); diff --git a/tests/registry.md b/tests/registry.md index db11e4c467a55..2ea0103160cf4 100644 --- a/tests/registry.md +++ b/tests/registry.md @@ -127,7 +127,6 @@ subsystem, add a row to the Subsystems table. Then update this file: | `WM` | WebMCP | `library/{webmcp, webmcp-disabled}` | | `WO` | Workers | `page/workers`
`library/{shared-worker, browsercontext-service-worker-policy}` | | `WS` | WebSocket | `library/{web-socket, route-web-socket}` | -| `ZP` | Zip extraction | `library/unit/extract-zip` | ## Tests @@ -5573,10 +5572,3 @@ 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 |