Skip to content

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

15.4 KB
Raw Blame
//! Loopback HTTP listener for OAuth / magic-link callbacks (RFC 8252).
//!
//! Used as the preferred desktop redirect target ahead of the `openhuman://`
//! deep link: the frontend asks the shell to bind a one-shot HTTP server on a
//! fixed loopback port, hands the resulting URL to the backend as
//! `redirectUri`, and waits for the `loopback-oauth-callback` Tauri event.
//!
//! Lifecycle is spawn-on-demand: each call to
//! [`start_loopback_oauth_listener`] supersedes any previously-running
//! listener, binds `127.0.0.1:<port>`, accepts connections until either the
//! state-matching `/auth` request arrives or `timeout_secs` elapses, then
//! shuts the listener down. If bind fails (port already in use), the command
//! returns an error and the caller falls back to the deep-link path.
//!
//! Only the `/auth` path is honored — favicons and stray requests get a
//! 404 and keep the loop alive. The state nonce is generated in the shell
//! and returned to the caller; the backend must echo it back as `state=` on
//! the redirect so a hostile page on the same loopback origin cannot fake a
//! callback.

use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::Mutex;
use std::time::Duration;

use rand::RngCore;
use serde::Serialize;
use tauri::Emitter;

use crate::AppRuntime;
type AppHandle = tauri::AppHandle<AppRuntime>;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use tokio::net::{TcpListener, TcpSocket};
use tokio::sync::oneshot;
use tokio::time::timeout;

const LOOPBACK_CALLBACK_EVENT: &str = "loopback-oauth-callback";
const READ_BUFFER_BYTES: usize = 8 * 1024;
const PER_CONNECTION_READ_TIMEOUT: Duration = Duration::from_secs(5);

struct ActiveListener {
    id: u64,
    tx: oneshot::Sender<()>,
    done: Option<tauri::async_runtime::JoinHandle<()>>,
}

static NEXT_LISTENER_ID: AtomicU64 = AtomicU64::new(1);
static ACTIVE_LISTENER: Mutex<Option<ActiveListener>> = Mutex::new(None);

#[derive(Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct StartResult {
    /// Full redirect URI the backend should redirect to, e.g.
    /// `http://127.0.0.1:53824/auth`. State is appended by the caller.
    /// Serializes as `redirectUri` so the TS-side `result.redirectUri`
    /// destructure works.
    pub redirect_uri: String,
    /// State nonce the backend must echo back as `?state=<value>`.
    pub state: String,
}

#[derive(Serialize, Clone)]
struct CallbackPayload {
    /// Full callback URL including query string. Frontend re-uses the existing
    /// `handleAuthDeepLink` parser by converting it to an `openhuman://` URL.
    url: String,
}

/// Signal the active listener to stop and return its join handle so the caller
/// can await its full teardown — critical when re-binding a fixed port, since
/// macOS releases the socket only after the owning task drops the listener.
fn take_active_listener() -> Option<tauri::async_runtime::JoinHandle<()>> {
    if let Ok(mut guard) = ACTIVE_LISTENER.lock() {
        if let Some(mut active) = guard.take() {
            let _ = active.tx.send(());
            return active.done.take();
        }
    }
    None
}

fn cancel_active_listener() {
    let _ = take_active_listener();
}

fn install_active_listener(
    id: u64,
    tx: oneshot::Sender<()>,
    done: tauri::async_runtime::JoinHandle<()>,
) {
    if let Ok(mut guard) = ACTIVE_LISTENER.lock() {
        if let Some(mut old) = guard.replace(ActiveListener {
            id,
            tx,
            done: Some(done),
        }) {
            let _ = old.tx.send(());
            // The previous listener's join handle is dropped here without an
            // await — only the new-start path needs to await teardown. Stray
            // installs (none today) would simply leak the wait, not break.
            old.done.take();
        }
    }
}

/// Only clear the global slot if it still belongs to this listener's id.
/// A superseded listener's exit must NOT wipe out the newer sender installed
/// by the start that cancelled it.
fn clear_active_listener(id: u64) {
    if let Ok(mut guard) = ACTIVE_LISTENER.lock() {
        if guard.as_ref().map(|active| active.id) == Some(id) {
            *guard = None;
        }
    }
}

/// Bind a loopback TCP listener on the given port (or 0 for ephemeral). Sets
/// SO_REUSEADDR so re-binding the same port soon after a previous listener
/// dropped doesn't trip EADDRINUSE on the TIME_WAIT window.
fn bind_loopback(port: u16) -> Result<TcpListener, String> {
    let sock_addr: std::net::SocketAddr = format!("127.0.0.1:{port}")
        .parse()
        .map_err(|err| format!("parse 127.0.0.1:{port} failed: {err}"))?;
    let socket = TcpSocket::new_v4().map_err(|err| format!("TcpSocket::new_v4 failed: {err}"))?;
    socket
        .set_reuseaddr(true)
        .map_err(|err| format!("set_reuseaddr failed: {err}"))?;
    socket
        .bind(sock_addr)
        .map_err(|err| format!("bind 127.0.0.1:{port} failed: {err}"))?;
    socket
        .listen(16)
        .map_err(|err| format!("listen on 127.0.0.1:{port} failed: {err}"))
}

