Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

feat(http): extract dictation WebSocket authorization into its own function

Steven Enamakela81ef9b1919d
diff --git a/crates/openhuman-rpc/src/server/http/dictation.rs b/crates/openhuman-rpc/src/server/http/dictation.rs
index c28cf2435f..0019d0611d 100644
--- a/crates/openhuman-rpc/src/server/http/dictation.rs
+++ b/crates/openhuman-rpc/src/server/http/dictation.rs
@@ -35,6 +35,26 @@ pub(super) async fn dictation_ws_handler(
) -> Response {
log::info!("[ws] dictation WebSocket upgrade requested");
+ if let Err(response) = authorize_dictation_request(&headers, &query) {
+ return response;
+ }
+
+ ws.on_upgrade(|socket| async move {
+ let config = match openhuman_core::config::rpc::load_config_with_timeout().await {
+ Ok(c) => Arc::new(c),
+ Err(e) => {
+ log::error!("[ws] failed to load config for dictation: {e}");
+ return;
+ }
+ };
+ openhuman_core::voice::streaming::handle_dictation_ws(socket, config).await;
+ })
+}
+
+fn authorize_dictation_request(
+ headers: &axum::http::HeaderMap,
+ query: &DictationQuery,
+) -> Result<(), Response> {
// Origin check (same allowlist Socket.IO enforces): native clients send no
// Origin and are accepted; cross-origin browser pages are rejected even if
// they somehow hold the bearer.
@@ -44,7 +64,7 @@ pub(super) async fn dictation_ws_handler(
.map(str::trim);
if !crate::server::socketio::origin_is_allowed(origin) {
log::warn!("[ws] dictation upgrade rejected: disallowed origin {origin:?}");
- return (
+ return Err((
StatusCode::FORBIDDEN,
Json(json!({
"ok": false,
@@ -52,7 +72,7 @@ pub(super) async fn dictation_ws_handler(
"message": "Origin not allowed for the dictation WebSocket."
})),
)
- .into_response();
+ .into_response());
}
// Bearer check: header first, then `?token=` for browser WebSocket clients.
@@ -75,7 +95,7 @@ pub(super) async fn dictation_ws_handler(
.unwrap_or(false);
if !bearer_ok {
log::warn!("[ws] dictation upgrade rejected: missing or invalid bearer token");
- return (
+ return Err((
StatusCode::UNAUTHORIZED,
Json(json!({
"ok": false,
@@ -83,17 +103,12 @@ pub(super) async fn dictation_ws_handler(
"message": "Missing or invalid token. Supply 'Authorization: Bearer <core>' or ?token=<core>."
})),
)
- .into_response();
+ .into_response());
}
- ws.on_upgrade(|socket| async move {
- let config = match openhuman_core::config::rpc::load_config_with_timeout().await {
- Ok(c) => Arc::new(c),
- Err(e) => {
- log::error!("[ws] failed to load config for dictation: {e}");
- return;
- }
- };
- openhuman_core::voice::streaming::handle_dictation_ws(socket, config).await;
- })
+ Ok(())
}
+
+#[cfg(test)]
+#[path = "dictation_tests.rs"]
+mod tests;