-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathzip.ts
More file actions
175 lines (148 loc) · 5.13 KB
/
zip.ts
File metadata and controls
175 lines (148 loc) · 5.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* @fileoverview Zip extraction with security limits and path
* traversal validation. Uses adm-zip under the hood; pre-validates
* every entry before extraction so the disk-write phase can't be
* tricked into escaping the target directory.
*/
import { safeMkdir } from '../fs/safe'
import { normalizePath } from '../paths/normalize'
import { ArrayFrom, ArrayPrototypeSlice } from '../primordials/array'
import { ErrorCtor } from '../primordials/error'
import { SetCtor } from '../primordials/map-set'
import { PromiseAll } from '../primordials/promise'
import {
assertArchiveExists,
DEFAULT_MAX_ENTRIES,
DEFAULT_MAX_FILE_SIZE,
DEFAULT_MAX_TOTAL_SIZE,
getAdmZip,
getPath,
validatePathWithinBase,
} from './_internal'
import type { ExtractOptions } from './types'
/**
* Extract a zip archive to a directory.
*
* @param archivePath - Path to zip file
* @param outputDir - Directory to extract to
* @param options - Extraction options
*
* @example
* ```typescript
* await extractZip('/tmp/archive.zip', '/tmp/output')
* await extractZip('/tmp/archive.zip', '/tmp/output', { strip: 1 })
* ```
*/
export async function extractZip(
archivePath: string,
outputDir: string,
options: ExtractOptions = {},
): Promise<void> {
// Normalize the "missing archive" surface — throws ENOENT before
// AdmZip can surface its generic "Invalid filename" message.
assertArchiveExists(archivePath)
const {
maxEntries = DEFAULT_MAX_ENTRIES,
maxFileSize = DEFAULT_MAX_FILE_SIZE,
maxTotalSize = DEFAULT_MAX_TOTAL_SIZE,
strip = 0,
} = options
// Normalize output directory path for cross-platform compatibility
const normalizedOutputDir = normalizePath(outputDir)
await safeMkdir(normalizedOutputDir)
const AdmZip = getAdmZip()
const zip = new AdmZip(archivePath)
const path = getPath()
// Pre-validate all entries for security
const entries = zip.getEntries()
// entries.length>maxEntries fires only on archives crafted for the
// DoS test; null-byte detection fires only on adversarial entries.
// isDirectory branch fires on archives with directory entries.
/* c8 ignore start */
if (entries.length > maxEntries) {
throw new ErrorCtor(
`Archive has too many entries: ${entries.length} (limit: ${maxEntries})`,
)
}
/* c8 ignore stop */
let totalExtractedSize = 0
for (const entry of entries) {
if (entry.isDirectory) {
continue
}
/* c8 ignore start */
if (entry.entryName.includes('\0')) {
throw new ErrorCtor(
`Invalid null byte in archive entry name: ${entry.entryName}`,
)
}
/* c8 ignore stop */
// Check individual file size
const uncompressedSize = entry.header.size
if (uncompressedSize > maxFileSize) {
throw new ErrorCtor(
`File size exceeds limit: ${entry.entryName} (${uncompressedSize} bytes > ${maxFileSize} bytes)`,
)
}
// Check total extracted size
totalExtractedSize += uncompressedSize
if (totalExtractedSize > maxTotalSize) {
throw new ErrorCtor(
`Total extracted size exceeds limit: ${totalExtractedSize} bytes > ${maxTotalSize} bytes`,
)
}
// ZIP entries always use forward slashes per ZIP specification
const parts = entry.entryName.split('/')
if (parts.length <= strip) {
continue
}
const strippedPath = ArrayPrototypeSlice(parts, strip).join('/')
const targetPath = path.join(normalizedOutputDir, strippedPath)
// Validate path is within target directory (prevents path traversal)
validatePathWithinBase(targetPath, normalizedOutputDir, entry.entryName)
}
// strip===0 vs strip>0 cases tested separately; isDirectory arms
// only fire on archives with directory entries (most don't).
/* c8 ignore start */
if (strip === 0) {
for (const entry of entries) {
if (!entry.isDirectory) {
const targetPath = path.join(normalizedOutputDir, entry.entryName)
validatePathWithinBase(targetPath, normalizedOutputDir, entry.entryName)
}
}
zip.extractAllTo(normalizedOutputDir, true)
} else {
const path = getPath()
const entries = zip.getEntries()
const dirsToCreate = new SetCtor<string>()
for (const entry of entries) {
if (entry.isDirectory) {
continue
}
// ZIP entries always use forward slashes per ZIP specification
const parts = entry.entryName.split('/')
if (parts.length <= strip) {
continue
}
const strippedPath = ArrayPrototypeSlice(parts, strip).join('/')
const targetPath = path.join(normalizedOutputDir, strippedPath)
dirsToCreate.add(path.dirname(targetPath))
}
// Create all directories
await PromiseAll(ArrayFrom(dirsToCreate).map(dir => safeMkdir(dir)))
for (const entry of entries) {
if (entry.isDirectory) {
continue
}
const parts = entry.entryName.split('/')
if (parts.length <= strip) {
continue
}
const strippedPath = ArrayPrototypeSlice(parts, strip).join('/')
const targetPath = path.join(normalizedOutputDir, strippedPath)
zip.extractEntryTo(entry, path.dirname(targetPath), false, true)
}
}
/* c8 ignore stop */
}