@preventive/triage 1.0.0-alpha.44 → 1.0.0-alpha.46
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/client/linked-findings.js +3 -0
- package/common/bundle-metadata.d.ts +1 -0
- package/common/bundle-metadata.js +1 -1
- package/common/bundle-sources.js +25 -2
- package/common/default-scan-models.ts +1 -1
- package/common/dependency-reviews.d.ts +0 -5
- package/common/dependency-reviews.js +0 -17
- package/common/line-diff.js +5 -4
- package/common/managed/agent-tasks.ts +292 -0
- package/common/managed/github-agents.ts +92 -0
- package/common/managed/github-logins.ts +49 -0
- package/common/managed/roles.ts +7 -0
- package/common/managed/routes.js +88 -7
- package/common/managed/team-app.d.ts +5 -2
- package/common/managed/team-app.js +2 -2
- package/common/server-info.ts +3 -0
- package/common/workspace-app.js +6 -3
- package/out/brotli-fallback.js +3 -3
- package/out/chunk-2XUEZHIP.js +18 -18
- package/out/chunk-2ZDZWLFY.js +1 -0
- package/out/chunk-3PS4KWD2.js +10 -0
- package/out/chunk-4HI2MSAV.js +17 -0
- package/out/chunk-4UFAMJAX.js +1 -0
- package/out/chunk-7C7DWL7I.js +1 -0
- package/out/chunk-7XRR4OFU.js +1 -0
- package/out/chunk-BC2UCLFJ.js +1 -0
- package/out/chunk-BOIXMUU5.js +3 -0
- package/out/chunk-C4Z6ZBXV.js +6 -6
- package/out/chunk-CC4OM6TZ.js +3 -0
- package/out/chunk-G24A42V6.js +2 -0
- package/out/chunk-GDPCLSOS.js +2 -0
- package/out/chunk-GO7FT4OD.js +1 -0
- package/out/chunk-I2MDGSQX.js +3 -0
- package/out/chunk-IYPY47MC.js +1 -0
- package/out/chunk-IZ7POWI3.js +18 -0
- package/out/chunk-JCIIBLKR.js +1 -1
- package/out/chunk-JKGQ3O7O.js +6 -0
- package/out/chunk-LE5ZWLG4.js +235 -0
- package/out/chunk-MSION3G4.js +36 -0
- package/out/chunk-NRPC74AM.js +1 -1
- package/out/chunk-OKRLLE5I.js +17 -0
- package/out/chunk-OOJM4CTU.js +1 -1
- package/out/chunk-PNHBZGDD.js +1 -1
- package/out/chunk-PXK7EU4V.js +1 -0
- package/out/chunk-QB4CBOX2.js +1 -0
- package/out/chunk-R3NZFXVR.js +1 -0
- package/out/chunk-R5O6FAW4.js +1 -1
- package/out/chunk-S6GIXWQW.js +52 -0
- package/out/chunk-SXV2T5C4.js +80 -0
- package/out/chunk-TUII34K2.js +1 -1
- package/out/chunk-V3KWNNMY.js +1 -0
- package/out/chunk-VFWMQ3LG.js +2 -0
- package/out/chunk-VH4XMZIW.js +2 -0
- package/out/chunk-WFYLMF6Z.js +10 -0
- package/out/chunk-WQ74ZBZF.js +7 -0
- package/out/client-managed-import.js +3 -3
- package/out/client-managed.js +31 -35
- package/out/client-sync.js +11 -11
- package/out/compare.js +35 -0
- package/out/dialogs.js +51 -0
- package/out/github-issue-authorized.html +1 -1
- package/out/github-issue-authorized.js +1 -1
- package/out/graph.js +17 -17
- package/out/index.html +30 -34
- package/out/prism.js +14 -14
- package/out/scan.js +2 -0
- package/out/terminal.js +294 -293
- package/out/view.css +1 -1
- package/out/view.js +16 -107
- package/package.json +53 -6
- package/risk/actions/findings.js +161 -0
- package/risk/actions/resolve.js +219 -0
- package/risk/actions/scripts/commands.js +182 -0
- package/risk/actions/scripts/grade.js +113 -0
- package/risk/actions/scripts/packages.js +123 -0
- package/risk/actions/scripts/shell.js +169 -0
- package/risk/actions/uses.js +76 -0
- package/risk/actions/workflow.js +110 -0
- package/risk/files/hosts.js +6 -6
- package/risk/files/readability.js +23 -17
- package/risk/files/runs-code.js +18 -6
- package/risk/files/script.js +7 -3
- package/risk/files/stamps.js +8 -7
- package/risk/index.d.ts +71 -1
- package/risk/index.js +6 -2
- package/risk/npm/assess.js +1 -1
- package/risk/npm/compare.js +34 -10
- package/risk/npm/files.js +413 -2
- package/risk/npm/tarball.js +56 -73
- package/server-e2e/app.ts +5 -4
- package/server-e2e/http.ts +5 -2
- package/server-managed/agent-merges.ts +198 -0
- package/server-managed/app-agents.ts +30 -0
- package/server-managed/avatar-store.ts +1 -0
- package/server-managed/bundle-cache.ts +7 -1
- package/server-managed/combined.ts +3 -2
- package/server-managed/commit-coauthors.ts +83 -0
- package/server-managed/commit-lockfiles.ts +72 -0
- package/server-managed/config.ts +6 -0
- package/server-managed/db-methods.ts +66 -19
- package/server-managed/db-neon.ts +89 -21
- package/server-managed/db-schema.ts +13 -2
- package/server-managed/db.ts +9 -1
- package/server-managed/dependabot-access.ts +60 -0
- package/server-managed/dependabot-alerts.ts +185 -0
- package/server-managed/dependency-review-comments.ts +97 -0
- package/server-managed/dependency-reviews.ts +78 -18
- package/server-managed/github-accounts.ts +33 -0
- package/server-managed/github-actions.ts +71 -0
- package/server-managed/github-app.ts +33 -13
- package/server-managed/github-errors.ts +2 -0
- package/server-managed/github-issue-links.ts +11 -5
- package/server-managed/github-link-oauth.ts +69 -0
- package/server-managed/github-links.ts +204 -0
- package/server-managed/github-oauth.ts +5 -2
- package/server-managed/github-pulls.ts +2 -2
- package/server-managed/github-requests.ts +39 -0
- package/server-managed/github-users.ts +30 -0
- package/server-managed/http-response.ts +10 -2
- package/server-managed/http.ts +1251 -97
- package/server-managed/index.ts +13 -8
- package/server-managed/kept-briefly.ts +37 -0
- package/server-managed/locked-packages.ts +37 -16
- package/server-managed/lockfile-scope.ts +49 -0
- package/server-managed/npm-delta-worker.js +23 -0
- package/server-managed/npm-delta.ts +178 -0
- package/server-managed/npm-insights.ts +20 -11
- package/server-managed/npm-kept.ts +83 -6
- package/server-managed/npm-loads.ts +114 -37
- package/server-managed/npm-packages.ts +110 -64
- package/server-managed/npm-risk.ts +4 -1
- package/server-managed/pretty-print.ts +67 -23
- package/server-managed/pull-comment-reviews.ts +161 -0
- package/server-managed/pull-dependencies.ts +300 -0
- package/server-managed/pull-dependency-reviews.ts +318 -0
- package/server-managed/pull-reviews.ts +205 -0
- package/server-managed/queue.ts +16 -0
- package/server-managed/release-commits.ts +84 -0
- package/server-managed/rename-diff.ts +22 -13
- package/server-managed/report-sources.ts +1 -1
- package/server-managed/report-store.ts +42 -9
- package/server-managed/repository-actions.ts +218 -0
- package/server-managed/repository-agents.ts +220 -0
- package/server-managed/repository-browser.ts +166 -37
- package/server-managed/repository-changes.ts +284 -0
- package/server-managed/repository-code.ts +52 -0
- package/server-managed/repository-dependency-reviews.ts +157 -0
- package/server-managed/repository-history.ts +461 -0
- package/server-managed/repository-issues.ts +181 -0
- package/server-managed/repository-pulse.ts +439 -0
- package/server-managed/repository-risk-audit.ts +13 -0
- package/server-managed/repository-risk-read.ts +170 -0
- package/server-managed/repository-risk.ts +133 -118
- package/server-managed/reviewed-dependencies.ts +149 -0
- package/server-managed/session.ts +27 -9
- package/server-managed/sql.ts +6 -3
- package/server-managed/storage-stores.ts +5 -0
- package/server-managed/storage.ts +4 -2
- package/server-managed/team-app.ts +19 -3
- package/server-managed/team-reports.ts +24 -12
- package/server-managed/upstream-cache.ts +12 -0
- package/server-managed/workspace-shares.ts +4 -1
- package/server.ts +25 -5
- package/out/chunk-2ABLKCOY.js +0 -46
- package/out/chunk-2KBF5TZ7.js +0 -2
- package/out/chunk-35VE6CDH.js +0 -2
- package/out/chunk-3BZ7JERB.js +0 -1
- package/out/chunk-4XCDFGDP.js +0 -10
- package/out/chunk-5LSXOLUS.js +0 -1
- package/out/chunk-5RCM2J63.js +0 -1
- package/out/chunk-7B6HB4D7.js +0 -1
- package/out/chunk-AJ4GTATZ.js +0 -3
- package/out/chunk-CH4UN6WK.js +0 -225
- package/out/chunk-CMQA4DM6.js +0 -1
- package/out/chunk-CVVJAXVY.js +0 -2
- package/out/chunk-DIJJNTRH.js +0 -10
- package/out/chunk-EDKLBQQN.js +0 -17
- package/out/chunk-F47DXCEV.js +0 -33
- package/out/chunk-FK3DZEKL.js +0 -1
- package/out/chunk-G6AJWENO.js +0 -1
- package/out/chunk-GJ4IUCNS.js +0 -1
- package/out/chunk-GNXOKIGP.js +0 -80
- package/out/chunk-GZ4MS3TE.js +0 -13
- package/out/chunk-GZWM7UQM.js +0 -2
- package/out/chunk-IU3CCINR.js +0 -3
- package/out/chunk-K3X5EJBQ.js +0 -2
- package/out/chunk-KEX2ATEW.js +0 -2
- package/out/chunk-KYCNJVB7.js +0 -1
- package/out/chunk-LPCTJYNA.js +0 -7
- package/out/chunk-O4RJY5TP.js +0 -2
- package/out/chunk-OD4LPN3H.js +0 -1
- package/out/chunk-QWDWTBM7.js +0 -1
- package/out/chunk-QYWKC557.js +0 -1
- package/out/chunk-R75XOKUP.js +0 -1
- package/out/chunk-SWTLADW5.js +0 -1
- package/out/chunk-T7OKH4VW.js +0 -1
- package/out/chunk-TZ5KB2Y5.js +0 -10
- package/out/chunk-UNTRGE26.js +0 -2
- package/out/chunk-UTR2LVME.js +0 -2
- package/out/chunk-UYAXVRVL.js +0 -1
- package/out/chunk-V3ME6FI5.js +0 -2
- package/out/chunk-V5O7M2YT.js +0 -1
- package/out/chunk-VIHGO77Y.js +0 -1
- package/out/chunk-W4J3MGLB.js +0 -1
- package/out/chunk-WGG7KQUH.js +0 -1
- package/out/chunk-XYFXXB2G.js +0 -10
- package/out/chunk-ZESLQCTF.js +0 -6
package/risk/npm/files.js
CHANGED
|
@@ -2,21 +2,432 @@
|
|
|
2
2
|
// npmFileRisk), and every host its scripts name: for compare.js's findings
|
|
3
3
|
// and held.js's, and for the viewer, which reads its files' tags from it.
|
|
4
4
|
import { bundleFileSizes, bundleFilesAsMap } from '../../common/bundle-sources.js'
|
|
5
|
+
import { NPM_PROSE } from '../files/readability.js'
|
|
6
|
+
import { npmIsScript } from '../files/script.js'
|
|
5
7
|
import { npmFileRisk } from '../files/stamps.js'
|
|
6
8
|
|
|
7
9
|
// What a version's files are kept by: its bundle where it is one, else its
|
|
8
10
|
// files' list, else itself.
|
|
9
11
|
const keyOf = version => version.bundle ?? version.json ?? version
|
|
10
12
|
|
|
13
|
+
// A string's escapes as JavaScript reads them (`\x2e`, `\u002e`, `\u{2e}`, a
|
|
14
|
+
// control's letter, and an octal's, `\056`, as code outside strict mode
|
|
15
|
+
// reads it), a line continuation for nothing, any other for itself: `\X2E`
|
|
16
|
+
// and `\U002E` too, their letters in lowercase alone introducing a code.
|
|
17
|
+
const ESCAPE = /\\(?:x([\dA-Fa-f]{2})|u([\dA-Fa-f]{4})|u\{([\dA-Fa-f]{1,6})\}|([0-3][0-7]{0,2}|[4-7][0-7]?)|(\r\n|[\s\S]))/gu
|
|
18
|
+
const CONTROLS = { b: '\b', f: '\f', n: '\n', r: '\r', t: '\t', v: '\v' }
|
|
19
|
+
const unescaped = body => body.replaceAll(ESCAPE, (escape, x, u, braced, octal, other) => {
|
|
20
|
+
if (octal !== undefined) return String.fromCodePoint(Number.parseInt(octal, 8))
|
|
21
|
+
if (other !== undefined) return /^(?:\r\n|[\n\r\u2028\u2029])$/u.test(other) ? '' : CONTROLS[other] ?? other
|
|
22
|
+
const code = Number.parseInt(x ?? u ?? braced, 16)
|
|
23
|
+
return code <= 0x10FFFF ? String.fromCodePoint(code) : escape
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
// The characters a filesystem telling neither case apart (folded) takes for
|
|
27
|
+
// one of a package's name, which npm keeps to ASCII: its cases, and those
|
|
28
|
+
// Unicode cases into it (`ı`, `ſ`, the Kelvin sign).
|
|
29
|
+
const FOLDING_INTO = { i: '\u0131', k: '\u212A', s: '\u017F' }
|
|
30
|
+
const foldsInto = char => new Set([char, char.toLowerCase(), char.toUpperCase(), ...FOLDING_INTO[char.toLowerCase()] ?? ''])
|
|
31
|
+
|
|
32
|
+
// Each string a text writes out that may name a file of its package, its
|
|
33
|
+
// value starting `.`, with an escape, or with one of `firsts` (its name's
|
|
34
|
+
// first character, foldsInto, and `#` where its package.json files map
|
|
35
|
+
// specifiers of its own): from any quote no backslash escapes (an even run of them
|
|
36
|
+
// before it, or none) to the next of its kind none escapes, a template's
|
|
37
|
+
// across lines, the others' within their line, a line continuation aside.
|
|
38
|
+
// Tried from every such quote, not only where code's tokens say a string
|
|
39
|
+
// starts, which code can be written to have read wrong (a regular
|
|
40
|
+
// expression, a comment or a template's text taken for another): so one in
|
|
41
|
+
// a comment, or in another string, is read too. Linear on any text, each
|
|
42
|
+
// quote's string read to the next quote of its kind.
|
|
43
|
+
function quoted(firsts) {
|
|
44
|
+
const starts = String.raw`(?=[.\\${[...firsts].map(char => String.raw`\u{${char.codePointAt(0).toString(16)}}`).join('')}])`
|
|
45
|
+
const line = quote => String.raw`${quote}(?=${starts}((?:[^${quote}\\\n]|\\(?:\r\n|[\s\S]))*)${quote})`
|
|
46
|
+
return new RegExp(String.raw`(?<=(?:^|[^\\])(?:\\\\)*)(?:${line('"')}|${line("'")}|\x60(?=${starts}((?:[^\x60\\]|\\[\s\S])*)\x60))`, 'gu')
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
// A string's values, from its quote and what it holds: as JavaScript reads
|
|
50
|
+
// its escapes (unescaped), and a template's as written too, as a tag reads it
|
|
51
|
+
// (`String.raw`); none for a template with something put in it, a `${` no
|
|
52
|
+
// backslash escapes (`\${` its text).
|
|
53
|
+
function valuesOf(quote, body) {
|
|
54
|
+
if (!body.includes('\\')) return quote === '`' && body.includes('${') ? [] : [body]
|
|
55
|
+
if (quote !== '`') return [unescaped(body)]
|
|
56
|
+
return body.replaceAll(/\\[\s\S]/gu, '').includes('${') ? [] : [unescaped(body), body]
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
// Where a string is what `import` takes, rather than what `require` may: a
|
|
60
|
+
// declaration's (`import './x'`, `from './x'`), on the keyword's line with
|
|
61
|
+
// blanks alone between, as a line break could begin another statement
|
|
62
|
+
// there (`from\n'./x'.split(',').forEach(require)`), and not a template,
|
|
63
|
+
// which `from` would be called with as a tag; or the whole first argument of
|
|
64
|
+
// `import(…)`, on its line too.
|
|
65
|
+
const DECLARED = /(?<=(?<![\p{ID_Continue}$.#])(?:import|from)[ \t]*)/uy
|
|
66
|
+
const CALLED = /(?<=(?<![\p{ID_Continue}$.#])import[ \t]*\([ \t]*)/uy
|
|
67
|
+
const ARGUMENT_END = /[ \t]*[,)]/uy
|
|
68
|
+
const matchesAt = (regex, text, index) => {
|
|
69
|
+
regex.lastIndex = index
|
|
70
|
+
return regex.test(text)
|
|
71
|
+
}
|
|
72
|
+
const imported = (text, quote, start, end) => (quote !== '`' && matchesAt(DECLARED, text, start)) || (matchesAt(CALLED, text, start) && matchesAt(ARGUMENT_END, text, end))
|
|
73
|
+
|
|
74
|
+
// A relative path: from `./` or `../`, or `.` or `..` alone.
|
|
75
|
+
const RELATIVE = /^\.\.?(?:\/|$)/u
|
|
76
|
+
// What `require` doesn't run as JavaScript: JSON, and a native addon, by
|
|
77
|
+
// their names as it knows them, in lowercase: `.JSON` it runs.
|
|
78
|
+
const NOT_RUN = /\.(?:json|node)$/u
|
|
79
|
+
// The extensions `require` tries a path with that names no file.
|
|
80
|
+
const TRIED = ['.js', '.json', '.node']
|
|
81
|
+
// A path `require` takes as a directory's alone, never a file's: one ending
|
|
82
|
+
// in `/`, or in `.` or `..` after one, or either alone, as node tells it, by
|
|
83
|
+
// `/` alone, on Windows too (`./payload/`, not `.\payload\`).
|
|
84
|
+
const DIRECTORY = /(?:^|\/)\.{0,2}$/u
|
|
85
|
+
// A name with an extension, which `import` runs only where it knows it: not
|
|
86
|
+
// `.npmignore`, nor `payload`.
|
|
87
|
+
const EXTENSION = /[^/.][^/]*\.[^/]*$/u
|
|
88
|
+
|
|
89
|
+
// A path's parts, `spec`'s from `parts`, each `..` a directory up; null for
|
|
90
|
+
// one leading past the top.
|
|
91
|
+
export function resolved(parts, spec) {
|
|
92
|
+
const all = [...parts]
|
|
93
|
+
for (const part of spec.split('/')) {
|
|
94
|
+
if (part === '..' && all.pop() === undefined) return null
|
|
95
|
+
if (part !== '..' && part !== '.' && part !== '') all.push(part)
|
|
96
|
+
}
|
|
97
|
+
return all
|
|
98
|
+
}
|
|
99
|
+
// Those parts as a path in the package where it is installed (`root`, its
|
|
100
|
+
// directory's parts, `node_modules/<name>`), told as a filesystem telling
|
|
101
|
+
// neither case nor Unicode's normalization apart tells them (folded,
|
|
102
|
+
// `../LOADS/x` for `loads`): null for one outside it.
|
|
103
|
+
const within = (root, parts) => parts !== null && root.every((part, index) => index < parts.length && (parts[index] === part || folded(parts[index]) === part)) ? parts.slice(root.length).join('/') : null
|
|
104
|
+
// `spec` from the directory `dir` ('' for the package's top) as a path in the
|
|
105
|
+
// package, one leading out of it and back in by its name too
|
|
106
|
+
// (`../<name>/x`); null for one leading elsewhere.
|
|
107
|
+
const joined = (root, dir, spec) => within(root, resolved([...root, ...dir === '' ? [] : dir.split('/')], spec))
|
|
108
|
+
// A path as `require` takes it: its `\` a separator on Windows, a name's
|
|
109
|
+
// character elsewhere, so read both ways.
|
|
110
|
+
const separated = spec => new Set([spec, spec.replaceAll('\\', '/')])
|
|
111
|
+
// Each relative one of those (RELATIVE), from `dir`, as a path in the package
|
|
112
|
+
// (joined).
|
|
113
|
+
const requiredPaths = (root, dir, spec) => [...separated(spec)].filter(path => RELATIVE.test(path)).map(path => joined(root, dir, path))
|
|
114
|
+
// What a URL reads otherwise than a path: an escape, a query, a fragment, a
|
|
115
|
+
// `\`, a tab or a line break, or a blank or a control at its end, which it
|
|
116
|
+
// drops.
|
|
117
|
+
const URL_READ = /[%?#\\\t\n\r]|[\0- ]$/u
|
|
118
|
+
// A relative path as `import` takes it, and `exports` and `imports` targets,
|
|
119
|
+
// a URL's: its query and fragment dropped, tabs and line breaks too, `\` a
|
|
120
|
+
// separator, its escapes read (`%2E`); null for one escaping a separator
|
|
121
|
+
// (`%2F`), as node refuses, or leading out of the package (joined).
|
|
122
|
+
function urlJoined(root, dir, spec) {
|
|
123
|
+
if (!RELATIVE.test(spec)) return null
|
|
124
|
+
if (!URL_READ.test(spec)) return joined(root, dir, spec)
|
|
125
|
+
try {
|
|
126
|
+
const base = [...root, ...dir === '' ? [] : dir.split('/')].map(part => `${encodeURIComponent(part)}/`).join('')
|
|
127
|
+
const { pathname } = new URL(spec, `file:///${base}`)
|
|
128
|
+
return /%(?:2f|5c)/iu.test(pathname) ? null : within(root, resolved([], decodeURIComponent(pathname)))
|
|
129
|
+
} catch {
|
|
130
|
+
return null
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
// Where a package is installed (`node_modules/<name>`), as parts, folded
|
|
135
|
+
// (within); none for one with no name, or one no directory could be.
|
|
136
|
+
const NAME = /^(?:@[^/]+\/)?[^/@.][^/]*$/u
|
|
137
|
+
const rootOf = name => typeof name === 'string' && NAME.test(name) && name.split('/').every(part => part !== '.' && part !== '..') ? ['node_modules', ...name.split('/')].map(folded) : []
|
|
138
|
+
|
|
139
|
+
// A path as a filesystem telling neither case nor Unicode's normalization
|
|
140
|
+
// apart takes it, as Windows' and macOS's do by default: `Payload.txt` for
|
|
141
|
+
// `payload.txt`; a lone surrogate the U+FFFD node writes a name with for it.
|
|
142
|
+
const folded = path => path.toWellFormed().normalize('NFC').toUpperCase().toLowerCase()
|
|
143
|
+
// The files a path names once installed, as a filesystem tells them
|
|
144
|
+
// (folded): its own, and for a .npmignore, a .gitignore npm installs as that;
|
|
145
|
+
// and the filesystems node may find them on, each telling the files a path
|
|
146
|
+
// names (`named`) by the path as it takes it (`fold`): one telling case
|
|
147
|
+
// apart (`cased`), as Linux's does, and one telling neither case nor
|
|
148
|
+
// Unicode's normalization apart (`folding`, folded), a path folded naming
|
|
149
|
+
// those with what follows it in ASCII (`.json`, `/index`), which folds as
|
|
150
|
+
// it is, every file the other names among them.
|
|
151
|
+
function installedOf(files) {
|
|
152
|
+
const exact = new Map(), index = new Map()
|
|
153
|
+
const put = (map, key, path) => map.has(key) ? map.get(key).push(path) : map.set(key, [path])
|
|
154
|
+
for (const path of files.keys()) {
|
|
155
|
+
const aliases = /(?:^|\/)\.gitignore$/u.test(path) ? [path, path.replace(/\.gitignore$/u, '.npmignore')] : [path]
|
|
156
|
+
for (const alias of aliases) {
|
|
157
|
+
put(index, folded(alias), path)
|
|
158
|
+
put(exact, alias, path)
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
const keyed = key => index.get(key) ?? []
|
|
162
|
+
return { installed: path => path === null ? [] : keyed(folded(path)), cased: { named: key => exact.get(key) ?? [], fold: path => path }, folding: { named: keyed, fold: folded } }
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
// Every string a JSON value holds, however deep it nests them, read
|
|
166
|
+
// without recursing, which nesting deeper than a call stack would end.
|
|
167
|
+
function leaves(value) {
|
|
168
|
+
const found = [], pending = [value]
|
|
169
|
+
while (pending.length > 0) {
|
|
170
|
+
const item = pending.pop()
|
|
171
|
+
if (typeof item === 'string') found.push(item)
|
|
172
|
+
else if (item && typeof item === 'object') for (const each of Object.values(item)) pending.push(each)
|
|
173
|
+
}
|
|
174
|
+
return found
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
// The targets an `exports` or `imports` map holds (leaves), by its keys
|
|
178
|
+
// (those `subpaths` tells, `./x`, `#x`), each a pattern's where its key is
|
|
179
|
+
// one (`./lib/*`), as node puts what a consumer asks for in a target's `*`
|
|
180
|
+
// only then; a map with none of those keys its `.`'s whole (`"exports":
|
|
181
|
+
// "./x"`, or conditions alone).
|
|
182
|
+
function mapped(map, subpaths) {
|
|
183
|
+
const keyed = map && typeof map === 'object' && !Array.isArray(map) && Object.keys(map).some(subpaths) ? Object.entries(map) : [['.', map]]
|
|
184
|
+
return keyed.flatMap(([key, value]) => leaves(value).map(target => ({ key, pattern: key.includes('*'), target })))
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// A package.json's `name`, `main`, and the paths its `exports` and `imports`
|
|
188
|
+
// name (`./x`, mapped); and its maps by their keys (`./x`, `#x`), its
|
|
189
|
+
// `imports` to the package's own name too (`<name>/x`), read past a byte
|
|
190
|
+
// order mark as node reads it; none where it is no JSON object.
|
|
191
|
+
function entriesOf(text) {
|
|
192
|
+
const none = { name: null, main: null, targets: [], exports: [], hasExports: false, imports: [] }
|
|
193
|
+
if (typeof text !== 'string') return none
|
|
194
|
+
try {
|
|
195
|
+
const { name, main, exports, imports } = JSON.parse(text.replace(/^\uFEFF/u, ''))
|
|
196
|
+
const subpaths = mapped(exports, key => key.startsWith('.')).filter(({ key, target }) => key.startsWith('.') && target.startsWith('./'))
|
|
197
|
+
const own = mapped(imports, key => key.startsWith('#')).filter(({ key }) => key.startsWith('#'))
|
|
198
|
+
const targets = [...subpaths, ...own.filter(({ target }) => target.startsWith('./'))]
|
|
199
|
+
return { name, main: typeof main === 'string' && main !== '' ? main : null, targets, exports: subpaths, hasExports: exports !== undefined && exports !== null, imports: own }
|
|
200
|
+
} catch {
|
|
201
|
+
return none
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
// A path as a filesystem reads it, from the package's top: each run of `/`
|
|
206
|
+
// one, none leading.
|
|
207
|
+
const collapsed = path => path.replaceAll(/\/+/gu, '/').replace(/^\//u, '')
|
|
208
|
+
|
|
209
|
+
// The paths an `exports` or `imports` target names, from the directory of the
|
|
210
|
+
// package.json naming it, read as a URL (urlJoined): its own, `*` and all,
|
|
211
|
+
// or where its key is a pattern (`pattern`, mapped) each file it matches, as a filesystem tells them (folded, collapsed), each of
|
|
212
|
+
// its `*` the same text, as node puts what a consumer asks for (`pkg/x`) in
|
|
213
|
+
// every one, where the pattern names what follows the last (`./lib/*.txt`);
|
|
214
|
+
// where it names nothing (`./*`), only one with no extension that isn't prose
|
|
215
|
+
// by its name (not `LICENSE`), the rest a consumer reaches only asking for
|
|
216
|
+
// them by their names. What a `*` stands for may be text with a `/` before
|
|
217
|
+
// it, after it, both or neither, or a `/` alone, the filesystem folding it
|
|
218
|
+
// into a `/` beside it (`pkg//` reaching `lib/.txt` through `./lib/*.txt`):
|
|
219
|
+
// each such shape's length told by one `x`'s, a file of the pattern's is the
|
|
220
|
+
// pattern with its text in each; a `/` before or after it counted apart only
|
|
221
|
+
// where it folds into a `/` of the pattern's, or the top, beside one `*` and
|
|
222
|
+
// not another, else its text holds it, or it folds alike at every one.
|
|
223
|
+
function exported(names, root, dir, target, pattern) {
|
|
224
|
+
const path = urlJoined(root, dir, target)
|
|
225
|
+
if (path === null || !pattern || !path.includes('*')) return [path]
|
|
226
|
+
const parts = folded(path).split('*'), stars = parts.length - 1
|
|
227
|
+
const head = parts[0].replace(/\/+$/u, ''), slashes = collapsed(parts.join('/')), tail = parts.at(-1).replace(/^\/+/u, '')
|
|
228
|
+
const both = sides => sides.includes(true) && sides.includes(false) ? ['', '/'] : ['']
|
|
229
|
+
const afters = both(parts.slice(1).map(part => part.startsWith('/'))), befores = both(parts.slice(0, -1).map((part, index) => part.endsWith('/') || (index === 0 && part === '')))
|
|
230
|
+
const shapes = befores.flatMap(before => afters.map(after => ({ after, at: collapsed(`${parts[0]}${before}x`).length - 1, before, length: collapsed(parts.join(`${before}x${after}`)).length })))
|
|
231
|
+
return names.filter(([name, file]) => {
|
|
232
|
+
if (!file.startsWith(head) || !file.endsWith(tail) || (parts.at(-1) === '' && (EXTENSION.test(name) || NPM_PROSE.test(name)))) return false
|
|
233
|
+
return file === slashes || shapes.some(({ after, at, before, length }) => {
|
|
234
|
+
const core = (file.length - length) / stars + 1
|
|
235
|
+
if (!Number.isInteger(core) || core <= 0) return false
|
|
236
|
+
const built = parts.join(`${before}${file.slice(at, at + core)}${after}`)
|
|
237
|
+
return built === file || ((built.includes('//') || built.startsWith('/')) && collapsed(built) === file)
|
|
238
|
+
})
|
|
239
|
+
}).map(([name]) => name)
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
// What a map's entries (`{ dir, key, target }` each) take `value` to, as
|
|
243
|
+
// node maps a specifier through `exports` or `imports`: the target of a key
|
|
244
|
+
// that is it, or of a pattern's (`#lib/*`, `./lib/*`, one `*`) it matches,
|
|
245
|
+
// what it matches put in each of the target's `*`; EVERY for one that would
|
|
246
|
+
// put in more than MAX_EXPANSION characters, which only a package written to
|
|
247
|
+
// hold up its reading has (`*` thousands of times over a long specifier), and
|
|
248
|
+
// whose path a URL's reading could shorten again (tabs, a query, `//`).
|
|
249
|
+
const MAX_EXPANSION = 64 * 1024
|
|
250
|
+
const EVERY = Symbol('every file')
|
|
251
|
+
const mappedTo = (entries, value) => entries.flatMap(({ dir, key, target }) => {
|
|
252
|
+
const star = key.indexOf('*')
|
|
253
|
+
if (star === -1 ? key !== value : star !== key.lastIndexOf('*') || value.length < key.length || !value.startsWith(key.slice(0, star)) || !value.endsWith(key.slice(star + 1))) return []
|
|
254
|
+
if (star === -1) return [{ dir, target }]
|
|
255
|
+
const sub = value.slice(star, value.length - (key.length - star - 1))
|
|
256
|
+
return [{ dir, target: (target.split('*').length - 1) * sub.length > MAX_EXPANSION ? EVERY : target.replaceAll('*', sub) }]
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
// A package's own subpath (`./x`, `.`) as its `exports` map it (mappedTo), as
|
|
260
|
+
// node resolves a specifier by its own name (`<name>/x`), read as URLs; with
|
|
261
|
+
// no `exports` at all, the path itself, as node finds the package where it
|
|
262
|
+
// is installed (`root`).
|
|
263
|
+
const selfResolved = (root, { exports, hasExports }, spec) => hasExports
|
|
264
|
+
? mappedTo(exports.map(entry => ({ ...entry, dir: '' })), spec).map(({ target }) => target === EVERY ? EVERY : urlJoined(root, '', target))
|
|
265
|
+
: [urlJoined(root, '', spec)]
|
|
266
|
+
|
|
267
|
+
// The path of the first of `extensions` a path takes that names a file of a
|
|
268
|
+
// version's on a filesystem (`system`, installedOf's), each it tells for it
|
|
269
|
+
// its own: none at the package's top, which is no file.
|
|
270
|
+
function firstNamed({ named, fold }, path, extensions) {
|
|
271
|
+
const key = path === '' ? null : fold(path)
|
|
272
|
+
const extension = key === null ? undefined : extensions.find(each => named(`${key}${each}`).length > 0)
|
|
273
|
+
return extension === undefined ? null : `${path}${extension}`
|
|
274
|
+
}
|
|
275
|
+
const indexFile = (system, path) => firstNamed(system, path === '' ? 'index' : `${path}/index`, TRIED)
|
|
276
|
+
|
|
277
|
+
// The files a path `require` takes finds, the first it tries that names a
|
|
278
|
+
// file: its own, one by its name and an extension (`.js`, `.json`, `.node`,
|
|
279
|
+
// `payload.JSON` as a filesystem folding case finds it), but where it takes
|
|
280
|
+
// it as a directory's (`directory`, DIRECTORY); else a directory's
|
|
281
|
+
// package.json `main`, found so or by its `index` and an extension; else the
|
|
282
|
+
// directory's `index` so, as node falls back to it; on a filesystem
|
|
283
|
+
// (`system`, installedOf's), as each finds its own first.
|
|
284
|
+
function requiredFiles(files, system, root, path, directory) {
|
|
285
|
+
if (path === null) return []
|
|
286
|
+
const file = directory ? null : firstNamed(system, path, ['', ...TRIED])
|
|
287
|
+
if (file !== null) return [file]
|
|
288
|
+
const mains = system.named(path === '' ? 'package.json' : `${system.fold(path)}/package.json`).map(manifest => entriesOf(files.get(manifest)).main).filter(main => main !== null)
|
|
289
|
+
.flatMap(main => [...separated(main)].map(spec => joined(root, path, spec))).filter(at => at !== null)
|
|
290
|
+
.map(at => firstNamed(system, at, ['', ...TRIED]) ?? indexFile(system, at)).filter(found => found !== null)
|
|
291
|
+
return mains.length > 0 ? mains : [indexFile(system, path)].filter(found => found !== null)
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
// The `#` specifiers each package.json of a version's files maps
|
|
295
|
+
// (`imports`), each from its directory.
|
|
296
|
+
const specifiersOf = files => [...files].filter(([path]) => /(?:^|\/)package\.json$/iu.test(path))
|
|
297
|
+
.flatMap(([path, text]) => entriesOf(text).imports.map(({ key, target }) => ({ dir: path.slice(0, -'package.json'.length - 1), key, target })))
|
|
298
|
+
|
|
299
|
+
// How many `exports` and `imports` patterns a package's package.json files
|
|
300
|
+
// have each matched against its files (exported): more than a package names
|
|
301
|
+
// (4 at most of popular ones). One naming more has every file read as one it
|
|
302
|
+
// may load.
|
|
303
|
+
const MAX_PATTERNS = 64
|
|
304
|
+
|
|
305
|
+
// The files of a version node runs as JavaScript though their names say
|
|
306
|
+
// otherwise (script.js npmIsScript), read as scripts all the same
|
|
307
|
+
// (stamps.js npmFileRisk's `script`): any a script of it, or a file read so,
|
|
308
|
+
// names by a relative path written out whole as a string (quoted), anywhere
|
|
309
|
+
// in its text, whatever takes it, or by the package's own name
|
|
310
|
+
// (`'<name>/x.txt'`); as `require` runs any but JSON and a native addon
|
|
311
|
+
// (`require('./data.txt')`, `require('./.npmignore')`, the .gitignore npm
|
|
312
|
+
// installs as that), and the `main` of a directory's package.json; and as
|
|
313
|
+
// `import` runs one whose name has no extension (`import('./payload')`), as a
|
|
314
|
+
// URL (urlJoined), a string an import declaration or `import(…)` takes whole
|
|
315
|
+
// read as it alone (imported). And the package's own `main`, and what the
|
|
316
|
+
// `exports` and `imports` of its package.json files name, their patterns'
|
|
317
|
+
// too, which a `require` reaching them runs whatever their names. A path a
|
|
318
|
+
// script builds is not followed (`'./x' + '.txt'`, a template with something
|
|
319
|
+
// put in it), nor one written in a string run as code, which runs-code.js
|
|
320
|
+
// tells. Each path names the files a filesystem telling neither case nor
|
|
321
|
+
// Unicode's normalization apart finds by it (folded), as Windows' and
|
|
322
|
+
// macOS's do, a loader running one where its name or theirs says it does;
|
|
323
|
+
// one that isn't text among them too, which node runs decoded as UTF-8 and
|
|
324
|
+
// readability.js npmBinaryCode tells, though nothing it loads is read, but
|
|
325
|
+
// where it can't parse (tarball.js npmRunnable's `runnable`: an image).
|
|
326
|
+
const loadedByVersion = new WeakMap()
|
|
327
|
+
export function npmLoadedScripts(version) {
|
|
328
|
+
let loaded = loadedByVersion.get(keyOf(version))
|
|
329
|
+
if (loaded) return loaded
|
|
330
|
+
loaded = new Set()
|
|
331
|
+
const files = bundleFilesAsMap(version), { installed, cased, folding } = installedOf(files), queue = []
|
|
332
|
+
const top = entriesOf(files.get(files.has('package.json') ? 'package.json' : installed('package.json')[0]))
|
|
333
|
+
const root = rootOf(top.name)
|
|
334
|
+
// What a loader takes (`by`) runs, as the name it is asked by tells, or
|
|
335
|
+
// the name of the file it finds.
|
|
336
|
+
const runs = (path, by) => !(by === 'require' ? NOT_RUN : EXTENSION).test(path)
|
|
337
|
+
// Each path once a loader and whether its name runs: the files it names
|
|
338
|
+
// are the same however else it is cased.
|
|
339
|
+
const tried = new Set()
|
|
340
|
+
const add = (path, by) => {
|
|
341
|
+
const key = path === null ? null : `${by}\0${runs(path, by)}\0${folded(path)}`
|
|
342
|
+
if (key === null || tried.has(key)) return
|
|
343
|
+
tried.add(key)
|
|
344
|
+
for (const file of installed(path)) {
|
|
345
|
+
const text = files.get(file)
|
|
346
|
+
if (loaded.has(file) || npmIsScript(file, text) || text?.runnable === false || (!runs(path, by) && !runs(file, by))) continue
|
|
347
|
+
loaded.add(file)
|
|
348
|
+
if (typeof text === 'string') queue.push(file)
|
|
349
|
+
}
|
|
350
|
+
}
|
|
351
|
+
// On either filesystem: where one folding case finds none, neither does
|
|
352
|
+
// one telling case apart, which finds fewer.
|
|
353
|
+
const requires = (path, directory) => {
|
|
354
|
+
const found = requiredFiles(files, folding, root, path, directory)
|
|
355
|
+
if (found.length > 0) for (const file of [...found, ...requiredFiles(files, cased, root, path, directory)]) add(file, 'require')
|
|
356
|
+
}
|
|
357
|
+
requires('', true)
|
|
358
|
+
const targets = [...files].filter(([path]) => /(?:^|\/)package\.json$/iu.test(path))
|
|
359
|
+
.flatMap(([path, text]) => entriesOf(text).targets.map(({ pattern, target }) => [path.slice(0, -'package.json'.length - 1), target, pattern && target.includes('*')]))
|
|
360
|
+
if (targets.filter(([, , pattern]) => pattern).length > MAX_PATTERNS) for (const path of files.keys()) add(path, 'require')
|
|
361
|
+
else {
|
|
362
|
+
// Each file's path and its path as a filesystem tells it (folded), for
|
|
363
|
+
// the patterns to match.
|
|
364
|
+
const names = targets.some(([, , pattern]) => pattern) ? [...files.keys()].map(name => [name, folded(name)]) : []
|
|
365
|
+
for (const [dir, target, pattern] of targets) for (const file of exported(names, root, dir, target, pattern)) add(file, 'require')
|
|
366
|
+
}
|
|
367
|
+
for (const [path, text] of files) if (npmIsScript(path, text)) queue.push(path)
|
|
368
|
+
const specifiers = specifiersOf(files)
|
|
369
|
+
// The package's own name, as a filesystem tells it (rootOf), a path from
|
|
370
|
+
// it after a `/`, or a `\\` as on Windows.
|
|
371
|
+
const named = value => root.length > 0 && value.length > top.name.length && '/\\'.includes(value[top.name.length])
|
|
372
|
+
&& folded(value.slice(0, top.name.length)) === root.slice(1).join('/')
|
|
373
|
+
const seen = new Set(), strings = quoted([...root.length > 0 ? foldsInto(top.name[0]) : [], ...specifiers.length > 0 ? ['#'] : []])
|
|
374
|
+
const subpathsOf = spec => selfResolved(root, top, spec)
|
|
375
|
+
// A path loaded, or for EVERY, every file, once (mappedTo).
|
|
376
|
+
let every = false
|
|
377
|
+
const reach = (path, by) => {
|
|
378
|
+
if (path !== EVERY) return add(path, by)
|
|
379
|
+
if (!every) for (const file of files.keys()) add(file, 'require')
|
|
380
|
+
every = true
|
|
381
|
+
}
|
|
382
|
+
// A `#` specifier as the package's `imports` map it (mappedTo), to a path
|
|
383
|
+
// of its own, read as a URL (urlJoined), or to its own name, as its
|
|
384
|
+
// `exports` map that (subpathsOf), loaded as code takes it (`by`), once
|
|
385
|
+
// each.
|
|
386
|
+
const own = (value, by) => {
|
|
387
|
+
if (seen.has(`${by}\0#\0${value}`)) return
|
|
388
|
+
seen.add(`${by}\0#\0${value}`)
|
|
389
|
+
for (const { dir, target } of mappedTo(specifiers, value)) {
|
|
390
|
+
const paths = target === EVERY ? [EVERY] : target.startsWith('./') ? [urlJoined(root, dir, target)] : named(target) ? subpathsOf(`./${target.slice(top.name.length + 1)}`)
|
|
391
|
+
: root.length > 0 && target === top.name ? subpathsOf('.') : []
|
|
392
|
+
for (const path of paths) reach(path, by)
|
|
393
|
+
}
|
|
394
|
+
}
|
|
395
|
+
// A string's value from the directory `dir`, a load `import` takes where
|
|
396
|
+
// its place says so (`importing`), else one `require` may take, once each.
|
|
397
|
+
const follow = (value, dir, importing) => {
|
|
398
|
+
if (value.startsWith('#')) return own(value, importing() ? 'import' : 'require')
|
|
399
|
+
const [from, spec] = named(value) ? ['', `./${value.slice(top.name.length + 1)}`] : [dir, value]
|
|
400
|
+
if (!RELATIVE.test(spec.replaceAll('\\', '/'))) return
|
|
401
|
+
const by = importing() ? 'import' : 'require', key = `${by}\0${from}\0${spec}`
|
|
402
|
+
if (seen.has(key)) return
|
|
403
|
+
seen.add(key)
|
|
404
|
+
if (named(value)) for (const path of subpathsOf(spec)) reach(path, by)
|
|
405
|
+
if (named(value) && top.hasExports) return
|
|
406
|
+
if (by === 'require') for (const required of requiredPaths(root, from, spec)) requires(required, DIRECTORY.test(spec))
|
|
407
|
+
add(urlJoined(root, from, spec), 'import')
|
|
408
|
+
}
|
|
409
|
+
for (let index = 0; index < queue.length; index++) {
|
|
410
|
+
const path = queue[index], text = files.get(path)
|
|
411
|
+
const dir = path.includes('/') ? path.slice(0, path.lastIndexOf('/')) : ''
|
|
412
|
+
for (const { 1: double, 2: single, 3: template, index: start } of text.matchAll(strings)) {
|
|
413
|
+
const body = double ?? single ?? template, quote = text[start]
|
|
414
|
+
for (const value of valuesOf(quote, body)) follow(value, dir, () => imported(text, quote, start, start + body.length + 2))
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
loadedByVersion.set(keyOf(version), loaded)
|
|
418
|
+
return loaded
|
|
419
|
+
}
|
|
420
|
+
|
|
11
421
|
// What risk reads of each file of a version (stamps.js npmFileRisk), once a
|
|
12
|
-
// version and path
|
|
422
|
+
// version and path: as a script too where node runs it as one though its name
|
|
423
|
+
// says otherwise (npmLoadedScripts).
|
|
13
424
|
const reads = new WeakMap()
|
|
14
425
|
export function npmVersionFileRisk(version, path) {
|
|
15
426
|
let files = reads.get(keyOf(version))
|
|
16
427
|
if (!files) reads.set(keyOf(version), files = new Map())
|
|
17
428
|
if (!files.has(path)) {
|
|
18
429
|
const text = bundleFilesAsMap(version).get(path)
|
|
19
|
-
files.set(path, npmFileRisk(path, typeof text === 'string' ? text : null, bundleFileSizes(version).get(path)))
|
|
430
|
+
files.set(path, npmFileRisk(path, typeof text === 'string' ? text : null, bundleFileSizes(version).get(path), npmLoadedScripts(version).has(path)))
|
|
20
431
|
}
|
|
21
432
|
return files.get(path)
|
|
22
433
|
}
|
package/risk/npm/tarball.js
CHANGED
|
@@ -1,74 +1,43 @@
|
|
|
1
|
-
// A package's files from its tarball, as
|
|
2
|
-
// a
|
|
3
|
-
//
|
|
4
|
-
//
|
|
5
|
-
//
|
|
1
|
+
// A package's files from its tarball, as it holds them, whatever installs it
|
|
2
|
+
// (npm renames a .gitignore .npmignore, pnpm and yarn keep it): gunzipped and
|
|
3
|
+
// read as a tar archive by @preventive/archive, which refuses one malformed, a
|
|
4
|
+
// name or a symlink that would climb out of it, and a name repeated as another
|
|
5
|
+
// entry; each file by its path there, its first component dropped; a text
|
|
6
|
+
// where it is UTF-8 with no NUL, else null. Held to the npm viewer's bounds
|
|
7
|
+
// (server-managed/npm-packages.ts readNpmTar).
|
|
8
|
+
import { CompressionError, decompress } from '@preventive/archive/compression.js'
|
|
9
|
+
import { ArchiveError, unpack } from '@preventive/archive/tar.js'
|
|
10
|
+
|
|
6
11
|
const MAX_PACKAGE_FILES = 20_000
|
|
7
12
|
export const MAX_PACKAGE_BYTES = 64 * 1024 * 1024
|
|
13
|
+
// The entries its tar stream may hold, a directory's too, read before any is
|
|
14
|
+
// counted as a file, as npm-packages.ts bounds them (MAX_NPM_TAR_ENTRIES).
|
|
15
|
+
export const MAX_TAR_ENTRIES = 2 * MAX_PACKAGE_FILES
|
|
8
16
|
// What its tar stream may unpack to, its headers and padding beside its
|
|
9
17
|
// files, as npm-loads.ts bounds it (MAX_TAR_BYTES).
|
|
10
18
|
export const MAX_TAR_BYTES = 96 * 1024 * 1024
|
|
11
19
|
|
|
12
|
-
const latin1 = new TextDecoder('latin1')
|
|
13
20
|
const utf8 = new TextDecoder('utf-8', { fatal: true, ignoreBOM: true })
|
|
14
|
-
const utf8Lenient = bytes => new TextDecoder().decode(bytes)
|
|
15
|
-
// A header's text field, to its first NUL.
|
|
16
|
-
const textOf = (bytes, start, length) => {
|
|
17
|
-
const field = bytes.subarray(start, start + length)
|
|
18
|
-
const end = field.indexOf(0)
|
|
19
|
-
return utf8Lenient(end === -1 ? field : field.subarray(0, end))
|
|
20
|
-
}
|
|
21
21
|
|
|
22
|
-
//
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
for (let i = 1; i < 12; i++) size = size * 256 + bytes[offset + i]
|
|
27
|
-
return size
|
|
28
|
-
}
|
|
29
|
-
const text = textOf(bytes, offset, 12).trim()
|
|
30
|
-
return /^[0-7]+$/u.test(text) ? Number.parseInt(text, 8) : Number.NaN
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
// A pax header's path: records `<length> <key>=<value>\n`, their lengths in
|
|
34
|
-
// bytes.
|
|
35
|
-
function paxPath(data) {
|
|
36
|
-
let at = 0, path = null
|
|
37
|
-
while (at < data.length) {
|
|
38
|
-
const space = data.indexOf(0x20, at)
|
|
39
|
-
const length = Number.parseInt(latin1.decode(data.subarray(at, space)), 10)
|
|
40
|
-
if (space === -1 || !Number.isSafeInteger(length) || length <= 0) break
|
|
41
|
-
const record = utf8Lenient(data.subarray(space + 1, at + length - 1))
|
|
42
|
-
if (record.startsWith('path=')) path = record.slice(5)
|
|
43
|
-
at += length
|
|
44
|
-
}
|
|
45
|
-
return path
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
const packagePath = raw => {
|
|
49
|
-
const parts = raw.split('/').slice(1).filter(part => part !== '' && part !== '.')
|
|
50
|
-
return parts.length === 0 || parts.includes('..') ? null : parts.join('/')
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
// The files of a tar archive, by path: their bytes.
|
|
22
|
+
// The files of a tar archive, by path: their bytes. A file at its top is not
|
|
23
|
+
// extracted, and one under another top directory at the same path replaces
|
|
24
|
+
// an earlier one, as extraction would. Past MAX_TAR_ENTRIES it is given up on
|
|
25
|
+
// as `package-too-large`, before any more of it is read.
|
|
54
26
|
function npmTarFiles(tar) {
|
|
27
|
+
let entries
|
|
28
|
+
try {
|
|
29
|
+
entries = unpack(tar, { maxEntries: MAX_TAR_ENTRIES })
|
|
30
|
+
} catch (error) {
|
|
31
|
+
if (error instanceof ArchiveError && error.limited) throw new Error('package-too-large', { cause: error })
|
|
32
|
+
throw error
|
|
33
|
+
}
|
|
55
34
|
const files = new Map()
|
|
56
|
-
let
|
|
57
|
-
|
|
58
|
-
const
|
|
59
|
-
if (
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
const data = tar.subarray(start, start + size)
|
|
63
|
-
offset = start + Math.ceil(size / 512) * 512
|
|
64
|
-
if (type === 'x' || type === 'L') { longName = type === 'x' ? paxPath(data) : textOf(data, 0, data.length); continue }
|
|
65
|
-
if (type === 'g' || type === 'K') continue
|
|
66
|
-
const prefix = textOf(header, 257, 6) === 'ustar' ? textOf(header, 345, 155) : ''
|
|
67
|
-
const name = longName ?? (prefix ? `${prefix}/${textOf(header, 0, 100)}` : textOf(header, 0, 100))
|
|
68
|
-
longName = null
|
|
69
|
-
const path = ['0', '\0', '7'].includes(type) ? packagePath(name) : null
|
|
70
|
-
if (path === null) continue
|
|
71
|
-
total += size - (files.get(path)?.length ?? 0)
|
|
35
|
+
let total = 0
|
|
36
|
+
for (const { name, type, data } of entries) {
|
|
37
|
+
const slash = name.indexOf('/')
|
|
38
|
+
if ((type !== 'file' && type !== 'contiguous-file') || slash === -1) continue
|
|
39
|
+
const path = name.slice(slash + 1)
|
|
40
|
+
total += data.length - (files.get(path)?.length ?? 0)
|
|
72
41
|
files.set(path, data)
|
|
73
42
|
if (files.size > MAX_PACKAGE_FILES || total > MAX_PACKAGE_BYTES) throw new Error('package-too-large')
|
|
74
43
|
}
|
|
@@ -81,18 +50,31 @@ export function npmFileText(bytes) {
|
|
|
81
50
|
try { return utf8.decode(bytes) } catch { return null }
|
|
82
51
|
}
|
|
83
52
|
|
|
84
|
-
// A gzipped tarball's tar stream,
|
|
85
|
-
// unpacks past MAX_TAR_BYTES, before any
|
|
53
|
+
// A gzipped tarball's tar stream, through @preventive/archive's gzip, given up
|
|
54
|
+
// on as `package-too-large` once it unpacks past MAX_TAR_BYTES, before any
|
|
55
|
+
// more of it is held.
|
|
86
56
|
async function gunzip(gzipped) {
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
if (size > MAX_TAR_BYTES) { await reader.cancel(); throw new Error('package-too-large') }
|
|
93
|
-
chunks.push(read.value)
|
|
57
|
+
try {
|
|
58
|
+
return await decompress(gzipped, 'gzip', { limit: MAX_TAR_BYTES })
|
|
59
|
+
} catch (error) {
|
|
60
|
+
if (error instanceof CompressionError && error.limited) throw new Error('package-too-large', { cause: error })
|
|
61
|
+
throw error
|
|
94
62
|
}
|
|
95
|
-
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
// Whether a file that isn't text may run as JavaScript all the same, as node
|
|
66
|
+
// decodes it as UTF-8 (npm/files.js npmLoadedScripts): only where its first
|
|
67
|
+
// character no UTF-8 encodes, or its first NUL, which JavaScript holds only
|
|
68
|
+
// in a string, a template, a regular expression or a comment, may sit in
|
|
69
|
+
// one, after a quote, a backtick, a slash, a `<` or `-` (an HTML-like
|
|
70
|
+
// comment, `<!--`, `-->`) or on a hashbang's line. An image's, a font's or
|
|
71
|
+
// WebAssembly's is in its first bytes, before any.
|
|
72
|
+
const OPENS = [0x27, 0x22, 0x60, 0x2F, 0x3C, 0x2D]
|
|
73
|
+
function npmRunnable(bytes) {
|
|
74
|
+
const start = bytes[0] === 0xEF && bytes[1] === 0xBB && bytes[2] === 0xBF ? 3 : 0
|
|
75
|
+
if (bytes[start] === 0x23 && bytes[start + 1] === 0x21) return true
|
|
76
|
+
const opens = Math.min(...OPENS.map(byte => bytes.indexOf(byte, start)).filter(at => at !== -1))
|
|
77
|
+
return Number.isFinite(opens) && npmFileText(bytes.subarray(0, opens)) !== null
|
|
96
78
|
}
|
|
97
79
|
|
|
98
80
|
// A file's bytes' digest, as the managed server gives one of a file that
|
|
@@ -103,11 +85,12 @@ const sha256 = async bytes => `sha256-${btoa(String.fromCodePoint(...new Uint8Ar
|
|
|
103
85
|
// viewer holds a version (ui/view/npm-package.js npmPackageDetails), a source
|
|
104
86
|
// map listing them (`kind: 'sourcemap'`): `json.sources` their paths, sorted,
|
|
105
87
|
// `json.sourcesContent` their texts, `fileSizes` by path, and `npmBinaries`,
|
|
106
|
-
// those that aren't text, `{ size, digest }` by path,
|
|
88
|
+
// those that aren't text, `{ size, digest, runnable }` by path (npmRunnable),
|
|
89
|
+
// which compare.js and
|
|
107
90
|
// diff.js tell them by.
|
|
108
91
|
export async function npmTarballVersion(gzipped) {
|
|
109
92
|
const files = [...npmTarFiles(await gunzip(gzipped))].toSorted(([a], [b]) => a < b ? -1 : a > b ? 1 : 0)
|
|
110
93
|
const texts = files.map(([, bytes]) => npmFileText(bytes))
|
|
111
|
-
const binaries = await Promise.all(files.filter((_, at) => texts[at] === null).map(async ([path, bytes]) => [path, { size: bytes.length, digest: await sha256(bytes) }]))
|
|
94
|
+
const binaries = await Promise.all(files.filter((_, at) => texts[at] === null).map(async ([path, bytes]) => [path, { size: bytes.length, digest: await sha256(bytes), runnable: npmRunnable(bytes) }]))
|
|
112
95
|
return { kind: 'sourcemap', json: { version: 3, sources: files.map(([path]) => path), sourcesContent: texts }, fileSizes: new Map(files.map(([path, bytes]) => [path, bytes.length])), npmBinaries: new Map(binaries) }
|
|
113
96
|
}
|
package/server-e2e/app.ts
CHANGED
|
@@ -102,11 +102,12 @@ import { createBusReceiver } from './bus-receiver.ts'
|
|
|
102
102
|
import { e2eStorageLines } from '../server-common/storage-log.ts'
|
|
103
103
|
|
|
104
104
|
// Each embedding gets its own stores, transports, timers and shutdown state.
|
|
105
|
-
|
|
106
|
-
|
|
105
|
+
// `ui: false` serves only the API (see ./http.ts).
|
|
106
|
+
export async function createE2eApp(config = loadConfig(), { ui = true }: { ui?: boolean } = {}) {
|
|
107
|
+
return await initializeApp(rollback => assembleE2eApp(config, ui, rollback))
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
async function assembleE2eApp(config: Config, rollback: AsyncDisposableStack) {
|
|
110
|
+
async function assembleE2eApp(config: Config, ui: boolean, rollback: AsyncDisposableStack) {
|
|
110
111
|
// All external inputs (env vars + optional config.json) are parsed
|
|
111
112
|
// and validated in ./config.ts; destructure into the uppercase names
|
|
112
113
|
// the rest of this module uses.
|
|
@@ -388,7 +389,7 @@ async function assembleE2eApp(config: Config, rollback: AsyncDisposableStack) {
|
|
|
388
389
|
// below.
|
|
389
390
|
const httpServer = createHttpServer({
|
|
390
391
|
wss, restDeps: objstoreRestDeps, sseServer, serverInfo: SERVER_INFO, isOriginAllowed,
|
|
391
|
-
isShuttingDown, track, handleSaveRest,
|
|
392
|
+
isShuttingDown, track, handleSaveRest, ui,
|
|
392
393
|
restPutIdleTimeoutMs: REST_PUT_IDLE_TIMEOUT_MS, debug: DEBUG,
|
|
393
394
|
})
|
|
394
395
|
const requestHandler = httpServer.listeners('request')[0]!
|