forked from 1b-agent/opencadstudio-upstream
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 <[email protected]>
This commit is contained in:
1 parent
d0bfa48037
commit
1fec34d84a
7 files changed
+195
-60
No files matched your search
+15
-2
@@ -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;
|
||||
}
|
||||
|
||||
@@ -8,8 +8,10 @@
|
||||
//! through the same `rest::plan` table as the headless server; requests are
|
||||
//! forwarded into the GUI's automation queue (`Envelope`, exactly like the
|
||||
//! native bridge in `transport`) and the JSON response is written back over
|
||||
//! HTTP. The channel binds loopback only — anything that can reach the port
|
||||
//! can drive the session, exactly like the headless server.
|
||||
//! HTTP. The channel binds loopback only and, like the headless server,
|
||||
//! answers only requests carrying the session's bearer token
|
||||
//! ([`rest::api_token`]): loopback is shared by every process and user on
|
||||
//! the machine, so reaching the port proves nothing.
|
||||
|
||||
use super::{Envelope, Reply};
|
||||
use crate::rest::{self, HttpRequest, Plan, RETRYABLE};
|
||||
@@ -58,6 +60,7 @@ fn listen(
|
||||
let listener = TcpListener::bind(("127.0.0.1", port))?;
|
||||
bound_port.store(listener.local_addr()?.port(), Ordering::SeqCst);
|
||||
eprintln!("OpenCADStudio GUI REST listening on http://127.0.0.1:{port}/api/v1");
|
||||
rest::announce_token();
|
||||
// One thread per connection: a `getpoint` or `user_select` request stays
|
||||
// open until the person answers, and must not block the other reads
|
||||
// (get_selection…).
|
||||
@@ -79,8 +82,8 @@ fn listen(
|
||||
let _ = stream.set_read_timeout(Some(Duration::from_secs(15)));
|
||||
let _ = stream.set_write_timeout(Some(Duration::from_secs(15)));
|
||||
if let Ok(Some(request)) = rest::read_request(&mut stream) {
|
||||
if request.method == "OPTIONS" {
|
||||
let _ = rest::write_response(&mut stream, 204, &Value::Null);
|
||||
if !request.authorized() {
|
||||
let _ = rest::write_response(&mut stream, 401, &rest::unauthorized());
|
||||
} else {
|
||||
let (status, body) = forward(&request, &sender, &document_cache, &stream);
|
||||
let _ = rest::write_response(&mut stream, status, &body);
|
||||
@@ -353,7 +356,8 @@ mod tests {
|
||||
fn post(port: u16, target: &str, body: &str) -> std::net::TcpStream {
|
||||
let mut stream = std::net::TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
let http = format!(
|
||||
"POST {target} HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Length: {}\r\n\r\n{}",
|
||||
"POST {target} HTTP/1.1\r\nHost: 127.0.0.1\r\nAuthorization: Bearer {}\r\nContent-Length: {}\r\n\r\n{}",
|
||||
rest::api_token(),
|
||||
body.len(),
|
||||
body
|
||||
);
|
||||
@@ -447,7 +451,10 @@ mod tests {
|
||||
/// GET without a body — the read half of the REST surface.
|
||||
fn get(port: u16, target: &str) -> std::net::TcpStream {
|
||||
let mut stream = std::net::TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
let http = format!("GET {target} HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n");
|
||||
let http = format!(
|
||||
"GET {target} HTTP/1.1\r\nHost: 127.0.0.1\r\nAuthorization: Bearer {}\r\n\r\n",
|
||||
rest::api_token()
|
||||
);
|
||||
stream.write_all(http.as_bytes()).unwrap();
|
||||
stream
|
||||
}
|
||||
|
||||
+99
-15
@@ -9,7 +9,11 @@
|
||||
//! the identical handlers with identical validation, undo and idempotency.
|
||||
//!
|
||||
//! Conventions: one request per connection (`Connection: close`), JSON
|
||||
//! bodies, permissive CORS for local browser clients, loopback bind only.
|
||||
//! bodies, loopback bind only, and every request carries
|
||||
//! `Authorization: Bearer <token>` (see [`api_token`]). Loopback is shared by
|
||||
//! every process and user on the machine, and a web page can aim requests at
|
||||
//! it too, so the port alone proves nothing; no CORS headers are sent, so a
|
||||
//! browser page cannot read an answer either.
|
||||
//! Connections are served one thread each: a slow op (plot, open a large
|
||||
//! drawing) or a client that connects and stalls must never keep other
|
||||
//! callers — `state`, `get_selection`, `cancel`, `operation` — from being
|
||||
@@ -102,11 +106,62 @@ pub fn serve(port: u16) {
|
||||
listen(OpenCADStudio::new(), port, Arc::new(AtomicU16::new(0)));
|
||||
}
|
||||
|
||||
/// The secret every TCP automation client presents: `OCS_API_TOKEN` when the
|
||||
/// launcher set one (so a script knows it in advance), otherwise 32 random
|
||||
/// bytes printed once on stderr when a listener starts. The GUI's private
|
||||
/// channel keeps its own per-session token in the user-only descriptor.
|
||||
pub fn api_token() -> &'static str {
|
||||
static TOKEN: std::sync::OnceLock<String> = std::sync::OnceLock::new();
|
||||
TOKEN.get_or_init(|| {
|
||||
if let Some(token) = std::env::var("OCS_API_TOKEN")
|
||||
.ok()
|
||||
.filter(|token| !token.trim().is_empty())
|
||||
{
|
||||
return token.trim().to_owned();
|
||||
}
|
||||
let mut secret = [0u8; 32];
|
||||
getrandom::fill(&mut secret).expect("system random source");
|
||||
secret.iter().map(|byte| format!("{byte:02x}")).collect()
|
||||
})
|
||||
}
|
||||
|
||||
/// Tells a listener's user how to authenticate. A token the launcher chose
|
||||
/// is not echoed back.
|
||||
pub(crate) fn announce_token() {
|
||||
if std::env::var("OCS_API_TOKEN").map_or(true, |token| token.trim().is_empty()) {
|
||||
eprintln!("API token (or set OCS_API_TOKEN before launch): {}", api_token());
|
||||
}
|
||||
}
|
||||
|
||||
/// Whether `presented` is the session's token, compared in constant time.
|
||||
pub(crate) fn token_matches(presented: Option<&str>) -> bool {
|
||||
let expected = api_token().as_bytes();
|
||||
let Some(presented) = presented.map(str::as_bytes) else {
|
||||
return false;
|
||||
};
|
||||
presented.len() == expected.len()
|
||||
&& presented
|
||||
.iter()
|
||||
.zip(expected)
|
||||
.fold(0u8, |difference, (a, b)| difference | (a ^ b))
|
||||
== 0
|
||||
}
|
||||
|
||||
/// The answer to a request without the session's token.
|
||||
pub(crate) fn unauthorized() -> Value {
|
||||
json!({
|
||||
"ok": false,
|
||||
"code": "unauthorized",
|
||||
"error": "Send the session token (printed at startup, or OCS_API_TOKEN): Authorization: Bearer <token> over HTTP, \"token\" in each --serve --port request",
|
||||
})
|
||||
}
|
||||
|
||||
pub(crate) struct HttpRequest {
|
||||
pub(crate) method: String,
|
||||
pub(crate) path: String,
|
||||
query: Vec<(String, String)>,
|
||||
body: Vec<u8>,
|
||||
bearer: Option<String>,
|
||||
}
|
||||
|
||||
impl HttpRequest {
|
||||
@@ -120,6 +175,10 @@ impl HttpRequest {
|
||||
pub(crate) fn json(&self) -> Value {
|
||||
serde_json::from_slice(&self.body).unwrap_or(Value::Null)
|
||||
}
|
||||
|
||||
pub(crate) fn authorized(&self) -> bool {
|
||||
token_matches(self.bearer.as_deref())
|
||||
}
|
||||
}
|
||||
|
||||
/// One request handed from a connection thread to the dispatcher that owns
|
||||
@@ -144,6 +203,7 @@ fn listen(mut app: OpenCADStudio, port: u16, bound_port: Arc<AtomicU16>) {
|
||||
let bound = listener.local_addr().map(|a| a.port()).unwrap_or(port);
|
||||
bound_port.store(bound, Ordering::SeqCst);
|
||||
eprintln!("OpenCADStudio REST listening on http://127.0.0.1:{bound}/api/v1");
|
||||
announce_token();
|
||||
let (jobs, incoming) = std::sync::mpsc::channel::<Job>();
|
||||
// Connections are served one thread each: a slow op (plot, open a large
|
||||
// drawing) or a client that connects and stalls must never keep other
|
||||
@@ -185,8 +245,8 @@ fn serve_connection(mut stream: TcpStream, jobs: std::sync::mpsc::Sender<Job>) {
|
||||
let Ok(Some(request)) = read_request(&mut stream) else {
|
||||
return;
|
||||
};
|
||||
if request.method == "OPTIONS" {
|
||||
let _ = write_response(&mut stream, 204, &Value::Null);
|
||||
if !request.authorized() {
|
||||
let _ = write_response(&mut stream, 401, &unauthorized());
|
||||
return;
|
||||
}
|
||||
let (reply, answer) = std::sync::mpsc::channel();
|
||||
@@ -243,6 +303,7 @@ pub(crate) fn read_request(stream: &mut TcpStream) -> std::io::Result<Option<Htt
|
||||
|
||||
let mut content_length = 0usize;
|
||||
let mut header_bytes = 0usize;
|
||||
let mut bearer = None;
|
||||
loop {
|
||||
let mut header = String::new();
|
||||
match read_line_capped(&mut reader, &mut header, MAX_LINE) {
|
||||
@@ -265,6 +326,14 @@ pub(crate) fn read_request(stream: &mut TcpStream) -> std::io::Result<Option<Htt
|
||||
{
|
||||
content_length = value.trim().parse().unwrap_or(0);
|
||||
}
|
||||
if let Some((name, value)) = header.split_once(':') {
|
||||
if name.trim().eq_ignore_ascii_case("authorization") {
|
||||
bearer = value
|
||||
.trim()
|
||||
.strip_prefix("Bearer ")
|
||||
.map(|token| token.trim().to_owned());
|
||||
}
|
||||
}
|
||||
}
|
||||
if content_length > MAX_BODY {
|
||||
return Ok(Some(HttpRequest {
|
||||
@@ -272,6 +341,7 @@ pub(crate) fn read_request(stream: &mut TcpStream) -> std::io::Result<Option<Htt
|
||||
path,
|
||||
query,
|
||||
body: Vec::new(),
|
||||
bearer,
|
||||
}));
|
||||
}
|
||||
let mut body = vec![0u8; content_length];
|
||||
@@ -283,6 +353,7 @@ pub(crate) fn read_request(stream: &mut TcpStream) -> std::io::Result<Option<Htt
|
||||
path,
|
||||
query,
|
||||
body,
|
||||
bearer,
|
||||
}))
|
||||
}
|
||||
|
||||
@@ -328,6 +399,7 @@ pub(crate) fn write_response(stream: &mut TcpStream, status: u16, body: &Value)
|
||||
201 => "Created",
|
||||
204 => "No Content",
|
||||
400 => "Bad Request",
|
||||
401 => "Unauthorized",
|
||||
403 => "Forbidden",
|
||||
404 => "Not Found",
|
||||
405 => "Method Not Allowed",
|
||||
@@ -337,7 +409,7 @@ pub(crate) fn write_response(stream: &mut TcpStream, status: u16, body: &Value)
|
||||
_ => "OK",
|
||||
};
|
||||
let header = format!(
|
||||
"HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nAccess-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS\r\nAccess-Control-Allow-Headers: Content-Type\r\nConnection: close\r\n\r\n",
|
||||
"HTTP/1.1 {status} {reason}\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
|
||||
body.len()
|
||||
);
|
||||
stream.write_all(header.as_bytes())?;
|
||||
@@ -733,6 +805,7 @@ mod tests {
|
||||
path: path.to_string(),
|
||||
query: Vec::new(),
|
||||
body: body.as_bytes().to_vec(),
|
||||
bearer: None,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -763,19 +836,30 @@ mod tests {
|
||||
}
|
||||
assert_ne!(port, 0, "listener never bound a port");
|
||||
|
||||
let status = |authorization: &str| {
|
||||
let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
stream
|
||||
.write_all(
|
||||
format!("GET /api/v1/ready HTTP/1.1\r\nHost: 127.0.0.1\r\n{authorization}\r\n")
|
||||
.as_bytes(),
|
||||
)
|
||||
.unwrap();
|
||||
let mut status_line = String::new();
|
||||
BufReader::new(stream)
|
||||
.read_line(&mut status_line)
|
||||
.expect("readiness answered");
|
||||
status_line
|
||||
};
|
||||
// Stalls the thread that accepted it: no request line, ever.
|
||||
let _stalled = TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
let mut stream = TcpStream::connect(("127.0.0.1", port)).unwrap();
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::from_secs(5)))
|
||||
.unwrap();
|
||||
stream
|
||||
.write_all(b"GET /api/v1/ready HTTP/1.1\r\nHost: 127.0.0.1\r\n\r\n")
|
||||
.unwrap();
|
||||
let mut reader = BufReader::new(stream);
|
||||
let mut status_line = String::new();
|
||||
reader.read_line(&mut status_line).expect("readiness answered");
|
||||
assert!(status_line.contains("200"), "{status_line}");
|
||||
let answered = status(&format!("Authorization: Bearer {}\r\n", api_token()));
|
||||
assert!(answered.contains("200"), "{answered}");
|
||||
// Anything else on the machine that finds the port is turned away.
|
||||
assert!(status("").contains("401"));
|
||||
assert!(status("Authorization: Bearer guess\r\n").contains("401"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
@@ -6,6 +6,7 @@
|
||||
"description": "REST surface over the OpenCAD Studio automation dispatcher. The server runs headless via `OpenCADStudio --http <port>`, binds 127.0.0.1 only, and keeps one drawing session alive across requests. All bodies are JSON; failure bodies carry a symbolic `code` (see the automation README for the catalogue)."
|
||||
},
|
||||
"servers": [{"url": "http://127.0.0.1:8090"}],
|
||||
"security": [{"bearer": []}],
|
||||
"paths": {
|
||||
"/api/v1/ready": {
|
||||
"get": {
|
||||
@@ -255,5 +256,10 @@
|
||||
"responses": {"200": {"description": "Operation response"}}
|
||||
}
|
||||
}
|
||||
},
|
||||
"components": {
|
||||
"securitySchemes": {
|
||||
"bearer": {"type": "http", "scheme": "bearer", "description": "OCS_API_TOKEN, or the token printed on stderr at startup."}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user