@preventive/triage 1.0.0-alpha.74 → 1.0.0-alpha.76

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.
Files changed (58) hide show
  1. package/common/finding-duplicates.js +4 -4
  2. package/common/finding-filters.js +11 -13
  3. package/common/managed/roles.ts +6 -0
  4. package/common/managed/routes.js +4 -3
  5. package/common/managed/team-app.d.ts +2 -0
  6. package/common/managed/team-app.js +31 -14
  7. package/out/activity.js +1 -1
  8. package/out/{chunk-C6OPCPCA.js → chunk-24LZZYAP.js} +1 -1
  9. package/out/{chunk-VRAJCPV5.js → chunk-EYTPAODY.js} +2 -2
  10. package/out/chunk-ITUKKCCK.js +10 -0
  11. package/out/{chunk-BLPHP4U3.js → chunk-IVDE66N5.js} +1 -1
  12. package/out/{chunk-VS7RV24Z.js → chunk-MFAB7P3T.js} +2 -2
  13. package/out/chunk-O6FA2R5G.js +53 -0
  14. package/out/chunk-OCE6KQGJ.js +40 -0
  15. package/out/{chunk-YI4D6XVX.js → chunk-RHOQDAU4.js} +1 -1
  16. package/out/{chunk-2IHCVA3Y.js → chunk-T6EXDRB7.js} +1 -1
  17. package/out/chunk-UZC7POI4.js +255 -0
  18. package/out/chunk-WGQ7D5DB.js +1 -0
  19. package/out/client-managed-import.js +1 -1
  20. package/out/client-managed.js +30 -30
  21. package/out/compare.js +4 -4
  22. package/out/dialogs.js +1 -1
  23. package/out/graph.js +6 -6
  24. package/out/index.html +8 -8
  25. package/out/prism.js +1 -1
  26. package/out/scan.js +1 -1
  27. package/out/view.css +1 -1
  28. package/out/view.js +4 -4
  29. package/package.json +5 -2
  30. package/risk/files/characters.js +142 -8
  31. package/risk/files/hosts.js +4 -3
  32. package/risk/files/readability.js +18 -13
  33. package/risk/files/runs-code.js +41 -80
  34. package/risk/files/script.js +210 -48
  35. package/risk/index.d.ts +4 -2
  36. package/risk/npm/assess.js +1 -1
  37. package/risk/npm/tarball.js +35 -19
  38. package/server-managed/dependency-review-comments.ts +22 -10
  39. package/server-managed/http.ts +74 -25
  40. package/server-managed/locked-packages.ts +62 -5
  41. package/server-managed/npm-packages.ts +7 -5
  42. package/server-managed/queue.ts +8 -7
  43. package/server-managed/repository-browser.ts +6 -3
  44. package/server-managed/repository-code.ts +3 -2
  45. package/server-managed/repository-risk-caches.ts +37 -0
  46. package/server-managed/repository-risk-read.ts +41 -18
  47. package/server-managed/repository-risk-stasis.ts +61 -0
  48. package/server-managed/repository-risk-turn.ts +97 -0
  49. package/server-managed/repository-risk.ts +128 -102
  50. package/server-managed/scan-downloads.ts +44 -10
  51. package/server-managed/team-app-summaries.ts +3 -1
  52. package/server-managed/team-app.ts +4 -2
  53. package/server-managed/team-reports.ts +8 -3
  54. package/out/chunk-5YP7P5KA.js +0 -1
  55. package/out/chunk-74AEKRES.js +0 -10
  56. package/out/chunk-DQJEVHRE.js +0 -52
  57. package/out/chunk-ESJZZF2Y.js +0 -255
  58. package/out/chunk-GV5U7OLP.js +0 -40
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@preventive/triage",
3
- "version": "1.0.0-alpha.74",
3
+ "version": "1.0.0-alpha.76",
4
4
  "description": "Client & relay server for triaging of automated reports",
5
5
  "license": "MIT OR Apache-2.0",
