telemetry-redact-rule.ts 955 B

1234567891011121314151617181920212223242526272829
  1. import type { Context } from 'cordis'
  2. /**
  3. * Deployment-style redaction rule for the telemetry e2e: scrubs the fixture
  4. * credential from body strings, exactly as a real deployment would mount its
  5. * own rules on the `telemetry/record` waterfall.
  6. */
  7. const SECRET = /sk-e2efixture[0-9]+/g
  8. const PLACEHOLDER = '[E2E-REDACTED]'
  9. function scrub(value: unknown): unknown {
  10. if (typeof value === 'string') return value.replace(SECRET, PLACEHOLDER)
  11. if (Array.isArray(value)) return value.map(scrub)
  12. if (value !== null && typeof value === 'object') {
  13. return Object.fromEntries(Object.entries(value).map(([key, entry]) => [key, scrub(entry)]))
  14. }
  15. return value
  16. }
  17. export const name = 'telemetry-redact-rule'
  18. /** Mount the fixture scrub rule onto the redact waterfall. */
  19. export function apply(ctx: Context): void {
  20. ctx.on('telemetry/record', (_record, next) => {
  21. const record = next()
  22. return { ...record, body: scrub(record.body) }
  23. })
  24. }