๐Ÿ“ฆ payloadcms / payload

๐Ÿ“„ audit-dependencies.mjs ยท 635 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635#!/usr/bin/env node

import { execSync } from 'child_process'
import fs from 'fs'
import path from 'path'
import chalk from 'chalk'

// Simple semver check functions - avoid external dependency
function satisfies(version, range) {
  // Handle >=x.y.z format
  if (range.startsWith('>=')) {
    const minVersion = range.substring(2)
    return compareVersions(version, minVersion) >= 0
  }
  // Handle <x.y.z format
  if (range.startsWith('<') && !range.startsWith('<=')) {
    const maxVersion = range.substring(1)
    return compareVersions(version, maxVersion) < 0
  }
  // Handle <=x.y.z format
  if (range.startsWith('<=')) {
    const maxVersion = range.substring(2)
    return compareVersions(version, maxVersion) <= 0
  }
  return true
}

function compareVersions(v1, v2) {
  const parts1 = v1.split('.').map(Number)
  const parts2 = v2.split('.').map(Number)
  for (let i = 0; i < Math.max(parts1.length, parts2.length); i++) {
    const p1 = parts1[i] || 0
    const p2 = parts2[i] || 0
    if (p1 > p2) return 1
    if (p1 < p2) return -1
  }
  return 0
}

function maxSatisfying(range, fixedVersion) {
  // Check if a range like ^5.28.4 could satisfy >=5.28.5
  const fixedVer = fixedVersion.replace('>=', '').replace('>', '')

  if (range.startsWith('^')) {
    const baseVersion = range.substring(1)
    const baseParts = baseVersion.split('.').map(Number)
    const fixedParts = fixedVer.split('.').map(Number)

    // For ^x.y.z, allows >=x.y.z <(x+1).0.0
    // Check if major versions match and fixed version is within range
    if (baseParts[0] === fixedParts[0]) {
      return true // Same major version, caret range can include the fix
    }
  }

  if (range.startsWith('~')) {
    const baseVersion = range.substring(1)
    const baseParts = baseVersion.split('.').map(Number)
    const fixedParts = fixedVer.split('.').map(Number)

    // For ~x.y.z, allows >=x.y.z <x.(y+1).0
    if (baseParts[0] === fixedParts[0] && baseParts[1] === fixedParts[1]) {
      return true // Same major.minor, tilde range can include the fix
    }
  }

  return false
}

const severity = process.argv[2] || 'high'
const outputFile = 'audit_output.json'

console.log(`Auditing for ${severity}+ vulnerabilities...`)

let auditJson
try {
  const auditOutput = execSync('pnpm audit --prod --json', {
    encoding: 'utf-8',
    maxBuffer: 10 * 1024 * 1024, // 10MB buffer
  })
  auditJson = JSON.parse(auditOutput)
} catch (error) {
  // pnpm audit exits with non-zero when vulnerabilities are found
  if (error.stdout) {
    auditJson = JSON.parse(error.stdout)
  } else {
    throw error
  }
}

const severityLevels = ['low', 'moderate', 'high', 'critical']
const minSeverityIndex = severityLevels.indexOf(severity)

