verify-config-source-ownership.ts 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /**
  2. * Gate for forbidden credential or endpoint environment inlines in shipped
  3. * Cordis configuration.
  4. * @module scripts/verify-config-source-ownership
  5. */
  6. import { globSync, readFileSync } from 'node:fs'
  7. import { resolve, sep } from 'node:path'
  8. const ROOT = resolve(import.meta.dirname, '..')
  9. /** Shipped Cordis configuration these rules apply to. */
  10. const SHIPPED_CONFIG_GLOBS = [
  11. 'apps/*/config/*.yml',
  12. 'examples/*/*.cordis.yml',
  13. 'examples/*/cordis.yml',
  14. // Bundle identity comes from the package manifest, not the domain directory.
  15. 'packages/*/*/cordis.patch.yml',
  16. // The Python runtime ships its own default composition inside the wheel.
  17. 'python/*/src/**/cordis.yml',
  18. ]
  19. /** Ordinary single-line configuration forms this source check rejects; not full YAML analysis. */
  20. const INLINE_DENY = /^\s*(apiKey|baseURL|apiKeyEnv|authToken|headers)\s*:\s*!!js\b/
  21. /** Return every forbidden inline environment form in shipped configuration. */
  22. export function collectConfigSourceOwnershipViolations(root: string): string[] {
  23. const failures: string[] = []
  24. for (const glob of SHIPPED_CONFIG_GLOBS) {
  25. for (const file of globSync(glob, { cwd: root })) {
  26. const rel = file.split(sep).join('/')
  27. readFileSync(resolve(root, rel), 'utf8').split('\n').forEach((line, index) => {
  28. if (!INLINE_DENY.test(line)) return
  29. failures.push(
  30. `${rel}:${String(index + 1)}: inlines a credential or endpoint from the environment.`
  31. + ' The adapter resolves apiKeyEnv through ctx.credentials and the endpoint through the'
  32. + ' environment snapshot; inlining here bypasses both ladders.',
  33. )
  34. })
  35. }
  36. }
  37. return failures
  38. }
  39. if (process.argv[1] && import.meta.filename === resolve(process.argv[1])) {
  40. const failures = collectConfigSourceOwnershipViolations(ROOT)
  41. if (failures.length > 0) {
  42. process.stderr.write('verify-config-source-ownership: configuration source ownership violated:\n')
  43. for (const failure of failures) process.stderr.write(` ${failure}\n`)
  44. process.exit(1)
  45. }
  46. process.stdout.write(
  47. 'verify-config-source-ownership: no credential or endpoint uses the ordinary inline environment form'
  48. + ' in shipped configuration.\n',
  49. )
  50. }