Self-hosted, sharable kanban for humans and agents. One 4.7 MB binary, 5 MB of RAM, 10 to 60 tokens per agent reply, starts in 3 ms, zero setup.
Rust ai-agentsclaude-codeclicodexcoding-agentskanbanlocal-firstmcpmcp-servermodel-context-protocolproductivityproject-managementraspberry-pirustself-hostedsqlitetask-manager
Add a listen setting to open the board on the network
emanuele-em9e3436af33c1
diff --git a/src/home.rs b/src/home.rsindex 400c142..213ebf2 100644--- a/src/home.rs+++ b/src/home.rs@@ -1,6 +1,7 @@//! The data folder: where it is, what it holds, and the settings in `config.toml`.use std::fs::{self, File, OpenOptions, TryLockError};+use std::net::{IpAddr, Ipv4Addr};use std::path::{Path, PathBuf};use std::time::{Duration, Instant};@@ -16,6 +17,12 @@ const CONFIG_TEMPLATE: &str = r#"# Kanzo settings.# Port for the board at http://localhost:<port>.port = 4242+# Address the board listens on. 127.0.0.1 keeps it on this machine. Use+# "0.0.0.0" to open it from other devices, for example when Kanzo runs on a+# Raspberry Pi with no screen. There are no logins: anyone who can reach the+# port can read and change the board, and use its MCP endpoint.+listen = "127.0.0.1"+# Board columns, left to right. New tasks go in the first one;# tasks in the last one count as done.statuses = ["todo", "doing", "review", "done"]@@ -45,6 +52,7 @@ const LOCK_WAIT: Duration = Duration::from_secs(10);#[serde(default, deny_unknown_fields)]pub struct Config {pub port: u16,+ pub listen: IpAddr,pub statuses: Vec<String>,pub mcp_http: bool,}@@ -53,6 +61,7 @@ impl Default for Config {fn default() -> Self {Self {port: DEFAULT_PORT,+ listen: IpAddr::V4(Ipv4Addr::LOCALHOST),statuses: ["todo", "doing", "review", "done"].map(String::from).to_vec(),mcp_http: true,}@@ -195,6 +204,7 @@ mod tests {assert_eq!(home.config.port, DEFAULT_PORT);assert_eq!(home.config.statuses, ["todo", "doing", "review", "done"]);assert!(home.config.mcp_http);+ assert!(home.config.listen.is_loopback());}#[test]diff --git a/src/mcp.rs b/src/mcp.rsindex a95bb8b..eb03513 100644--- a/src/mcp.rs+++ b/src/mcp.rs@@ -39,17 +39,12 @@ pub async fn serve(home: Home) -> Result<()> {Ok(())}-/// MCP over HTTP, mounted at `/mcp` by the board. Only browsers on the board's own origin may call it.-pub fn http_service(- home: Home,- port: u16,- shutdown: CancellationToken,-) -> StreamableHttpService<Kanzo, LocalSessionManager> {+/// MCP over HTTP, mounted at `/mcp` by the board. The board's own Host and Origin+/// checks (`web::protect`) guard it, including when it listens on the network, so+/// rmcp's localhost-only list is turned off here.+pub fn http_service(home: Home, shutdown: CancellationToken) -> StreamableHttpService<Kanzo, LocalSessionManager> {let ctx = Arc::new(Ctx { home, cwd: None, repos: Vec::new() });- let config = StreamableHttpServerConfig::default()- .with_allowed_hosts(["localhost", "127.0.0.1"])- .with_allowed_origins([format!("http://localhost:{port}"), format!("http://127.0.0.1:{port}")])- .with_cancellation_token(shutdown);+ let config = StreamableHttpServerConfig::default().disable_allowed_hosts().with_cancellation_token(shutdown);StreamableHttpService::new(move || Ok(Kanzo { ctx: ctx.clone(), tool_router: Kanzo::tool_router() }),Default::default(),diff --git a/src/web.rs b/src/web.rsindex c5edbee..3e95d98 100644--- a/src/web.rs+++ b/src/web.rs@@ -1,11 +1,11 @@//! `kanzo`: the board page, the small JSON API behind it, and MCP over HTTP at `/mcp`.-use std::net::SocketAddr;+use std::net::{IpAddr, SocketAddr};use std::sync::Arc;use anyhow::{Context, Result, bail};use axum::extract::{Path, Query, Request, State};-use axum::http::{StatusCode, header};+use axum::http::{HeaderValue, StatusCode, header};use axum::middleware::{self, Next};use axum::response::{Html, IntoResponse, Response};use axum::routing::{get, post};@@ -22,8 +22,9 @@ const INDEX: &str = include_str!("index.html");pub async fn serve(home: Home, port: Option<u16>, open: bool) -> Result<()> {let port = port.unwrap_or(home.config.port);+ let listen = home.config.listen;let url = format!("http://localhost:{port}");- let listener = match tokio::net::TcpListener::bind(SocketAddr::from(([127, 0, 0, 1], port))).await {+ let listener = match tokio::net::TcpListener::bind(SocketAddr::new(listen, port)).await {Ok(l) => l,Err(e) if e.kind() == std::io::ErrorKind::AddrInUse => {if is_kanzo(port).await {@@ -35,9 +36,16 @@ pub async fn serve(home: Home, port: Option<u16>, open: bool) -> Result<()> {}bail!("port {port} is used by another program; choose another with --port or in config.toml");}- Err(e) => return Err(e).context(format!("cannot listen on port {port}")),+ Err(e) => return Err(e).context(format!("cannot listen on {listen} port {port}")),};+ let guard = Guard::new(port, !listen.is_loopback());println!("Kanzo board at {url}, data in {}", home.dir.display());+ if guard.lan {+ if let Some(name) = guard.names.iter().find(|n| n.ends_with(".local")) {+ println!("On your network: http://{name}:{port} (or this machine's IP address)");+ }+ println!("Anyone who can reach port {port} can read and change this board.");+ }if home.config.mcp_http {println!("MCP over HTTP at {url}/mcp");}@@ -46,7 +54,7 @@ pub async fn serve(home: Home, port: Option<u16>, open: bool) -> Result<()> {}// Cancelling this also ends open MCP streams, which would otherwise hold up shutdown.let shutdown = CancellationToken::new();- axum::serve(listener, router(home, port, shutdown.clone()))+ axum::serve(listener, router(home, guard, shutdown.clone())).with_graceful_shutdown(async move {let _ = tokio::signal::ctrl_c().await;shutdown.cancel();@@ -55,10 +63,10 @@ pub async fn serve(home: Home, port: Option<u16>, open: bool) -> Result<()> {Ok(())}-fn router(home: Home, port: u16, shutdown: CancellationToken) -> Router {+fn router(home: Home, guard: Guard, shutdown: CancellationToken) -> Router {let mut app = Router::new();if home.config.mcp_http {- app = app.nest_service("/mcp", crate::mcp::http_service(home.clone(), port, shutdown));+ app = app.nest_service("/mcp", crate::mcp::http_service(home.clone(), shutdown));}app.route("/", get(|| async { Html(INDEX) })).route("/api/ping", get(|| async { "kanzo" }))@@ -69,24 +77,75 @@ fn router(home: Home, port: u16, shutdown: CancellationToken) -> Router {.route("/api/tasks/{id}", get(get_task).patch(update_task).delete(delete_task)).route("/api/tasks/{id}/move", post(move_task)).route("/api/tasks/{id}/comments", post(add_comment))- .layer(middleware::from_fn_with_state(port, local_only))+ .layer(middleware::from_fn_with_state(Arc::new(guard), protect)).with_state(Arc::new(home))}-/// Rejects requests whose Host is not this machine, so a web page on another-/// domain cannot reach the board through DNS rebinding, and requests that a-/// browser sends from any page other than the board itself.-async fn local_only(State(port): State<u16>, req: Request, next: Next) -> Response {+/// Which Host headers the board answers to.+///+/// DNS rebinding needs a domain name the attacker controls, so the board only+/// accepts `localhost`, IP addresses and, when it listens on the network, this+/// machine's own names. It never accepts other domain names.+struct Guard {+ port: u16,+ lan: bool,+ /// This machine's names, like `pi` and `pi.local`; filled only when `lan`.+ names: Vec<String>,+}++impl Guard {+ fn new(port: u16, lan: bool) -> Self {+ let mut names = Vec::new();+ if lan {+ let run = |cmd: &str, args: &[&str]| {+ std::process::Command::new(cmd)+ .args(args)+ .output()+ .ok()+ .filter(|o| o.status.success())+ .map(|o| String::from_utf8_lossy(&o.stdout).trim().to_ascii_lowercase())+ };+ // macOS keeps its Bonjour name apart; `hostname` there can be a DHCP address.+ let found = [run("scutil", &["--get", "LocalHostName"]), run("hostname", &[])];+ for full in found.into_iter().flatten().filter(|n| !n.is_empty() && n.parse::<IpAddr>().is_err()) {+ let short = full.split('.').next().unwrap_or_default().to_string();+ for name in [format!("{short}.local"), full, short] {+ if !names.contains(&name) {+ names.push(name);+ }+ }+ }+ }+ Self { port, lan, names }+ }++ fn allows(&self, host: &str) -> bool {+ let (name, port) = match host.rsplit_once(':') {+ Some((n, p)) if !p.contains(']') => (n, Some(p)),+ _ => (host, None),+ };+ if port.is_some_and(|p| p.parse() != Ok(self.port)) {+ return false;+ }+ let name = name.trim_start_matches('[').trim_end_matches(']').trim_end_matches('.').to_ascii_lowercase();+ let ip = name.parse::<IpAddr>().ok();+ if name == "localhost" || ip.is_some_and(|ip| ip.is_loopback()) {+ return true;+ }+ self.lan && (ip.is_some() || self.names.contains(&name))+ }+}++/// Refuses requests for a Host this board does not answer to, and browser+/// requests sent from any page other than the board itself.+async fn protect(State(guard): State<Arc<Guard>>, req: Request, next: Next) -> Response {let ok = {- let header = |name| req.headers().get(name).and_then(|h: &header::HeaderValue| h.to_str().ok());+ let header = |name| req.headers().get(name).and_then(|h: &HeaderValue| h.to_str().ok());let host = header(header::HOST).unwrap_or("");- let host_ok = ["localhost", "127.0.0.1"].iter().any(|h| host == *h || host == format!("{h}:{port}"));- let origin_ok = header(header::ORIGIN)- .is_none_or(|o| o == format!("http://localhost:{port}") || o == format!("http://127.0.0.1:{port}"));- host_ok && origin_ok+ guard.allows(host) && header(header::ORIGIN).is_none_or(|o| o.eq_ignore_ascii_case(&format!("http://{host}")))};if !ok {- return (StatusCode::FORBIDDEN, "kanzo only answers its own board on localhost").into_response();+ return (StatusCode::FORBIDDEN, "kanzo does not answer requests from other sites").into_response();}next.run(req).await}@@ -240,3 +299,27 @@ fn open_browser(url: &str) {eprintln!("Open {url} in your browser.");}}++#[cfg(test)]+mod tests {+ use super::*;++ #[test]+ fn hosts() {+ let local = Guard { port: 4242, lan: false, names: vec![] };+ for ok in ["localhost", "localhost:4242", "127.0.0.1:4242", "[::1]:4242", "LOCALHOST:4242"] {+ assert!(local.allows(ok), "{ok}");+ }+ for bad in ["evil.com", "evil.com:4242", "localhost:80", "192.168.1.5:4242", "", "pi.local:4242"] {+ assert!(!local.allows(bad), "{bad}");+ }++ let lan = Guard { port: 4242, lan: true, names: vec!["pi".into(), "pi.local".into()] };+ for ok in ["localhost:4242", "192.168.1.5:4242", "[fe80::1]:4242", "pi.local:4242", "pi:4242", "PI.LOCAL."] {+ assert!(lan.allows(ok), "{ok}");+ }+ for bad in ["evil.com:4242", "pi.evil.com:4242", "192.168.1.5:80"] {+ assert!(!lan.allows(bad), "{bad}");+ }+ }+}