const advisories = auditJson.advisories || {}
const vulnerabilities = Object.entries(advisories)
  .filter(([_, advisory]) => {
    const advSeverityIndex = severityLevels.indexOf(advisory.severity)
    return advSeverityIndex >= minSeverityIndex
  })
  .map(([_, advisory]) => {
    const affectedPackages = []
    const firstDependentPaths = []
    const paths = advisory.findings.flatMap((finding) => finding.paths)

    // Find deepest path to check direct dependency
    let deepestPath = ''
    for (const path of paths) {
      const topLevelPkg = path.split(' > ')[0]
      if (topLevelPkg.startsWith('packages/')) {
        if (!affectedPackages.includes(topLevelPkg)) {
          affectedPackages.push(topLevelPkg)
          firstDependentPaths.push(path.split(' > ')[1] || '')
        }
        if (path.split(' > ').length > deepestPath.split(' > ').length) {
          deepestPath = path
        }
      }
    }

    // Walk up the dependency chain to find the minimum fixable dependency
    let directDepVersion = null
    let blockingDep = null // Track which dep blocks the fix

    // If no paths, the vulnerable package itself might be a direct/peer dependency
    if (!deepestPath && paths.length === 0 && advisory.patched_versions !== '<0.0.0') {
      try {
        const latestVersion = execSync(`pnpm view "${advisory.module_name}" version`, {
          encoding: 'utf-8',
          maxBuffer: 10 * 1024 * 1024,
          stdio: ['pipe', 'pipe', 'ignore'],
        }).trim()

        // Check if latest version satisfies the patched version range
        if (satisfies(latestVersion, advisory.patched_versions)) {
          directDepVersion = latestVersion
        }
      } catch (error) {
        // Ignore errors
      }
    } else if (deepestPath && advisory.patched_versions !== '<0.0.0') {
      const parts = deepestPath.split(' > ')
      const vulnerablePkg = advisory.module_name

      // Find the first non-workspace dependency in the chain (the actual direct dep we can update)
      let actualDirectDepIndex = -1
      let actualDirectDepName = null
      for (let i = 1; i < parts.length; i++) {
        const match = parts[i].match(/^(.+?)@(.+)$/)
        if (match && !match[2].startsWith('link:') && !match[2].startsWith('workspace:')) {
          actualDirectDepIndex = i
          actualDirectDepName = match[1]
          break
        }
      }

      // Walk from parent of vulnerable package upward (skip vulnerable package itself)
      for (let i = parts.length - 2; i >= 1; i--) {
        const depStr = parts[i]
        const match = depStr.match(/^(.+?)@(.+)$/)
        if (!match) continue

        const depName = match[1]
        const currentVersion = match[2]

        // Skip workspace packages - can't update them via npm
        if (currentVersion.startsWith('link:') || currentVersion.startsWith('workspace:')) {
          continue
        }

        // Track this as potentially blocking
        if (!blockingDep) {
          blockingDep = `${depName}@${currentVersion}`
        }

        // Get latest version of this dependency
        try {
          const latestVersion = execSync(`pnpm view "${depName}" version`, {
            encoding: 'utf-8',
            maxBuffer: 10 * 1024 * 1024,
            stdio: ['pipe', 'pipe', 'ignore'],
          }).trim()

          // Skip if already on latest
          if (latestVersion === currentVersion) continue

          // Check if latest version of this dep has fixed the vulnerable transitive dep
          const depsOutput = execSync(
            `pnpm view "${depName}@${latestVersion}" dependencies --json`,
            {
              encoding: 'utf-8',
              maxBuffer: 10 * 1024 * 1024,
              stdio: ['pipe', 'pipe', 'ignore'],
            },
          )
          if (!depsOutput.trim()) continue

          const deps = JSON.parse(depsOutput)
          const vulnerableDepVersion = deps[vulnerablePkg]

          if (vulnerableDepVersion) {
            // Check what version would actually be resolved
            // Use pnpm view to get the max satisfying version for the range
            try {
              const viewOutput = execSync(
                `pnpm view "${vulnerablePkg}@${vulnerableDepVersion}" --json 2>/dev/null`,
                {
                  encoding: 'utf-8',
                  maxBuffer: 10 * 1024 * 1024,
                  stdio: ['pipe', 'pipe', 'ignore'],
                },
              ).trim()

              if (!viewOutput) throw new Error('No output')

              const viewData = JSON.parse(viewOutput)
              // If array, take the last (latest) version, otherwise single object
              const resolvedVersion = Array.isArray(viewData)
                ? viewData[viewData.length - 1].version
                : viewData.version

              // Check if the resolved version satisfies the fix
              const fixedVersionRange = advisory.patched_versions
              if (satisfies(resolvedVersion, fixedVersionRange)) {
                // Found a fix! Clear the blocking dep
                blockingDep = null

                // Calculate what version of the direct dependency is needed
                if (i === actualDirectDepIndex) {
                  // fixable dep IS the actual direct dep
                  directDepVersion = latestVersion
                } else {
                  // Need to find what version of actual direct dep includes this fix
                  if (actualDirectDepName) {
                    try {
                      const directDepLatest = execSync(
                        `pnpm view "${actualDirectDepName}" version`,
                        {
                          encoding: 'utf-8',
                          maxBuffer: 10 * 1024 * 1024,
                          stdio: ['pipe', 'pipe', 'ignore'],
                        },
                      ).trim()
                      directDepVersion = directDepLatest
                    } catch (e) {
                      directDepVersion = latestVersion
                    }
                  }
                }
                break
              }
            } catch (resolveError) {
              // If we can't resolve, fall back to range check
              const fixedVersionRange = advisory.patched_versions
              if (maxSatisfying(vulnerableDepVersion, fixedVersionRange)) {
                // Found a fix! Clear the blocking dep
                blockingDep = null

                // Calculate direct dep version
                if (i === actualDirectDepIndex) {
                  directDepVersion = latestVersion
                } else {
                  if (actualDirectDepName) {
                    try {
                      const directDepLatest = execSync(
                        `pnpm view "${actualDirectDepName}" version`,
                        {
                          encoding: 'utf-8',
                          maxBuffer: 10 * 1024 * 1024,
                          stdio: ['pipe', 'pipe', 'ignore'],
                        },
                      ).trim()
                      directDepVersion = directDepLatest
                    } catch (e) {
                      directDepVersion = latestVersion
                    }
                  }
                }
                break
              }
            }
          }
        } catch (error) {
          // Ignore errors, continue walking up
        }
      }
    }

    // Get the direct dep (first non-workspace dependency in the chain)
    let directDep = advisory.module_name
    if (deepestPath) {
      const parts = deepestPath.split(' > ')
      for (let i = 1; i < parts.length; i++) {
        const match = parts[i].match(/^(.+?)@(.+)$/)
        if (match && !match[2].startsWith('link:') && !match[2].startsWith('workspace:')) {
          directDep = match[1]
          break
        }
      }
    }

    const hasDirectUpdate = directDepVersion !== null

    // Build the fix chain showing what the dependency path would look like after the fix
    let fixChain = null
    if (hasDirectUpdate && deepestPath && directDepVersion) {
      try {
        const parts = deepestPath.split(' > ')
        const pkgPath = parts[0]

        // Helper to resolve workspace link to actual version
        const resolveWorkspaceLink = (linkPath) => {
          try {
            // linkPath is like "link:../drizzle" or "workspace:*"
            const relativePath = linkPath.replace(/^(link:|workspace:)/, '').replace(/^\.\.\//, '')
            const packageJsonPath = path.join(
              process.cwd(),
              'packages',
              relativePath,
              'package.json',
            )
            const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
            return packageJson.version
          } catch (e) {
            return null
          }
        }

        // Recursively resolve the same path as the vulnerable one, but with updated versions
        const resolveChain = (parentPkg, parentVersion, targetDepName, depth) => {
          try {
            // Check if parentVersion is a workspace link
            const isLink = parentVersion && parentVersion.startsWith('link:')
            let depsOutput

            if (isLink) {
              // Read from local package.json
              const relativePath = parentVersion
                .replace(/^(link:|workspace:)/, '')
                .replace(/^\.\.\//, '')
              const packageJsonPath = path.join(
                process.cwd(),
                'packages',
                relativePath,
                'package.json',
              )
              const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
              depsOutput = JSON.stringify(packageJson.dependencies || {})
            } else {
              // Read from npm registry
              depsOutput = execSync(
                `pnpm view "${parentPkg}@${parentVersion}" dependencies --json`,
                {
                  encoding: 'utf-8',
                  maxBuffer: 10 * 1024 * 1024,
                  stdio: ['pipe', 'pipe', 'ignore'],
                },
              ).trim()
            }

            if (!depsOutput) return null

            const deps = JSON.parse(depsOutput)
            const depRange = deps[targetDepName]

            if (!depRange) return null

            // Check if this is a workspace link
            if (depRange.startsWith('link:') || depRange.startsWith('workspace:')) {
              const version = resolveWorkspaceLink(depRange)
              return version ? `link:${depRange.replace(/^(link:|workspace:)/, '')}` : null
            }

            // Resolve what version this range would give us
            const viewOutput = execSync(
              `pnpm view "${targetDepName}@${depRange}" --json 2>/dev/null`,
              {
                encoding: 'utf-8',
                maxBuffer: 10 * 1024 * 1024,
                stdio: ['pipe', 'pipe', 'ignore'],
              },
            ).trim()

            if (!viewOutput) return null

            const viewData = JSON.parse(viewOutput)
            const resolvedVersion = Array.isArray(viewData)
              ? viewData[viewData.length - 1].version
              : viewData.version

            return resolvedVersion
          } catch (e) {
            return null
          }
        }

        // Start building chain from package -> direct dep with new version
        const chain = [pkgPath, `${directDep}@${directDepVersion}`]

        // Try to find the shortest path from direct dep to the fixed vulnerable package
        // First check if direct dep directly depends on the vulnerable package
        const directDepDeps = (() => {
          try {
            const output = execSync(
              `pnpm view "${directDep}@${directDepVersion}" dependencies --json`,
              {
                encoding: 'utf-8',
                maxBuffer: 10 * 1024 * 1024,
                stdio: ['pipe', 'pipe', 'ignore'],
              },
            ).trim()
            return output ? JSON.parse(output) : {}
          } catch (e) {
            return {}
          }
        })()

        if (directDepDeps[advisory.module_name]) {
          // Direct path exists
          const resolvedVersion = (() => {
            try {
              const viewOutput = execSync(
                `pnpm view "${advisory.module_name}@${directDepDeps[advisory.module_name]}" --json`,
                {
                  encoding: 'utf-8',
                  maxBuffer: 10 * 1024 * 1024,
                  stdio: ['pipe', 'pipe', 'ignore'],
                },
              ).trim()
              const viewData = JSON.parse(viewOutput)
              return Array.isArray(viewData)
                ? viewData[viewData.length - 1].version
                : viewData.version
            } catch (e) {
              return advisory.patched_versions.replace('>=', '')
            }
          })()
          chain.push(`${advisory.module_name}@${resolvedVersion}`)
        } else {
          // Walk through the original path
          let currentPkg = directDep
          let currentVersion = directDepVersion

          for (let j = 2; j < parts.length; j++) {
            const currentDepName = parts[j].match(/^(.+?)@/)?.[1]
            if (!currentDepName) continue

            const resolvedVersion = resolveChain(currentPkg, currentVersion, currentDepName, j)

            if (resolvedVersion) {
              chain.push(`${currentDepName}@${resolvedVersion}`)
              currentPkg = currentDepName
              currentVersion = resolvedVersion
            } else {
              // Can't resolve, stop here
              break
            }
          }
        }

        fixChain = chain
      } catch (e) {
        // If we can't build the chain, just leave it null
      }
    }

    affectedPackages.sort()

    return {
      package: advisory.module_name,
      title: advisory.title,
      severity: advisory.severity,
      vulnerable: advisory.vulnerable_versions,
      fixedIn: advisory.patched_versions,
      url: advisory.url,
      findings: advisory.findings,
      affectedPackages: affectedPackages,
      firstDependent: firstDependentPaths[0] || advisory.module_name,
      directDep: directDep,
      directDepVersion: directDepVersion,
      hasDirectUpdate: hasDirectUpdate,
      blockingDep: blockingDep,
      fixChain: fixChain,
    }
  })
  .sort((a, b) => {
    const severityOrder = { critical: 0, high: 1, moderate: 2, low: 3 }
    return severityOrder[a.severity] - severityOrder[b.severity]
  })

fs.writeFileSync(outputFile, JSON.stringify(vulnerabilities, null, 2))

if (vulnerabilities.length > 0) {
  console.log(chalk.bold(`\nFound ${vulnerabilities.length} ${severity}+ vulnerabilities:\n`))

  for (const vuln of vulnerabilities) {
    console.log(chalk.bold.cyan(`${vuln.package}`))
    if (vuln.title) {
      console.log(`  ${chalk.dim('Title:')} ${vuln.title}`)
    }
    if (vuln.severity) {
      const severityColors = {
        low: chalk.gray,
        moderate: chalk.yellow,
        high: chalk.red,
        critical: chalk.bgRed.white,
      }
      const colorFn = severityColors[vuln.severity] || chalk.white
      console.log(`  ${chalk.gray('Severity:')} ${colorFn(vuln.severity.toUpperCase())}`)
    }
    console.log(`  ${chalk.gray('Vulnerable:')} ${chalk.red(vuln.vulnerable)}`)
    console.log(`  ${chalk.gray('Fixed in:')} ${chalk.green(vuln.fixedIn)}`)
    console.log(`  ${chalk.gray('Info:')} ${chalk.blue(vuln.url)}`)

    if (vuln.affectedPackages.length > 0) {
      const pkgList = vuln.affectedPackages.join(', ')
      console.log(`  ${chalk.gray('Affects:')} ${pkgList} ${chalk.gray('via')} ${vuln.directDep}`)
    }

    if (vuln.hasDirectUpdate && vuln.directDepVersion) {
      console.log(
        `  ${chalk.gray('Fix:')} Update ${chalk.yellow(vuln.directDep)} to ${chalk.yellow(vuln.directDepVersion)}`,
      )

      // Show the new dependency chain after the fix
      if (vuln.fixChain) {
        console.log(`  ${chalk.gray('Fixed chain:')}`)
        vuln.fixChain.forEach((dep, i) => {
          const indent = '    ' + '  '.repeat(i)
          if (i === 0) {
            console.log(`${indent}${chalk.dim(dep)}`)
          } else {
            const match = dep.match(/^(.+?)@(.+)$/)
            if (match) {
              const [, pkg, version] = match
              // Check if it's a workspace link and resolve to actual version
              let displayVersion = version
              if (version.startsWith('link:') || version.startsWith('workspace:')) {
                try {
                  const relativePath = version
                    .replace(/^(link:|workspace:)/, '')
                    .replace(/^\.\.\//, '')
                  const packageJsonPath = path.join(
                    process.cwd(),
                    'packages',
                    relativePath,
                    'package.json',
                  )
                  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
                  displayVersion = `${packageJson.version} ${chalk.dim('(workspace)')}`
                } catch (e) {
                  displayVersion = version
                }
              }
              const formatted = `${chalk.white(pkg)} ${chalk.dim(displayVersion)}`
              console.log(`${indent}${chalk.dim('โ””โ”€')} ${formatted}`)
            } else {
              console.log(`${indent}${chalk.dim('โ””โ”€')} ${dep}`)
            }
          }
        })
      }
    } else if (!vuln.hasDirectUpdate) {
      if (vuln.blockingDep) {
        console.log(
          `  ${chalk.gray('Fix:')} ${chalk.red('No fix available')} (blocked by ${chalk.red(vuln.blockingDep)})`,
        )
      } else {
        console.log(`  ${chalk.gray('Fix:')} ${chalk.red('No fix available')}`)
      }
    }

    // Show full dependency paths
    const paths = vuln.findings
      .flatMap((finding) => finding.paths)
      .filter((path) => path.split(' > ')[0].startsWith('packages/'))
    if (paths.length > 0) {
      console.log(`  ${chalk.gray('Paths:')}`)
      paths.slice(0, 3).forEach((path) => {
        const parts = path.split(' > ')
        parts.forEach((part, i) => {
          const indent = '    ' + '  '.repeat(i)
          if (i === 0) {
            console.log(`${indent}${chalk.dim(part)}`)
          } else {
            const match = part.match(/^(.+?)@(.+)$/)
            if (match) {
              const [, pkg, version] = match
              let displayVersion = version
              // Check if it's a workspace link
              if (version.startsWith('link:') || version.startsWith('workspace:')) {
                try {
                  const relativePath = version
                    .replace(/^(link:|workspace:)/, '')
                    .replace(/^\.\.\//, '')
                  const packageJsonPath = path.join(
                    process.cwd(),
                    'packages',
                    relativePath,
                    'package.json',
                  )
                  const packageJson = JSON.parse(fs.readFileSync(packageJsonPath, 'utf-8'))
                  displayVersion = `${packageJson.version} ${chalk.dim('(workspace)')}`
                } catch (e) {
                  displayVersion = version
                }
              }
              // Check if this is the blocking dependency
              const isBlocking = vuln.blockingDep === `${pkg}@${version}`
              const pkgColor = isBlocking ? chalk.red : chalk.white
              const formatted = `${pkgColor(pkg)} ${chalk.dim(displayVersion)}`
              console.log(`${indent}${chalk.dim('โ””โ”€')} ${formatted}`)
            } else {
              console.log(`${indent}${chalk.dim('โ””โ”€')} ${part}`)
            }
          }
        })
      })
      if (paths.length > 3) {
        console.log(chalk.dim(`    ... and ${paths.length - 3} more`))
      }
    }

    console.log()
  }

  console.log(chalk.dim(`Output written to ${outputFile}`))
  console.log(
    chalk.dim(`Rerun with: node ./.github/workflows/audit-dependencies.cjs ${severity}\n`),
  )
  process.exit(1)
} else {
  console.log(chalk.green('โœ“ No actionable vulnerabilities'))
  process.exit(0)
}