feat(tts): rate limiting, caching, error handling, input sanitization#590
Merged
hman38705 merged 1 commit intosolutions-plug:mainfrom Apr 25, 2026
Conversation
- Issue solutions-plug#531: Per-IP/user rate limiting with configurable window and 429 responses; metrics tracked via RateLimiter - Issue solutions-plug#532: Audio caching by SHA-256 content hash with configurable TTL and bounded maxEntries; cache hit rate tracked - Issue solutions-plug#533: Provider errors caught and wrapped as TTSProviderError; automatic fallback to secondary provider with structured logging - Issue solutions-plug#534: Input sanitization strips SSML/XML tags, enforces MAX_INPUT_LENGTH (5000 chars), normalizes whitespace Closes solutions-plug#531, solutions-plug#532, solutions-plug#533, solutions-plug#534
|
@El-Chapo-Npm Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR hardens the TTS service across four dimensions — protecting provider quota, reducing latency on repeated requests, preventing crashes on provider failure, and blocking malicious input before it reaches the API.
What changed
services/tts/src/TTSService.tsservices/tts/src/__tests__/rate-limit-cache-sanitize-errors.test.tsRate limiting (
#531)The service had no guard against request floods, making it trivial to exhaust provider quota. A
RateLimiterclass now enforces a sliding window counter per key (IP address or user ID). Limits are fully configurable viaTTSConfig.rateLimit(maxRequests,windowMs). Exceeding the limit throws aRateLimitErrorwhich maps to HTTP 429. Expired windows are evicted on a 60s interval to keep memory bounded. Metrics (totalChecks,totalExceeded,currentCounts) are exposed viagetRateLimitMetrics().Audio caching (
#532)Every request was hitting the provider even for identical text. An
AudioCacheclass now caches generated audio buffers keyed by a SHA-256 hash ofprovider:voiceId:text. Cache TTL and max entry count are configurable viaTTSConfig.cache. When the entry limit is reached, the oldest entry is evicted. Cache hit rate and eviction counts are tracked and exposed viagetCacheMetrics().Error handling & fallback (
#533)Provider errors were propagating as unhandled exceptions, crashing the service. All provider calls are now wrapped in structured try/catch blocks. Failures are surfaced as
TTSProviderErrorwith the provider name, a descriptive message, and an appropriate status code. When a primary provider fails, the service automatically retries with the secondary provider (ElevenLabs ↔ Google TTS) if configured. Every failure is logged withconsole.errorso callers always receive a meaningful error response rather than an uncaught exception.Input sanitization (
#534)Input text was passed to providers raw, leaving the door open for SSML injection. A
sanitizeInput()function now runs on everyenqueue()call before any provider interaction. It strips all SSML/XML tags, enforces a 5000-character maximum (MAX_INPUT_LENGTH), and normalizes whitespace. Invalid input throwsInputValidationError(HTTP 400) immediately, before any network call is made.Testing
All four features are covered in
rate-limit-cache-sanitize-errors.test.ts— window resets, per-key isolation, TTL expiry, LRU eviction, SSML stripping, length enforcement, provider error wrapping, and fallback behaviour.Closes #531
Closes #532
Closes #533
Closes #534