Skip to content

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

fix(http): return 503 for socket.io when realtime is disabled

Steven Enamakela687f659c167
diff --git a/crates/openhuman-rpc/src/server/http/mod.rs b/crates/openhuman-rpc/src/server/http/mod.rs
index 5fd31205d7..6e2f73c373 100644
--- a/crates/openhuman-rpc/src/server/http/mod.rs
+++ b/crates/openhuman-rpc/src/server/http/mod.rs
@@ -98,7 +98,43 @@ pub fn build_core_http_router(socketio_enabled: bool) -> Router {
return router.layer(socket_layer);
}
+ // Socket.IO is off (`--jsonrpc-only`): answer its path before the bearer
+ // middleware can mistake the handshake for an unauthenticated request.
+ // Sits inside CORS so browser clients can read the body.
router
+ .layer(middleware::from_fn(socketio_disabled_middleware))
+ .layer(middleware::from_fn(cors::cors_middleware))
+}
+
+/// Stable machine-readable `error` code of the "Socket.IO is off" response.
+/// Clients (the app's connection test) match on it.
+pub const SOCKETIO_DISABLED_ERROR: &str = "socketio_disabled";
+
+/// Replies `503` with a JSON body naming the cause to any `/socket.io` request.
+///
+/// Only installed when Socket.IO is disabled. Without it the request falls
+/// through to the bearer middleware, which logs and returns a misleading 401
+/// (the Socket.IO handshake carries its token in the `auth` payload, never in
+/// an `Authorization` header) (#5656).
+async fn socketio_disabled_middleware(req: Request, next: Next) -> Response {
+ let path = req.uri().path();
+ if path == "/socket.io" || path.starts_with("/socket.io/") {
+ log::info!(
+ "[http] {} {} -> 503 socketio_disabled (core started with --jsonrpc-only; realtime is off)",
+ req.method(),
+ path
+ );
+ return (
+ StatusCode::SERVICE_UNAVAILABLE,
+ Json(json!({
+ "ok": false,
+ "error": SOCKETIO_DISABLED_ERROR,
+ "message": "Socket.IO (realtime) is disabled on this core. It was started with --jsonrpc-only; restart it without that flag to enable realtime chat and events."
+ })),
+ )
+ .into_response();
+ }
+ next.run(req).await
}
/// Middleware for logging incoming HTTP requests.
@@ -174,3 +210,7 @@ async fn not_found_handler() -> impl IntoResponse {
#[cfg(test)]
#[path = "inference_route_tests.rs"]
mod inference_route_tests;
+
+#[cfg(test)]
+#[path = "socketio_disabled_tests.rs"]
+mod socketio_disabled_tests;
diff --git a/crates/openhuman-rpc/src/server/http/socketio_disabled_tests.rs b/crates/openhuman-rpc/src/server/http/socketio_disabled_tests.rs
new file mode 100644
index 0000000000..c2727d3d30
--- /dev/null
+++ b/crates/openhuman-rpc/src/server/http/socketio_disabled_tests.rs
@@ -0,0 +1,60 @@
+//! `/socket.io` on a core started with Socket.IO disabled (`--jsonrpc-only`)
+//! answers with a specific 503, not the bearer middleware's 401 (#5656).
+
+use std::sync::Once;
+
+use axum::body::Body;
+use axum::http::{Request, StatusCode};
+use tower::ServiceExt;
+
+use super::{build_core_http_router, SOCKETIO_DISABLED_ERROR};
+use crate::core_host::core::auth::CORE_TOKEN_ENV_VAR;
+
+fn ensure_rpc_token() {
+ static INIT: Once = Once::new();
+ INIT.call_once(|| {
+ // SAFETY: test-only init serialized via `Once`.
+ unsafe { std::env::set_var(CORE_TOKEN_ENV_VAR, "socketio-disabled-tests-token") };
+ let tmp = tempfile::tempdir().expect("tempdir");
+ crate::core_host::core::auth::init_rpc_token(tmp.path()).expect("init rpc token");
+ });
+}
+
+async fn get(router: axum::Router, uri: &str) -> (StatusCode, serde_json::Value) {
+ let resp = router
+ .oneshot(Request::get(uri).body(Body::empty()).unwrap())
+ .await
+ .unwrap();
+ let status = resp.status();
+ let bytes = axum::body::to_bytes(resp.into_body(), 1 << 20).await.unwrap();
+ (status, serde_json::from_slice(&bytes).unwrap_or_default())
+}
+
+#[tokio::test]
+async fn disabled_socketio_answers_503_with_cause_not_401() {
+ ensure_rpc_token();
+ for uri in ["/socket.io/?EIO=4&transport=polling", "/socket.io/", "/socket.io"] {
+ let (status, body) = get(build_core_http_router(false), uri).await;
+ assert_eq!(status, StatusCode::SERVICE_UNAVAILABLE, "{uri}");
+ assert_eq!(body["error"], SOCKETIO_DISABLED_ERROR, "{uri}");
+ assert!(
+ body["message"].as_str().unwrap().contains("--jsonrpc-only"),
+ "{uri}"
+ );
+ }
+}
+
+#[tokio::test]
+async fn disabled_socketio_does_not_change_other_routes() {
+ ensure_rpc_token();
+ // A protected route still demands the bearer.
+ let resp = build_core_http_router(false)
+ .oneshot(Request::post("/rpc").body(Body::empty()).unwrap())
+ .await
+ .unwrap();
+ assert_eq!(resp.status(), StatusCode::UNAUTHORIZED);
+ // A look-alike prefix is not treated as Socket.IO.
+ let (status, body) = get(build_core_http_router(false), "/socket.iox").await;
+ assert_ne!(body["error"], SOCKETIO_DISABLED_ERROR);
+ assert_ne!(status, StatusCode::SERVICE_UNAVAILABLE);
+}