fn random_state_nonce() -> String {
    let mut bytes = [0u8; 16];
    rand::rng().fill_bytes(&mut bytes);
    hex::encode(bytes)
}

/// Parse the request target (path + query) out of an HTTP/1.x request head.
fn parse_request_target(head: &str) -> Option<&str> {
    let first_line = head.split("\r\n").next()?;
    let mut parts = first_line.split_whitespace();
    let method = parts.next()?;
    let target = parts.next()?;
    if method.eq_ignore_ascii_case("GET") {
        Some(target)
    } else {
        None
    }
}

/// Return the value of `state=` in a query string, if present.
fn extract_state(query: &str) -> Option<&str> {
    query
        .split('&')
        .filter_map(|pair| pair.split_once('='))
        .find(|(k, _)| *k == "state")
        .map(|(_, v)| v)
}

/// Outcome of classifying one HTTP request against the loopback accept loop.
/// Extracted so routing logic can be unit-tested without a live `AppHandle`.
#[derive(Debug, PartialEq)]
enum RequestOutcome {
    /// `/auth` matched and state is valid. Caller should send 200, emit callback.
    AuthCallback { callback_url: String },
    /// `/auth` matched but `state=` was missing or wrong. Caller sends 400.
    StateMismatch,
    /// Path is not `/auth`. Caller sends 404.
    NotFound,
    /// Method is not GET. Caller sends 405.
    MethodNotAllowed,
}

/// Classify one HTTP/1.x request received by the loopback accept loop.
fn classify_request(head: &str, expected_state: &str, bound_port: u16) -> RequestOutcome {
    let target = match parse_request_target(head) {
        Some(t) => t.to_string(),
        None => return RequestOutcome::MethodNotAllowed,
    };

    let (path, query) = match target.split_once('?') {
        Some((p, q)) => (p, q),
        None => (target.as_str(), ""),
    };

    if path != "/auth" {
        return RequestOutcome::NotFound;
    }

    match extract_state(query) {
        Some(s) if s == expected_state => {
            let callback_url = format!("http://127.0.0.1:{bound_port}{target}");
            RequestOutcome::AuthCallback { callback_url }
        }
        _ => RequestOutcome::StateMismatch,
    }
}

const SUCCESS_BODY: &str = "<!doctype html><meta charset=utf-8><title>Signed in</title>\
<body style=\"font-family:system-ui;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;color:#1c1c1e;background:#f5f5f7\">\
<div style=\"text-align:center\"><h2 style=\"margin:0 0 8px\">You're signed in.</h2>\
<p style=\"margin:0;color:#6e6e73\">You can close this tab and return to OpenHuman.</p></div>\
<script>setTimeout(function(){window.close()},250)</script></body>";

const FAILURE_BODY: &str = "<!doctype html><meta charset=utf-8><title>Sign-in not completed</title>\
<body style=\"font-family:system-ui;display:flex;align-items:center;justify-content:center;height:100vh;margin:0;color:#1c1c1e;background:#f5f5f7\">\
<div style=\"text-align:center\"><h2 style=\"margin:0 0 8px\">Sign-in was not completed.</h2>\
<p style=\"margin:0;color:#6e6e73\">No credentials were received. Return to OpenHuman to try again.</p></div></body>";

/// Whether a state-matched callback actually delivered a credential. OpenRouter's
/// Deny redirects back with nothing but the echoed `state=`, and an OAuth error
/// carries `error=`; neither may show the "signed in" page. Only a non-empty
/// `code` (OAuth / PKCE) or `token` (backend login) counts as a credential.
fn callback_carries_credential(query: &str) -> bool {
    let pairs: Vec<(&str, &str)> = query.split('&').filter_map(|p| p.split_once('=')).collect();
    !pairs.iter().any(|(k, _)| *k == "error")
        && pairs
            .iter()
            .any(|(k, v)| matches!(*k, "code" | "token") && !v.is_empty())
}

/// Page served for a state-matched callback: success only when it carried a credential.
fn callback_body(query: &str) -> &'static str {
    if callback_carries_credential(query) {
        SUCCESS_BODY
    } else {
        log::warn!("[loopback-oauth] callback carried no credential (denied or error)");
        FAILURE_BODY
    }
}

fn http_response(status: &str, body: &str) -> Vec<u8> {
    format!(
        "HTTP/1.1 {status}\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: {len}\r\nConnection: close\r\nCache-Control: no-store\r\n\r\n{body}",
        len = body.len(),
    )
    .into_bytes()
}

