@juspay/neurolink 10.12.4 → 10.12.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +6 -0
- package/dist/browser/neurolink.min.js +408 -408
- package/dist/lib/processors/archive/ArchiveProcessor.js +13 -67
- package/dist/lib/processors/archive/zipEntryReader.d.ts +51 -0
- package/dist/lib/processors/archive/zipEntryReader.js +81 -0
- package/dist/lib/processors/document/OpenDocumentProcessor.js +13 -1
- package/dist/lib/processors/document/PptxProcessor.js +58 -12
- package/dist/lib/types/processor.d.ts +15 -0
- package/dist/processors/archive/ArchiveProcessor.js +13 -67
- package/dist/processors/archive/zipEntryReader.d.ts +51 -0
- package/dist/processors/archive/zipEntryReader.js +80 -0
- package/dist/processors/document/OpenDocumentProcessor.js +13 -1
- package/dist/processors/document/PptxProcessor.js +58 -12
- package/dist/types/processor.d.ts +15 -0
- package/package.json +4 -3
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
*/
|
|
38
38
|
import * as path from "path";
|
|
39
39
|
import { BaseFileProcessor } from "../base/BaseFileProcessor.js";
|
|
40
|
+
import { isDecompressionBoundExceeded, readZipEntryWithinLimit, } from "./zipEntryReader.js";
|
|
40
41
|
import { SIZE_LIMITS_MB } from "../config/index.js";
|
|
41
42
|
import { FileErrorCode } from "../errors/index.js";
|
|
42
43
|
// =============================================================================
|
|
@@ -159,71 +160,6 @@ const SINGLE_STREAM_TOOLS = {
|
|
|
159
160
|
xz: "xz",
|
|
160
161
|
zst: "zstd",
|
|
161
162
|
};
|
|
162
|
-
/**
|
|
163
|
-
* Read one ZIP entry's bytes without trusting the size it declares.
|
|
164
|
-
*
|
|
165
|
-
* `entry.getData()` cannot be used for this. It sizes its output buffer from
|
|
166
|
-
* the central-directory `size` field, which the archive author chooses, and
|
|
167
|
-
* adm-zip only arms its own guard when that field is positive:
|
|
168
|
-
*
|
|
169
|
-
* const option = version >= 15 && expectedLength > 0
|
|
170
|
-
* ? { maxOutputLength: expectedLength } : {};
|
|
171
|
-
*
|
|
172
|
-
* So an entry declaring 0 disables the bound and the caller's `size > maxSize`
|
|
173
|
-
* check in one move — `0 > 5MB` is false, and the inflate then runs uncapped.
|
|
174
|
-
* The declared size is the attack, so nothing here may depend on it: the cap
|
|
175
|
-
* comes from our own limit and is handed to the decoder.
|
|
176
|
-
*
|
|
177
|
-
* CRC is verified on both paths rather than dropped, so bypassing `getData()`
|
|
178
|
-
* does not also quietly lose its corruption check — a STORED entry is copied
|
|
179
|
-
* out rather than decoded, but it can be damaged just the same. It detects
|
|
180
|
-
* damage, not malice — the CRC field is attacker-controlled too.
|
|
181
|
-
*/
|
|
182
|
-
function readZipEntryWithinLimit(entry, maxBytes, zlibModule) {
|
|
183
|
-
const compressed = entry.getCompressedData();
|
|
184
|
-
const matchesCrc = (data) => (zlibModule.crc32(data) >>> 0) === (entry.header.crc >>> 0);
|
|
185
|
-
// STORED: the bytes are already the payload, so its own length is the bound.
|
|
186
|
-
if (entry.header.method === ZIP_METHOD_STORED) {
|
|
187
|
-
if (compressed.length > maxBytes) {
|
|
188
|
-
return { status: "too-large" };
|
|
189
|
-
}
|
|
190
|
-
return matchesCrc(compressed)
|
|
191
|
-
? { status: "ok", buffer: compressed }
|
|
192
|
-
: { status: "corrupt" };
|
|
193
|
-
}
|
|
194
|
-
if (entry.header.method !== ZIP_METHOD_DEFLATED) {
|
|
195
|
-
return { status: "unsupported-method" };
|
|
196
|
-
}
|
|
197
|
-
let inflated;
|
|
198
|
-
try {
|
|
199
|
-
inflated = zlibModule.inflateRawSync(compressed, {
|
|
200
|
-
maxOutputLength: maxBytes,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
catch (error) {
|
|
204
|
-
if (isDecompressionBoundExceeded(error)) {
|
|
205
|
-
return { status: "too-large" };
|
|
206
|
-
}
|
|
207
|
-
return { status: "corrupt" };
|
|
208
|
-
}
|
|
209
|
-
return matchesCrc(inflated)
|
|
210
|
-
? { status: "ok", buffer: inflated }
|
|
211
|
-
: { status: "corrupt" };
|
|
212
|
-
}
|
|
213
|
-
/** ZIP compression methods this reader handles (APPNOTE 4.4.5). */
|
|
214
|
-
const ZIP_METHOD_STORED = 0;
|
|
215
|
-
const ZIP_METHOD_DEFLATED = 8;
|
|
216
|
-
/**
|
|
217
|
-
* Whether a zlib rejection is the output bound firing rather than bad input.
|
|
218
|
-
*
|
|
219
|
-
* `maxOutputLength` aborts an inflate the moment its output would pass the cap,
|
|
220
|
-
* which is the whole point — but it surfaces as a plain `RangeError`, and a
|
|
221
|
-
* bomb reported as "failed to decompress" reads as a corrupt upload and invites
|
|
222
|
-
* the user to send it again. It will fail identically every time.
|
|
223
|
-
*
|
|
224
|
-
* Keyed on `code`, not the message: the message embeds a byte count.
|
|
225
|
-
*/
|
|
226
|
-
const isDecompressionBoundExceeded = (error) => error?.code === "ERR_BUFFER_TOO_LARGE";
|
|
227
163
|
/** File extensions recognized as archive formats */
|
|
228
164
|
const SUPPORTED_ARCHIVE_EXTENSIONS = [".zip", ".tar", ".gz", ".tgz", ".bz2", ".tbz2", ".jar", ".xz", ".txz", ".zst", ".tzst"];
|
|
229
165
|
// =============================================================================
|
|
@@ -1419,6 +1355,7 @@ export class ArchiveProcessor extends BaseFileProcessor {
|
|
|
1419
1355
|
.sort((a, b) => a.uncompressedSize - b.uncompressedSize);
|
|
1420
1356
|
let totalExtracted = 0;
|
|
1421
1357
|
let extractCount = 0;
|
|
1358
|
+
const zlibModule = await import("zlib");
|
|
1422
1359
|
for (const entry of candidates) {
|
|
1423
1360
|
if (extractCount >= ARCHIVE_CONFIG.MAX_EXTRACT_ENTRIES) {
|
|
1424
1361
|
break;
|
|
@@ -1431,8 +1368,17 @@ export class ArchiveProcessor extends BaseFileProcessor {
|
|
|
1431
1368
|
if (!zipEntry) {
|
|
1432
1369
|
continue;
|
|
1433
1370
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1371
|
+
// This path was already bounded, but only by coincidence: entries
|
|
1372
|
+
// declaring 0 are dropped above, oversized declarations are dropped
|
|
1373
|
+
// above, and adm-zip caps everything else at the size it declares.
|
|
1374
|
+
// That leaves the ceiling resting on library internals we do not
|
|
1375
|
+
// trust anywhere else in this file, so it is spelled out here.
|
|
1376
|
+
const read = readZipEntryWithinLimit(zipEntry, Math.min(ARCHIVE_CONFIG.MAX_EXTRACT_ENTRY_SIZE, ARCHIVE_CONFIG.MAX_TOTAL_EXTRACT_SIZE - totalExtracted), zlibModule);
|
|
1377
|
+
if (read.status !== "ok") {
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
const data = read.buffer;
|
|
1381
|
+
if (data.length === 0) {
|
|
1436
1382
|
continue;
|
|
1437
1383
|
}
|
|
1438
1384
|
// Simple binary detection: check for null bytes in first 512 bytes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded ZIP entry reading, shared by every processor that opens a ZIP.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from ArchiveProcessor because the Office formats are ZIPs too:
|
|
5
|
+
* .pptx and .odt read their entries directly and so can be handed the same
|
|
6
|
+
* bomb, and need the same refusal. One implementation rather than three means
|
|
7
|
+
* a correction to the guard lands everywhere at once.
|
|
8
|
+
*
|
|
9
|
+
* .docx and .xlsx deliberately do NOT use this. mammoth and exceljs unzip for
|
|
10
|
+
* themselves and were measured refusing a 400MB bomb at 46MB and 53MB peak,
|
|
11
|
+
* so wrapping them in a pre-scan bought no safety and roughly tripled the cost
|
|
12
|
+
* of every ordinary document.
|
|
13
|
+
*
|
|
14
|
+
* @module processors/archive/zipEntryReader
|
|
15
|
+
*/
|
|
16
|
+
import type { ArchiveEntryReadResult, BoundedZipEntry } from "../../types/index.js";
|
|
17
|
+
/** ZIP compression methods this reader handles (APPNOTE 4.4.5). */
|
|
18
|
+
export declare const ZIP_METHOD_STORED = 0;
|
|
19
|
+
export declare const ZIP_METHOD_DEFLATED = 8;
|
|
20
|
+
/**
|
|
21
|
+
* Whether a zlib rejection is the output bound firing rather than bad input.
|
|
22
|
+
*
|
|
23
|
+
* `maxOutputLength` aborts an inflate the moment its output would pass the cap,
|
|
24
|
+
* which is the whole point — but it surfaces as a plain `RangeError`, and a
|
|
25
|
+
* bomb reported as "failed to decompress" reads as a corrupt upload and invites
|
|
26
|
+
* the user to send it again. It will fail identically every time.
|
|
27
|
+
*
|
|
28
|
+
* Keyed on `code`, not the message: the message embeds a byte count.
|
|
29
|
+
*/
|
|
30
|
+
export declare const isDecompressionBoundExceeded: (error: unknown) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Read one ZIP entry's bytes without trusting the size it declares.
|
|
33
|
+
*
|
|
34
|
+
* `entry.getData()` cannot be used for this. It sizes its output buffer from
|
|
35
|
+
* the central-directory `size` field, which the archive author chooses, and
|
|
36
|
+
* adm-zip only arms its own guard when that field is positive:
|
|
37
|
+
*
|
|
38
|
+
* const option = version >= 15 && expectedLength > 0
|
|
39
|
+
* ? { maxOutputLength: expectedLength } : {};
|
|
40
|
+
*
|
|
41
|
+
* So an entry declaring 0 disables the bound and the caller's `size > maxSize`
|
|
42
|
+
* check in one move — `0 > 5MB` is false, and the inflate then runs uncapped.
|
|
43
|
+
* The declared size is the attack, so nothing here may depend on it: the cap
|
|
44
|
+
* comes from our own limit and is handed to the decoder.
|
|
45
|
+
*
|
|
46
|
+
* CRC is verified on both paths rather than dropped, so bypassing `getData()`
|
|
47
|
+
* does not also quietly lose its corruption check — a STORED entry is copied
|
|
48
|
+
* out rather than decoded, but it can be damaged just the same. It detects
|
|
49
|
+
* damage, not malice — the CRC field is attacker-controlled too.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readZipEntryWithinLimit(entry: BoundedZipEntry, maxBytes: number, zlibModule: typeof import("zlib")): ArchiveEntryReadResult;
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded ZIP entry reading, shared by every processor that opens a ZIP.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from ArchiveProcessor because the Office formats are ZIPs too:
|
|
5
|
+
* .pptx and .odt read their entries directly and so can be handed the same
|
|
6
|
+
* bomb, and need the same refusal. One implementation rather than three means
|
|
7
|
+
* a correction to the guard lands everywhere at once.
|
|
8
|
+
*
|
|
9
|
+
* .docx and .xlsx deliberately do NOT use this. mammoth and exceljs unzip for
|
|
10
|
+
* themselves and were measured refusing a 400MB bomb at 46MB and 53MB peak,
|
|
11
|
+
* so wrapping them in a pre-scan bought no safety and roughly tripled the cost
|
|
12
|
+
* of every ordinary document.
|
|
13
|
+
*
|
|
14
|
+
* @module processors/archive/zipEntryReader
|
|
15
|
+
*/
|
|
16
|
+
/** ZIP compression methods this reader handles (APPNOTE 4.4.5). */
|
|
17
|
+
export const ZIP_METHOD_STORED = 0;
|
|
18
|
+
export const ZIP_METHOD_DEFLATED = 8;
|
|
19
|
+
/**
|
|
20
|
+
* Whether a zlib rejection is the output bound firing rather than bad input.
|
|
21
|
+
*
|
|
22
|
+
* `maxOutputLength` aborts an inflate the moment its output would pass the cap,
|
|
23
|
+
* which is the whole point — but it surfaces as a plain `RangeError`, and a
|
|
24
|
+
* bomb reported as "failed to decompress" reads as a corrupt upload and invites
|
|
25
|
+
* the user to send it again. It will fail identically every time.
|
|
26
|
+
*
|
|
27
|
+
* Keyed on `code`, not the message: the message embeds a byte count.
|
|
28
|
+
*/
|
|
29
|
+
export const isDecompressionBoundExceeded = (error) => error?.code === "ERR_BUFFER_TOO_LARGE";
|
|
30
|
+
/**
|
|
31
|
+
* Read one ZIP entry's bytes without trusting the size it declares.
|
|
32
|
+
*
|
|
33
|
+
* `entry.getData()` cannot be used for this. It sizes its output buffer from
|
|
34
|
+
* the central-directory `size` field, which the archive author chooses, and
|
|
35
|
+
* adm-zip only arms its own guard when that field is positive:
|
|
36
|
+
*
|
|
37
|
+
* const option = version >= 15 && expectedLength > 0
|
|
38
|
+
* ? { maxOutputLength: expectedLength } : {};
|
|
39
|
+
*
|
|
40
|
+
* So an entry declaring 0 disables the bound and the caller's `size > maxSize`
|
|
41
|
+
* check in one move — `0 > 5MB` is false, and the inflate then runs uncapped.
|
|
42
|
+
* The declared size is the attack, so nothing here may depend on it: the cap
|
|
43
|
+
* comes from our own limit and is handed to the decoder.
|
|
44
|
+
*
|
|
45
|
+
* CRC is verified on both paths rather than dropped, so bypassing `getData()`
|
|
46
|
+
* does not also quietly lose its corruption check — a STORED entry is copied
|
|
47
|
+
* out rather than decoded, but it can be damaged just the same. It detects
|
|
48
|
+
* damage, not malice — the CRC field is attacker-controlled too.
|
|
49
|
+
*/
|
|
50
|
+
export function readZipEntryWithinLimit(entry, maxBytes, zlibModule) {
|
|
51
|
+
const compressed = entry.getCompressedData();
|
|
52
|
+
const matchesCrc = (data) => (zlibModule.crc32(data) >>> 0) === (entry.header.crc >>> 0);
|
|
53
|
+
// STORED: the bytes are already the payload, so its own length is the bound.
|
|
54
|
+
if (entry.header.method === ZIP_METHOD_STORED) {
|
|
55
|
+
if (compressed.length > maxBytes) {
|
|
56
|
+
return { status: "too-large" };
|
|
57
|
+
}
|
|
58
|
+
return matchesCrc(compressed)
|
|
59
|
+
? { status: "ok", buffer: compressed }
|
|
60
|
+
: { status: "corrupt" };
|
|
61
|
+
}
|
|
62
|
+
if (entry.header.method !== ZIP_METHOD_DEFLATED) {
|
|
63
|
+
return { status: "unsupported-method" };
|
|
64
|
+
}
|
|
65
|
+
let inflated;
|
|
66
|
+
try {
|
|
67
|
+
inflated = zlibModule.inflateRawSync(compressed, {
|
|
68
|
+
maxOutputLength: maxBytes,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (isDecompressionBoundExceeded(error)) {
|
|
73
|
+
return { status: "too-large" };
|
|
74
|
+
}
|
|
75
|
+
return { status: "corrupt" };
|
|
76
|
+
}
|
|
77
|
+
return matchesCrc(inflated)
|
|
78
|
+
? { status: "ok", buffer: inflated }
|
|
79
|
+
: { status: "corrupt" };
|
|
80
|
+
}
|
|
81
|
+
//# sourceMappingURL=zipEntryReader.js.map
|
|
@@ -7,7 +7,9 @@
|
|
|
7
7
|
* @module processors/document/OpenDocumentProcessor
|
|
8
8
|
*/
|
|
9
9
|
import { createRequire } from "node:module";
|
|
10
|
+
import * as zlib from "node:zlib";
|
|
10
11
|
import { BaseFileProcessor } from "../base/BaseFileProcessor.js";
|
|
12
|
+
import { readZipEntryWithinLimit } from "../archive/zipEntryReader.js";
|
|
11
13
|
import { SIZE_LIMITS } from "../config/index.js";
|
|
12
14
|
const require = createRequire(import.meta.url);
|
|
13
15
|
// Re-import for local use within this file
|
|
@@ -64,7 +66,17 @@ export class OpenDocumentProcessor extends BaseFileProcessor {
|
|
|
64
66
|
// Try to get content.xml
|
|
65
67
|
const contentEntry = zip.getEntry("content.xml");
|
|
66
68
|
if (contentEntry) {
|
|
67
|
-
|
|
69
|
+
// Bounded rather than `getData()`: an ODF file is a ZIP, so its
|
|
70
|
+
// content.xml can declare any uncompressed size it likes, and
|
|
71
|
+
// `maxSizeMB` only ever saw the compressed archive on the way in.
|
|
72
|
+
const read = readZipEntryWithinLimit(contentEntry, SIZE_LIMITS.DOCUMENT_MAX_MB * 1024 * 1024, zlib);
|
|
73
|
+
if (read.status === "too-large") {
|
|
74
|
+
throw new Error(`content.xml exceeds the ${SIZE_LIMITS.DOCUMENT_MAX_MB}MB limit for OpenDocument content`);
|
|
75
|
+
}
|
|
76
|
+
if (read.status !== "ok") {
|
|
77
|
+
throw new Error("content.xml could not be read from the archive");
|
|
78
|
+
}
|
|
79
|
+
const xmlContent = read.buffer.toString("utf-8");
|
|
68
80
|
const extracted = this.extractTextFromXml(xmlContent);
|
|
69
81
|
textContent = extracted.text;
|
|
70
82
|
paragraphCount = extracted.paragraphCount;
|
|
@@ -27,6 +27,47 @@
|
|
|
27
27
|
* ```
|
|
28
28
|
*/
|
|
29
29
|
import AdmZip from "adm-zip";
|
|
30
|
+
import * as zlib from "node:zlib";
|
|
31
|
+
import { readZipEntryWithinLimit } from "../archive/zipEntryReader.js";
|
|
32
|
+
import { SIZE_LIMITS } from "../config/index.js";
|
|
33
|
+
/**
|
|
34
|
+
* Total budget for everything read out of one presentation.
|
|
35
|
+
*
|
|
36
|
+
* This class is a static utility with no `BaseFileProcessor` behind it, so
|
|
37
|
+
* there is no `maxSizeMB` to inherit and nothing else was bounding these
|
|
38
|
+
* reads at all — not even a late check. A .pptx is a ZIP, so any entry in it
|
|
39
|
+
* can declare whatever uncompressed size its author likes.
|
|
40
|
+
*/
|
|
41
|
+
const PPTX_MAX_TOTAL_BYTES = SIZE_LIMITS.DOCUMENT_MAX_MB * 1024 * 1024;
|
|
42
|
+
/**
|
|
43
|
+
* Read entries out of one presentation, refusing once the total passes budget.
|
|
44
|
+
*
|
|
45
|
+
* The budget spans the call rather than each entry: a deck of two hundred
|
|
46
|
+
* slides that each sit just under a per-entry limit is the obvious way around
|
|
47
|
+
* one. Returns null for an absent or unreadable entry, which every call site
|
|
48
|
+
* already treats as "no content here".
|
|
49
|
+
*/
|
|
50
|
+
function createBoundedEntryReader() {
|
|
51
|
+
let spent = 0;
|
|
52
|
+
return (entry) => {
|
|
53
|
+
if (!entry) {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
const remaining = PPTX_MAX_TOTAL_BYTES - spent;
|
|
57
|
+
if (remaining <= 0) {
|
|
58
|
+
throw new Error(`PPTX content exceeds the ${SIZE_LIMITS.DOCUMENT_MAX_MB}MB limit`);
|
|
59
|
+
}
|
|
60
|
+
const read = readZipEntryWithinLimit(entry, remaining, zlib);
|
|
61
|
+
if (read.status === "too-large") {
|
|
62
|
+
throw new Error(`PPTX content exceeds the ${SIZE_LIMITS.DOCUMENT_MAX_MB}MB limit`);
|
|
63
|
+
}
|
|
64
|
+
if (read.status !== "ok") {
|
|
65
|
+
return null;
|
|
66
|
+
}
|
|
67
|
+
spent += read.buffer.length;
|
|
68
|
+
return read.buffer.toString("utf-8");
|
|
69
|
+
};
|
|
70
|
+
}
|
|
30
71
|
/**
|
|
31
72
|
* Regex to match text content within PowerPoint XML `<a:t>` elements.
|
|
32
73
|
* These elements contain the actual visible text on slides.
|
|
@@ -63,14 +104,17 @@ export class PptxProcessor {
|
|
|
63
104
|
static async extractText(content) {
|
|
64
105
|
const zip = new AdmZip(content);
|
|
65
106
|
const entries = zip.getEntries();
|
|
107
|
+
const readEntry = createBoundedEntryReader();
|
|
66
108
|
// Collect slide entries with their slide numbers for sorting
|
|
67
109
|
const slides = [];
|
|
68
110
|
for (const entry of entries) {
|
|
69
111
|
const match = entry.entryName.match(SLIDE_ENTRY_REGEX);
|
|
70
112
|
if (match) {
|
|
71
113
|
const slideNumber = parseInt(match[1], 10);
|
|
72
|
-
const xmlContent = entry
|
|
73
|
-
|
|
114
|
+
const xmlContent = readEntry(entry);
|
|
115
|
+
if (xmlContent !== null) {
|
|
116
|
+
slides.push({ slideNumber, xml: xmlContent });
|
|
117
|
+
}
|
|
74
118
|
}
|
|
75
119
|
}
|
|
76
120
|
// Sort slides by number (slide1, slide2, ...)
|
|
@@ -82,7 +126,7 @@ export class PptxProcessor {
|
|
|
82
126
|
parts.push(`Presentation: ${slides.length} slide(s)\n`);
|
|
83
127
|
for (const slide of slides) {
|
|
84
128
|
const texts = PptxProcessor.extractTextFromXml(slide.xml);
|
|
85
|
-
const notes = PptxProcessor.extractNotesForSlide(zip, slide.slideNumber);
|
|
129
|
+
const notes = PptxProcessor.extractNotesForSlide(zip, slide.slideNumber, readEntry);
|
|
86
130
|
// Emit a slide section when it has either body text or speaker notes.
|
|
87
131
|
if (texts.length > 0 || notes) {
|
|
88
132
|
parts.push(`### Slide ${slide.slideNumber}`);
|
|
@@ -111,12 +155,11 @@ export class PptxProcessor {
|
|
|
111
155
|
* @param slideNumber - 1-indexed slide number
|
|
112
156
|
* @returns The notes text (runs joined by a space), or null when absent
|
|
113
157
|
*/
|
|
114
|
-
static extractNotesForSlide(zip, slideNumber) {
|
|
115
|
-
const
|
|
116
|
-
if (
|
|
158
|
+
static extractNotesForSlide(zip, slideNumber, readEntry) {
|
|
159
|
+
const relsXml = readEntry(zip.getEntry(SLIDE_RELS_NAME(slideNumber)));
|
|
160
|
+
if (relsXml === null) {
|
|
117
161
|
return null;
|
|
118
162
|
}
|
|
119
|
-
const relsXml = relsEntry.getData().toString("utf-8");
|
|
120
163
|
let notesTarget = null;
|
|
121
164
|
RELATIONSHIP_TAG_REGEX.lastIndex = 0;
|
|
122
165
|
for (let match = RELATIONSHIP_TAG_REGEX.exec(relsXml); match !== null; match = RELATIONSHIP_TAG_REGEX.exec(relsXml)) {
|
|
@@ -134,11 +177,11 @@ export class PptxProcessor {
|
|
|
134
177
|
const normalized = notesTarget
|
|
135
178
|
.replace(/^\.\.\//, "ppt/")
|
|
136
179
|
.replace(/^\/+/, "");
|
|
137
|
-
const
|
|
138
|
-
if (
|
|
180
|
+
const notesXml = readEntry(zip.getEntry(normalized));
|
|
181
|
+
if (notesXml === null) {
|
|
139
182
|
return null;
|
|
140
183
|
}
|
|
141
|
-
const notes = PptxProcessor.extractTextFromXml(
|
|
184
|
+
const notes = PptxProcessor.extractTextFromXml(notesXml).join(" ");
|
|
142
185
|
return notes.trim() || null;
|
|
143
186
|
}
|
|
144
187
|
/**
|
|
@@ -175,6 +218,7 @@ export class PptxProcessor {
|
|
|
175
218
|
static async extractSlides(content, slideNumbers) {
|
|
176
219
|
const zip = new AdmZip(content);
|
|
177
220
|
const entries = zip.getEntries();
|
|
221
|
+
const readEntry = createBoundedEntryReader();
|
|
178
222
|
// Collect all slides
|
|
179
223
|
const slides = [];
|
|
180
224
|
for (const entry of entries) {
|
|
@@ -182,8 +226,10 @@ export class PptxProcessor {
|
|
|
182
226
|
if (match) {
|
|
183
227
|
const slideNumber = parseInt(match[1], 10);
|
|
184
228
|
if (slideNumbers.includes(slideNumber)) {
|
|
185
|
-
const xmlContent = entry
|
|
186
|
-
|
|
229
|
+
const xmlContent = readEntry(entry);
|
|
230
|
+
if (xmlContent !== null) {
|
|
231
|
+
slides.push({ slideNumber, xml: xmlContent });
|
|
232
|
+
}
|
|
187
233
|
}
|
|
188
234
|
}
|
|
189
235
|
}
|
|
@@ -774,6 +774,21 @@ export type ArchiveDecompressionResult = {
|
|
|
774
774
|
* perfectly well-formed, and telling the user it is damaged would send them to
|
|
775
775
|
* re-create a file that was never broken.
|
|
776
776
|
*/
|
|
777
|
+
/**
|
|
778
|
+
* The slice of an adm-zip entry the bounded reader depends on.
|
|
779
|
+
*
|
|
780
|
+
* Structural rather than adm-zip's own `IZipEntry` so the reader states what it
|
|
781
|
+
* actually needs — the compressed bytes and the header fields it refuses to
|
|
782
|
+
* trust — instead of importing a library type it would then have to satisfy in
|
|
783
|
+
* full when building a test double.
|
|
784
|
+
*/
|
|
785
|
+
export type BoundedZipEntry = {
|
|
786
|
+
getCompressedData: () => Buffer;
|
|
787
|
+
header: {
|
|
788
|
+
method: number;
|
|
789
|
+
crc: number;
|
|
790
|
+
};
|
|
791
|
+
};
|
|
777
792
|
export type ArchiveEntryReadResult = {
|
|
778
793
|
readonly status: "ok";
|
|
779
794
|
readonly buffer: Buffer;
|
|
@@ -37,6 +37,7 @@
|
|
|
37
37
|
*/
|
|
38
38
|
import * as path from "path";
|
|
39
39
|
import { BaseFileProcessor } from "../base/BaseFileProcessor.js";
|
|
40
|
+
import { isDecompressionBoundExceeded, readZipEntryWithinLimit, } from "./zipEntryReader.js";
|
|
40
41
|
import { SIZE_LIMITS_MB } from "../config/index.js";
|
|
41
42
|
import { FileErrorCode } from "../errors/index.js";
|
|
42
43
|
// =============================================================================
|
|
@@ -159,71 +160,6 @@ const SINGLE_STREAM_TOOLS = {
|
|
|
159
160
|
xz: "xz",
|
|
160
161
|
zst: "zstd",
|
|
161
162
|
};
|
|
162
|
-
/**
|
|
163
|
-
* Read one ZIP entry's bytes without trusting the size it declares.
|
|
164
|
-
*
|
|
165
|
-
* `entry.getData()` cannot be used for this. It sizes its output buffer from
|
|
166
|
-
* the central-directory `size` field, which the archive author chooses, and
|
|
167
|
-
* adm-zip only arms its own guard when that field is positive:
|
|
168
|
-
*
|
|
169
|
-
* const option = version >= 15 && expectedLength > 0
|
|
170
|
-
* ? { maxOutputLength: expectedLength } : {};
|
|
171
|
-
*
|
|
172
|
-
* So an entry declaring 0 disables the bound and the caller's `size > maxSize`
|
|
173
|
-
* check in one move — `0 > 5MB` is false, and the inflate then runs uncapped.
|
|
174
|
-
* The declared size is the attack, so nothing here may depend on it: the cap
|
|
175
|
-
* comes from our own limit and is handed to the decoder.
|
|
176
|
-
*
|
|
177
|
-
* CRC is verified on both paths rather than dropped, so bypassing `getData()`
|
|
178
|
-
* does not also quietly lose its corruption check — a STORED entry is copied
|
|
179
|
-
* out rather than decoded, but it can be damaged just the same. It detects
|
|
180
|
-
* damage, not malice — the CRC field is attacker-controlled too.
|
|
181
|
-
*/
|
|
182
|
-
function readZipEntryWithinLimit(entry, maxBytes, zlibModule) {
|
|
183
|
-
const compressed = entry.getCompressedData();
|
|
184
|
-
const matchesCrc = (data) => (zlibModule.crc32(data) >>> 0) === (entry.header.crc >>> 0);
|
|
185
|
-
// STORED: the bytes are already the payload, so its own length is the bound.
|
|
186
|
-
if (entry.header.method === ZIP_METHOD_STORED) {
|
|
187
|
-
if (compressed.length > maxBytes) {
|
|
188
|
-
return { status: "too-large" };
|
|
189
|
-
}
|
|
190
|
-
return matchesCrc(compressed)
|
|
191
|
-
? { status: "ok", buffer: compressed }
|
|
192
|
-
: { status: "corrupt" };
|
|
193
|
-
}
|
|
194
|
-
if (entry.header.method !== ZIP_METHOD_DEFLATED) {
|
|
195
|
-
return { status: "unsupported-method" };
|
|
196
|
-
}
|
|
197
|
-
let inflated;
|
|
198
|
-
try {
|
|
199
|
-
inflated = zlibModule.inflateRawSync(compressed, {
|
|
200
|
-
maxOutputLength: maxBytes,
|
|
201
|
-
});
|
|
202
|
-
}
|
|
203
|
-
catch (error) {
|
|
204
|
-
if (isDecompressionBoundExceeded(error)) {
|
|
205
|
-
return { status: "too-large" };
|
|
206
|
-
}
|
|
207
|
-
return { status: "corrupt" };
|
|
208
|
-
}
|
|
209
|
-
return matchesCrc(inflated)
|
|
210
|
-
? { status: "ok", buffer: inflated }
|
|
211
|
-
: { status: "corrupt" };
|
|
212
|
-
}
|
|
213
|
-
/** ZIP compression methods this reader handles (APPNOTE 4.4.5). */
|
|
214
|
-
const ZIP_METHOD_STORED = 0;
|
|
215
|
-
const ZIP_METHOD_DEFLATED = 8;
|
|
216
|
-
/**
|
|
217
|
-
* Whether a zlib rejection is the output bound firing rather than bad input.
|
|
218
|
-
*
|
|
219
|
-
* `maxOutputLength` aborts an inflate the moment its output would pass the cap,
|
|
220
|
-
* which is the whole point — but it surfaces as a plain `RangeError`, and a
|
|
221
|
-
* bomb reported as "failed to decompress" reads as a corrupt upload and invites
|
|
222
|
-
* the user to send it again. It will fail identically every time.
|
|
223
|
-
*
|
|
224
|
-
* Keyed on `code`, not the message: the message embeds a byte count.
|
|
225
|
-
*/
|
|
226
|
-
const isDecompressionBoundExceeded = (error) => error?.code === "ERR_BUFFER_TOO_LARGE";
|
|
227
163
|
/** File extensions recognized as archive formats */
|
|
228
164
|
const SUPPORTED_ARCHIVE_EXTENSIONS = [".zip", ".tar", ".gz", ".tgz", ".bz2", ".tbz2", ".jar", ".xz", ".txz", ".zst", ".tzst"];
|
|
229
165
|
// =============================================================================
|
|
@@ -1419,6 +1355,7 @@ export class ArchiveProcessor extends BaseFileProcessor {
|
|
|
1419
1355
|
.sort((a, b) => a.uncompressedSize - b.uncompressedSize);
|
|
1420
1356
|
let totalExtracted = 0;
|
|
1421
1357
|
let extractCount = 0;
|
|
1358
|
+
const zlibModule = await import("zlib");
|
|
1422
1359
|
for (const entry of candidates) {
|
|
1423
1360
|
if (extractCount >= ARCHIVE_CONFIG.MAX_EXTRACT_ENTRIES) {
|
|
1424
1361
|
break;
|
|
@@ -1431,8 +1368,17 @@ export class ArchiveProcessor extends BaseFileProcessor {
|
|
|
1431
1368
|
if (!zipEntry) {
|
|
1432
1369
|
continue;
|
|
1433
1370
|
}
|
|
1434
|
-
|
|
1435
|
-
|
|
1371
|
+
// This path was already bounded, but only by coincidence: entries
|
|
1372
|
+
// declaring 0 are dropped above, oversized declarations are dropped
|
|
1373
|
+
// above, and adm-zip caps everything else at the size it declares.
|
|
1374
|
+
// That leaves the ceiling resting on library internals we do not
|
|
1375
|
+
// trust anywhere else in this file, so it is spelled out here.
|
|
1376
|
+
const read = readZipEntryWithinLimit(zipEntry, Math.min(ARCHIVE_CONFIG.MAX_EXTRACT_ENTRY_SIZE, ARCHIVE_CONFIG.MAX_TOTAL_EXTRACT_SIZE - totalExtracted), zlibModule);
|
|
1377
|
+
if (read.status !== "ok") {
|
|
1378
|
+
continue;
|
|
1379
|
+
}
|
|
1380
|
+
const data = read.buffer;
|
|
1381
|
+
if (data.length === 0) {
|
|
1436
1382
|
continue;
|
|
1437
1383
|
}
|
|
1438
1384
|
// Simple binary detection: check for null bytes in first 512 bytes
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded ZIP entry reading, shared by every processor that opens a ZIP.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from ArchiveProcessor because the Office formats are ZIPs too:
|
|
5
|
+
* .pptx and .odt read their entries directly and so can be handed the same
|
|
6
|
+
* bomb, and need the same refusal. One implementation rather than three means
|
|
7
|
+
* a correction to the guard lands everywhere at once.
|
|
8
|
+
*
|
|
9
|
+
* .docx and .xlsx deliberately do NOT use this. mammoth and exceljs unzip for
|
|
10
|
+
* themselves and were measured refusing a 400MB bomb at 46MB and 53MB peak,
|
|
11
|
+
* so wrapping them in a pre-scan bought no safety and roughly tripled the cost
|
|
12
|
+
* of every ordinary document.
|
|
13
|
+
*
|
|
14
|
+
* @module processors/archive/zipEntryReader
|
|
15
|
+
*/
|
|
16
|
+
import type { ArchiveEntryReadResult, BoundedZipEntry } from "../../types/index.js";
|
|
17
|
+
/** ZIP compression methods this reader handles (APPNOTE 4.4.5). */
|
|
18
|
+
export declare const ZIP_METHOD_STORED = 0;
|
|
19
|
+
export declare const ZIP_METHOD_DEFLATED = 8;
|
|
20
|
+
/**
|
|
21
|
+
* Whether a zlib rejection is the output bound firing rather than bad input.
|
|
22
|
+
*
|
|
23
|
+
* `maxOutputLength` aborts an inflate the moment its output would pass the cap,
|
|
24
|
+
* which is the whole point — but it surfaces as a plain `RangeError`, and a
|
|
25
|
+
* bomb reported as "failed to decompress" reads as a corrupt upload and invites
|
|
26
|
+
* the user to send it again. It will fail identically every time.
|
|
27
|
+
*
|
|
28
|
+
* Keyed on `code`, not the message: the message embeds a byte count.
|
|
29
|
+
*/
|
|
30
|
+
export declare const isDecompressionBoundExceeded: (error: unknown) => boolean;
|
|
31
|
+
/**
|
|
32
|
+
* Read one ZIP entry's bytes without trusting the size it declares.
|
|
33
|
+
*
|
|
34
|
+
* `entry.getData()` cannot be used for this. It sizes its output buffer from
|
|
35
|
+
* the central-directory `size` field, which the archive author chooses, and
|
|
36
|
+
* adm-zip only arms its own guard when that field is positive:
|
|
37
|
+
*
|
|
38
|
+
* const option = version >= 15 && expectedLength > 0
|
|
39
|
+
* ? { maxOutputLength: expectedLength } : {};
|
|
40
|
+
*
|
|
41
|
+
* So an entry declaring 0 disables the bound and the caller's `size > maxSize`
|
|
42
|
+
* check in one move — `0 > 5MB` is false, and the inflate then runs uncapped.
|
|
43
|
+
* The declared size is the attack, so nothing here may depend on it: the cap
|
|
44
|
+
* comes from our own limit and is handed to the decoder.
|
|
45
|
+
*
|
|
46
|
+
* CRC is verified on both paths rather than dropped, so bypassing `getData()`
|
|
47
|
+
* does not also quietly lose its corruption check — a STORED entry is copied
|
|
48
|
+
* out rather than decoded, but it can be damaged just the same. It detects
|
|
49
|
+
* damage, not malice — the CRC field is attacker-controlled too.
|
|
50
|
+
*/
|
|
51
|
+
export declare function readZipEntryWithinLimit(entry: BoundedZipEntry, maxBytes: number, zlibModule: typeof import("zlib")): ArchiveEntryReadResult;
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bounded ZIP entry reading, shared by every processor that opens a ZIP.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from ArchiveProcessor because the Office formats are ZIPs too:
|
|
5
|
+
* .pptx and .odt read their entries directly and so can be handed the same
|
|
6
|
+
* bomb, and need the same refusal. One implementation rather than three means
|
|
7
|
+
* a correction to the guard lands everywhere at once.
|
|
8
|
+
*
|
|
9
|
+
* .docx and .xlsx deliberately do NOT use this. mammoth and exceljs unzip for
|
|
10
|
+
* themselves and were measured refusing a 400MB bomb at 46MB and 53MB peak,
|
|
11
|
+
* so wrapping them in a pre-scan bought no safety and roughly tripled the cost
|
|
12
|
+
* of every ordinary document.
|
|
13
|
+
*
|
|
14
|
+
* @module processors/archive/zipEntryReader
|
|
15
|
+
*/
|
|
16
|
+
/** ZIP compression methods this reader handles (APPNOTE 4.4.5). */
|
|
17
|
+
export const ZIP_METHOD_STORED = 0;
|
|
18
|
+
export const ZIP_METHOD_DEFLATED = 8;
|
|
19
|
+
/**
|
|
20
|
+
* Whether a zlib rejection is the output bound firing rather than bad input.
|
|
21
|
+
*
|
|
22
|
+
* `maxOutputLength` aborts an inflate the moment its output would pass the cap,
|
|
23
|
+
* which is the whole point — but it surfaces as a plain `RangeError`, and a
|
|
24
|
+
* bomb reported as "failed to decompress" reads as a corrupt upload and invites
|
|
25
|
+
* the user to send it again. It will fail identically every time.
|
|
26
|
+
*
|
|
27
|
+
* Keyed on `code`, not the message: the message embeds a byte count.
|
|
28
|
+
*/
|
|
29
|
+
export const isDecompressionBoundExceeded = (error) => error?.code === "ERR_BUFFER_TOO_LARGE";
|
|
30
|
+
/**
|
|
31
|
+
* Read one ZIP entry's bytes without trusting the size it declares.
|
|
32
|
+
*
|
|
33
|
+
* `entry.getData()` cannot be used for this. It sizes its output buffer from
|
|
34
|
+
* the central-directory `size` field, which the archive author chooses, and
|
|
35
|
+
* adm-zip only arms its own guard when that field is positive:
|
|
36
|
+
*
|
|
37
|
+
* const option = version >= 15 && expectedLength > 0
|
|
38
|
+
* ? { maxOutputLength: expectedLength } : {};
|
|
39
|
+
*
|
|
40
|
+
* So an entry declaring 0 disables the bound and the caller's `size > maxSize`
|
|
41
|
+
* check in one move — `0 > 5MB` is false, and the inflate then runs uncapped.
|
|
42
|
+
* The declared size is the attack, so nothing here may depend on it: the cap
|
|
43
|
+
* comes from our own limit and is handed to the decoder.
|
|
44
|
+
*
|
|
45
|
+
* CRC is verified on both paths rather than dropped, so bypassing `getData()`
|
|
46
|
+
* does not also quietly lose its corruption check — a STORED entry is copied
|
|
47
|
+
* out rather than decoded, but it can be damaged just the same. It detects
|
|
48
|
+
* damage, not malice — the CRC field is attacker-controlled too.
|
|
49
|
+
*/
|
|
50
|
+
export function readZipEntryWithinLimit(entry, maxBytes, zlibModule) {
|
|
51
|
+
const compressed = entry.getCompressedData();
|
|
52
|
+
const matchesCrc = (data) => (zlibModule.crc32(data) >>> 0) === (entry.header.crc >>> 0);
|
|
53
|
+
// STORED: the bytes are already the payload, so its own length is the bound.
|
|
54
|
+
if (entry.header.method === ZIP_METHOD_STORED) {
|
|
55
|
+
if (compressed.length > maxBytes) {
|
|
56
|
+
return { status: "too-large" };
|
|
57
|
+
}
|
|
58
|
+
return matchesCrc(compressed)
|
|
59
|
+
? { status: "ok", buffer: compressed }
|
|
60
|
+
: { status: "corrupt" };
|
|
61
|
+
}
|
|
62
|
+
if (entry.header.method !== ZIP_METHOD_DEFLATED) {
|
|
63
|
+
return { status: "unsupported-method" };
|
|
64
|
+
}
|
|
65
|
+
let inflated;
|
|
66
|
+
try {
|
|
67
|
+
inflated = zlibModule.inflateRawSync(compressed, {
|
|
68
|
+
maxOutputLength: maxBytes,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
if (isDecompressionBoundExceeded(error)) {
|
|
73
|
+
return { status: "too-large" };
|
|
74
|
+
}
|
|
75
|
+
return { status: "corrupt" };
|
|
76
|
+
}
|
|
77
|
+
return matchesCrc(inflated)
|
|
78
|
+
? { status: "ok", buffer: inflated }
|
|
79
|
+
: { status: "corrupt" };
|
|
80
|
+
}
|