@zairakai/js-dev-tools 3.17.1 → 3.18.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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zairakai/js-dev-tools",
3
- "version": "3.17.1",
3
+ "version": "3.18.0",
4
4
  "description": "The unified and centralized development toolkit for JavaScript/TypeScript projects - ESLint, Prettier, Stylelint, Knip, Vitest, Git Hooks, and GitLab CI pipeline templates",
5
5
  "keywords": [
6
6
  "dev-tools",
@@ -10,7 +10,8 @@
10
10
  * - tag: tag main when a runtime range changed since the last tag (see `tag` in .dependency-update.json).
11
11
  * - cascade: after the publication of this package, start the update of the projects that use it.
12
12
  * - audit: look for security advisories, open one issue per advisory and, with AUTO_FIX=true, start an update to fix them
13
- * (UPDATE_URGENT=true: the new versions are taken without waiting, and `npm audit fix` is run).
13
+ * (UPDATE_URGENT=true: the new versions are taken without waiting, `npm audit fix` is run and an override that pins a
14
+ * vulnerable version is raised).
14
15
  * - policy: in the repository dependency-policy, lift the entries whose condition is met (merge request).
15
16
  * - policy-validate: in the repository dependency-policy, check the shape of the files.
16
17
  * - policy-check: fail when the change since a ref updates a package of the policy (GitLab or GitHub, it only reads).
@@ -316,6 +317,35 @@ export function syncDependabot(text, names, ecosystem) {
316
317
  return text.replace(pattern, () => [`${indent}${begin}`, ...rules, `${indent}${end}`].join('\n'))
317
318
  }
318
319
 
320
+ /**
321
+ * The overrides that pin a version inside the vulnerable range of an advisory (`npm audit` never changes an override), with
322
+ * the newest stable version of the same major that is outside of that range. `versionsOf(name)` lists the published versions.
323
+ */
324
+ export function fixedOverrides(overrides, vulnerabilities, versionsOf) {
325
+ const fixes = {}
326
+
327
+ for (const [name, pin] of Object.entries(overrides ?? {})) {
328
+ const pinned = 'string' === typeof pin ? pin : pin?.['.']
329
+ const range = vulnerabilities?.[name]?.range
330
+
331
+ if (null === semver.valid(pinned) || 'string' !== typeof range || null === semver.validRange(range)) continue
332
+ if (!semver.satisfies(pinned, range)) continue
333
+
334
+ const candidates = (versionsOf(name) ?? []).filter(
335
+ (version) =>
336
+ null !== semver.valid(version) &&
337
+ null === semver.prerelease(version) &&
338
+ semver.major(version) === semver.major(pinned) &&
339
+ !semver.satisfies(version, range)
340
+ )
341
+ const best = semver.maxSatisfying(candidates, '*')
342
+
343
+ if (best && semver.gt(best, pinned)) fixes[name] = best
344
+ }
345
+
346
+ return fixes
347
+ }
348
+
319
349
  /**
320
350
  * The packages of a lock file that the refresh may update: the ones of the group are left out, because they are trusted
321
351
  * and not made to wait, and the cooling date would refuse a version that was just published.
@@ -644,6 +674,29 @@ function ncu(args) {
644
674
 
645
675
  // ---------------------------------------------------------------------------------------------------------------------
646
676
 
677
+ /** Raise the overrides that pin a vulnerable version, then install. Returns the raised ones. */
678
+ function raiseVulnerableOverrides() {
679
+ const manifest = JSON.parse(readFileSync('package.json', 'utf8'))
680
+ if (!manifest.overrides) return {}
681
+
682
+ const report = JSON.parse(capture('npm', ['audit', '--json']) || '{}')
683
+ const fixes = fixedOverrides(manifest.overrides, report.vulnerabilities, (name) =>
684
+ JSON.parse(capture('npm', ['view', name, 'versions', '--json']) || '[]')
685
+ )
686
+
687
+ for (const [name, version] of Object.entries(fixes)) {
688
+ if ('string' === typeof manifest.overrides[name]) manifest.overrides[name] = version
689
+ else manifest.overrides[name]['.'] = version
690
+ }
691
+
692
+ if (0 < Object.keys(fixes).length) {
693
+ writeFileSync('package.json', `${JSON.stringify(manifest, null, 2)}\n`)
694
+ run('npm', ['install', '--no-audit', '--no-fund'])
695
+ }
696
+
697
+ return fixes
698
+ }
699
+
647
700
  async function update(config) {
648
701
  assertEnvironment()
649
702
 
@@ -692,6 +745,14 @@ async function update(config) {
692
745
  } catch (error) {
693
746
  log(`npm audit fix did not apply: ${secret(error.message).split('\n')[0]}`)
694
747
  }
748
+
749
+ // npm audit fix leaves the overrides alone: one that pins a vulnerable version is raised here.
750
+ try {
751
+ const raised = raiseVulnerableOverrides()
752
+ for (const [name, version] of Object.entries(raised)) log(`The override of ${name} is raised to ${version}.`)
753
+ } catch (error) {
754
+ log(`The overrides are not raised this time: ${secret(error.message).split('\n')[0]}`)
755
+ }
695
756
  }
696
757
  }
697
758