#[tauri::command]
pub async fn start_loopback_oauth_listener(
    app: AppHandle,
    port: u16,
    timeout_secs: u64,
) -> Result<StartResult, String> {
    // Await the previous listener's task ending so the OS has actually
    // released the fixed loopback port. SO_REUSEADDR alone is not enough on
    // macOS — the prior socket must be dropped first.
    if let Some(done) = take_active_listener() {
        let _ = done.await;
    }

    // Prefer the caller's requested port (so the backend allowlist, if any,
    // matches) but fall back to an ephemeral OS-assigned port if the requested
    // one is taken by another process (stale openhuman, second instance,
    // unrelated service). The backend `redirectUri` whitelist restricts host
    // but not port, so an ephemeral fallback is safe.
    let listener: TcpListener = match bind_loopback(port) {
        Ok(l) => l,
        Err(primary_err) => {
            log::warn!(
                "[loopback-oauth] bind on requested port {port} failed ({primary_err}); retrying on ephemeral port"
            );
            bind_loopback(0).map_err(|err| {
                format!(
                    "bind 127.0.0.1:{port} failed ({primary_err}); ephemeral fallback also failed: {err}"
                )
            })?
        }
    };
    // Use the listener's actual bound port for the emitted callback URL so
    // the frontend rewrite (`^https?://127.0.0.1:\d+/auth`) always matches,
    // even if a future change moves to port 0.
    let bound_port = listener
        .local_addr()
        .map(|addr| addr.port())
        .unwrap_or(port);
    log::info!("[loopback-oauth] listening on 127.0.0.1:{bound_port}");

    let state = random_state_nonce();
    let redirect_uri = format!("http://127.0.0.1:{bound_port}/auth");

    let (cancel_tx, cancel_rx) = oneshot::channel::<()>();
    let listener_id = NEXT_LISTENER_ID.fetch_add(1, Ordering::Relaxed);

    let expected_state = state.clone();
    let done = tauri::async_runtime::spawn(async move {
        let lifetime = Duration::from_secs(timeout_secs.max(1));
        let run = run_accept_loop(listener, app, expected_state, bound_port, cancel_rx);
        match timeout(lifetime, run).await {
            Ok(()) => log::info!("[loopback-oauth] listener finished"),
            Err(_) => log::warn!(
                "[loopback-oauth] listener timed out after {}s",
                lifetime.as_secs()
            ),
        }
        clear_active_listener(listener_id);
    });
    install_active_listener(listener_id, cancel_tx, done);

    Ok(StartResult {
        redirect_uri,
        state,
    })
}

#[tauri::command]
pub async fn stop_loopback_oauth_listener() -> Result<(), String> {
    cancel_active_listener();
    Ok(())
}

async fn run_accept_loop(
    listener: TcpListener,
    app: AppHandle,
    expected_state: String,
    bound_port: u16,
    mut cancel_rx: oneshot::Receiver<()>,
) {
    loop {
        tokio::select! {
            _ = &mut cancel_rx => {
                log::debug!("[loopback-oauth] cancelled by new start or explicit stop");
                return;
            }
            accept = listener.accept() => {
                let (mut socket, peer) = match accept {
                    Ok(pair) => pair,
                    Err(err) => {
                        log::warn!("[loopback-oauth] accept failed: {err}");
                        continue;
                    }
                };
                if !peer.ip().is_loopback() {
                    log::warn!("[loopback-oauth] rejecting non-loopback peer {peer}");
                    let _ = socket.shutdown().await;
                    continue;
                }

                let mut buf = vec![0u8; READ_BUFFER_BYTES];
                let read = match timeout(PER_CONNECTION_READ_TIMEOUT, socket.read(&mut buf)).await {
                    Ok(Ok(n)) => n,
                    Ok(Err(err)) => {
                        log::debug!("[loopback-oauth] read error from {peer}: {err}");
                        continue;
                    }
                    Err(_) => {
                        log::debug!("[loopback-oauth] read timeout from {peer}");
                        continue;
                    }
                };
                if read == 0 {
                    continue;
                }

                let head = String::from_utf8_lossy(&buf[..read]);
                match classify_request(&head, &expected_state, bound_port) {
                    RequestOutcome::MethodNotAllowed => {
                        let _ = socket
                            .write_all(&http_response("405 Method Not Allowed", "method not allowed"))
                            .await;
                    }
                    RequestOutcome::NotFound => {
                        let _ = socket
                            .write_all(&http_response("404 Not Found", "not found"))
                            .await;
                    }
                    RequestOutcome::StateMismatch => {
                        log::warn!(
                            "[loopback-oauth] /auth with missing or mismatched state — ignoring"
                        );
                        let _ = socket
                            .write_all(&http_response("400 Bad Request", "state mismatch"))
                            .await;
                    }
                    RequestOutcome::AuthCallback { callback_url } => {
                        let query = callback_url.split_once('?').map_or("", |(_, q)| q);
                        let body = callback_body(query);
                        // Still emit: the frontend must leave its pending state and
                        // report the failure instead of waiting for the timeout.
                        let _ = socket.write_all(&http_response("200 OK", body)).await;
                        let _ = socket.flush().await;
                        if let Err(err) =
                            app.emit(LOOPBACK_CALLBACK_EVENT, CallbackPayload { url: callback_url })
                        {
                            log::warn!("[loopback-oauth] emit callback event failed: {err}");
                        }
                        return;
                    }
                }
            }
        }
    }
}

#[cfg(test)]
#[path = "loopback_oauth_tests.rs"]
mod tests;