6
6
  "author": {
@@ -211,8 +211,11 @@
211
211
  "server-managed/repository-actions.ts",
212
212
  "server-managed/repository-risk.ts",
213
213
  "server-managed/repository-risk-assess.ts",
214
+ "server-managed/repository-risk-caches.ts",
214
215
  "server-managed/repository-risk-audit.ts",
215
216
  "server-managed/repository-risk-read.ts",
217
+ "server-managed/repository-risk-stasis.ts",
218
+ "server-managed/repository-risk-turn.ts",
216
219
  "server-managed/scan-downloads.ts",
217
220
  "server-managed/session.ts",
218
221
  "server-managed/slugs.ts",
@@ -449,7 +452,7 @@
449
452
  "@preventive/publishers": "^1.1.0",
450
453
  "@preventive/report": "^1.0.1",
451
454
  "@preventive/sourcemap": "1.0.0-alpha.1",
452
- "@preventive/upstream": "^1.9.1",
455
+ "@preventive/upstream": "^1.10.0",
453
456
  "css-tree": "^3.2.1",
454
457
  "oxc-minify": "^0.158.0",
455
458
  "oxc-parser": "^0.152.0",
@@ -1,9 +1,11 @@
1
1
  // The characters a text holds that can make it read other than it runs, or
2
2
  // hide text in it: control and bidirectional characters, and the invisible
3
3
  // characters a payload or a name no one sees can be spelled in (variation
4
- // selectors, Hangul fillers, tags). Shared with a code viewer that shows each
4
+ // selectors, Hangul fillers, tags), and letters out of place in a word of
5
+ // another script (mixedScripts). Shared with a code viewer that shows each
5
6
  // as a labelled box (ui/view/control-chars.js), which leaves what spells an
6
- // emoji, a recommended flag or a line's end as it is (passed).
7
+ // emoji, a recommended flag or a line's end as it is (passed), and marks
8
+ // each letter out of place.
7
9
 
8
10
  // What a text holds beyond printing characters and its line breaks: the C0
9
11
  // controls other than tab, line feed and carriage return, DEL, the C1
@@ -27,16 +29,21 @@ export const UNSEEN = /[\u0000-\u0008\u000B-\u001F\u007F-\u009F\u00A0\u1680\u200
27
29
 
28
30
  // A file's text: `kind` 'ascii' or 'utf8', by whether it holds anything past
29
31
  // ASCII, else 'binary' (which the server tells by its bytes, sending no
30
- // text); `controls` the control and invisible characters a text holds, how
31
- // often by code point, or null for none.
32
+ // text); `controls` the control and invisible characters a text holds, and
33
+ // its letters out of place (mixedScripts), how often by code point, or null
34
+ // for none.
32
35
  export function npmTextEncoding(text) {
33
36
  if (typeof text !== 'string') return { kind: 'binary', controls: null }
34
37
  const controls = new Map()
35
38
  for (const [char] of text.matchAll(CONTROLS)) controls.set(char.codePointAt(0), (controls.get(char.codePointAt(0)) ?? 0) + 1)
36
- // The invisible characters are all past ASCII: looked for in a text that
37
- // has any, from its first.
39
+ // The invisible characters are all past ASCII, as is a letter of any
40
+ // script but Latin, which a word of mixed scripts holds: looked for in a
41
+ // text that has any, from its first.
38
42
  const past = text.search(NON_ASCII)
39
- if (past !== -1) for (const [code, times] of invisibleCharacters(text, past) ?? []) controls.set(code, times)
43
+ if (past !== -1) {
44
+ for (const [code, times] of invisibleCharacters(text, past) ?? []) controls.set(code, times)
45
+ mixedScripts(text, (at, code) => controls.set(code, (controls.get(code) ?? 0) + 1), past)
46
+ }
40
47
  return { kind: past === -1 ? 'ascii' : 'utf8', controls: controls.size > 0 ? controls : null }
41
48
  }
42
49
 
@@ -83,7 +90,7 @@ export function passed(text, at, code, lineEnd) {
83
90
  // grapheme joiner, the Khmer inherent vowels and the Mongolian free variation
84
91
  // selectors), which tables of the characters a name may hold, as JavaScript
85
92
  // parsers have, hold too; nor what of these hides nothing (benign).
86
- const INVISIBLE = /[\u115F\u1160\u3164\uFFA0\u{E0001}\u{E0020}-\u{E007F}]|[\uFE00-\uFE0F]|[\u{E0100}-\u{E01EF}]/u
93
+ export const INVISIBLE = /[\u115F\u1160\u3164\uFFA0\u{E0001}\u{E0020}-\u{E007F}]|[\uFE00-\uFE0F]|[\u{E0100}-\u{E01EF}]/u
87
94
  // The code units they are in, to look for them by, faster than by code point:
88
95
  // the high surrogate starting those past the Basic Multilingual Plane (tags,
89
96
  // the selectors' supplement) too, told apart from its others once found. No
@@ -205,3 +212,130 @@ export function invisibleCharacters(text, from = 0) {
205
212
  return found
206
213
  }
207
214
 
215
+ // Letters out of place in a word of more than one script, as a look-alike
216
+ // spells a name or a host other than the one it reads as (`food` or
217
+ // `google.com` with a Cyrillic `o`, U+043E): a word a run of letters, marks,
218
+ // digits, `_` and `$`, those of Common and Inherited script of none, as are
219
+ // the default-ignorables (Invisible's fillers). As UTS #39's Highly
220
+ // Restrictive level has it, a word of one script is fine, as is one of Latin,
221
+ // Han and kana, of Latin, Han and Bopomofo, or of Latin, Han and Hangul. Not
222
+ // a word wholly of a script that reads as another (`ape` spelled in
223
+ // Cyrillic), as most of some scripts' words would be.
224
+ const WORD = String.raw`\p{L}\p{M}\p{Nd}_$`
225
+ const NONE = String.raw`\p{sc=Zyyy}\p{sc=Zinh}\p{Default_Ignorable_Code_Point}`
226
+ // A word's character of a script but Latin, which one of mixed scripts
227
+ // holds: past Latin-1, as all are, told first, which passes the rest faster.
228
+ const FOREIGN = new RegExp(String.raw`(?=[^\0-\xFF])[[${WORD}]--[\p{sc=Latn}${NONE}]]`, 'gv')
229
+ export const NON_LATIN = new RegExp(FOREIGN.source, 'v')
230
+ const NO_SCRIPT = new RegExp(`[${NONE}]`, 'uy')
231
+ const WORD_CHAR = new RegExp(`[${WORD}]`, 'uy')
232
+
233
+ // The scripts by their codes (Unicode 17.0.0), the most met first: those this
234
+ // engine knows, as one with older tables knows no letter of a newer script.
235
+ const SCRIPTS = ('Latn Cyrl Grek Hani Hira Kana Hang Bopo Arab Hebr Armn Geor Thai Deva Adlm Aghb Ahom Armi Avst Bali Bamu'
236
+ + ' Bass Batk Beng Berf Bhks Brah Brai Bugi Buhd Cakm Cans Cari Cham Cher Chrs Copt Cpmn Cprt Diak Dogr Dsrt'
237
+ + ' Dupl Egyp Elba Elym Ethi Gara Glag Gong Gonm Goth Gran Gujr Gukh Guru Hano Hatr Hluw Hmng Hmnp Hung Ital'
238
+ + ' Java Kali Kawi Khar Khmr Khoj Kits Knda Krai Kthi Lana Laoo Lepc Limb Lina Linb Lisu Lyci Lydi Mahj Maka'
239
+ + ' Mand Mani Marc Medf Mend Merc Mero Mlym Modi Mong Mroo Mtei Mult Mymr Nagm Nand Narb Nbat Newa Nkoo Nshu'
240
+ + ' Ogam Olck Onao Orkh Orya Osge Osma Ougr Palm Pauc Perm Phag Phli Phlp Phnx Plrd Prti Rjng Rohg Runr Samr'
241
+ + ' Sarb Saur Sgnw Shaw Shrd Sidd Sidt Sind Sinh Sogd Sogo Sora Soyo Sund Sunu Sylo Syrc Tagb Takr Tale Talu'
242
+ + ' Taml Tang Tavt Tayo Telu Tfng Tglg Thaa Tibt Tirh Tnsa Todr Tols Toto Tutg Ugar Vaii Vith Wara Wcho Xpeo'
243
+ + ' Xsux Yezi Yiii Zanb').split(' ').flatMap(code => {
244
+ try {
245
+ return [{ code, char: new RegExp(String.raw`\p{sc=${code}}`, 'uy') }]
246
+ } catch {
247
+ return []
248
+ }
249
+ })
250
+ const UNKNOWN = { code: 'Zzzz', char: /\p{sc=Zzzz}/uy }
251
+ const isOf = ({ char }, text, at) => {
252
+ char.lastIndex = at
253
+ return char.test(text)
254
+ }
255
+ // The script of the character at `at`, the last found tried first.
256
+ let last = UNKNOWN
257
+ function scriptAt(text, at) {
258
+ if (!isOf(last, text, at)) last = SCRIPTS.find(script => isOf(script, text, at)) ?? UNKNOWN
259
+ return last.code
260
+ }
261
+
262
+ // A script's name by its code, and the script a code point is of.
263
+ const SCRIPT_NAMES = new Intl.DisplayNames('en', { type: 'script' })
264
+ export const scriptName = code => SCRIPT_NAMES.of(code)
265
+ export const scriptOf = code => scriptAt(String.fromCodePoint(code), 0)
266
+
267
+ // A run read a stretch at a time, as a regular expression's loop over a
268
+ // megabyte past Latin-1 runs out of stack: where one from `at` stops.
269
+ const STRETCH = 4096
270
+ function runEnd(run, text, at) {
271
+ for (let from = at; ; from = run.lastIndex) {
272
+ run.lastIndex = from
273
+ run.test(text)
274
+ if (run.lastIndex - from < STRETCH) return run.lastIndex
275
+ }
276
+ }
277
+ // A word; one of `scripts` or of no script; and a passage of such words and
278
+ // what lies between them.
279
+ const WORD_RUN = new RegExp(`[${WORD}]{0,${STRETCH}}`, 'uy')
280
+ const scriptsOf = scripts => `[${scripts.map(code => String.raw`\p{sc=${code}}`).join('')}${NONE}]`
281
+ const wordOf = scripts => new RegExp(String.raw`[[${WORD}]&&${scriptsOf(scripts)}]{0,${STRETCH}}`, 'vy')
282
+ const passageOf = scripts => new RegExp(String.raw`[^[${WORD}]--${scriptsOf(scripts)}]{0,${STRETCH}}`, 'vy')
283
+ // The words of the scripts UTS #39 allows together; and a script's word and
284
+ // passage, made once asked for, its passage of those it is allowed with.
285
+ const TOGETHER = [['Latn', 'Hani', 'Hira', 'Kana'], ['Latn', 'Hani', 'Bopo'], ['Latn', 'Hani', 'Hang']]
286
+ const ALLOWED = TOGETHER.map(wordOf)
287
+ const RUNS = new Map()
288
+ function runsOf(script) {
289
+ if (!RUNS.has(script)) RUNS.set(script, { word: wordOf([script]), passage: passageOf(TOGETHER.find(scripts => scripts.includes(script)) ?? [script]) })
290
+ return RUNS.get(script)
291
+ }
292
+ const covers = (word, text, start, end) => runEnd(word, text, start) === end
293
+
294
+ // Where the word holding `at` starts, from `floor` on.
295
+ function wordStart(text, at, floor) {
296
+ let start = at
297
+ while (start > floor) {
298
+ const back = start - (codePointBefore(text, start) > 0xffff ? 2 : 1)
299
+ WORD_CHAR.lastIndex = back
300
+ if (!WORD_CHAR.test(text)) break
301
+ start = back
302
+ }
303
+ return start
304
+ }
305
+
306
+ // Each letter of a word of mixed scripts but its main script's, the most of
307
+ // its letters' (Latin on a tie, else the first), to `found`: counted, then
308
+ // visited, none kept, as a word can be a megabyte long.
309
+ function misplaced(text, start, end, found) {
310
+ const letters = visit => {
311
+ for (let at = start; at < end; at += text.codePointAt(at) > 0xffff ? 2 : 1) {
312
+ NO_SCRIPT.lastIndex = at
313
+ if (!NO_SCRIPT.test(text)) visit(at, scriptAt(text, at))
314
+ }
315
+ }
316
+ const counts = new Map()
317
+ letters((at, script) => counts.set(script, (counts.get(script) ?? 0) + 1))
318
+ let home = 'Latn'
319
+ for (const [script, times] of counts) if (times > (counts.get(home) ?? 0)) home = script
320
+ letters((at, script) => {
321
+ if (script !== home) found(at, text.codePointAt(at), script, home)
322
+ })
323
+ }
324
+
325
+ // Each letter out of place in a text, in order, as `found(at, code point, its
326
+ // script, its word's)`, by their scripts' codes; `from`: where to look from,
327
+ // as the first character past ASCII. From a word holding a letter of a script
328
+ // but Latin, the passage of that script's words is read as one run; the word
329
+ // it stops at, holding a letter of another, is read whole, and on from it.
330
+ export function mixedScripts(text, found, from = 0) {
331
+ FOREIGN.lastIndex = from
332
+ for (let floor = 0; FOREIGN.test(text); FOREIGN.lastIndex = floor) {
333
+ const at = FOREIGN.lastIndex - (codePointBefore(text, FOREIGN.lastIndex) > 0xffff ? 2 : 1)
334
+ const stop = runEnd(runsOf(scriptAt(text, at)).passage, text, wordStart(text, at, floor))
335
+ if (stop === text.length) return
336
+ const start = wordStart(text, stop, floor)
337
+ const end = runEnd(WORD_RUN, text, start)
338
+ if (!covers(runsOf(scriptAt(text, stop)).word, text, start, end) && !ALLOWED.some(word => covers(word, text, start, end))) misplaced(text, start, end, found)
339
+ floor = end
340
+ }
341
+ }
@@ -13,7 +13,7 @@
13
13
  // (`'https://' + name`, `${host}`) goes unseen, so a version without these
14
14
  // labels is not one that sends nothing.
15
15
  import { NPM_PROSE, SOURCE_MAP } from './readability.js'
16
- import { NPM_MAX_FOUND, jsMatchAll, lineOf, npmIsScript } from './script.js'
16
+ import { NPM_MAX_FOUND, jsMatchAll, jsOffset, lineOf, npmIsScript } from './script.js'
17
17
 
18
18
  // How many hosts a file is told naming at most, the first it names.
19
19
  const NPM_MAX_HOSTS = 1000
@@ -86,7 +86,8 @@ function githubNamed(url) {
86
86
  export function npmHosts(path, text, script = false) {
87
87
  if (!npmIsScript(path, text, script) || !(text.includes('://') || text.includes(':\\/\\/'))) return null
88
88
  const hosts = new Map(), line = lineOf(text)
89
- for (const { 1: token, index } of jsMatchAll(text)) {
89
+ for (const found of jsMatchAll(text)) {
90
+ const { 1: token } = found
90
91
  if (hosts.size >= NPM_MAX_HOSTS) break
91
92
  // Read unescaped, its offsets as near: on the same line but in a template
92
93
  // across lines; a regular expression without its closing slash and flags,
@@ -97,7 +98,7 @@ export function npmHosts(path, text, script = false) {
97
98
  for (const { 1: name, index: at } of string.matchAll(URL_HOST)) {
98
99
  const host = name.toLowerCase()
99
100
  const named = host === 'github.com' ? githubNamed(/^[^\s'"\u0060]*/u.exec(string.slice(at))[0]) : host
100
- if (!hosts.has(named) && (NAMED.test(host) || IPV4.test(host)) && !LOCAL.test(host)) hosts.set(named, line(index + at))
101
+ if (!hosts.has(named) && (NAMED.test(host) || IPV4.test(host)) && !LOCAL.test(host)) hosts.set(named, line(jsOffset(found, at)))
101
102
  if (hosts.size >= NPM_MAX_HOSTS) break
102
103
  }
103
104
  }
@@ -1,9 +1,10 @@
1
1
  // How a file's text reads, for a reviewer: its category (binary, a source
2
2
  // map, code with unexpectedly long lines or an inline map, minified, else
3
- // ASCII or UTF-8), its control and invisible characters, its longest lines,
4
- // an obfuscator's fingerprint, a large code file, and a file become
5
- // unreadable between two versions.
6
- import { CONTROLS, npmTextEncoding } from './characters.js'
3
+ // ASCII or UTF-8), its control and invisible characters, its letters out of
4
+ // place in a word of another script, its longest lines, an obfuscator's
5
+ // fingerprint, a large code file, and a file become unreadable between two
6
+ // versions.
7
+ import { CONTROLS, INVISIBLE, npmTextEncoding } from './characters.js'
7
8
  import { MINIFIED_AVERAGE, minifiable, minifiedAverage } from './script.js'
8
9
 
9
10
  // Lines longer than anyone writes by hand.
@@ -83,8 +84,9 @@ export const npmBecameUnreadable = (path, was, now, script = false) => Boolean(w
83
84
  // not), minified (code mostly on long lines, or named .min., or minified
84
85
  // into shorter lines: see MINIFIED_AVERAGE), else utf8 or ascii; and
85
86
  // whatever it is, the control, bidirectional or invisible characters a text
86
- // holds (`controls`), found in one pass, which count as a label of their own
87
- // beside it each, control and invisible (npmReadabilityLabels). Prose is
87
+ // holds and its letters out of place (`controls`), found in one pass, which
88
+ // count as a label of their own beside it each, control, invisible and
89
+ // mixed-script (npmReadabilityLabels). Prose is
88
90
  // readable whatever its lines' lengths, and an inline source map's line
89
91
  // counts for none, nor for how much of the code is on long lines. With
90
92
  // npmTextEncoding's `kind` and `controls`, its `longest` line's length and
@@ -135,8 +137,8 @@ export function npmFileReadability(path, text, script = false) {
135
137
  export const npmTaggedCategory = readability => readability.category === 'long' ? readability.kind : readability.category
136
138
 
137
139
  // What a file is labelled, in its tags and in the Overview's warning and
138
- // chips, each finding it: its control characters, its invisible ones
139
- // (characterLabels) and its long lines, where it holds them, beside its category (how its text is encoded, for one whose long
140
+ // chips, each finding it: its control characters, its invisible ones, its
141
+ // letters out of place (characterLabels) and its long lines, where it holds them, beside its category (how its text is encoded, for one whose long
140
142
  // lines are unexpected), but for ASCII or UTF-8, which a file that can't be
141
143
  // read as it is counts under as none of them. A minified file's long lines
142
144
  // are labelled too, as its inline map's are; prose's and a source map's
@@ -161,6 +163,7 @@ export const READABILITY = new Map([
161
163
  ['binary', { name: 'Binary', tag: 'Binary', mark: 'warn' }],
162
164
  ['controls', { name: 'Control chars', tag: 'Controls', mark: 'warn' }],
163
165
  ['invisible', { name: 'Invisible chars', tag: 'Invisible', mark: 'warn' }],
166
+ ['mixed-script', { name: 'Mixed scripts', tag: 'Look-alikes', mark: 'warn' }],
164
167
  ['long', { name: 'Long lines', tag: 'Long lines', mark: 'warn' }],
165
168
  ['minified', { name: 'Minified', tag: 'Minified', short: 'Min', mark: 'notice' }],
166
169
  ['map', { name: 'Source maps', tag: 'Map', mark: 'notice' }],
@@ -170,11 +173,13 @@ export const READABILITY = new Map([
170
173
  ])
171
174
 
172
175
  // The characters a text holds of a label's kind, `controls` (C0, DEL, C1
173
- // and bidirectional controls) or `invisible`, `[point, times]`, the most
174
- // found first: those npmTextEncoding found in one pass, told apart by code
175
- // point. The labels a text's characters take, of those two.
176
+ // and bidirectional controls), `invisible` or `mixed-script` (letters out of
177
+ // place), `[point, times]`, the most found first: those npmTextEncoding found
178
+ // in one pass, told apart by code point. The labels a text's characters
179
+ // take, of those three.
176
180
  const CONTROL = new RegExp(CONTROLS.source, 'u')
181
+ const labelOf = char => CONTROL.test(char) ? 'controls' : INVISIBLE.test(char) ? 'invisible' : 'mixed-script'
177
182
  export const charactersOf = (controls, label) => [...controls ?? []]
178
- .filter(([point]) => CONTROL.test(String.fromCodePoint(point)) === (label === 'controls')).toSorted((a, b) => b[1] - a[1] || a[0] - b[0])
179
- const characterLabels = controls => controls ? ['controls', 'invisible'].filter(label => charactersOf(controls, label).length > 0) : []
183
+ .filter(([point]) => labelOf(String.fromCodePoint(point)) === label).toSorted((a, b) => b[1] - a[1] || a[0] - b[0])
184
+ const characterLabels = controls => controls ? ['controls', 'invisible', 'mixed-script'].filter(label => charactersOf(controls, label).length > 0) : []
180
185
 
@@ -19,9 +19,7 @@
19
19
  // `import … from 'node:child_process'`); a declaration file (.d.ts) read as
20
20
  // TypeScript, as node runs it (stripping its types, `declare function
21
21
  // eval(…)` among them, a function named, not called). A call written not to
22
- // read as one (`window['ev' + 'al']`) goes unseen, as does one in a template
23
- // literal in another's substitution (`${`${eval(x)}`}`), whose backtick ends
24
- // the outer one as the regular expression telling strings reads it.
22
+ // read as one (`window['ev' + 'al']`) goes unseen.
25
23
  //
26
24
  // Labels to assist a reviewer, making the plain cases less likely to be
27
25
  // missed, not a check to rely on: code can come to eval, Function,
@@ -30,7 +28,7 @@
30
28
  // required by a name computed, a path or URL put together, the
31
29
  // registry's host read from config), so a file without a label is not one
32
30
  // without them.
33
- import { NPM_MAX_FOUND, jsMatchAll, lineOf, npmIsScript } from './script.js'
31
+ import { NPM_MAX_FOUND, jsMatchAll, jsOffset, lineOf, npmIsScript } from './script.js'
34
32
 
35
33
  // What may come between a call's parts: blanks and comments
36
34
  // (`Function/* note */(…)`), each comment ending at its first `*/`.
@@ -56,10 +54,12 @@ const ENV = String.raw`process${GAP}\.${GAP}env(?![\p{ID_Continue}$])(?!${GAP}(?
56
54
  // nor one variable asked for (`'HOME' in process.env`), nor its entries or
57
55
  // values picked among as they are read (`Object.entries(process.env)
58
56
  // .reduce(…)`, as npm's agent finds its proxy's, `.filter(…)`, `.find(…)`).
57
+ // Matched to its `(`, the rest looked ahead on, so a copy's `{` is one
58
+ // jsMatchAll reads.
59
59
  const PICKED = String.raw`${GAP}\)${GAP}\??\.${GAP}(?:filter|find|findLast|some|every|reduce)(?![\p{ID_Continue}$])`
60
60
  const COPY = String.raw`(?:\{${GAP}\.\.\.${GAP})?`
61
- const ENV_WHOLE = String.raw`(?=JSON|Object|URLSearchParams)(?<![\p{ID_Continue}$.])(?:JSON${GAP}\.${GAP}stringify|URLSearchParams)${GAP}\(${GAP}${COPY}${ENV}`
62
- + String.raw`|(?=Object)(?<![\p{ID_Continue}$.])Object${GAP}\.${GAP}(?:values|entries)${GAP}\(${GAP}${COPY}${ENV}(?!${PICKED})`
61
+ const ENV_WHOLE = String.raw`(?=JSON|Object|URLSearchParams)(?<![\p{ID_Continue}$.])(?:JSON${GAP}\.${GAP}stringify|URLSearchParams)${GAP}\((?=${GAP}${COPY}${ENV})`
62
+ + String.raw`|(?=Object)(?<![\p{ID_Continue}$.])Object${GAP}\.${GAP}(?:values|entries)${GAP}\((?=${GAP}${COPY}${ENV}(?!${PICKED}))`
63
63
  // What a shell command, and a place code runs from again later, may say
64
64
  // (SHELL, PERSISTENCE), and what of the credentials is told in any case:
65
65
  // trufflehog (TRUFFLEHOG), GCP's metadata host, and a string naming gh
@@ -427,7 +427,7 @@ export function npmRunsCode(path, text, nodeRuns = false) {
427
427
  if (!offsets) found.set(kind, [offset])
428
428
  else if (offsets.length < NPM_MAX_FOUND) offsets.push(offset)
429
429
  }
430
- if (script) read(text, 0, at)
430
+ if (script) read(text, at)
431
431
  if (shell && SAYS_ANY.test(text)) {
432
432
  // Its comments blanked, its lines where they were.
433
433
  const comments = BATCH.test(path) ? BATCH_COMMENT : POWERSHELL.test(path) || POWERSHELL_HASHBANG.test(text) ? POWERSHELL_COMMENT : SHELL_COMMENT
@@ -443,31 +443,36 @@ export function npmRunsCode(path, text, nodeRuns = false) {
443
443
  return new Map([...RUNS_CODE.keys()].filter(kind => found.has(kind)).map(kind => [kind, [...new Set(found.get(kind).map(line))].toSorted((a, b) => a - b)]))
444
444
  }
445
445
 
446
- // Where code (a file's, or a template literal's substitution's, at `base` in
447
- // the file) does a kind, as at(kind, offset), in order: its code's, what its
448
- // strings hold, a string naming a module where it is loaded, and what a
449
- // template literal's substitutions (`${eval(x)}`) do, code again, read as its
450
- // own; a PUT request (`put`) and the registry's host (`registry`) as their
446
+ // Where a file's code does a kind, as at(kind, offset), in order: its code's,
447
+ // a template literal's substitutions' (`${eval(x)}`) among it, what its
448
+ // strings and its templates' text hold, a string naming a module where it is
449
+ // loaded; a PUT request (`put`) and the registry's host (`registry`) as their
451
450
  // own, for the file to pair. Most code says none of it at all, told before
452
451
  // anything is read.
453
- function read(code, base, at) {
452
+ function read(code, at) {
454
453
  if (!SAYS_ANY.test(code)) return
455
454
  // The string before, and where, a place's part maybe; the command line
456
455
  // since a program's name, and where it is named; where the string before
457
456
  // ends; whether the strings since a program's name are its arguments, an
458
457
  // option or its value each, and whether the last is an option.
459
458
  let before = null, command = null, end = 0, option = false, program = false
460
- for (const { 0: token, 1: string, groups, index } of jsMatchAll(code, READS)) {
461
- if (string === undefined) {
462
- for (const kind of CODE_KINDS) if (groups[kind]) at(kind, base + index)
459
+ // Those of the code around a template, kept while what its substitutions
460
+ // hold, which follows it whole (jsMatchAll), is read as code of its own.
461
+ const around = []
462
+ for (const match of jsMatchAll(code, READS)) {
463
+ const { 0: token, 1: text, groups, index } = match
464
+ while (around.length > 0 && index >= around.at(-1).end) ({ before, command, end, option, program } = around.pop())
465
+ if (index < end) {
466
+ around.push({ before, command, end, option, program })
467
+ before = command = null
468
+ option = program = false
469
+ end = index
470
+ }
471
+ if (text === undefined) {
472
+ for (const kind of CODE_KINDS) if (groups[kind]) at(kind, index)
463
473
  continue
464
474
  }
465
- if (string[0] === '/') continue
466
- const inner = string[0] === '`' && SAYS_ANY.test(string) ? substitutions(string) : []
467
- for (const [from, to] of inner) read(string.slice(from, to), base + index + from, at)
468
- // What a string holds, a template's text alone, its substitutions' code
469
- // read as code, its comments and regular expressions aside.
470
- const text = inner.length === 0 ? string : textOf(string, inner)
475
+ if (text[0] === '/') continue
471
476
  // A token's name as the string's whole (`within` 0, its quote) is a key,
472
477
  // not one where its absence is told, it is dropped or only set.
473
478
  const keyed = () => {
@@ -477,20 +482,20 @@ function read(code, base, at) {
477
482
  // Each kind searched for before its places are listed, as matchAll copies
478
483
  // its regular expression each time.
479
484
  const held = HOLDS_ANY.test(text) ? HOLDS.flatMap(([kind, holds]) => text.search(holds) === -1 ? [] : [...text.matchAll(holds)].map(({ index: within }) => [kind, within])) : []
480
- for (const [kind, within] of held) if (kind !== 'tokens' || within > 0 || keyed()) at(kind, base + index + within)
481
- if (text.includes('registry.') && REGISTRY_URL.test(unescaped(text).replaceAll(URL_STRIPPED, ''))) at('registry', base + index)
482
- for (const [kind, name] of MODULES) if (name.test(string) && loaded(code, index)) at(kind, base + index)
483
- if (PROFILE.test(string) && HOME_BEFORE.test(code.slice(Math.max(0, index - 100), index))) at('persistence', base + index)
485
+ for (const [kind, within] of held) if (kind !== 'tokens' || within > 0 || keyed()) at(kind, jsOffset(match, within))
486
+ if (text.includes('registry.') && REGISTRY_URL.test(unescaped(text).replaceAll(URL_STRIPPED, ''))) at('registry', index)
487
+ for (const [kind, name] of MODULES) if (name.test(text) && loaded(code, index)) at(kind, index)
488
+ if (PROFILE.test(text) && HOME_BEFORE.test(code.slice(Math.max(0, index - 100), index))) at('persistence', index)
484
489
  for (const [part, dir, file] of PARTS) {
485
- if (dir.test(string)) at(`${part}-dir`, base + index)
486
- if (file.test(string)) at(`${part}-file`, base + index)
490
+ if (dir.test(text)) at(`${part}-dir`, index)
491
+ if (file.test(text)) at(`${part}-file`, index)
487
492
  }
488
493
  if (program && ARGUMENTS_BETWEEN.test(code.slice(end, index))) {
489
- if (PUBLISH.test(string)) at('publish', base + index)
490
- program = OPTION.test(string) || option
491
- option = OPTION.test(string)
494
+ if (PUBLISH.test(text)) at('publish', index)
495
+ program = OPTION.test(text) || option
496
+ option = OPTION.test(text)
492
497
  } else program = false
493
- if (PROGRAM.test(string)) [program, option] = [true, false]
498
+ if (PROGRAM.test(text)) [program, option] = [true, false]
494
499
  if (command && command.arguments < 20 && VALUES_BETWEEN.test(code.slice(end, index))) {
495
500
  const argument = text.slice(1, -1)
496
501
  command.line += ` ${SEPARATES.test(argument) ? `"${argument.replaceAll('"', "'")}"` : argument}`
@@ -502,14 +507,14 @@ function read(code, base, at) {
502
507
  command = null
503
508
  }
504
509
  } else command = null
505
- if (COMMAND.test(string)) command = { arguments: 0, at: base + index, line: string.slice(1, -1) }
510
+ if (COMMAND.test(text)) command = { arguments: 0, at: index, line: text.slice(1, -1) }
506
511
  if (before && before.text.length + text.length < 200 && ARGUMENTS_BETWEEN.test(code.slice(end, index))) {
507
512
  const [first, second] = [before.text.slice(1, -1), text.slice(1, -1)]
508
513
  if (!PLACE.test(first) && !PLACE.test(second) && PLACE.test(`${first}/${second}`)) at('persistence', before.at)
509
514
  }
510
- before = { text, at: base + index }
511
- if (PUT.test(string) && METHOD.test(code.slice(Math.max(0, index - 300), index))) at('put', base + index)
512
- end = index + token.length
515
+ before = { text, at: index }
516
+ if (PUT.test(text) && METHOD.test(code.slice(Math.max(0, index - 300), index))) at('put', index)
517
+ end = Math.max(end, index + token.length)
513
518
  }
514
519
  }
515
520
 
@@ -522,50 +527,6 @@ function loaded(code, index) {
522
527
  return LOADS.test(before) && !TYPE_ONLY.test(before)
523
528
  }
524
529
 
525
- // A template literal with its substitutions' code blanked, its text in place.
526
- function textOf(template, inner) {
527
- const parts = []
528
- let from = 0
529
- for (const [start, end] of inner) {
530
- parts.push(template.slice(from, start), ' '.repeat(end - start))
531
- from = end
532
- }
533
- parts.push(template.slice(from))
534
- return parts.join('')
535
- }
536
-
537
- // Where each of a template literal's substitutions' code starts and ends, in
538
- // it: from its `${` to the `}` that closes it.
539
- function substitutions(template) {
540
- const found = []
541
- for (let i = template.indexOf('${'); i !== -1; i = template.indexOf('${', i)) {
542
- // Escaped by an odd run of backslashes before it (`\${`, not `\\${`).
543
- let run = 0
544
- while (template[i - 1 - run] === '\\') run++
545
- if (run % 2 === 1) {
546
- i += 2
547
- continue
548
- }
549
- const end = closing(template, i + 2)
550
- found.push([i + 2, end])
551
- i = end
552
- }
553
- return found
554
- }
555
-
556
- // Where the `}` closing code from `from` in a text is, or the text's end for
557
- // none: the first its braces leave unbalanced, those in its strings, regular
558
- // expressions and comments aside (`${/* } */ x}`, `${/}/.test(x)}`), read as
559
- // script.js reads them.
560
- function closing(text, from) {
561
- let depth = 0
562
- for (const { 0: token, index } of jsMatchAll(text, '[{}]', from)) {
563
- if (token === '{') depth++
564
- else if (token === '}' && depth-- === 0) return index
565
- }
566
- return text.length
567
- }
568
-
569
530
  // What `found` holds (npmRunsCode's kinds, hosts.js's services) that
570
531
  // `was` didn't, each with its lines: what is new to a file, as its tags say,
571
532
  // `was` null for one the version compared with didn't hold, or held none in.