Commit 0bc4fb4
authored
fix(security): meter and throttle the deployed-chat TTS relay (#6212)
* fix(security): meter and throttle the deployed-chat TTS relay
POST /api/proxy/tts/stream treated "a live public chat exists" as
authorization to spend the platform ElevenLabs key. A public chat id is
handed to every visitor, so any anonymous caller could synthesize speech
with no length cap, no rate limit and no usage accounting.
Bring the relay in line with its STT sibling (/api/speech/token):
- Resolve the chat's workspace and bill synthesized characters to that
payer via a new `voice-output` usage source, so spend is attributable
and counts against the plan's usage limit (402 once exceeded).
- Throttle per IP before any database work, and per chat afterwards, to
bound both one caller hammering many chats and many callers hammering
one chat.
- Cap `text` at 2000 characters and allowlist `voiceId`/`modelId`, so the
caller can no longer choose an unbounded charge, a premium or cloned
voice, or the billing model.
- Drop `Access-Control-Allow-Origin: *`, which let any third-party page
read the audio; deployed chat and the Office embed are same-origin.
* fix(security): correct TTS metering, pricing and body cap
Follow-up review of the previous commit found five defects in it:
- Usage rows collided. `usage_log.event_key` is unique and inserts are
conflict-do-nothing, and the key is derived from the entry's stable
fields. With no explicit sourceReference, two synthesis calls of equal
character count in the same workspace produced the same key, so every
repeat length went unbilled — defeating the metering this change is
for. Each call now carries a unique sourceReference.
- Priced at $0.10 per 1k characters, twice the published ElevenLabs
Flash/Turbo rate of $0.05, which would have overcharged customers 2x.
- No body cap, so an anonymous caller could make the route buffer up to
the shared 50 MB default before validation. Now 16 KB, as the STT
sibling does.
- Threshold settlement ran per sentence: several queries and a possible
Stripe call on a realtime path. The workflow execution that produced
the text already settles the payer.
- The per-IP bucket was described as preventing database amplification.
getClientIp trusts the leftmost X-Forwarded-For, so an attacker rotates
past it; the comment now says the per-chat bucket is load-bearing.
* refactor(chat): share the deployed-chat auth gate across voice routes
Review of the previous commits surfaced duplication and one more gap:
- The TTS and STT routes had grown near-identical copies of the chat
auth + payer lookup. Extracted to resolveDeployedChatCaller, so the
gate and the payer resolve together and cannot drift per route — that
duplication is how the unmetered TTS path shipped in the first place.
- Neither copy filtered chat.archivedAt, so an archived chat could still
authorize spend against its former owner's workspace. The shared
lookup now filters it, fixing both routes at once. Note: not covered
by a test — the db chain mock does not evaluate WHERE clauses, so an
assertion here could not fail.
- Replaced the route's hand-rolled 429 builder with the existing
enforceIpRateLimit helper, and added enforceChatRateLimit alongside
the per-user/IP/workspace helpers. Gains the standard Retry-After and
X-RateLimit-Reset headers plus throttle logging.
- Dropped a test that asserted a module the route no longer imports was
never called: it could not fail.
- Narrowed the contract: unexported the single-use allowlists and
dropped .passthrough() now that the body is a closed shape.
* fix(security): fail closed when voice-output usage cannot be recorded
Review round 1 findings:
- A ledger write failure previously logged and streamed the audio anyway,
leaving the spend unrecorded and the payer's usage understated. The
caller is anonymous, so serving audio we could not charge for is the
unmetered spend this route exists to prevent — it now returns 500.
- Use generateId() from @sim/utils/id rather than crypto.randomUUID, per
the AGENTS.md ID rule. generateId returns a full UUID v4, so the
per-call uniqueness the usage_log event_key depends on is unchanged.
* fix(chat): split long TTS text so the relay cap cannot drop audio
The client sentence-splits on Western `.!?` only, so text that never
matches — CJK punctuation, or a list with no terminal punctuation —
accumulates and is flushed as one block at the end of the stream. Against
the new 2000-character relay cap that block is rejected and the whole
message plays no audio, a regression introduced by adding the cap.
Split to cap-sized pieces at the single point that enqueues synthesis, so
both the per-sentence path and the end-of-stream flush are covered.
Prefers a whitespace or CJK punctuation boundary, falling back to a hard
cut when a block has none. The server cap stays as the enforcement point.
* fix(security): release the vendor stream when metering rejects the request
The fail-closed branch returned 500 with the ElevenLabs response body
still open, so synthesis and download kept consuming vendor and runtime
resources for a caller that was already rejected. Cancel it before
returning, and assert the cancellation in the test.1 parent 806ea0c commit 0bc4fb4
16 files changed
Lines changed: 19038 additions & 113 deletions
File tree
- apps/sim
- app
- (interfaces)/chat
- [identifier]
- hooks
- api
- proxy/tts/stream
- speech/token
- users/me/usage-logs
- lib
- api/contracts
- media
- billing/core
- chat
- core/rate-limiter
- packages/db
- migrations
- meta
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3 | 3 | | |
4 | 4 | | |
5 | 5 | | |
| 6 | + | |
6 | 7 | | |
7 | 8 | | |
8 | 9 | | |
| |||
49 | 50 | | |
50 | 51 | | |
51 | 52 | | |
52 | | - | |
| 53 | + | |
53 | 54 | | |
54 | 55 | | |
55 | 56 | | |
| |||
Lines changed: 57 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
Lines changed: 42 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| 5 | + | |
5 | 6 | | |
6 | 7 | | |
7 | 8 | | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
8 | 46 | | |
9 | 47 | | |
10 | 48 | | |
| |||
79 | 117 | | |
80 | 118 | | |
81 | 119 | | |
82 | | - | |
| 120 | + | |
83 | 121 | | |
84 | 122 | | |
85 | 123 | | |
| |||
156 | 194 | | |
157 | 195 | | |
158 | 196 | | |
159 | | - | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
160 | 200 | | |
161 | 201 | | |
162 | 202 | | |
| |||
0 commit comments