verify-config-source-ownership.ts 2.1 KB

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