@@ -18,6 +18,7 @@ require('../common'); |
18 | 18 | const assert = require('assert'); |
19 | 19 | const { test } = require('node:test'); |
20 | 20 | const zlib = require('zlib'); |
| 21 | +const fs = require('fs'); |
21 | 22 | const path = require('path'); |
22 | 23 | const { spawnSync } = require('child_process'); |
23 | 24 | const tmpdir = require('../common/tmpdir'); |
@@ -76,6 +77,116 @@ test('a local/central encryption-flag disagreement is rejected', () => { |
76 | 77 | readsThrow(buf, 'a.txt'); |
77 | 78 | }); |
78 | 79 |
|
| 80 | +// 1d. A failed lazy local-header check must not leave a file-backed entry in |
| 81 | +// partially resolved state. Otherwise, a later read uses -1 as its file offset |
| 82 | +// and reads from the descriptor's current position. |
| 83 | +test('failed local-header validation does not poison file-backed entries', async () => { |
| 84 | + const decoy = Buffer.from('SCANNER-SEES-BENIGN!!'); |
| 85 | + const payload = Buffer.from('UNZIP-SEES-PAYLOAD!!!'); |
| 86 | + const entry = zlib.ZipEntry.createSync( |
| 87 | + 'entry.txt', payload, { method: 'store' }); |
| 88 | + const chunks = []; |
| 89 | + for (const chunk of zlib.createZipArchiveSync( |
| 90 | + [entry], { baseOffset: decoy.length })) { |
| 91 | + chunks.push(chunk); |
| 92 | + } |
| 93 | + const buf = Buffer.concat([decoy, ...chunks]); |
| 94 | + buf.writeUInt32LE(zlib.crc32(decoy), centralOffset(buf) + 16); |
| 95 | + |
| 96 | + tmpdir.refresh(); |
| 97 | + const file = path.join(tmpdir.path, 'mismatched.zip'); |
| 98 | + fs.writeFileSync(file, buf); |
| 99 | + const invalid = { code: 'ERR_ZIP_INVALID_ARCHIVE' }; |
| 100 | + |
| 101 | + { |
| 102 | + const zip = zlib.ZipFile.openSync(file); |
| 103 | + try { |
| 104 | + const fileEntry = zip.getSync('entry.txt'); |
| 105 | + assert.ok(fileEntry.modified instanceof Date); |
| 106 | + assert.throws(() => fileEntry.contentSync(), invalid); |
| 107 | + assert.throws(() => fileEntry.contentSync(), invalid); |
| 108 | + } finally { |
| 109 | + zip.closeSync(); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + { |
| 114 | + const zip = zlib.ZipFile.openSync(file); |
| 115 | + try { |
| 116 | + const fileEntry = zip.getSync('entry.txt'); |
| 117 | + assert.throws(() => fileEntry.contentSync(), invalid); |
| 118 | + assert.throws(() => fileEntry.contentSync(), invalid); |
| 119 | + } finally { |
| 120 | + zip.closeSync(); |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + { |
| 125 | + const zip = await zlib.ZipFile.open(file); |
| 126 | + try { |
| 127 | + const fileEntry = await zip.get('entry.txt'); |
| 128 | + assert.ok(fileEntry.modified instanceof Date); |
| 129 | + await assert.rejects(fileEntry.content(), invalid); |
| 130 | + await assert.rejects(fileEntry.content(), invalid); |
| 131 | + } finally { |
| 132 | + await zip.close(); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + { |
| 137 | + const zip = await zlib.ZipFile.open(file); |
| 138 | + try { |
| 139 | + const fileEntry = await zip.get('entry.txt'); |
| 140 | + await assert.rejects(fileEntry.content(), invalid); |
| 141 | + await assert.rejects(fileEntry.content(), invalid); |
| 142 | + } finally { |
| 143 | + await zip.close(); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + { |
| 148 | + const zip = await zlib.ZipFile.open(file); |
| 149 | + try { |
| 150 | + const fileEntry = await zip.get('entry.txt'); |
| 151 | + assert.ok(fileEntry.modified instanceof Date); |
| 152 | + await assert.rejects(async () => { |
| 153 | + for await (const chunk of fileEntry.contentIterator()) { |
| 154 | + assert.ok(Buffer.isBuffer(chunk)); |
| 155 | + } |
| 156 | + }, invalid); |
| 157 | + await assert.rejects(async () => { |
| 158 | + for await (const chunk of await zip.stream('entry.txt')) { |
| 159 | + assert.ok(Buffer.isBuffer(chunk)); |
| 160 | + } |
| 161 | + }, invalid); |
| 162 | + } finally { |
| 163 | + await zip.close(); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + { |
| 168 | + const zip = zlib.ZipFile.openSync(file); |
| 169 | + try { |
| 170 | + assert.throws(() => zip.compactSync(), invalid); |
| 171 | + } finally { |
| 172 | + zip.closeSync(); |
| 173 | + } |
| 174 | + } |
| 175 | + |
| 176 | + { |
| 177 | + const zip = await zlib.ZipFile.open(file); |
| 178 | + try { |
| 179 | + await assert.rejects(async () => { |
| 180 | + for await (const chunk of zip.compact()) { |
| 181 | + assert.ok(Buffer.isBuffer(chunk)); |
| 182 | + } |
| 183 | + }, invalid); |
| 184 | + } finally { |
| 185 | + await zip.close(); |
| 186 | + } |
| 187 | + } |
| 188 | +}); |
| 189 | + |
79 | 190 | // 2. zipFiles must reject a FIFO source rather than block on open() forever. |
80 | 191 | test('zipFiles rejects a FIFO source instead of hanging', () => { |
81 | 192 | if (process.platform === 'win32') return; // no mkfifo |
|