Skip to content

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

test(rpc): only the core bearer completes the Socket.IO handshake

M3gA-Mind87d5a73b53a9
diff --git a/crates/openhuman-rpc/src/server/socketio_tests.rs b/crates/openhuman-rpc/src/server/socketio_tests.rs
index cd6ae1d5e4..9e5803535a 100644
--- a/crates/openhuman-rpc/src/server/socketio_tests.rs
+++ b/crates/openhuman-rpc/src/server/socketio_tests.rs
@@ -214,3 +214,85 @@ fn origin_allowlist_rejects_unparseable_origin() {
None
));
}
+
+/// Install the per-process RPC bearer once (it is a process-wide `OnceLock`
+/// that sibling tests may already have seeded) and return the live value.
+fn ensure_rpc_token() -> String {
+ // Idempotent: a no-op when a sibling test already seeded the bearer.
+ crate::core_host::core::auth::init_rpc_token_with_value("socketio-handshake-tests-token")
+ .expect("seed rpc bearer");
+ crate::core_host::core::auth::get_rpc_token()
+ .expect("rpc bearer installed")
+ .to_string()
+}
+
+/// Drive one Engine.IO v4 long-polling session: open, send the Socket.IO
+/// CONNECT with `auth`, then poll until the server either emits `ready` or
+/// disconnects the namespace. Returns everything the server sent.
+async fn polling_handshake(base: &str, auth: serde_json::Value) -> String {
+ let http = reqwest::Client::new();
+ let open = http
+ .get(format!("{base}/socket.io/?EIO=4&transport=polling"))
+ .send()
+ .await
+ .expect("engine.io open")
+ .text()
+ .await
+ .unwrap();
+ let open: serde_json::Value =
+ serde_json::from_str(open.trim_start_matches('0')).expect("engine.io open packet");
+ let sid = open["sid"].as_str().expect("engine.io sid").to_owned();
+ let url = format!("{base}/socket.io/?EIO=4&transport=polling&sid={sid}");
+ http.post(&url)
+ .body(format!("40{auth}"))
+ .send()
+ .await
+ .expect("socket.io connect");
+ let mut received = String::new();
+ for _ in 0..5 {
+ let batch = tokio::time::timeout(std::time::Duration::from_secs(5), async {
+ http.get(&url).send().await.ok()?.text().await.ok()
+ })
+ .await
+ .ok()
+ .flatten()
+ .unwrap_or_default();
+ received.push_str(&batch);
+ received.push('\u{1e}');
+ if received.contains("\"ready\"") || received.split('\u{1e}').any(|p| p == "41") {
+ break;
+ }
+ }
+ received
+}
+
+/// H5: the handshake bearer gate, end to end over a real socket. Only the
+/// per-process core bearer gets `ready`; a missing or wrong token is
+/// disconnected before any event reaches the client.
+#[tokio::test]
+async fn only_the_core_bearer_completes_the_socketio_handshake() {
+ let token = ensure_rpc_token();
+ let (layer, _io) = super::attach_socketio();
+ let app = axum::Router::new().layer(layer);
+ let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
+ let base = format!("http://{}", listener.local_addr().unwrap());
+ tokio::spawn(async move { axum::serve(listener, app).await.unwrap() });
+
+ for (auth, label) in [
+ (
+ serde_json::json!({ "token": "not-the-core-token" }),
+ "wrong",
+ ),
+ (serde_json::json!({}), "absent"),
+ ] {
+ let received = polling_handshake(&base, auth).await;
+ assert!(!received.contains("\"ready\""), "{label}: {received}");
+ assert!(
+ received.split('\u{1e}').any(|packet| packet == "41"),
+ "{label} token must be disconnected: {received}"
+ );
+ }
+
+ let received = polling_handshake(&base, serde_json::json!({ "token": token })).await;
+ assert!(received.contains("\"ready\""), "valid token: {received}");
+}