Skip to content

Enterprise agent platform for building, deploying, and operating AI teammates.

fix: expose streaming TTS headers to cross-origin clients (#3807)

Chris Qinf39aa72b5eeb
diff --git a/src/xagent/web/api/speech.py b/src/xagent/web/api/speech.py
index 63f95b66f..8e31f203a 100644
--- a/src/xagent/web/api/speech.py
+++ b/src/xagent/web/api/speech.py
@@ -407,7 +407,6 @@ class _TTSAudioResponse(StreamingResponse):
"Cache-Control": "no-store",
"X-Accel-Buffering": "no",
"X-Audio-Format": first.format,
- "Access-Control-Expose-Headers": "X-Audio-Format, X-Audio-Sample-Rate",
}
if first.sample_rate is not None:
headers["X-Audio-Sample-Rate"] = str(first.sample_rate)
diff --git a/src/xagent/web/app.py b/src/xagent/web/app.py
index a3aa4b917..b1da2273e 100644
--- a/src/xagent/web/app.py
+++ b/src/xagent/web/app.py
@@ -1704,8 +1704,8 @@ app.add_middleware(
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
- # The upload retry loop paces itself by this hint across origins.
- expose_headers=["Retry-After"],
+ # Cross-origin clients read retry hints and streaming audio metadata.
+ expose_headers=["Retry-After", "X-Audio-Format", "X-Audio-Sample-Rate"],
)
app.add_middleware(PublicWidgetCORSMiddleware)
app.add_middleware(SessionMiddleware, secret_key=get_session_secret(), same_site="lax")
diff --git a/tests/web/api/test_speech_streaming.py b/tests/web/api/test_speech_streaming.py
index ba30a586d..03be2f0f6 100644
--- a/tests/web/api/test_speech_streaming.py
+++ b/tests/web/api/test_speech_streaming.py
@@ -17,6 +17,7 @@ from sqlalchemy.orm import sessionmaker
from starlette.websockets import WebSocket, WebSocketDisconnect
from tests.web.auth_token_cases import build_access_token
+from xagent.config import get_cors_origins
from xagent.core.model.asr.base import ASRStreamEvent, ASRStreamSession
from xagent.core.model.chat.token_context import MediaCallType
from xagent.core.model.media_metering import record_provider_usage
@@ -741,9 +742,9 @@ def test_tts_stream_has_audio_metadata_and_exactly_one_usage_delivery(api):
assert response.status_code == 200, response.text
assert response.content == b"firstsecond"
assert response.headers["content-type"] == "audio/pcm"
+ assert response.headers["x-audio-format"] == "pcm"
assert response.headers["x-audio-sample-rate"] == "24000"
assert response.headers["cache-control"] == "no-store"
- assert "X-Audio-Sample-Rate" in response.headers["access-control-expose-headers"]
assert api.tts.calls == [
{"text": "Hello", "voice": "override", "format": "pcm", "sample_rate": 24000}
]
@@ -1028,6 +1029,7 @@ async def test_tts_session_timeout_before_audio_is_a_504(api, monkeypatch):
async def test_tts_completion_through_full_app_stack(api, monkeypatch, caplog, failure):
from xagent.web.app import app
+ origin = get_cors_origins()[0].encode()
if failure == "provider":
api.tts.error_after_first = RuntimeError("synthetic-provider-secret")
elif failure:
@@ -1054,6 +1056,7 @@ async def test_tts_completion_through_full_app_stack(api, monkeypatch, caplog, f
"headers": [
(b"authorization", api.headers["Authorization"].encode()),
(b"content-type", b"application/json"),
+ (b"origin", origin),
],
"server": ("testserver", 80),
"client": ("127.0.0.1", 1234),
@@ -1073,6 +1076,12 @@ async def test_tts_completion_through_full_app_stack(api, monkeypatch, caplog, f
await asyncio.wait_for(app(scope, incoming.get, send), 10)
assert messages[0]["status"] == 200
headers = dict(messages[0]["headers"])
+ assert headers[b"access-control-allow-origin"] == origin
+ exposed_headers = {
+ name.strip().lower()
+ for name in headers[b"access-control-expose-headers"].split(b",")
+ }
+ assert {b"x-audio-format", b"x-audio-sample-rate"} <= exposed_headers
assert headers[b"x-content-type-options"] == b"nosniff"
assert headers[b"x-frame-options"] == b"DENY"
assert any(message.get("body") == b"first" for message in messages)