#8103: fix(tts): sanitize API keys from error messages
stale
Cluster:
Voice Call and TTS Improvements
## Summary
Add error sanitization to TTS functions to prevent API key leakage in logs and error tracking systems (CWE-532).
## The Problem
When ElevenLabs or OpenAI TTS API calls fail, error messages could potentially contain sensitive information including API keys if the underlying `fetch()` call includes request details in its error context. These errors are then logged or propagated to error tracking services, exposing credentials.
## Changes
- Added `sanitizeTtsError()` function that redacts common API key patterns:
- OpenAI API keys (`sk-...`)
- ElevenLabs API keys (`sk_...`)
- Bearer tokens in Authorization headers
- `xi-api-key` header values
- Wrapped error handling in `elevenLabsTTS()` and `openaiTTS()` to sanitize errors before propagation
- Added comprehensive tests for the sanitization function
## Test Plan
- [x] Added `src/tts/tts.sanitize-error.test.ts` with tests for all redaction patterns
- [x] Verified existing TTS tests still pass
- [x] Verified build succeeds
## Related
- CWE-532: Insertion of Sensitive Information into Log File
- OWASP A09:2021 - Security Logging and Monitoring Failures
---
Internal reference: VULN-155
This PR was generated with the following prompt:
> Sanitize TTS error messages to prevent API key leakage in logs (CWE-532)
🤖 Discovered by [bitsec.ai](https://bitsec.ai)
<!-- greptile_comment -->
<h2>Greptile Overview</h2>
<h3>Greptile Summary</h3>
Adds a `sanitizeTtsError()` helper to redact common credential patterns (OpenAI `sk-...`, ElevenLabs `sk_...`, Bearer tokens, and `xi-api-key` header values) and uses it when propagating errors from `elevenLabsTTS()` and `openaiTTS()`. Also introduces a dedicated Vitest suite to validate redaction behavior.
This fits into the existing TTS provider implementations by ensuring provider-call failures don’t accidentally leak secrets via error messages that are logged by `textToSpeech()`/`textToSpeechTelephony()` or forwarded to error tracking.
<h3>Confidence Score: 4/5</h3>
- This PR is likely safe to merge and improves secret-handling, with a few robustness/debuggability gaps to consider.
- Changes are localized to TTS error handling and include tests, reducing regression risk. Main concerns are loss of original error context when rethrowing and incomplete sanitization if full error objects (stack/cause) are logged elsewhere.
- src/tts/tts.ts (error wrapping/sanitization completeness)
<!-- greptile_other_comments_section -->
<!-- /greptile_comment -->
Most Similar PRs
#8339: fix(tts): validate ElevenLabs base URL against allowlist
by yubrew · 2026-02-03
82.2%
#8317: fix(tts): add dynamic timeout and retry logic for ElevenLabs TTS
by camtang26 · 2026-02-03
78.2%
#16089: fix(tts): clarify directive syntax in prompts and strip malformed tags
by kmixter · 2026-02-14
77.6%
#21110: fix(tts): deliver audio via structured mediaUrl instead of MEDIA: t...
by hydro13 · 2026-02-19
74.8%
#22088: fix(web): sanitize media errors to prevent PII leak
by ashiabbott · 2026-02-20
73.3%
#13318: fix(agents): prevent sanitizeUserFacingText from rewriting conversa...
by hleliofficiel · 2026-02-10
72.8%
#16708: fix(security): OC-17 add token redaction to error formatting, depre...
by aether-ai-agent · 2026-02-15
72.7%
#22086: fix(tts): honor explicit config provider and model/voice settings
by AIflow-Labs · 2026-02-20
72.6%
#3647: fix: sanitize tool arguments in session history
by nhangen · 2026-01-29
72.3%
#18182: fix(security): redact sensitive data in OTEL log exports (CWE-532)
by brandonwise · 2026-02-16
72.1%