Skip to content

VoIP Media Streams WS 403s on answer: ticket passed via query string, which Twilio doesn't forward (use <Parameter>) #1073

Closed

Description

@vybe

Summary

With #1069 merged (fd6ba602), the REST trigger works and outbound calls now place and ring — but the call drops within ~1s of answer, and Twilio logs error 31920 (Stream – WebSocket – Handshake Error). Root cause: the Media Streams auth ticket is passed in the query string of the <Stream> URL, and Twilio Media Streams does not forward query parameters on the <Stream url> WebSocket. The backend WS handler therefore sees ticket=None, closes before accept(), and the handshake fails with HTTP 403.

VoIP outbound is still unusable end-to-end. Observed on dev HEAD fd6ba602.

Backend log (real call)

services.voip_service: VoIP outbound placed: agent=cornelius-m call_id=voip_u4swU... twilio_sid=CA1e91...
INFO:  172.28.0.6:35086 - "WebSocket /api/voip/voice/voip_u4swU..." 403
INFO:  connection rejected (403 Forbidden)

Root cause

  • services/voip_service.py:104 builds wss://HOST/api/voip/voice/{call_id}?ticket={ticket}.
  • routers/voip.py:225 reads ticket: Optional[str] = Query(default=None).
  • adapters/transports/twilio_media_stream.py:177: info = consume_ticket(ticket) if ticket else None; if not info or info.get("scope") != f"voip:{call_id}": await websocket.close(code=4001, ...).

Closing before accept() surfaces as HTTP 403 during the WS handshake → Twilio 31920 → PSTN leg drops on answer. Twilio requires custom data to be passed via <Parameter> child elements, not the URL query string.

Evidence it's the query string, not the tunnel/proxy

Controlled isolation test on a live instance behind Cloudflare Tunnel:

  1. Minted a throwaway ticket via POST /api/ws/ticket; confirmed Redis EXISTS ws_ticket:<t> = 1.
  2. Opened a WebSocket through the public Cloudflare tunnel to wss://HOST/api/voip/voice/PROBE?ticket=<t> (got 403 — scope mismatch, expected).
  3. Re-checked Redis: EXISTS ws_ticket:<t> = 0 — consume_ticket ran and GETDEL'd it.

→ The proxy/tunnel does forward the WS query string and the backend reads it. The only difference in the failing real-call path is the Twilio client, which doesn't send the query string. (The real call's minted voip ticket stayed unconsumed in Redis until TTL expiry.)

Suggested fix

Pass the ticket as a Media Streams <Parameter> (Twilio forwards these in the start event), keep call_id in the path:

<Response><Connect>
  <Stream url="wss://HOST/api/voip/voice/{call_id}">
    <Parameter name="ticket" value="{ticket}"/>
  </Stream>
</Connect></Response>

In handle_media_stream, accept() first, read start.customParameters.ticket from the first Twilio start frame, validate, and close if invalid (reordering auth to after accept). Optionally keep the query-string read as a fallback for non-Twilio clients. Add an integration test feeding a Twilio-style start event with customParameters.ticket.

Environment

construct @ fd6ba602, VOIP_ENABLED=true, tunnel api/voip/.* confirmed forwarding WS+query, per-agent Twilio binding configured. Everything except this ticket-transport mismatch is in place.


Filed by the Trinity Ops Agent while bringing up VoIP on the construct instance. Follow-on to #1069.

Activity

  1. added
    bug
    priority-p1
    type-bug
    on Jun 4, 2026
  2. vybe commented on Jun 4, 2026

    @vybe
    ContributorAuthor

    /claim

  3. added
    status-in-progress
    on Jun 4, 2026
  4. github-actions commented on Jun 4, 2026

    @github-actions

    Claimed by @vybe. Label status-in-progress added.

    Next steps:

    1. Create branch: feature/1073-<short-slug>
    2. Implement the feature
    3. Open a PR with Fixes #1073
  5. added
    status-in-dev
    and removed
    status-in-progress
    on Jun 4, 2026
  6. vybe commented on Jun 4, 2026

    @vybe
    ContributorAuthor

    Merged to dev via #1075 (squash cfee2754). Awaiting the next release cut (dev → main) to close. Validation: 26 VoIP unit tests pass, sys.modules lint green, security clean.

  7. removed
    status-in-dev
    on Aug 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bugpriority-p1type-bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions