fix(automation): TCP automation requires a session token

--http (headless and GUI-hosted) and --serve --port answered anything
that reached the loopback port, and the REST responses allowed every
web origin. Loopback is shared by every process and user on the
machine, so the port proved nothing.

- Every REST request now needs Authorization: Bearer <token>, and every
  --serve --port request line a "token" field; otherwise 401 /
  unauthorized (the socket is closed).
- The token is OCS_API_TOKEN when the launcher sets it, else 32 random
  bytes printed on stderr at startup; compared in constant time.
- No CORS headers are sent, so a web page cannot read an answer.
- stdio --serve and --mcp are unchanged (private to their parent / the
  GUI's own token).

GHSA-c27p-xfx2-9r5x

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Hakan SevenandClaude Opus 5.5 committed 2026-09-30 21:48:20 +03:00
1 parent d0bfa48037
commit 1fec34d84a
7 files changed
+195 -60

No files matched your search

+15 -2
View File
@@ -29,7 +29,8 @@ use super::OpenCADStudio;
/// Run the headless JSON server. Default transport is stdin/stdout; with
/// `--port <N>` it instead listens on `127.0.0.1:<N>` and serves one client at
/// a time (the document session persists across reconnects).
/// a time (the document session persists across reconnects); every socket
/// request carries `"token"` (see `rest::api_token`).
#[cfg(not(target_arch = "wasm32"))]
pub fn serve() {
let mut app = OpenCADStudio::new();
@@ -163,6 +164,7 @@ fn serve_socket_with_idle(
let bound = listener.local_addr().map(|a| a.port()).unwrap_or(port);
bound_port.store(bound, std::sync::atomic::Ordering::SeqCst);
eprintln!("OpenCADStudio --serve listening on 127.0.0.1:{bound}");
crate::rest::announce_token();
for stream in listener.incoming().flatten() {
let _ = stream.set_read_timeout(Some(idle));
let Ok(read_half) = stream.try_clone() else {
@@ -178,7 +180,18 @@ fn serve_socket_with_idle(
if line.is_empty() {
continue;
}
let resp = app.automation_op(line);
// Loopback is shared by every process and user on the machine:
// each request carries the session token, like the GUI's own
// channel, and a connection that fails it is dropped.
let mut request: Value = serde_json::from_str(line).unwrap_or(Value::Null);
if !crate::rest::token_matches(request["token"].as_str()) {
let _ = writeln!(writer, "{}", crate::rest::unauthorized());
break;
}
if let Some(object) = request.as_object_mut() {
object.remove("token");
}
let resp = app.automation_op(&request.to_string());
if writeln!(writer, "{resp}").is_err() {
break;
}