feat: run sandbox transports in disposable workers
The control plane opened ssh connections itself, so a malicious ssh server attacked the process that held the database, the master key, and every room's keys. It now starts one worker per target and workspace. The worker gets one target key on stdin, binds loopback behind a random token, and serves exec, files, the pty, and the preview proxy. It has no database and no master key, and a closed stdin ends it. The preview HTTP client moved into the worker too. The app sees a typed response, and the worker caps bodies, output tails, handshakes, and transport waits. Direct targets now reject loopback, private, link-local, multicast, unspecified, carrier-grade NAT, and documentation addresses when a target is created and again when its worker starts. TOGETHER_ALLOW_PRIVATE_TARGETS=1 lifts the check for a server that runs on one private network.
This commit is contained in:
1 parent
a8f7e74ba0
commit
fc684115b5
16 files changed
+1355
-208
No files matched your search
+111
-6
@@ -4,14 +4,17 @@ use std::sync::atomic::AtomicBool;
|
||||
use std::time::Duration;
|
||||
|
||||
use axum::Json;
|
||||
use axum::extract::ws::WebSocketUpgrade;
|
||||
use axum::extract::ws::{Message as WsMessage, WebSocket, WebSocketUpgrade};
|
||||
use axum::extract::{Path, Query, State};
|
||||
use axum::http::HeaderMap;
|
||||
use axum::http::{HeaderMap, HeaderValue};
|
||||
use axum::response::Response;
|
||||
use futures::{SinkExt, StreamExt};
|
||||
use rhema_sandbox::Sandbox;
|
||||
use rhema_sandbox::exec_util::shquote;
|
||||
use serde::Deserialize;
|
||||
use serde_json::{Value, json};
|
||||
use tokio_tungstenite::tungstenite::Message as TMessage;
|
||||
use tokio_tungstenite::tungstenite::client::IntoClientRequest;
|
||||
|
||||
use crate::auth::AuthUser;
|
||||
use crate::error::{AppError, AppResult};
|
||||
@@ -215,8 +218,8 @@ pub async fn git(
|
||||
})))
|
||||
}
|
||||
|
||||
/// The web terminal upgrade. The ssh connection is opened only after
|
||||
/// the socket is accepted, with the same target and key as the agent.
|
||||
/// The web terminal upgrade. The control plane only proxies bytes: the
|
||||
/// room's worker owns the ssh connection and the pty.
|
||||
pub async fn terminal(
|
||||
user: AuthUser,
|
||||
State(state): State<Shared>,
|
||||
@@ -229,8 +232,110 @@ pub async fn terminal(
|
||||
}
|
||||
member_of(&state, &room, &user.user.id)?;
|
||||
let (workspace, target) = room_workspace(&state, &room)?;
|
||||
let worker = crate::worker::client_for(&state, &workspace, &target)
|
||||
.await
|
||||
.map_err(|err| AppError::new(axum::http::StatusCode::BAD_GATEWAY, err))?;
|
||||
let account = user.user.id.clone();
|
||||
Ok(ws.on_upgrade(move |socket| {
|
||||
crate::ssh::terminal(socket, state, room, account, workspace, target)
|
||||
Ok(ws.on_upgrade(move |socket| async move {
|
||||
proxy_terminal(socket, worker, state, room, account).await;
|
||||
}))
|
||||
}
|
||||
|
||||
async fn proxy_terminal(
|
||||
socket: WebSocket,
|
||||
worker: crate::worker::WorkerClient,
|
||||
state: Shared,
|
||||
room: String,
|
||||
account: String,
|
||||
) {
|
||||
let url = format!("ws://127.0.0.1:{}/pty?cols=100&rows=30", worker.http_port);
|
||||
let Ok(mut request) = url.into_client_request() else {
|
||||
return;
|
||||
};
|
||||
let Ok(token) = HeaderValue::from_str(&worker.token) else {
|
||||
return;
|
||||
};
|
||||
request.headers_mut().insert("x-rhema-token", token);
|
||||
let (worker_socket, _) = match tokio_tungstenite::connect_async(request).await {
|
||||
Ok(pair) => pair,
|
||||
Err(err) => {
|
||||
let (mut sink, _) = socket.split();
|
||||
let _ = sink
|
||||
.send(WsMessage::Text(
|
||||
json!({ "type": "error", "message": format!("the sandbox worker did not answer: {err}") })
|
||||
.to_string()
|
||||
.into(),
|
||||
))
|
||||
.await;
|
||||
return;
|
||||
}
|
||||
};
|
||||
let (mut browser_sink, mut browser_source) = socket.split();
|
||||
let (mut worker_sink, mut worker_source) = worker_socket.split();
|
||||
let mut access = tokio::time::interval(std::time::Duration::from_secs(2));
|
||||
loop {
|
||||
tokio::select! {
|
||||
_ = access.tick() => {
|
||||
if member_of(&state, &room, &account).is_err() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
message = worker_source.next() => {
|
||||
match message {
|
||||
Some(Ok(message)) => {
|
||||
if let Some(message) = from_worker(message)
|
||||
&& browser_sink.send(message).await.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
message = browser_source.next() => {
|
||||
match message {
|
||||
Some(Ok(message)) => {
|
||||
if let Some(message) = to_worker(message)
|
||||
&& worker_sink.send(message).await.is_err()
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
let _ = worker_sink.close().await;
|
||||
}
|
||||
|
||||
fn from_worker(message: TMessage) -> Option<WsMessage> {
|
||||
match message {
|
||||
TMessage::Text(text) => Some(WsMessage::Text(text.as_str().into())),
|
||||
TMessage::Binary(data) => Some(WsMessage::Binary(data)),
|
||||
TMessage::Ping(data) => Some(WsMessage::Ping(data)),
|
||||
TMessage::Pong(data) => Some(WsMessage::Pong(data)),
|
||||
TMessage::Close(frame) => Some(WsMessage::Close(frame.map(|frame| {
|
||||
axum::extract::ws::CloseFrame {
|
||||
code: frame.code.into(),
|
||||
reason: frame.reason.as_str().into(),
|
||||
}
|
||||
}))),
|
||||
TMessage::Frame(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn to_worker(message: WsMessage) -> Option<TMessage> {
|
||||
match message {
|
||||
WsMessage::Text(text) => Some(TMessage::Text(text.as_str().into())),
|
||||
WsMessage::Binary(data) => Some(TMessage::Binary(data)),
|
||||
WsMessage::Ping(data) => Some(TMessage::Ping(data)),
|
||||
WsMessage::Pong(data) => Some(TMessage::Pong(data)),
|
||||
WsMessage::Close(frame) => Some(TMessage::Close(frame.map(|frame| {
|
||||
tokio_tungstenite::tungstenite::protocol::CloseFrame {
|
||||
code: frame.code.into(),
|
||||
reason: frame.reason.as_str().into(),
|
||||
}
|
||||
}))),
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user