@preventive/deptree 1.0.0-alpha.10 → 1.0.0-alpha.11

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/npm.d.ts CHANGED
@@ -271,8 +271,12 @@ export interface NpmTree {
271
271
  // entry under one directory, named as tar reads it, with no `\` and no
272
272
  // more than 1024 segments, none in the package's own node_modules, where
273
273
  // npm installs its dependencies, no setuid, setgid or sticky bit, no link
274
- // name on a file, no global pax header nor a pax size, which tar's releases
275
- // read otherwise; each with the sha512 integrity the lockfile records. On
274
+ // name on a file, no global pax header, and no pax size with a long name, a
275
+ // long link or a global header between it and its entry, which tar's
276
+ // releases read otherwise; each with the sha512 integrity the lockfile
277
+ // records. A pax size is taken over the header's, as every tar release
278
+ // takes it; npm's tar writes one into each pax header it packs, as for a
279
+ // name ustar cannot hold. On
276
280
  // macOS, two names in one directory that differ only in case or
277
281
  // normalization are refused, as they would be one name there.
278
282
  //
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/deptree",
3
- "version": "1.0.0-alpha.10",
3
+ "version": "1.0.0-alpha.11",
4
4
  "description": "A dependency tree in memory, as a package manager would install it",
5
5
  "type": "module",
6
6
  "exports": {
@@ -22,16 +22,54 @@ export function registryTarball({ name, version, resolution }, where) {
22
22
 
23
23
  const FILES = new Set(['file', 'contiguous-file'])
24
24
 
25
+ // The headers that stand ahead of an entry: pax, global, long name, long link.
26
+ const EXTENDED = new Set([0x78, 0x67, 0x4c, 0x4b])
27
+ const PAX = 0x78
28
+ const BLOCK = 512
29
+ const blocks = (size) => BLOCK * Math.ceil(size / BLOCK)
30
+
31
+ // An extended header's size as the archive reader read it, which held it to
32
+ // 1 MiB: base 256, or octal digits to a NUL, a blank field 0.
33
+ function extendedSize(tar, at) {
34
+ const field = tar.subarray(at + 124, at + 136)
35
+ if (field[0] === 0x80 || field[0] === 0xff) return field.subarray(1).reduce((size, byte) => size * 256 + byte, 0)
36
+ const digits = /^ *([0-7]*)/u.exec(String.fromCodePoint(...field).split('\0')[0])[1]
37
+ return digits === '' ? 0 : Number.parseInt(digits, 8)
38
+ }
39
+
40
+ // Every tar release takes a pax size over the header's. Up to 7.5.15, as
41
+ // npm 10.9.8 and 11.16 and before bundle it, tar reads it into each header
42
+ // after the pax one up to its entry too, so a long name, a long link or a
43
+ // global header between them is read by it, and all that follows otherwise
44
+ // than later releases read it. The archive reader has checked the layout
45
+ // whole, so each header is where the sizes it took say.
46
+ function checkPaxSizes(tar, entries, where) {
47
+ if (!entries.some(({ pax }) => pax.has('size'))) return
48
+ let at = 0
49
+ for (const { storedName, data, pax } of entries) {
50
+ let afterPax = false
51
+ let between = false
52
+ for (let flag = tar[at + 156]; EXTENDED.has(flag); flag = tar[at + 156]) {
53
+ between ||= afterPax
54
+ afterPax ||= flag === PAX
55
+ at += BLOCK + blocks(extendedSize(tar, at))
56
+ }
57
+ if (between && pax.has('size')) throw new DeptreeError(`${quote(storedName)} has a pax size ahead of another extended header, which tar's releases read otherwise`, where)
58
+ at += BLOCK + blocks(data.length)
59
+ }
60
+ if (tar.length < at + 2 * BLOCK || tar.subarray(at, at + 2 * BLOCK).some((byte) => byte !== 0)) throw new Error('unreachable: no end of the archive where the archive reader found one')
61
+ }
62
+
25
63
  function unpack(entries, where) {
26
64
  const files = new Map()
27
65
  const ignores = new Set()
28
66
  let top
29
- for (const { storedName, storedLinkname, type, mode, data, pax, globalPax } of entries) {
67
+ for (const { storedName, storedLinkname, type, mode, data, globalPax } of entries) {
30
68
  const segments = storedName.replace(/(?<=.)\/$/u, '').split('/')
31
69
  if (segments.length > 1024 || /[\\\0]/u.test(storedName) || segments.some((segment) => segment === '' || segment === '.' || segment === '..')) {
32
70
  throw new DeptreeError(`${quote(storedName)} is a name tar reads otherwise`, where)
33
71
  }
34
- if (globalPax.size > 0 || pax.has('size')) throw new DeptreeError(`${quote(storedName)} has a pax header tar's releases read otherwise`, where)
72
+ if (globalPax.size > 0) throw new DeptreeError(`${quote(storedName)} has a pax header tar's releases read otherwise`, where)
35
73
  if (top !== undefined && segments[0] !== top) throw new DeptreeError('the tarball has entries under more than one directory', where)
36
74
  top = segments[0]
37
75
  if (!FILES.has(type)) continue
@@ -51,7 +89,8 @@ function unpack(entries, where) {
51
89
  }
52
90
 
53
91
  export async function fetchNpmPackage({ name, version, integrity }, where, check, fetching) {
54
- const { bytes, entries, inflated, about } = await fetchTarball(name, version, integrity, where, fetching)
92
+ const { bytes, tar, entries, inflated, about } = await fetchTarball(name, version, integrity, where, fetching)
55
93
  await check?.(bytes, inflated, where)
94
+ checkPaxSizes(tar, entries, where)
56
95
  return { ...unpack(entries, where), about }
57
96
  }
package/src/tarball.js CHANGED
@@ -146,5 +146,5 @@ export async function fetchTarball(name, version, given, where, { metadata, cach
146
146
  if (!await matchesIntegrity(bytes, integrity)) throw new DeptreeError(`the tarball is not ${integrity}`, where)
147
147
  if (bytes[0] !== 0x1f || bytes[1] !== 0x8b || bytes[2] !== 0x08) throw new DeptreeError('the tarball is not gzipped', where)
148
148
  const tar = await gunzip(bytes)
149
- return { bytes, entries: unpack(tar), inflated: tar.length, integrity, about: said }
149
+ return { bytes, tar, entries: unpack(tar), inflated: tar.length, integrity, about: said }
150
150
  }