verify-config-source-ownership.ts 2.1 KB

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