@preventive/archive 1.4.1 → 1.5.0
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/package.json +1 -1
- package/src/names.js +87 -1
- package/src/zip/unzip.js +6 -2
- package/zip.d.ts +9 -2
package/package.json
CHANGED
package/src/names.js
CHANGED
|
@@ -82,17 +82,28 @@ function child(parent, segment, make = true) {
|
|
|
82
82
|
|
|
83
83
|
const FIELDS = ['type', 'mode', 'uid', 'gid', 'mtime', 'uname', 'gname', 'linkname', 'devmajor', 'devminor']
|
|
84
84
|
|
|
85
|
+
// Linux's ELOOP bound
|
|
86
|
+
const MAX_HOPS = 40
|
|
87
|
+
const RESOLVING = Symbol('resolving')
|
|
88
|
+
const stepsOf = (target) => target.split('/').filter((segment) => segment !== '' && segment !== '.')
|
|
89
|
+
|
|
85
90
|
// Each path seen is an 'entry', a 'directory', or a directory 'implied' by a path or symlink target through it.
|
|
86
91
|
// Nothing may sit or be walked through inside a non-directory, so no chain of links leads out of the archive.
|
|
87
92
|
// A name repeats only as the same entry; `keep` keeps data to compare a repeat with, else it is refused.
|
|
93
|
+
// With `chains`, a symlink target may pass through symlinks, as a macOS framework's do, and implies nothing:
|
|
94
|
+
// settle() follows each once every entry is in. A name still never may.
|
|
88
95
|
export class Names {
|
|
89
96
|
#root = node()
|
|
90
97
|
#keep
|
|
91
98
|
#above
|
|
99
|
+
#links
|
|
100
|
+
#resolved = new Map()
|
|
101
|
+
#resolving = 0
|
|
92
102
|
|
|
93
|
-
constructor(keep = false, symlinksAbove = 0) {
|
|
103
|
+
constructor(keep = false, symlinksAbove = 0, chains = false) {
|
|
94
104
|
this.#keep = keep
|
|
95
105
|
this.#above = levelsAbove(symlinksAbove)
|
|
106
|
+
this.#links = chains ? [] : null
|
|
96
107
|
}
|
|
97
108
|
|
|
98
109
|
get above() {
|
|
@@ -135,6 +146,10 @@ export class Names {
|
|
|
135
146
|
// Past the root nothing it passes is the archive's.
|
|
136
147
|
#symlink(at, target, what) {
|
|
137
148
|
const steps = checkSymlinkTarget(at, target, this.#above)
|
|
149
|
+
if (this.#links !== null) {
|
|
150
|
+
this.#links.push({ at, what })
|
|
151
|
+
return
|
|
152
|
+
}
|
|
138
153
|
const path = at.split('/').slice(0, -1)
|
|
139
154
|
const nodes = this.#walk(path)
|
|
140
155
|
for (const [i, step] of steps.entries()) {
|
|
@@ -150,6 +165,77 @@ export class Names {
|
|
|
150
165
|
}
|
|
151
166
|
}
|
|
152
167
|
|
|
168
|
+
settle() {
|
|
169
|
+
for (const { at, what } of this.#links ?? []) {
|
|
170
|
+
const names = at.split('/')
|
|
171
|
+
const nodes = this.#walk(names)
|
|
172
|
+
this.#resolve(nodes.pop(), { nodes, names: names.slice(0, -1) }, what)
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
// Memoized by link: no name passes through one, so each sits at one place
|
|
177
|
+
#resolve(link, dir, what) {
|
|
178
|
+
const known = this.#resolved.get(link)
|
|
179
|
+
if (known === RESOLVING) throw new ArchiveError(`${what} leads through a loop of symlinks`)
|
|
180
|
+
if (known !== undefined) return known
|
|
181
|
+
// Each nested resolve is a link the outermost passes: refused before the stack is spent
|
|
182
|
+
if (this.#resolving > MAX_HOPS) throw new ArchiveError(`${what} leads through more than ${MAX_HOPS} symlinks`)
|
|
183
|
+
this.#resolved.set(link, RESOLVING)
|
|
184
|
+
this.#resolving++
|
|
185
|
+
try {
|
|
186
|
+
const end = this.#follow(dir, stepsOf(link.symlink), what)
|
|
187
|
+
this.#resolved.set(link, end)
|
|
188
|
+
return end
|
|
189
|
+
} finally {
|
|
190
|
+
this.#resolving--
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// Levels counted as checkSymlinkTarget counts them; past the root nothing is the archive's
|
|
195
|
+
#follow(from, steps, what) {
|
|
196
|
+
let nodes = [...from.nodes]
|
|
197
|
+
let names = [...from.names]
|
|
198
|
+
let depth = names.length
|
|
199
|
+
let outside = false
|
|
200
|
+
let hops = 0
|
|
201
|
+
for (const [i, step] of steps.entries()) {
|
|
202
|
+
if (step === '..') {
|
|
203
|
+
depth -= 1
|
|
204
|
+
if (depth < -this.#above) {
|
|
205
|
+
const how = this.#above === 0 ? 'outside the archive' : `more than ${this.#above} level${this.#above === 1 ? '' : 's'} above the archive`
|
|
206
|
+
throw new ArchiveError(`${what} leads ${how} through the symlinks it passes`)
|
|
207
|
+
}
|
|
208
|
+
if (outside || depth < 0) outside = true
|
|
209
|
+
else {
|
|
210
|
+
nodes.pop()
|
|
211
|
+
names.pop()
|
|
212
|
+
}
|
|
213
|
+
continue
|
|
214
|
+
}
|
|
215
|
+
depth += 1
|
|
216
|
+
if (outside) continue
|
|
217
|
+
const next = child(nodes.at(-1), step, false)
|
|
218
|
+
if (next?.symlink !== undefined) {
|
|
219
|
+
const end = this.#resolve(next, { nodes, names }, what)
|
|
220
|
+
hops += end.hops + 1
|
|
221
|
+
if (hops > MAX_HOPS) throw new ArchiveError(`${what} leads through more than ${MAX_HOPS} symlinks`)
|
|
222
|
+
// A link may end at a file, but no step, `..` included, goes on from one
|
|
223
|
+
if (!end.outside && end.nodes.at(-1).kind === 'entry' && i < steps.length - 1) {
|
|
224
|
+
throw new ArchiveError(`the target of ${what} passes through ${quote([...names, step].join('/'))}, which leads to ${quote(end.names.join('/'))}, not a directory`)
|
|
225
|
+
}
|
|
226
|
+
depth = end.depth
|
|
227
|
+
outside = end.outside
|
|
228
|
+
nodes = [...end.nodes]
|
|
229
|
+
names = [...end.names]
|
|
230
|
+
continue
|
|
231
|
+
}
|
|
232
|
+
if (next?.kind === 'entry' && i < steps.length - 1) throw new ArchiveError(`the target of ${what} passes through ${quote([...names, step].join('/'))}, which is not a directory`)
|
|
233
|
+
nodes.push(next ?? node())
|
|
234
|
+
names.push(step)
|
|
235
|
+
}
|
|
236
|
+
return { nodes, names, depth, outside, hops }
|
|
237
|
+
}
|
|
238
|
+
|
|
153
239
|
// `refusal` spells the path out only when it has to
|
|
154
240
|
#directory(at, refusal) {
|
|
155
241
|
if (at.kind === 'entry') throw new ArchiveError(refusal())
|
package/src/zip/unzip.js
CHANGED
|
@@ -200,9 +200,12 @@ async function entryOf(source, entry, names) {
|
|
|
200
200
|
}
|
|
201
201
|
|
|
202
202
|
// The layout is checked whole before the first entry is read
|
|
203
|
-
async function* entries(source, { limit = Infinity, symlinksAbove } = {}, keep = false) {
|
|
203
|
+
async function* entries(source, { limit = Infinity, symlinksAbove, symlinkChains = false } = {}, keep = false) {
|
|
204
204
|
if (limit !== Infinity && !(Number.isSafeInteger(limit) && limit >= 0)) throw new ArchiveError(`limit ${String(limit)} is not a whole number of bytes`)
|
|
205
|
-
|
|
205
|
+
if (typeof symlinkChains !== 'boolean') throw new ArchiveError(`symlinkChains ${String(symlinkChains)} is not a boolean`)
|
|
206
|
+
// Settled once every entry is in, before unzip() answers any; a stream has answered each by then
|
|
207
|
+
if (symlinkChains && !keep) throw new ArchiveError('symlinkChains is taken by unzip() alone, which answers no entry before every one is read')
|
|
208
|
+
const names = new Names(keep, symlinksAbove, symlinkChains)
|
|
206
209
|
const { end, r } = await findEnd(source)
|
|
207
210
|
const count = r.u16(end + 8)
|
|
208
211
|
if (r.u16(end + 4) !== 0 || r.u16(end + 6) !== 0 || count !== r.u16(end + 10)) throw new ArchiveError('the archive spans several disks', end)
|
|
@@ -239,6 +242,7 @@ async function* entries(source, { limit = Infinity, symlinksAbove } = {}, keep =
|
|
|
239
242
|
}
|
|
240
243
|
if (expected !== start) throw new ArchiveError('bytes belong to no entry', expected)
|
|
241
244
|
for (const entry of list) yield await entryOf(source, entry, names)
|
|
245
|
+
names.settle()
|
|
242
246
|
}
|
|
243
247
|
|
|
244
248
|
export async function unzip(bytes, options) {
|
package/zip.d.ts
CHANGED
|
@@ -5,8 +5,8 @@
|
|
|
5
5
|
* import { unzip, unzipStream, zip } from '@preventive/archive/zip.js'
|
|
6
6
|
* ```
|
|
7
7
|
*
|
|
8
|
-
* Names and symlinks are held to the rules of `@preventive/archive/tar.js
|
|
9
|
-
* takes zip64, so an archive holds at most 65534 entries and stays under 4 GiB.
|
|
8
|
+
* Names and symlinks are held to the rules of `@preventive/archive/tar.js`, but for `UnzipOptions.symlinkChains`.
|
|
9
|
+
* Neither writing nor reading takes zip64, so an archive holds at most 65534 entries and stays under 4 GiB.
|
|
10
10
|
*
|
|
11
11
|
* @module @preventive/archive/zip.js
|
|
12
12
|
*/
|
|
@@ -88,6 +88,13 @@ export interface UnzipOptions extends SymlinkOptions {
|
|
|
88
88
|
* past it, rejects with `limited` set.
|
|
89
89
|
*/
|
|
90
90
|
limit?: number;
|
|
91
|
+
/**
|
|
92
|
+
* Whether a symlink's target may pass through other symlinks, as a macOS framework's do (default: false)
|
|
93
|
+
*
|
|
94
|
+
* Each is then followed once every entry is read, and refused where it climbs past `symlinksAbove`, passes
|
|
95
|
+
* through a non-directory, loops or passes more than 40 symlinks. `unzipStream()` refuses it.
|
|
96
|
+
*/
|
|
97
|
+
symlinkChains?: boolean;
|
|
91
98
|
}
|
|
92
99
|
|
|
93
100
|
/**
|