Skip to content

Commit b851907

Browse files
authored
fix(zip): reject on malformed archives instead of crashing (#43688)
1 parent 0726df3 commit b851907

3 files changed

Lines changed: 21 additions & 0 deletions

File tree

‎packages/utils/zipFile.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ export class ZipFile {
4040
this._entries.set(entry.fileName, entry);
4141
});
4242
this._zipFile!.on('end', fulfill);
43+
this._zipFile!.on('error', reject);
4344
});
4445
});
4546
}
@@ -65,6 +66,7 @@ export class ZipFile {
6566
const buffers: Buffer[] = [];
6667
readStream.on('data', data => buffers.push(data));
6768
readStream.on('end', () => resolve(Buffer.concat(buffers)));
69+
readStream.on('error', reject);
6870
});
6971
});
7072
}

‎tests/library/browsercontext-har.spec.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import { browserTest as it, expect } from '../config/browserTest';
1818
import fs from 'fs';
1919
import path from 'path';
20+
import * as yazl from 'yazl';
2021
import { extractZip } from '../../packages/utils/third_party/extractZip';
2122

2223
it('HA-068 should context.routeFromHAR, matching the method and following redirects', async ({ context, asset }) => {
@@ -263,6 +264,23 @@ it('HA-085 should round-trip har.zip', {
263264
await expect(page2.locator('body')).toHaveCSS('background-color', 'rgb(255, 192, 203)');
264265
});
265266

267+
it('HA-114 should reject a malformed har.zip', async ({ contextFactory }, testInfo) => {
268+
const zipFile = new yazl.ZipFile();
269+
zipFile.addBuffer(Buffer.from('{}'), 'aa/x.har');
270+
zipFile.end();
271+
const chunks: Buffer[] = [];
272+
for await (const chunk of zipFile.outputStream)
273+
chunks.push(chunk as Buffer);
274+
// yauzl reports '..' in an entry name as an 'error' event while reading entries.
275+
const zip = Buffer.from(Buffer.concat(chunks).toString('latin1').replaceAll('aa/x.har', '../x.har'), 'latin1');
276+
const harPath = testInfo.outputPath('malformed.har.zip');
277+
await fs.promises.writeFile(harPath, zip);
278+
279+
const context = await contextFactory();
280+
const error = await context.routeFromHAR(harPath).catch(e => e);
281+
expect(error.message).toContain('invalid relative path: ../x.har');
282+
});
283+
266284
it('HA-086 should produce extracted zip', async ({ contextFactory, server }, testInfo) => {
267285
const harPath = testInfo.outputPath('har.har');
268286
const context1 = await contextFactory({ recordHar: { mode: 'minimal', path: harPath, content: 'attach' } });

‎tests/registry.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3158,6 +3158,7 @@ subsystem, add a row to the Subsystems table. Then update this file:
31583158
| `HA-111` | `library/har-websocket.spec.ts` | should still allow routeWebSocket to modify messages when capturing HAR |
31593159
| `HA-112` | `library/har-websocket.spec.ts` | should respect PLAYWRIGHT\_HAR\_NO\_WEBSOCKET\_FRAMES |
31603160
| `HA-113` | `library/har.spec.ts` | should store binary content with a textual mime type as base64 |
3161+
| `HA-114` | `library/browsercontext-har.spec.ts` | should reject a malformed har.zip |
31613162

31623163
### HE — Headful
31633164

0 commit comments

Comments
 (0)