Skip to main content

shell_tunnel/api/
router.rs

1//! API router configuration.
2
3use std::net::SocketAddr;
4use std::sync::Arc;
5
6use axum::{
7    extract::{
8        connect_info::IntoMakeServiceWithConnectInfo, DefaultBodyLimit, MatchedPath, Request, State,
9    },
10    http::{header::AUTHORIZATION, Method, StatusCode},
11    middleware::{self, Next},
12    response::Response,
13    routing::{any, get, post},
14    Router,
15};
16use tower_http::{
17    cors::{Any, CorsLayer},
18    trace::TraceLayer,
19};
20
21use super::handlers::{
22    api_info, create_session, delete_session, execute_command, execute_oneshot, get_session,
23    health, list_sessions, AppState,
24};
25use super::websocket::{ws_handler, ws_oneshot_handler};
26use crate::security::{
27    rate_limit_middleware, ApiKeyStore, AuthConfig, CapabilitySet, RateLimitConfig, RateLimiter,
28};
29
30/// Cross-Origin Resource Sharing (CORS) configuration.
31///
32/// CORS is a browser-enforced mechanism; non-browser clients (`curl`, SDKs)
33/// ignore it entirely. shell-tunnel therefore emits **no** permissive CORS headers
34/// by default: this blocks a malicious web page from reading responses cross-origin
35/// and, because the JSON execute endpoints require a preflight, from issuing the
36/// request at all — with zero impact on the intended non-browser consumers.
37///
38/// Note: CORS does **not** defend against DNS-rebinding (the attacker's host is
39/// rebound to `127.0.0.1`, making the request same-origin); that requires
40/// Host-header validation and is tracked separately.
41#[derive(Debug, Clone, Default)]
42pub struct CorsConfig {
43    /// Allow any origin/method/header (restores the permissive `Any` behavior).
44    /// Off by default; enable only for trusted browser-based UIs.
45    pub allow_any: bool,
46}
47
48/// Security configuration for the server.
49#[derive(Debug, Clone)]
50pub struct SecurityConfig {
51    /// Authentication configuration.
52    pub auth: AuthConfig,
53    /// Rate limiting configuration.
54    pub rate_limit: RateLimitConfig,
55    /// API keys to pre-register.
56    pub api_keys: Vec<String>,
57    /// Capabilities granted to the pre-registered keys and to the
58    /// auto-generated fallback key.
59    ///
60    /// `None` (the default) means **full-control** (wildcard) — the backward-
61    /// compatible behavior for a bare `--api-key` / `--require-auth` (spec §4).
62    /// `Some(set)` issues fine-grained tokens scoped to that set (spec §9).
63    pub capabilities: Option<CapabilitySet>,
64    /// CORS configuration.
65    pub cors: CorsConfig,
66    /// Host names this server answers to, when it is worth checking.
67    ///
68    /// `None` disables the check. It is meant for a loopback-bound server, where
69    /// DNS rebinding is the one attack CORS cannot stop: the attacker's name is
70    /// rebound to `127.0.0.1`, so the browser considers the request same-origin
71    /// and sends it. The `Host` header still carries the attacker's name, which
72    /// is what this compares. A published server is deliberately reachable under
73    /// a name we may not know, so the check does not apply there.
74    pub allowed_hosts: Option<Vec<String>>,
75}
76
77impl Default for SecurityConfig {
78    fn default() -> Self {
79        Self {
80            auth: AuthConfig::disabled(), // Disabled by default for ease of use
81            rate_limit: RateLimitConfig::default(),
82            api_keys: Vec::new(),
83            capabilities: None, // Full-control by default (legacy-compatible)
84            cors: CorsConfig::default(), // Restrictive by default (no permissive CORS)
85            allowed_hosts: None,
86        }
87    }
88}
89
90/// Build a permissive CORS layer when `allow_any` is set, otherwise `None`.
91///
92/// Returning `None` means no CORS headers are emitted — the secure default.
93fn cors_layer(cfg: &CorsConfig) -> Option<CorsLayer> {
94    cfg.allow_any.then(|| {
95        CorsLayer::new()
96            .allow_origin(Any)
97            .allow_methods(Any)
98            .allow_headers(Any)
99    })
100}
101
102impl SecurityConfig {
103    /// Create a secure configuration.
104    pub fn secure() -> Self {
105        Self {
106            auth: AuthConfig::default(),
107            rate_limit: RateLimitConfig::default(),
108            api_keys: Vec::new(),
109            capabilities: None,
110            cors: CorsConfig::default(),
111            allowed_hosts: None,
112        }
113    }
114
115    /// Create a development configuration (no auth, relaxed limits).
116    pub fn development() -> Self {
117        Self {
118            auth: AuthConfig::disabled(),
119            rate_limit: RateLimitConfig::relaxed(),
120            api_keys: Vec::new(),
121            capabilities: None,
122            cors: CorsConfig::default(),
123            allowed_hosts: None,
124        }
125    }
126
127    /// Add an API key.
128    pub fn with_api_key(mut self, key: impl Into<String>) -> Self {
129        self.api_keys.push(key.into());
130        self
131    }
132
133    /// Scope the issued tokens to a fine-grained capability set (spec §9).
134    ///
135    /// Applies to the pre-registered keys and to the auto-generated fallback
136    /// key. Without this, tokens are full-control (legacy-compatible).
137    pub fn with_capabilities(mut self, capabilities: CapabilitySet) -> Self {
138        self.capabilities = Some(capabilities);
139        self
140    }
141
142    /// Answer only to these host names.
143    pub fn with_allowed_hosts(mut self, hosts: Vec<String>) -> Self {
144        self.allowed_hosts = Some(hosts);
145        self
146    }
147
148    /// Enable permissive (`Any`) CORS. Opt-in; only for trusted browser UIs.
149    pub fn with_cors_allow_any(mut self) -> Self {
150        self.cors.allow_any = true;
151        self
152    }
153}
154
155/// Register `key` into `store` with `capabilities` (fine-grained), or as a
156/// legacy full-control key when `capabilities` is `None` (spec §4/§9).
157fn register_key(store: &ApiKeyStore, key: &str, capabilities: &Option<CapabilitySet>) {
158    match capabilities {
159        Some(caps) => store.add_key_with_capabilities(key, caps.clone(), "configured"),
160        None => store.add_key(key),
161    }
162}
163
164/// Create the API router with all routes configured.
165pub fn create_router() -> Router {
166    create_router_with_state(AppState::new())
167}
168
169/// Create the API router with custom state (no security).
170pub fn create_router_with_state(state: AppState) -> Router {
171    // Session routes
172    let session_routes = Router::new()
173        .route("/", get(list_sessions).post(create_session))
174        .route("/{id}", get(get_session).delete(delete_session))
175        .route("/{id}/execute", post(execute_command))
176        .route("/{id}/ws", any(ws_handler));
177
178    // API v1 routes
179    let api_v1 = Router::new()
180        .route("/", get(api_info))
181        .route("/execute", post(execute_oneshot))
182        .route("/ws", any(ws_oneshot_handler))
183        .nest("/fs", fs_routes())
184        .nest("/sessions", session_routes);
185
186    // Build main router. This "no security" convenience constructor uses the
187    // restrictive CORS default (no permissive CORS headers emitted).
188    Router::new()
189        .route("/health", get(health))
190        .nest("/api/v1", api_v1)
191        .layer(TraceLayer::new_for_http())
192        .with_state(state)
193}
194
195/// The capability a route requires (Phase A spec §3).
196///
197/// Declared here at the router layer — co-located with the route definitions,
198/// which are the single source of truth for the matched-path strings this maps
199/// against. Not a per-handler attribute.
200#[derive(Debug, Clone, Copy, PartialEq, Eq)]
201pub enum RequiredCapability {
202    /// No authentication at all (e.g. `/health`).
203    Public,
204    /// Authenticated only: any valid token passes, no specific capability
205    /// required (spec §3 "인증만" tier, e.g. `GET /api/v1`).
206    Authenticated,
207    /// Requires a specific capability string (set membership, or wildcard).
208    Capability(&'static str),
209}
210
211/// Map a matched route (`method` + axum [`MatchedPath`]) to its required
212/// capability (spec §3 table).
213///
214/// Keyed on the **full nested** `MatchedPath` (confirmed against the real router
215/// structure) plus the HTTP method, because one path can require different
216/// capabilities per method (e.g. `GET /api/v1/sessions` = read, `POST` = manage).
217///
218/// Unknown routes fail **closed** to [`RequiredCapability::Authenticated`]: an
219/// unmapped route still requires a valid token, never less than that.
220pub fn required_capability(method: &Method, matched_path: &str) -> RequiredCapability {
221    use RequiredCapability::{Authenticated, Capability, Public};
222
223    // HEAD is GET without a body, and axum's `get()` serves it automatically, so
224    // its authorization must equal GET's. Keyed separately below, every GET
225    // route would need a twin HEAD arm — and a forgotten twin falls through to
226    // the closed default `Authenticated`, which means any valid token, not the
227    // capability GET requires. Normalising here is the one place it cannot be
228    // forgotten. Matched as `&str` (rather than comparing `Method` values) so
229    // the table below stays exactly as it reads for every other method.
230    let method = match method.as_str() {
231        "HEAD" => "GET",
232        other => other,
233    };
234
235    match (method, matched_path) {
236        (_, "/health") => Public,
237        ("GET", "/api/v1") => Authenticated,
238        ("POST", "/api/v1/execute") => Capability("exec"),
239        (_, "/api/v1/ws") => Capability("exec"),
240        ("GET", "/api/v1/sessions") => Capability("session.read"),
241        ("POST", "/api/v1/sessions") => Capability("session.manage"),
242        ("GET", "/api/v1/sessions/{id}") => Capability("session.read"),
243        ("DELETE", "/api/v1/sessions/{id}") => Capability("session.manage"),
244        ("POST", "/api/v1/sessions/{id}/execute") => Capability("exec"),
245        (_, "/api/v1/sessions/{id}/ws") => Capability("exec"),
246        ("GET", "/api/v1/fs/list") => Capability("fs.read"),
247        ("GET", "/api/v1/fs/stat") => Capability("fs.read"),
248        ("GET", "/api/v1/fs/file") => Capability("fs.read"),
249        ("DELETE", "/api/v1/fs/file") => Capability("fs.write"),
250        ("POST", "/api/v1/fs/uploads") => Capability("fs.write"),
251        ("GET", "/api/v1/fs/uploads/{id}") => Capability("fs.write"),
252        ("PATCH", "/api/v1/fs/uploads/{id}") => Capability("fs.write"),
253        ("POST", "/api/v1/fs/uploads/{id}/complete") => Capability("fs.write"),
254        ("DELETE", "/api/v1/fs/uploads/{id}") => Capability("fs.write"),
255        _ => Authenticated,
256    }
257}
258
259/// Scope-aware authentication + authorization middleware (spec §5).
260///
261/// 1. Resolve the route's required capability from `method` + `MatchedPath`.
262/// 2. `Public` route or auth disabled → pass through.
263/// 3. Extract the bearer token; look up its `TokenRecord` in the store.
264///    Missing/invalid token → **401**.
265/// 4. `Authenticated` route → any valid token passes. `Capability(c)` route →
266///    the token's set must satisfy `c` (membership or wildcard), else **403**.
267async fn capability_auth_middleware(
268    State((store, audit)): State<(
269        std::sync::Arc<ApiKeyStore>,
270        std::sync::Arc<crate::audit::AuditSink>,
271    )>,
272    mut request: Request,
273    next: Next,
274) -> Result<Response, StatusCode> {
275    // Auth disabled → open server (existing behavior).
276    if !store.is_enabled() {
277        return Ok(next.run(request).await);
278    }
279
280    let method = request.method().clone();
281    // Owned so it can be used in the rejection logs after `request` is consumed.
282    let matched = request
283        .extensions()
284        .get::<MatchedPath>()
285        .map(|m| m.as_str().to_owned())
286        .unwrap_or_default();
287    let required = required_capability(&method, &matched);
288
289    // Public routes (e.g. /health) skip auth entirely.
290    if required == RequiredCapability::Public {
291        return Ok(next.run(request).await);
292    }
293
294    // Extract the bearer token and resolve its capabilities.
295    // Missing header, wrong prefix, or unregistered token → 401.
296    let token = request
297        .headers()
298        .get(AUTHORIZATION)
299        .and_then(|v| v.to_str().ok())
300        .and_then(|header| store.extract_key(header));
301
302    let identity = token.as_deref().and_then(|t| store.identity(t));
303
304    let capabilities = match token.as_deref().and_then(|t| store.capabilities(t)) {
305        Some(caps) => caps,
306        None => {
307            // Logged at debug to avoid a log-flood amplifier under probing; the
308            // token value itself is never logged. `missing-token` = no/malformed
309            // Authorization header, `invalid-token` = present but unregistered.
310            let reason = if token.is_none() {
311                "missing-token"
312            } else {
313                "invalid-token"
314            };
315            tracing::debug!(%method, path = %matched, reason, "auth rejected (401)");
316            // Probing is exactly what an audit trail is asked about afterwards,
317            // so this layer's refusals are recorded as well as successes.
318            //
319            // "This layer's" is the whole claim, not modesty. A request carrying
320            // a field the server does not recognise is turned away by the
321            // extractor, after this middleware has already let it through, and
322            // records nothing — so the trail is not a complete list of every
323            // refusal the server issued. `USAGE.md` §4 names that gap; keep the
324            // two in step if this ever grows a third case.
325            audit.record(
326                crate::audit::AuditEvent::new("denied")
327                    .with_route(format!("{method} {matched}"))
328                    .with_denial(401, reason),
329            );
330            return Err(StatusCode::UNAUTHORIZED);
331        }
332    };
333
334    // Handlers record what actually ran, and need to know who asked.
335    if let Some(identity) = identity.clone() {
336        request.extensions_mut().insert(identity);
337    }
338
339    match required {
340        // Already handled above, but keep the match exhaustive.
341        RequiredCapability::Public => Ok(next.run(request).await),
342        // Any valid token satisfies an authenticated-only route.
343        RequiredCapability::Authenticated => Ok(next.run(request).await),
344        // Specific capability: set membership (or wildcard) required, else 403.
345        RequiredCapability::Capability(cap) => {
346            if capabilities.satisfies(cap) {
347                Ok(next.run(request).await)
348            } else {
349                audit.record(
350                    crate::audit::AuditEvent::new("denied")
351                        .with_identity(identity)
352                        .with_route(format!("{method} {matched}"))
353                        .with_denial(403, format!("missing-capability:{cap}")),
354                );
355                tracing::debug!(
356                    %method,
357                    path = %matched,
358                    required = cap,
359                    "authorization denied (403): insufficient capability"
360                );
361                Err(StatusCode::FORBIDDEN)
362            }
363        }
364    }
365}
366
367/// Whether `header` names a host this server answers to.
368///
369/// Compared without the port, since the port is not what an attacker controls
370/// in a rebinding attack, and a legitimate caller may reach the same server
371/// through different ports.
372fn host_is_allowed(header: Option<&str>, allowed: &[String]) -> bool {
373    let Some(value) = header else {
374        // HTTP/1.1 requires a Host header; its absence is not a shape any
375        // ordinary client produces.
376        return false;
377    };
378
379    let host = value
380        .rsplit_once(':')
381        .map_or(value, |(host, port)| {
382            // Only strip a trailing port, not part of a bare IPv6 address.
383            if port.chars().all(|c| c.is_ascii_digit()) {
384                host
385            } else {
386                value
387            }
388        })
389        .trim_matches(|c| c == '[' || c == ']');
390
391    allowed
392        .iter()
393        .any(|candidate| candidate.eq_ignore_ascii_case(host))
394}
395
396/// Reject requests carrying a `Host` this server does not answer to.
397async fn host_check_middleware(
398    State(allowed): State<Arc<Vec<String>>>,
399    request: Request,
400    next: Next,
401) -> Result<Response, (StatusCode, String)> {
402    let header = request
403        .headers()
404        .get(axum::http::header::HOST)
405        .and_then(|value| value.to_str().ok());
406
407    if host_is_allowed(header, &allowed) {
408        return Ok(next.run(request).await);
409    }
410
411    // Named explicitly: an operator hitting this from a container or behind a
412    // proxy needs to know which name was refused and how to permit it.
413    let seen = header.unwrap_or("(none)").to_string();
414    tracing::debug!(host = %seen, "request refused: host not allowed");
415    Err((
416        StatusCode::FORBIDDEN,
417        format!(
418            "host {seen} is not allowed; pass --allow-host {seen} to permit it
419"
420        ),
421    ))
422}
423
424/// Create the API router with security enabled.
425pub fn create_secure_router(
426    state: AppState,
427    security: SecurityConfig,
428) -> (Router, Arc<ApiKeyStore>, Arc<RateLimiter>) {
429    // Create security components
430    let auth_store = Arc::new(ApiKeyStore::new(security.auth));
431    let rate_limiter = Arc::new(RateLimiter::new(security.rate_limit));
432
433    // Register API keys with their configured capabilities.
434    for key in &security.api_keys {
435        register_key(&auth_store, key, &security.capabilities);
436    }
437
438    // Session routes
439    let session_routes = Router::new()
440        .route("/", get(list_sessions).post(create_session))
441        .route("/{id}", get(get_session).delete(delete_session))
442        .route("/{id}/execute", post(execute_command))
443        .route("/{id}/ws", any(ws_handler));
444
445    // API v1 routes
446    let api_v1 = Router::new()
447        .route("/", get(api_info))
448        .route("/execute", post(execute_oneshot))
449        .route("/ws", any(ws_oneshot_handler))
450        .nest("/fs", fs_routes())
451        .nest("/sessions", session_routes);
452
453    let allowed_hosts = security.allowed_hosts.clone();
454
455    // Build main router with security layers
456    let mut router = Router::new()
457        .route("/health", get(health))
458        .nest("/api/v1", api_v1)
459        .layer(middleware::from_fn_with_state(
460            (Arc::clone(&auth_store), Arc::clone(&state.audit)),
461            capability_auth_middleware,
462        ))
463        .layer(middleware::from_fn_with_state(
464            Arc::clone(&rate_limiter),
465            rate_limit_middleware,
466        ))
467        .layer(TraceLayer::new_for_http());
468
469    // Outermost, so a rebound request is refused before it reaches the token
470    // store or the rate limiter's bookkeeping.
471    if let Some(hosts) = allowed_hosts {
472        router = router.layer(middleware::from_fn_with_state(
473            Arc::new(hosts),
474            host_check_middleware,
475        ));
476    }
477
478    // Permissive CORS only when explicitly opted in (default: restrictive).
479    if let Some(cors) = cors_layer(&security.cors) {
480        router = router.layer(cors);
481    }
482
483    let router = router.with_state(state);
484
485    (router, auth_store, rate_limiter)
486}
487
488/// Server configuration.
489#[derive(Debug, Clone)]
490pub struct ServerConfig {
491    /// Host address to bind to.
492    pub host: String,
493    /// Port to listen on.
494    pub port: u16,
495    /// Security configuration.
496    pub security: SecurityConfig,
497    /// Enable graceful shutdown on SIGTERM/SIGINT.
498    pub graceful_shutdown: bool,
499}
500
501impl ServerConfig {
502    pub fn new(host: impl Into<String>, port: u16) -> Self {
503        Self {
504            host: host.into(),
505            port,
506            security: SecurityConfig::default(),
507            graceful_shutdown: true,
508        }
509    }
510
511    pub fn bind_address(&self) -> String {
512        format!("{}:{}", self.host, self.port)
513    }
514
515    /// Enable security with the given configuration.
516    pub fn with_security(mut self, security: SecurityConfig) -> Self {
517        self.security = security;
518        self
519    }
520
521    /// Disable graceful shutdown.
522    pub fn without_graceful_shutdown(mut self) -> Self {
523        self.graceful_shutdown = false;
524        self
525    }
526}
527
528impl Default for ServerConfig {
529    fn default() -> Self {
530        Self {
531            host: "127.0.0.1".to_string(),
532            port: 3000,
533            security: SecurityConfig::default(),
534            graceful_shutdown: true,
535        }
536    }
537}
538
539/// Start the API server.
540pub async fn serve(config: ServerConfig) -> crate::Result<()> {
541    serve_with_state(config, AppState::new()).await
542}
543
544/// Bind the API server's port without starting to serve.
545///
546/// Callers that need to know the port before traffic flows — anything binding
547/// port 0, where the OS chooses — take the listener from here and hand it to
548/// [`serve_on`]. Splitting bind from serve is what makes an ephemeral port
549/// usable: the alternative is binding twice and racing whoever grabs it in
550/// between.
551pub async fn bind(config: &ServerConfig) -> crate::Result<tokio::net::TcpListener> {
552    tokio::net::TcpListener::bind(config.bind_address())
553        .await
554        .map_err(crate::error::ShellTunnelError::Io)
555}
556
557/// Start the API server with custom state.
558pub async fn serve_with_state(config: ServerConfig, state: AppState) -> crate::Result<()> {
559    let listener = bind(&config).await?;
560    serve_on(listener, config, state).await
561}
562
563/// Serve on an already-bound listener.
564pub async fn serve_on(
565    listener: tokio::net::TcpListener,
566    config: ServerConfig,
567    state: AppState,
568) -> crate::Result<()> {
569    let addr = config.bind_address();
570
571    // Create router with security
572    let (router, auth_store, _rate_limiter) = create_secure_router(state, config.security.clone());
573
574    // Log API key if auth is enabled and keys are registered
575    if auth_store.is_enabled() {
576        if auth_store.count() == 0 {
577            // Generate and register a key if none provided, scoped to the
578            // configured capabilities (full-control when unset).
579            let key = crate::security::generate_api_key();
580            register_key(&auth_store, &key, &config.security.capabilities);
581            tracing::info!("Generated API key: {}", key);
582        }
583        tracing::info!(
584            "Authentication enabled with {} API key(s)",
585            auth_store.count()
586        );
587    } else {
588        tracing::warn!("Authentication is DISABLED - server is open to all requests");
589    }
590
591    let _ = addr;
592    tracing::info!(
593        "Starting shell-tunnel API server on {}",
594        listener
595            .local_addr()
596            .map(|a| a.to_string())
597            .unwrap_or_else(|_| config.bind_address())
598    );
599
600    // Create service with connection info for rate limiting
601    let service: IntoMakeServiceWithConnectInfo<Router, SocketAddr> =
602        router.into_make_service_with_connect_info::<SocketAddr>();
603
604    if config.graceful_shutdown {
605        // Serve with graceful shutdown
606        axum::serve(listener, service)
607            .with_graceful_shutdown(shutdown_signal())
608            .await
609            .map_err(|e| {
610                crate::error::ShellTunnelError::Io(std::io::Error::other(e.to_string()))
611            })?;
612
613        tracing::info!("Server shutdown complete");
614    } else {
615        // Serve without graceful shutdown
616        axum::serve(listener, service).await.map_err(|e| {
617            crate::error::ShellTunnelError::Io(std::io::Error::other(e.to_string()))
618        })?;
619    }
620
621    Ok(())
622}
623
624/// Wait for shutdown signal (Ctrl+C or SIGTERM).
625async fn shutdown_signal() {
626    let ctrl_c = async {
627        tokio::signal::ctrl_c()
628            .await
629            .expect("Failed to install Ctrl+C handler");
630    };
631
632    #[cfg(unix)]
633    let terminate = async {
634        tokio::signal::unix::signal(tokio::signal::unix::SignalKind::terminate())
635            .expect("Failed to install SIGTERM handler")
636            .recv()
637            .await;
638    };
639
640    #[cfg(not(unix))]
641    let terminate = std::future::pending::<()>();
642
643    tokio::select! {
644        _ = ctrl_c => {
645            tracing::info!("Received Ctrl+C, initiating graceful shutdown...");
646        }
647        _ = terminate => {
648            tracing::info!("Received SIGTERM, initiating graceful shutdown...");
649        }
650    }
651}
652
653/// Filesystem routes, shared by the plain and the secured router.
654///
655/// Built in one place so the two constructors cannot drift apart — a route
656/// present in only one of them is reachable in only one deployment shape.
657fn fs_routes() -> Router<AppState> {
658    // The chunk-upload route carries request bodies up to `MAX_CHUNK_SIZE` (8
659    // MiB) — well above axum-core's own default body limit (2 MiB, axum-core
660    // 0.5.6's `DEFAULT_LIMIT`), which this app otherwise never overrides. A
661    // client sending a chunk at the server's own advertised `chunk_size` (4
662    // MiB default) would have it rejected before `append_chunk` ever ran.
663    //
664    // Raised only for this one path, via a merged sub-router, rather than
665    // `.layer()` on the whole `fs_routes` router: the latter would also raise
666    // the limit for `/list`, `/stat`, and `/file`, none of which need an 8
667    // MiB body, and a limit set any higher up would reach `/api/v1/execute`
668    // too. Set at the hard ceiling (`MAX_CHUNK_SIZE`) rather than the
669    // *configured* `chunk_size`, so a chunk larger than configured but still
670    // under the ceiling reaches `append_chunk`'s own `TooLarge` check and
671    // gets a 413 with a machine-readable body — if axum's limit cut it off
672    // first, the caller would get a bodyless 413 with no way to tell why.
673    let upload_session_routes = Router::new()
674        .route(
675            "/uploads/{id}",
676            get(super::fs::upload_status)
677                .patch(super::fs::append_chunk)
678                .delete(super::fs::cancel_upload),
679        )
680        .route_layer(DefaultBodyLimit::max(crate::fs::MAX_CHUNK_SIZE));
681
682    Router::new()
683        .route("/list", get(super::fs::list))
684        .route("/stat", get(super::fs::stat))
685        .route(
686            "/file",
687            get(super::fs::download).delete(super::fs::delete_file),
688        )
689        .route("/uploads", post(super::fs::create_upload))
690        .merge(upload_session_routes)
691        .route("/uploads/{id}/complete", post(super::fs::complete_upload))
692}
693
694#[cfg(test)]
695mod tests {
696    use super::*;
697
698    #[test]
699    fn test_server_config_default() {
700        let config = ServerConfig::default();
701        assert_eq!(config.host, "127.0.0.1");
702        assert_eq!(config.port, 3000);
703        assert_eq!(config.bind_address(), "127.0.0.1:3000");
704        assert!(config.graceful_shutdown);
705    }
706
707    #[test]
708    fn test_server_config_custom() {
709        let config = ServerConfig::new("0.0.0.0", 8080);
710        assert_eq!(config.bind_address(), "0.0.0.0:8080");
711    }
712
713    #[test]
714    fn test_server_config_with_security() {
715        let config = ServerConfig::new("0.0.0.0", 8080)
716            .with_security(SecurityConfig::secure().with_api_key("test-key"));
717
718        assert!(config.security.auth.enabled);
719        assert_eq!(config.security.api_keys.len(), 1);
720    }
721
722    #[test]
723    fn test_security_config_default() {
724        let config = SecurityConfig::default();
725        assert!(!config.auth.enabled); // Disabled by default
726        assert!(config.rate_limit.enabled);
727    }
728
729    #[test]
730    fn test_security_config_secure() {
731        let config = SecurityConfig::secure();
732        assert!(config.auth.enabled);
733        assert!(config.rate_limit.enabled);
734    }
735
736    #[test]
737    fn test_cors_restrictive_by_default() {
738        assert!(!SecurityConfig::default().cors.allow_any);
739        assert!(!SecurityConfig::secure().cors.allow_any);
740        assert!(cors_layer(&CorsConfig::default()).is_none());
741    }
742
743    #[test]
744    fn test_cors_allow_any_opt_in() {
745        let config = SecurityConfig::development().with_cors_allow_any();
746        assert!(config.cors.allow_any);
747        assert!(cors_layer(&config.cors).is_some());
748    }
749
750    #[test]
751    fn test_security_config_development() {
752        let config = SecurityConfig::development();
753        assert!(!config.auth.enabled);
754        assert!(config.rate_limit.enabled);
755    }
756
757    #[test]
758    fn test_router_creation() {
759        let _router = create_router();
760        // Router created successfully
761    }
762
763    #[test]
764    fn test_required_capability_mapping() {
765        use RequiredCapability::{Authenticated, Capability, Public};
766
767        // Public + authenticated-only tiers.
768        assert_eq!(required_capability(&Method::GET, "/health"), Public);
769        assert_eq!(required_capability(&Method::GET, "/api/v1"), Authenticated);
770
771        // exec routes (oneshot + session-scoped + WS).
772        assert_eq!(
773            required_capability(&Method::POST, "/api/v1/execute"),
774            Capability("exec")
775        );
776        assert_eq!(
777            required_capability(&Method::GET, "/api/v1/ws"),
778            Capability("exec")
779        );
780        assert_eq!(
781            required_capability(&Method::POST, "/api/v1/sessions/{id}/execute"),
782            Capability("exec")
783        );
784        assert_eq!(
785            required_capability(&Method::GET, "/api/v1/sessions/{id}/ws"),
786            Capability("exec")
787        );
788
789        // read vs manage split on the same path, keyed by method.
790        assert_eq!(
791            required_capability(&Method::GET, "/api/v1/sessions"),
792            Capability("session.read")
793        );
794        assert_eq!(
795            required_capability(&Method::POST, "/api/v1/sessions"),
796            Capability("session.manage")
797        );
798        assert_eq!(
799            required_capability(&Method::GET, "/api/v1/sessions/{id}"),
800            Capability("session.read")
801        );
802        assert_eq!(
803            required_capability(&Method::DELETE, "/api/v1/sessions/{id}"),
804            Capability("session.manage")
805        );
806    }
807
808    #[test]
809    fn test_required_capability_unknown_fails_closed() {
810        // An unmapped route requires at least a valid token (never less).
811        assert_eq!(
812            required_capability(&Method::GET, "/api/v1/unknown"),
813            RequiredCapability::Authenticated
814        );
815    }
816
817    // A direct unit-level check of the `HEAD` normalisation lived here once,
818    // hardcoding three fs paths. It is superseded by
819    // `every_get_fs_route_authorizes_head_identically` in `tests/fs_api.rs`,
820    // which derives the same check from the one authoritative route table
821    // (shared with `every_fs_route_declares_a_capability`) instead of a
822    // second, hand-maintained list that could drift from it.
823
824    #[test]
825    fn test_secure_router_creation() {
826        let state = AppState::new();
827        let security = SecurityConfig::secure().with_api_key("test-key");
828        let (router, auth_store, rate_limiter) = create_secure_router(state, security);
829
830        assert_eq!(auth_store.count(), 1);
831        assert!(auth_store.is_valid("test-key"));
832        assert!(rate_limiter.is_enabled());
833
834        // Router should be created
835        drop(router);
836    }
837}