Skip to main content

areev_server/
lib.rs

1//! areev-server — the opt-in thin HTTP surface and
2//! the local test console (`areev ui`).
3//!
4//! Deliberately minimal: std-only HTTP/1.1 on 127.0.0.1, one request per
5//! connection, JSON API + one embedded HTML page. This is a *local
6//! inspection console*, not a service — no auth, binds loopback only.
7
8use std::io::{BufRead, BufReader, Read, Write};
9use std::net::{TcpListener, TcpStream};
10use std::time::Duration;
11
12use areev_cal::{CalExecutor, CalExecutorConfig, CalStoreFacade, AreevFacade};
13use areev_loop_adapter::{now_ms, BorrowedSubstrate};
14use serde_json::{json, Value};
15use areev_loop::{Decision, Engine, RecStatus, RunOptions};
16
17/// Native OIDC (A3) — the second recorded dependency-policy exception, behind
18/// the non-default `oidc` feature. See the module docs for why a proxy
19/// cannot cover the case this exists for.
20#[cfg(feature = "oidc")]
21pub mod oidc;
22
23const CONSOLE_HTML: &str = include_str!("console.html");
24
25/// Per-connection read/write timeout — bounds slow-client (slowloris) attacks.
26const READ_TIMEOUT_SECS: u64 = 15;
27/// Hard ceiling on bytes read from one connection (1 MiB body cap + headroom
28/// for the request line and headers). Bounds memory against oversized requests.
29const MAX_CONN_BYTES: u64 = (1 << 20) + (128 << 10);
30/// Cap on total header bytes and header count per request.
31const MAX_HEADER_BYTES: usize = 64 * 1024;
32const MAX_HEADERS: usize = 100;
33/// Total wall-clock deadline for reading + handling one request. A per-read
34/// timeout cannot bound a slow-drip client (each byte resets it), so a watchdog
35/// shuts the socket down at this deadline.
36const REQUEST_DEADLINE_SECS: u64 = 30;
37/// Issue #126: an auth-failure streak from one source IP idle longer than
38/// this is treated as reset rather than accumulated against forever.
39const AUTH_FAILURE_IDLE_SECS: u64 = 15 * 60;
40/// Cap on distinct source IPs tracked for auth-failure throttling — the key
41/// space is attacker-controlled, so this bounds the memory an attacker can
42/// force the map to hold.
43const MAX_AUTH_FAILURE_IPS: usize = 4096;
44/// Longest accepted proxy-asserted identity. Principals are names, not
45/// documents; anything longer is a malformed or hostile header, and the
46/// string ends up in audit grains that replicate.
47const MAX_SSO_IDENTITY_BYTES: usize = 128;
48/// Principal names an SSO header may never assert.
49///
50/// `anonymous` is the restricted baseline and `user:console` is the owner
51/// default the per-request binding restores on drop — an identity header
52/// naming either would make audit records ambiguous between "a person the
53/// proxy vouched for" and "the console's own unauthenticated floor/ceiling".
54/// Neither is currently an escalation (`bind_principal` always builds a
55/// *restricted* set from the file's grants), but a name that reads as a
56/// system principal in an immutable, replicating audit trail is a defect
57/// whether or not it is exploitable today.
58const RESERVED_PRINCIPALS: [&str; 2] = ["anonymous", "user:console"];
59
60/// Consecutive failed authentications from one source IP after which further
61/// credential-bearing requests are refused with `429` until the streak goes
62/// idle (`AUTH_FAILURE_IDLE_SECS`).
63///
64/// Set high enough that a human fat-fingering a pasted token a few times is
65/// never locked out, and low enough that an online guessing attack gets a few
66/// attempts per quarter-hour instead of thousands per second. It is a brake
67/// on rate, not a replacement for token entropy — which is why `areev auth
68/// mint` (256-bit tokens) is the real control and this is the backstop.
69const MAX_CONSECUTIVE_AUTH_FAILURES: u32 = 10;
70
71
72/// One accepted connection: plaintext, or TLS when the server was built
73/// with the `tls` feature and configured with a certificate. The raw
74/// socket handle stays available for timeouts and the watchdog shutdown;
75/// all request/response bytes flow through this wrapper. A plaintext
76/// client hitting a TLS listener fails the handshake — there is no
77/// downgrade path by construction.
78enum Conn {
79    Plain(TcpStream),
80    #[cfg(feature = "tls")]
81    Tls(Box<rustls::StreamOwned<rustls::ServerConnection, TcpStream>>),
82}
83
84impl Read for Conn {
85    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
86        match self {
87            Conn::Plain(s) => s.read(buf),
88            #[cfg(feature = "tls")]
89            Conn::Tls(s) => s.read(buf),
90        }
91    }
92}
93
94impl Write for Conn {
95    fn write(&mut self, buf: &[u8]) -> std::io::Result<usize> {
96        match self {
97            Conn::Plain(s) => s.write(buf),
98            #[cfg(feature = "tls")]
99            Conn::Tls(s) => s.write(buf),
100        }
101    }
102    fn flush(&mut self) -> std::io::Result<()> {
103        match self {
104            Conn::Plain(s) => s.flush(),
105            #[cfg(feature = "tls")]
106            Conn::Tls(s) => s.flush(),
107        }
108    }
109}
110
111pub struct UiServer {
112    facade: std::sync::Arc<AreevFacade>,
113    executor: CalExecutor,
114    db_label: String,
115    /// `db_label`, redacted for DISPLAY (issue #124): every response body,
116    /// the served HTML, and any startup log line must use this, never the
117    /// raw label — on the Postgres backend `db_label` is a DSN with an
118    /// inline password. Computed once in `UiServer::new` via `redact_dsn`.
119    /// The raw `db_label` is kept only for the `resolve_for_memory` call
120    /// sites below, which must keep comparing against the exact `--db`
121    /// value a credential map's `memories` entry is written as.
122    db_label_display: String,
123    /// Shared secret required for access when set. It guards mutating
124    /// endpoints (`Bearer`); in console-auth mode (`auth_all`) it guards
125    /// every request via `Bearer` **or** HTTP `Basic` (password = token).
126    token: Option<String>,
127    /// When true, *every* request requires the token — the console page, all
128    /// reads, and all writes — and a `401` carries `WWW-Authenticate: Basic` so
129    /// browsers prompt. Set by [`UiServer::with_auth`].
130    auth_all: bool,
131    /// When false (the default), reject any request whose `Host` header is not
132    /// loopback — the standard DNS-rebinding defense for a localhost service.
133    /// Set true only when the operator intentionally serves to other hosts
134    /// (CLI `--allow-remote`), where a non-loopback `Host` is expected.
135    allow_remote: bool,
136    /// Origins (issue #125), normalized, whose cross-origin **POST**s are
137    /// accepted even though they are not loopback. The Origin drive-by
138    /// check is NOT lifted by `allow_remote` — HTTP Basic is browser-cached
139    /// and re-attached cross-site, so Origin is the only thing telling the
140    /// console's own page apart from an attacker's page riding a viewer's
141    /// cached credential. This is the exact-match allowlist an operator
142    /// populates instead (CLI `--allow-origin`), one entry per
143    /// [`allow_origin`](Self::allow_origin) call. No wildcards, no
144    /// suffix/subdomain matching.
145    allowed_origins: Vec<String>,
146    /// Host loop policy (§6.2) applied to the `/api/loop/*` engine — the
147    /// same `loop-policy.json` the CLI takes (`areev ui --policy`). Absent →
148    /// the closed default: nothing auto-applies, nothing is denied. Host
149    /// config, never persisted in the memory file.
150    loop_policy: Option<areev_loop::Policy>,
151    /// Multi-principal mode (`areev ui --auth areev-auth.json`): a credential
152    /// map resolving bearer/Basic tokens to principal names. Rights come
153    /// from the FILE's own grant grains — the map holds no policy and no
154    /// raw secrets. Unauthenticated requests run as `anonymous`
155    /// (read-only unless the file grants more). It changes *who* a request
156    /// is, never *whether* the shared `--token-env` secret is required: when
157    /// both are configured the secret keeps its contract (implied admin, and
158    /// still demanded on the requests it guards).
159    credentials: Option<areev_core::authz::CredentialMap>,
160    /// Whether destructive CAL is permitted, kept alongside the executor so
161    /// every builder can rebuild it without clobbering the others' settings.
162    allow_destructive_ops: bool,
163    /// Native TLS (the recorded dependency exception; `tls` cargo feature).
164    /// None = plaintext — the documented default is a TLS-terminating proxy.
165    #[cfg(feature = "tls")]
166    tls_config: Option<std::sync::Arc<rustls::ServerConfig>>,
167    /// SSO v0 — trusted-header auth (`areev ui --sso-header X-Forwarded-User
168    /// --sso-secret-env VAR`): an authenticating proxy does OIDC/SAML and
169    /// forwards the identity in this header. Honored ONLY when the request
170    /// also carries the proxy shared secret in `x-areev-proxy-secret` —
171    /// otherwise identity headers are attacker-controlled input and are
172    /// ignored entirely. Rights still come from the FILE's grant grains.
173    sso_header: Option<String>,
174    /// The proxy shared secrets this instance accepts, newest first.
175    ///
176    /// A list rather than a value so the secret can be **rotated without a
177    /// zero-overlap cutover** (#79). It is an impersonation-grade credential:
178    /// whoever holds it can present any identity header, approval-capable
179    /// principals included. Rotating one of those atomically across a proxy
180    /// fleet and a console is not achievable in practice, so the honest
181    /// choices were a brief outage or a window where the wrong secret is live
182    /// — and an operator facing either under suspected-compromise pressure
183    /// will defer the rotation, which is the outcome that actually costs.
184    ///
185    /// Two at a time, deliberately: enough for old-and-new, not enough to
186    /// accumulate a drawer of forgotten live credentials. Both are checked in
187    /// constant time and the comparison never short-circuits on the first
188    /// mismatch, so timing cannot reveal which one matched or how many are
189    /// configured.
190    sso_secrets: Vec<String>,
191    /// Optional groups header (A2, `--sso-groups-header X-Forwarded-Groups`):
192    /// a comma-separated list of IdP groups, honored under the same proxy
193    /// secret as `sso_header`. Resolved through the credential map's `groups`
194    /// table, and only when the asserted identity has no grants of its own —
195    /// see `resolve_sso_principal` for the precedence and why.
196    sso_groups_header: Option<String>,
197    /// Prefix stamped onto every proxy-asserted principal
198    /// (`--sso-principal-prefix`). Lets an operator keep IdP-sourced
199    /// identities visibly distinct from credential-map ones in grant grains
200    /// and audit records, so `GRANT` can target one population without
201    /// depending on IdP naming conventions never colliding with local ones.
202    sso_principal_prefix: Option<String>,
203    /// Native OIDC (A3, `oidc` feature). When configured, the console serves
204    /// its own login: `/auth/login` → the IdP → `/auth/callback` → an
205    /// `HttpOnly` session cookie. Unlike the trusted-header path, the
206    /// identity is proven by a signature over an issuer-published key set,
207    /// which is why an OIDC principal MAY approve by default — that
208    /// difference is the entire reason the feature exists.
209    #[cfg(feature = "oidc")]
210    oidc: Option<std::sync::Arc<oidc::OidcProvider>>,
211    /// Whether a proxy-asserted SSO identity may answer a HITL approval
212    /// (`POST /api/run/respond`). **Default false — deny.**
213    ///
214    /// `run.respond` already refuses shared-token and anonymous callers
215    /// because the approver's identity IS the audit record. An SSO identity
216    /// arrives in a header, trusted because the request also carried
217    /// `sso_secrets` — a static, shared, impersonation-grade value. Whoever
218    /// holds it can assert *any* identity, approval-capable principals
219    /// included, and the resulting audit grain is indistinguishable from a
220    /// genuine approval: a well-formed answer by a granted principal. The
221    /// blast radius of that one secret is the integrity of the whole HITL
222    /// trail — which is the control the governance story rests on.
223    ///
224    /// A credential-map principal is materially stronger (it holds a
225    /// per-principal secret, not a fleet-wide one) and is unaffected by this.
226    /// So the default fails closed and an operator who accepts the trade-off
227    /// opts in explicitly with `areev ui --sso-approvals allow`.
228    sso_approvals: bool,
229    /// Auth-failure counter (issue #126), keyed by source IP — the port is
230    /// deliberately excluded, since a NAT'd or proxied attacker's source
231    /// port varies per connection while the IP does not. Each entry is
232    /// `(consecutive failures, last-failure time)`; see
233    /// `note_auth_failure`/`reset_auth_failures`. The map is
234    /// attacker-controlled key space (one entry per source IP that ever
235    /// fails once) and is bounded at `MAX_AUTH_FAILURE_IPS` for exactly that
236    /// reason. The lock is held only for the map bookkeeping itself, never
237    /// across a response write.
238    ///
239    /// This counts and logs — it does **not** delay or lock out. See
240    /// `note_auth_failure` for why: `serve` is a strictly serial accept
241    /// loop, so any per-request sleep here would be a lever an
242    /// unauthenticated caller pulls to stall the console for everyone, not
243    /// just themselves.
244    auth_failures: std::sync::Mutex<std::collections::HashMap<std::net::IpAddr, (u32, std::time::Instant)>>,
245}
246
247/// A login-flow error, rendered as text rather than JSON: the audience is a
248/// browser mid-redirect, not a script. The message is ours, never the IdP's
249/// raw response body.
250#[cfg(feature = "oidc")]
251fn auth_error(msg: &str) -> (String, String, String) {
252    (
253        "400 Bad Request".into(),
254        "Content-Type: text/plain; charset=utf-8\r\nCache-Control: no-store\r\n".into(),
255        format!("login failed: {msg}\n\nStart again at /auth/login\n"),
256    )
257}
258
259#[cfg(feature = "oidc")]
260fn not_found_auth() -> (String, String, String) {
261    (
262        "404 Not Found".into(),
263        "Content-Type: text/plain; charset=utf-8\r\n".into(),
264        "no such auth endpoint\n".into(),
265    )
266}
267
268/// One cookie's value out of a `Cookie:` header, or `None`.
269///
270/// Deliberately exact on the name: a `Cookie` header is attacker-influenced
271/// (any script on any same-site origin can set cookies), so a prefix match
272/// would let `areev_session_decoy=...` shadow the real one.
273#[cfg(feature = "oidc")]
274fn cookie_value(header: &str, name: &str) -> Option<String> {
275    header.split(';').find_map(|pair| {
276        let (k, v) = pair.split_once('=')?;
277        (k.trim() == name).then(|| v.trim().to_string())
278    })
279}
280
281/// Validate a proxy-asserted identity before it becomes a principal.
282///
283/// The proxy secret decides *who may assert*; it says nothing about whether
284/// what was asserted is well-formed. An identity flows into audit grains that
285/// are immutable and replicate, so a control character here is a log-injection
286/// that outlives the request — and a name colliding with a system principal is
287/// an ambiguity no later reader can resolve.
288///
289/// Returns `None` for anything rejected, which the caller treats exactly like
290/// an absent header: the request proceeds as whatever its other credentials
291/// make it. Refusing the *request* would let a misconfigured proxy take the
292/// console down; refusing the *identity* fails closed on rights instead.
293fn sanitize_sso_identity(raw: &str) -> Option<String> {
294    let id = raw.trim();
295    if id.is_empty() || id.len() > MAX_SSO_IDENTITY_BYTES {
296        return None;
297    }
298    // No control characters (CR/LF above all — header smuggling into logs),
299    // and no internal whitespace: a principal is one token, and " user:a"
300    // vs "user:a" must not be two audit identities for one person.
301    if id.chars().any(|c| c.is_control() || c.is_whitespace()) {
302        return None;
303    }
304    if RESERVED_PRINCIPALS.contains(&id) {
305        return None;
306    }
307    Some(id.to_string())
308}
309
310/// Per-request principal binding. The server handles one request at a
311/// time, so rebinding the shared facade for the request's duration is
312/// race-free; the guard restores the owner default on drop — including on
313/// panic — so a failed handler can never leak a stray principal into the
314/// next request.
315struct RequestBinding<'a> {
316    facade: &'a AreevFacade,
317}
318
319impl<'a> RequestBinding<'a> {
320    fn bind(
321        facade: &'a AreevFacade,
322        map: &areev_core::authz::CredentialMap,
323        bearer: Option<&str>,
324        memory: &str,
325    ) -> Self {
326        // Memory-scoped resolution: a credential whose `memories` list
327        // excludes THIS memory is indistinguishable from an unknown token.
328        let principal =
329            bearer.and_then(|t| map.resolve_for_memory(t, memory).ok().map(str::to_string));
330        match principal {
331            Some(p) => {
332                if facade.bind_principal(&p).is_err() {
333                    // A store failure reading grants fails toward LESS
334                    // privilege, never more.
335                    Self::bind_anonymous(facade);
336                }
337            }
338            None => Self::bind_anonymous(facade),
339        }
340        RequestBinding { facade }
341    }
342
343    /// `anonymous` = a read-only baseline plus whatever the file explicitly
344    /// grants the `anonymous` principal — the formalized version of the old
345    /// token-less read-only console.
346    /// Bind a proxy-proven SSO identity directly (no credential map row
347    /// needed — the IdP said who this is; the FILE says what they may do).
348    fn bind_identity(facade: &'a AreevFacade, principal: &str) -> Self {
349        if facade.bind_principal(principal).is_err() {
350            Self::bind_anonymous(facade);
351        }
352        RequestBinding { facade }
353    }
354
355    fn bind_anonymous(facade: &AreevFacade) {
356        use areev_core::authz::{AuthzSet, Grant, Verb};
357        let mut grants = facade
358            .with_store(|m| m.authz_grants("anonymous"))
359            .unwrap_or_default();
360        grants.push(Grant { verbs: vec![Verb::Read], namespaces: vec!["*".to_string()] });
361        facade.bind(AuthzSet::restricted("anonymous", grants));
362    }
363}
364
365impl Drop for RequestBinding<'_> {
366    fn drop(&mut self) {
367        self.facade
368            .bind(areev_core::authz::AuthzSet::owner("user:console"));
369    }
370}
371
372impl UiServer {
373    pub fn new(facade: AreevFacade, db_label: String) -> Self {
374        let db_label_display = redact_dsn(&db_label);
375        UiServer {
376            facade: std::sync::Arc::new(facade),
377            executor: CalExecutor::new(CalExecutorConfig::default())
378                .with_governance(std::sync::Arc::new(areev_loop_adapter::LoopGovernance::new())),
379            db_label,
380            db_label_display,
381            token: None,
382            auth_all: false,
383            allow_remote: false,
384            allowed_origins: Vec::new(),
385            loop_policy: None,
386            credentials: None,
387            allow_destructive_ops: CalExecutorConfig::default().allow_destructive_ops,
388            #[cfg(feature = "tls")]
389            tls_config: None,
390            sso_header: None,
391            sso_secrets: Vec::new(),
392            sso_groups_header: None,
393            sso_principal_prefix: None,
394            #[cfg(feature = "oidc")]
395            oidc: None,
396            sso_approvals: false,
397            auth_failures: std::sync::Mutex::new(std::collections::HashMap::new()),
398        }
399    }
400
401    /// Enable trusted-header SSO (v0). `header` names the identity header
402    /// the authenticating proxy forwards; `secret` is the proxy's shared
403    /// secret, demanded in `x-areev-proxy-secret` on every SSO request.
404    pub fn with_sso(self, header: impl Into<String>, secret: impl Into<String>) -> Self {
405        self.with_sso_rotating(header, secret, None::<String>)
406    }
407
408    /// [`with_sso`](Self::with_sso) with a second secret accepted during a
409    /// rotation window (#79).
410    ///
411    /// `next` is the incoming secret; `secret` stays the one already deployed.
412    /// While both are configured either proves the proxy, so the fleet can be
413    /// moved over one node at a time and the old secret retired afterwards —
414    /// the way a TLS key is rotated. Order carries no privilege: they are
415    /// equally valid until one is removed, which is what makes the window
416    /// safe to run in both directions (roll forward, or roll back).
417    ///
418    /// The window is meant to be short. Nothing here enforces that — a
419    /// deadline the server could enforce would have to live in the file or a
420    /// clock it does not own — so retiring the old secret is the operator's
421    /// step, and `docs/runbooks/sso-secret-rotation.md` is the procedure.
422    pub fn with_sso_rotating(
423        mut self,
424        header: impl Into<String>,
425        secret: impl Into<String>,
426        next: Option<impl Into<String>>,
427    ) -> Self {
428        let header = header.into();
429        let secret = secret.into();
430        let next = next.map(Into::into).filter(|s| !s.trim().is_empty());
431        // An empty proxy secret would let `x-areev-proxy-secret:` (empty)
432        // prove any identity — refuse loudly at the library boundary too,
433        // not just in the CLI.
434        assert!(
435            !header.trim().is_empty() && !secret.trim().is_empty(),
436            "with_sso requires a non-empty header name and proxy secret"
437        );
438        // A rotation that deploys the same value twice is a rotation that did
439        // not happen, and it would read as "both live" in every log and
440        // status line. Refuse rather than silently accept the no-op.
441        assert!(
442            next.as_deref() != Some(secret.as_str()),
443            "the rotating proxy secret must differ from the current one"
444        );
445        self.sso_header = Some(header.to_ascii_lowercase());
446        self.sso_secrets = match next {
447            Some(n) => vec![secret, n],
448            None => vec![secret],
449        };
450        self
451    }
452
453    /// Serve native OIDC login for the console (A3).
454    #[cfg(feature = "oidc")]
455    pub fn with_oidc(mut self, provider: oidc::OidcProvider) -> Self {
456        self.oidc = Some(std::sync::Arc::new(provider));
457        self
458    }
459
460    /// The principal for this request's session cookie, if any.
461    #[cfg(feature = "oidc")]
462    fn oidc_principal(&self, cookie_header: Option<&str>) -> Option<String> {
463        let provider = self.oidc.as_ref()?;
464        let sid = cookie_value(cookie_header?, oidc::SESSION_COOKIE)?;
465        provider.principal_for_session(&sid)
466    }
467
468    /// The `/auth/*` endpoints (A3). Returns `(status, extra headers, body)`.
469    ///
470    /// These are the only routes that set headers of their own, which is why
471    /// they bypass `route_full`'s (status, content-type, body) contract.
472    #[cfg(feature = "oidc")]
473    fn oidc_route(
474        &self,
475        method: &str,
476        path: &str,
477        cookie_header: Option<&str>,
478    ) -> (String, String, String) {
479        let provider = match &self.oidc {
480            Some(p) => p,
481            None => return not_found_auth(),
482        };
483        let (base, query) = match path.split_once('?') {
484            Some((p, q)) => (p, q),
485            None => (path, ""),
486        };
487        let q = |key: &str| -> Option<String> {
488            query.split('&').find_map(|kv| {
489                let (k, v) = kv.split_once('=')?;
490                (k == key).then(|| urldecode(v))
491            })
492        };
493        // `Secure` only when the redirect URI is https: a loopback console
494        // over plain http must still be able to log in, and a `Secure` cookie
495        // the browser refuses to send would look like a broken login rather
496        // than a policy.
497        let secure = if provider.is_secure() { "; Secure" } else { "" };
498
499        match (method, base) {
500            ("GET", "/auth/login") => match provider.authorize_url() {
501                Ok(url) => (
502                    "302 Found".into(),
503                    format!("Location: {url}\r\nCache-Control: no-store\r\n"),
504                    String::new(),
505                ),
506                Err(e) => auth_error(&e.to_string()),
507            },
508            ("GET", "/auth/callback") => {
509                let (Some(code), Some(state)) = (q("code"), q("state")) else {
510                    // An IdP-side failure comes back as ?error=...; surface
511                    // it rather than a bare "missing code".
512                    return auth_error(&match q("error") {
513                        Some(e) => format!("the identity provider refused the login: {e}"),
514                        None => "callback missing `code` or `state`".to_string(),
515                    });
516                };
517                match provider.complete_login(&code, &state) {
518                    Ok(sid) => (
519                        "302 Found".into(),
520                        format!(
521                            "Location: /\r\nCache-Control: no-store\r\n\
522                             Set-Cookie: {}={sid}; HttpOnly; SameSite=Strict; Path=/{secure}\r\n",
523                            oidc::SESSION_COOKIE
524                        ),
525                        String::new(),
526                    ),
527                    Err(e) => auth_error(&e.to_string()),
528                }
529            }
530            // Logout accepts POST (the console's button, Origin-checked like
531            // every other mutation) and GET (a bookmarkable escape hatch —
532            // and a GET logout is not a CSRF worth defending: forcing someone
533            // to log out grants an attacker nothing).
534            ("POST", "/auth/logout") | ("GET", "/auth/logout") => {
535                if let Some(sid) = cookie_header.and_then(|c| cookie_value(c, oidc::SESSION_COOKIE))
536                {
537                    // Invalidate SERVER-side too. Clearing only the browser's
538                    // copy would leave a cookie captured beforehand alive.
539                    provider.logout(&sid);
540                }
541                (
542                    "302 Found".into(),
543                    format!(
544                        "Location: /\r\nCache-Control: no-store\r\n\
545                         Set-Cookie: {}=; HttpOnly; SameSite=Strict; Path=/{secure}; Max-Age=0\r\n",
546                        oidc::SESSION_COOKIE
547                    ),
548                    String::new(),
549                )
550            }
551            _ => not_found_auth(),
552        }
553    }
554
555    /// Honor an IdP groups header (A2) under the same proxy secret as the
556    /// identity header. Groups resolve through the credential map's `groups`
557    /// table; without a map, this does nothing.
558    pub fn with_sso_groups_header(mut self, header: impl Into<String>) -> Self {
559        let header = header.into();
560        assert!(
561            !header.trim().is_empty(),
562            "with_sso_groups_header requires a non-empty header name"
563        );
564        self.sso_groups_header = Some(header.to_ascii_lowercase());
565        self
566    }
567
568    /// Stamp `prefix` onto every proxy-asserted principal, so IdP-sourced
569    /// identities are visibly distinct from credential-map ones everywhere
570    /// they land — grants, logs, audit grains.
571    pub fn with_sso_principal_prefix(mut self, prefix: impl Into<String>) -> Self {
572        let prefix = prefix.into();
573        assert!(
574            !prefix.trim().is_empty(),
575            "with_sso_principal_prefix requires a non-empty prefix"
576        );
577        self.sso_principal_prefix = Some(prefix);
578        self
579    }
580
581    /// Resolve a proxy-proven request to its principal (A2).
582    ///
583    /// Precedence, most specific first:
584    /// 1. the asserted **identity**, when the file grants it anything;
585    /// 2. the first **group** (in header order) the credential map maps to a
586    ///    principal;
587    /// 3. nothing — the request falls through to `anonymous`.
588    ///
589    /// Identity-before-group is what makes an individual grant able to
590    /// override a role, which is the direction an operator expects (and the
591    /// direction that lets someone be *removed* from an exception without
592    /// editing the group). Groups are consulted only when the identity is
593    /// ungranted, so adding a groups header can never *narrow* a person who
594    /// already had rights.
595    ///
596    /// Returns `(principal, from_group)`. `from_group` matters downstream: a
597    /// role is not a person, so a group-derived principal can never answer an
598    /// approval — "someone in engineering approved this" is not an audit
599    /// record, whatever `--sso-approvals` says.
600    fn resolve_sso_principal(
601        &self,
602        identity: &str,
603        groups_raw: Option<&str>,
604    ) -> Option<(String, bool)> {
605        let stamp = |p: &str| match &self.sso_principal_prefix {
606            Some(prefix) if !p.starts_with(prefix.as_str()) => format!("{prefix}{p}"),
607            _ => p.to_string(),
608        };
609        let identity = stamp(identity);
610
611        // "Has grants of its own" is asked of the FILE, which is the only
612        // authority on rights. A store error answers "no" — falling toward
613        // the group (and ultimately anonymous) rather than assuming rights
614        // we could not read.
615        let identity_granted = self
616            .facade
617            .with_store(|m| m.authz_grants(&identity))
618            .map(|g| !g.is_empty())
619            .unwrap_or(false);
620        if identity_granted {
621            return Some((identity, false));
622        }
623
624        if let (Some(map), Some(raw)) = (&self.credentials, groups_raw) {
625            for group in raw.split(',') {
626                if let Some(principal) = map.principal_for_group(group) {
627                    return Some((principal.to_string(), true));
628                }
629            }
630        }
631
632        // No grants either way: still bind the identity. It is who the proxy
633        // says this is, `bind_principal` resolves it to the same empty grant
634        // set anonymous would get, and an audit line naming the person beats
635        // one naming "anonymous".
636        Some((identity, false))
637    }
638
639    /// Permit proxy-asserted SSO identities to answer HITL approvals
640    /// (`POST /api/run/respond`). **Off by default**: the identity header's
641    /// only proof is the shared, fleet-wide proxy secret, so whoever holds it
642    /// could approve as anyone — including the approver the audit record then
643    /// names.
644    ///
645    /// Turning this on is a deliberate statement that the proxy shared secret
646    /// is held to the same standard as an approver's own credential. That is
647    /// achievable (a proxy on a Unix socket, or mTLS between proxy and
648    /// console, with the secret never leaving the host) — it is just not
649    /// something the console can verify, so it cannot be the default.
650    pub fn with_sso_approvals(mut self, allow: bool) -> Self {
651        self.sso_approvals = allow;
652        self
653    }
654
655    /// Rebuild the executor from the current gate settings. Every builder
656    /// that touches the CAL surface goes through here so `--no-destructive-ops`
657    /// and `--policy` compose instead of clobbering each other's config —
658    /// and the governance host survives every rebuild. (Same shape as
659    /// `areev-mcp`'s; building `CalExecutorConfig::default()` in one builder
660    /// silently re-enabled destruction configured off by another.)
661    fn rebuild_executor(&mut self) {
662        self.executor = CalExecutor::new(CalExecutorConfig {
663            allow_destructive_ops: self.allow_destructive_ops,
664            ..CalExecutorConfig::default()
665        })
666        .with_governance(std::sync::Arc::new(match &self.loop_policy {
667            Some(p) => areev_loop_adapter::LoopGovernance::with_policy(p.clone()),
668            None => areev_loop_adapter::LoopGovernance::new(),
669        }));
670    }
671
672    /// Multi-principal mode: attach the credential map (`areev-auth.json`).
673    /// Tokens resolve to principal names; rights come from the memory
674    /// file's own grant grains; unauthenticated requests run as
675    /// `anonymous`. See `docs/cal-reference.md` §9.
676    pub fn with_credentials(mut self, map: areev_core::authz::CredentialMap) -> Self {
677        self.credentials = Some(map);
678        self
679    }
680
681    /// Attach a host loop policy to the console's loop routes, so a
682    /// console-triggered run honors the same auto-apply grants, denies, and
683    /// severity floors as `areev loop run --policy`.
684    pub fn with_loop_policy(mut self, policy: areev_loop::Policy) -> Self {
685        self.loop_policy = Some(policy);
686        // The CAL surface's governance host honors the same policy.
687        self.rebuild_executor();
688        self
689    }
690
691    /// The loop engine for a request: builtins + the host policy when one
692    /// was attached.
693    fn engine(&self) -> Engine {
694        match &self.loop_policy {
695            Some(p) => Engine::with_builtins().with_policy(p.clone()),
696            None => Engine::with_builtins(),
697        }
698    }
699
700    /// Accept requests whose `Host` header is non-loopback. Off by default so
701    /// the loopback console is protected against DNS-rebinding reads even when
702    /// unauthenticated; the CLI enables it under `--allow-remote`.
703    pub fn allow_remote(mut self, yes: bool) -> Self {
704        self.allow_remote = yes;
705        self
706    }
707
708    /// Accept a cross-origin **POST** whose `Origin` header exactly matches
709    /// this origin (scheme + host[:port], compared case-insensitively with a
710    /// single trailing `/` ignored). Call once per origin to allow — there
711    /// is no wildcard or subdomain form, by design: naming
712    /// `https://console.example.com` must never thereby accept
713    /// `https://evil-console.example.com`.
714    ///
715    /// `--allow-remote` does **not** imply this. The Origin check exists
716    /// because the console authenticates browsers with HTTP Basic, which the
717    /// browser caches and re-attaches to cross-site requests — Origin is the
718    /// only thing telling the console's own page apart from an attacker's
719    /// page riding a viewer's cached credential, so it is CSRF protection,
720    /// not defence in depth, and it stays enforced on every non-loopback
721    /// origin except the ones named here.
722    pub fn allow_origin(mut self, origin: impl Into<String>) -> Self {
723        self.allowed_origins.push(normalize_origin(&origin.into()));
724        self
725    }
726
727    /// Whether `origin` (a request's raw `Origin` header value) matches one
728    /// of the operator-configured [`allow_origin`](Self::allow_origin)
729    /// entries, after normalizing both sides identically. Exact match only.
730    fn origin_is_allowed(&self, origin: &str) -> bool {
731        let normalized = normalize_origin(origin);
732        self.allowed_origins.contains(&normalized)
733    }
734
735    /// Require a shared secret on **every** request (the console page, reads,
736    /// and writes). Browsers authenticate through the native `Basic` prompt
737    /// (any username; password = `token`); scripts may send `Authorization:
738    /// Bearer <token>`. Use this to serve the console to more than a single
739    /// trusted local operator. Pair with a TLS-terminating proxy for non-local
740    /// exposure — the token crosses the wire in the clear otherwise.
741    pub fn with_auth(mut self, token: String) -> Self {
742        // An empty token would authenticate `Authorization: Bearer ` (the
743        // empty credential) as the OWNER — refuse at the library boundary,
744        // not just in the CLI's --token-env validation.
745        assert!(
746            !token.trim().is_empty(),
747            "with_auth requires a non-empty token"
748        );
749        self.token = Some(token);
750        self.auth_all = true;
751        self
752    }
753
754    /// Permit or forbid destructive CAL (`FORGET <hash>`) from the query
755    /// console. Enabled by default; pass `false` to serve a read-only console.
756    /// Since the plain console is unauthenticated, disabling this is the safe
757    /// choice when the console is exposed beyond a trusted local operator.
758    pub fn allow_destructive_ops(mut self, allow: bool) -> Self {
759        self.allow_destructive_ops = allow;
760        self.rebuild_executor();
761        self
762    }
763
764    /// Bind and return the listener (lets callers learn the ephemeral port).
765    pub fn bind(addr: &str) -> std::io::Result<TcpListener> {
766        TcpListener::bind(addr)
767    }
768
769    /// Serve forever on an already-bound listener.
770    pub fn serve(&self, listener: TcpListener) -> std::io::Result<()> {
771        for stream in listener.incoming() {
772            match stream {
773                Ok(s) => {
774                    if let Err(e) = self.handle(s) {
775                        eprintln!("areev ui: request error: {e}");
776                    }
777                }
778                Err(e) => eprintln!("areev ui: accept error: {e}"),
779            }
780        }
781        Ok(())
782    }
783
784    /// Configure native TLS from PEM files (`tls` cargo feature). The
785    /// documented default remains a TLS-terminating proxy; this is for
786    /// deployments with nowhere to put one (edge boxes, appliances).
787    #[cfg(feature = "tls")]
788    pub fn with_tls(mut self, cert_pem: &str, key_pem: &str) -> std::io::Result<Self> {
789        use rustls::pki_types::pem::PemObject;
790        let pem_err =
791            |e: rustls::pki_types::pem::Error| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string());
792        let certs: Vec<rustls::pki_types::CertificateDer<'static>> =
793            rustls::pki_types::CertificateDer::pem_file_iter(cert_pem)
794                .map_err(pem_err)?
795                .collect::<Result<_, _>>()
796                .map_err(pem_err)?;
797        let key =
798            rustls::pki_types::PrivateKeyDer::from_pem_file(key_pem).map_err(pem_err)?;
799        let config = rustls::ServerConfig::builder()
800            .with_no_client_auth()
801            .with_single_cert(certs, key)
802            .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
803        self.tls_config = Some(std::sync::Arc::new(config));
804        Ok(self)
805    }
806
807    /// Record one failed authentication attempt from `ip` (issue #126) and
808    /// return the new consecutive-failure count — for the caller to LOG,
809    /// never to sleep on. An entry idle for more than
810    /// `AUTH_FAILURE_IDLE_SECS` is treated as reset rather than accumulating
811    /// forever. The map is bounded at `MAX_AUTH_FAILURE_IPS` — the key is
812    /// one attacker-controlled source IP per entry, so an unbounded map here
813    /// would itself be a memory-exhaustion vector; past the cap, the
814    /// stalest entry is evicted before the new one is inserted. The lock is
815    /// held only for this bookkeeping — never across the response write.
816    ///
817    /// **Deliberately no artificial delay or lockout on this count.**
818    /// `UiServer::serve` is a strictly SERIAL accept loop — one connection
819    /// handled at a time, per its own doc comment — so a `std::thread::sleep`
820    /// here, before a response is written, would not slow down one
821    /// attacker: it would stall the entire console for every caller,
822    /// including the operator, behind whatever backoff an unauthenticated
823    /// caller chooses to trigger by sending bad credentials. That trade is
824    /// worse than the problem this counter exists to fix (a 401 that reads
825    /// as ordinary traffic in the log). Rate limiting belongs in front of
826    /// this server — the TLS-terminating proxy the deployment profile
827    /// already calls for — where it can also see the caller's real address;
828    /// this count and the log line built from it are what such a rule is
829    /// written against. Do not reintroduce a sleep/lockout here without
830    /// first making the accept loop concurrent.
831    fn note_auth_failure(&self, ip: std::net::IpAddr) -> u32 {
832        let now = std::time::Instant::now();
833        let idle = Duration::from_secs(AUTH_FAILURE_IDLE_SECS);
834        let mut map = self
835            .auth_failures
836            .lock()
837            .unwrap_or_else(std::sync::PoisonError::into_inner);
838        match map.get_mut(&ip) {
839            Some((n, last)) if now.duration_since(*last) < idle => {
840                *n = n.saturating_add(1);
841                *last = now;
842                *n
843            }
844            Some(entry) => {
845                *entry = (1, now);
846                1
847            }
848            None => {
849                if map.len() >= MAX_AUTH_FAILURE_IPS {
850                    if let Some(stalest) =
851                        map.iter().min_by_key(|(_, (_, last))| *last).map(|(k, _)| *k)
852                    {
853                        map.remove(&stalest);
854                    }
855                }
856                map.insert(ip, (1, now));
857                1
858            }
859        }
860    }
861
862    /// Clear a source IP's failure streak after it authenticates successfully.
863    fn reset_auth_failures(&self, ip: std::net::IpAddr) {
864        let mut map = self
865            .auth_failures
866            .lock()
867            .unwrap_or_else(std::sync::PoisonError::into_inner);
868        map.remove(&ip);
869    }
870
871    /// A source IP's current consecutive-failure count (0 if it has none, or
872    /// if its last failure is older than `AUTH_FAILURE_IDLE_SECS`).
873    ///
874    /// The idle check is applied on READ as well as on write so a lockout
875    /// expires on its own: a stale entry that `note_auth_failure` has not
876    /// been called on again must not keep refusing forever, which is what
877    /// turns a brute-force brake into a self-inflicted outage.
878    fn auth_failure_count(&self, ip: std::net::IpAddr) -> u32 {
879        let idle = Duration::from_secs(AUTH_FAILURE_IDLE_SECS);
880        self.auth_failures
881            .lock()
882            .unwrap_or_else(std::sync::PoisonError::into_inner)
883            .get(&ip)
884            .filter(|(_, last)| last.elapsed() < idle)
885            .map(|(n, _)| *n)
886            .unwrap_or(0)
887    }
888
889    /// Test-only alias — tests read the counter directly because there is no
890    /// sleep to observe via timing (see `note_auth_failure`).
891    #[cfg(test)]
892    fn auth_failure_count_for_test(&self, ip: std::net::IpAddr) -> u32 {
893        self.auth_failure_count(ip)
894    }
895
896    /// Wrap an accepted socket per the TLS posture. The handshake happens
897    /// lazily on first read/write; a plaintext client fails it loudly.
898    fn wrap(&self, stream: TcpStream) -> std::io::Result<Conn> {
899        #[cfg(feature = "tls")]
900        if let Some(config) = &self.tls_config {
901            let session = rustls::ServerConnection::new(std::sync::Arc::clone(config))
902                .map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
903            return Ok(Conn::Tls(Box::new(rustls::StreamOwned::new(session, stream))));
904        }
905        Ok(Conn::Plain(stream))
906    }
907
908    fn handle(&self, stream: TcpStream) -> std::io::Result<()> {
909        // The server handles one connection at a time, so a single slow-drip
910        // client could otherwise hold it open indefinitely (a per-read timeout
911        // resets on every byte). A watchdog thread enforces a hard wall-clock
912        // deadline by shutting the socket down; it is unparked and joined the
913        // instant the request completes, so it adds no latency in the fast path.
914        let watchdog_stream = stream.try_clone()?;
915        let done = std::sync::Arc::new(std::sync::atomic::AtomicBool::new(false));
916        let watchdog_done = std::sync::Arc::clone(&done);
917        let watchdog = std::thread::spawn(move || {
918            std::thread::park_timeout(Duration::from_secs(REQUEST_DEADLINE_SECS));
919            if !watchdog_done.load(std::sync::atomic::Ordering::Acquire) {
920                let _ = watchdog_stream.shutdown(std::net::Shutdown::Both);
921            }
922        });
923        let raw = stream.try_clone()?;
924        let conn = self.wrap(stream)?;
925        let result = self.handle_request(&raw, conn);
926        done.store(true, std::sync::atomic::Ordering::Release);
927        watchdog.thread().unpark();
928        let _ = watchdog.join();
929        result
930    }
931
932    fn handle_request(&self, raw: &TcpStream, mut conn: Conn) -> std::io::Result<()> {
933        // Bound slow clients (slowloris) and total bytes read from the
934        // connection so a malicious client cannot stall the server or exhaust
935        // memory with an oversized request line / headers / body. The
936        // timeouts sit on the RAW socket, so they bound TLS handshakes too.
937        raw.set_read_timeout(Some(Duration::from_secs(READ_TIMEOUT_SECS)))?;
938        raw.set_write_timeout(Some(Duration::from_secs(READ_TIMEOUT_SECS)))?;
939        let mut reader = BufReader::new((&mut conn).take(MAX_CONN_BYTES));
940        let mut request_line = String::new();
941        reader.read_line(&mut request_line)?;
942        let mut parts = request_line.split_whitespace();
943        let method = parts.next().unwrap_or("").to_string();
944        let path = parts.next().unwrap_or("/").to_string();
945
946        // headers → content-length + authorization + origin + host
947        let mut content_length = 0usize;
948        let mut bearer: Option<String> = None;
949        let mut origin: Option<String> = None;
950        let mut host: Option<String> = None;
951        let mut sso_identity_raw: Option<String> = None;
952        let mut sso_groups_raw: Option<String> = None;
953        #[cfg(feature = "oidc")]
954        let mut cookie_header: Option<String> = None;
955        let mut proxy_secret: Option<String> = None;
956        let mut header_bytes = 0usize;
957        let mut header_count = 0usize;
958        loop {
959            let mut line = String::new();
960            let n = reader.read_line(&mut line)?;
961            if n == 0 {
962                break; // EOF / connection closed by peer
963            }
964            header_bytes += n;
965            header_count += 1;
966            if header_bytes > MAX_HEADER_BYTES || header_count > MAX_HEADERS {
967                return Err(std::io::Error::new(
968                    std::io::ErrorKind::InvalidData,
969                    "request headers too large",
970                ));
971            }
972            let l = line.trim();
973            if l.is_empty() {
974                break;
975            }
976            let low = l.to_ascii_lowercase();
977            if let Some(v) = low.strip_prefix("content-length:") {
978                content_length = v.trim().parse().unwrap_or(0);
979            }
980            if let Some(v) = low.strip_prefix("origin:") {
981                origin = Some(v.trim().to_string());
982            }
983            if let Some(v) = low.strip_prefix("host:") {
984                host = Some(v.trim().to_string());
985            }
986            #[cfg(feature = "oidc")]
987            if low.starts_with("cookie:") {
988                // Re-slice the ORIGINAL line: the lowercased copy would
989                // mangle a base64url session id.
990                if let Some((_, orig)) = l.split_once(':') {
991                    cookie_header = Some(orig.trim().to_string());
992                }
993            }
994            if let Some(v) = low.strip_prefix("x-areev-proxy-secret:") {
995                // Secrets are compared, never logged; the lowercased copy is
996                // wrong for comparison, so re-slice the original line.
997                let _ = v;
998                if let Some((_, orig)) = l.split_once(':') {
999                    proxy_secret = Some(orig.trim().to_string());
1000                }
1001            }
1002            if let Some(h) = &self.sso_header {
1003                if low.starts_with(&format!("{h}:")) {
1004                    if let Some((_, orig)) = l.split_once(':') {
1005                        sso_identity_raw = Some(orig.trim().to_string());
1006                    }
1007                }
1008            }
1009            if let Some(h) = &self.sso_groups_header {
1010                if low.starts_with(&format!("{h}:")) {
1011                    if let Some((_, orig)) = l.split_once(':') {
1012                        sso_groups_raw = Some(orig.trim().to_string());
1013                    }
1014                }
1015            }
1016            if low.starts_with("authorization:") {
1017                if let Some((_, v)) = l.split_once(':') {
1018                    let v = v.trim();
1019                    // Accept `Bearer <token>` (scripts/CLI) or HTTP `Basic`
1020                    // (browsers, via the native login prompt). For Basic the
1021                    // credential is base64(user:pass) and the token is the
1022                    // password; the username is ignored.
1023                    bearer = if let Some(t) =
1024                        v.strip_prefix("Bearer ").or_else(|| v.strip_prefix("bearer "))
1025                    {
1026                        Some(t.trim().to_string())
1027                    } else if let Some(b64) =
1028                        v.strip_prefix("Basic ").or_else(|| v.strip_prefix("basic "))
1029                    {
1030                        basic_auth_password(b64.trim())
1031                    } else {
1032                        Some(v.to_string())
1033                    };
1034                }
1035            }
1036        }
1037        // DNS-rebinding defense: a browser tricked into pointing an attacker
1038        // domain at 127.0.0.1 sends that domain in the Host header. Unless the
1039        // operator opted into remote serving, reject any non-loopback Host on
1040        // *every* method (the Origin check below only covers POST, so this is
1041        // what stops a drive-by page from reading memory over GET). A missing
1042        // Host (bare HTTP/1.0, some CLI clients) passes, mirroring Origin.
1043        if !self.allow_remote && host.as_deref().is_some_and(|h| !host_is_local(h)) {
1044            let payload = br#"{"ok":false,"error":"non-loopback Host rejected (use --allow-remote to serve remotely)"}"#;
1045            drop(reader);
1046            let mut out = conn;
1047            write!(
1048                out,
1049                "HTTP/1.1 403 Forbidden\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
1050                payload.len()
1051            )?;
1052            out.write_all(payload)?;
1053            return out.flush();
1054        }
1055
1056        let mut body = vec![0u8; content_length.min(1 << 20)];
1057        if content_length > 0 {
1058            reader.read_exact(&mut body)?;
1059        }
1060
1061        // Cross-origin drive-by protection: browsers attach an Origin header
1062        // to cross-site requests. Only loopback pages (the console itself),
1063        // or an origin the operator explicitly allowlisted with
1064        // --allow-origin (issue #125), may mutate; curl/CLI clients send no
1065        // Origin and pass through. `--allow-remote` does NOT lift this on
1066        // its own — see `allow_origin`'s doc comment for why.
1067        if method == "POST"
1068            && origin
1069                .as_deref()
1070                .is_some_and(|o| !origin_is_local(o) && !self.origin_is_allowed(o))
1071        {
1072            let payload = br#"{"ok":false,"error":"cross-origin request rejected"}"#;
1073            drop(reader);
1074            let mut out = conn;
1075            write!(
1076                out,
1077                "HTTP/1.1 403 Forbidden\r\nContent-Type: application/json\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
1078                payload.len()
1079            )?;
1080            out.write_all(payload)?;
1081            return out.flush();
1082        }
1083
1084        // Captured before the move below: issue #126's failure counter needs
1085        // to know whether a proxy secret was PRESENTED at all (a
1086        // wrong-guess attempt to count), independent of `sso_identity`,
1087        // which by then only ever reflects a *proven* identity.
1088        let proxy_secret_presented = proxy_secret.is_some();
1089        // The identity header is TRUSTED only when the proxy proved itself
1090        // (constant-time secret check). A forged header without the secret
1091        // is silently ignored — the request proceeds as whatever its other
1092        // credentials make it.
1093        //
1094        // [A2] The secret proves the PROXY; it says nothing about whether
1095        // what the proxy forwarded is well-formed. `sanitize_sso_identity`
1096        // is that second question, and a rejected identity is treated
1097        // exactly like an absent one.
1098        let proxy_proved = match &proxy_secret {
1099            Some(presented) => {
1100                // `fold`, not `any`: `any` short-circuits on the first match,
1101                // so during a rotation window the response time would say
1102                // WHICH secret was presented. Every configured secret is
1103                // compared, every time.
1104                self.sso_secrets
1105                    .iter()
1106                    .fold(false, |acc, s| ct_eq(s.as_bytes(), presented.as_bytes()) | acc)
1107            }
1108            None => false,
1109        };
1110        let sso_identity: Option<String> = sso_identity_raw
1111            .filter(|_| proxy_proved)
1112            .as_deref()
1113            .and_then(sanitize_sso_identity);
1114        let sso_groups: Option<String> = sso_groups_raw.filter(|_| proxy_proved);
1115        // [A3] The OIDC login endpoints own their own responses: they answer
1116        // with redirects and `Set-Cookie`, which the JSON `route` contract
1117        // (status, content-type, body) has no slot for. Handled here, where
1118        // the socket and its headers are already in hand.
1119        #[cfg(feature = "oidc")]
1120        if self.oidc.is_some() && path.starts_with("/auth/") {
1121            let (status, extra, payload) =
1122                self.oidc_route(&method, &path, cookie_header.as_deref());
1123            drop(reader);
1124            let mut out = conn;
1125            write!(
1126                out,
1127                "HTTP/1.1 {status}\r\n{extra}Content-Length: {}\r\nConnection: close\r\n\r\n",
1128                payload.len()
1129            )?;
1130            out.write_all(payload.as_bytes())?;
1131            return out.flush();
1132        }
1133        #[cfg(feature = "oidc")]
1134        let oidc_principal = self.oidc_principal(cookie_header.as_deref());
1135        #[cfg(not(feature = "oidc"))]
1136        let oidc_principal: Option<String> = None;
1137
1138        // [A1] Fail closed after repeated failures from one source.
1139        //
1140        // This REJECTS; it never delays. `serve` is a strictly serial accept
1141        // loop, so a per-request sleep would be a lever an unauthenticated
1142        // caller pulls to stall the console for everyone (the reasoning
1143        // recorded on `note_auth_failure`, which is why that counter only
1144        // ever counted). Rejection has the opposite shape: it is cheaper than
1145        // serving, costs the attacker their pipeline, and — placed HERE,
1146        // before `route` — touches no store, runs no constant-time scan, and
1147        // dispatches nothing.
1148        //
1149        // Only when auth is configured AND a credential was presented, the
1150        // same two conditions the counter itself uses: a console with no auth
1151        // has nothing to brute-force, and a browser's first credential-less
1152        // probe is not an attempt.
1153        //
1154        // AND NOT through a trusted proxy. Behind the documented deployment
1155        // (a TLS-terminating, authenticating proxy) every request shares the
1156        // proxy's source address, so a per-IP lockout would let one attacker's
1157        // ten bad guesses refuse *every* user behind it — a self-inflicted
1158        // outage in exactly the configuration we recommend. A request carrying
1159        // a verified proxy secret is proven to have come through that proxy,
1160        // and per-source throttling there is the proxy's job (the same
1161        // division of labour as TLS and the IdP handshake). Direct connections
1162        // — which is every connection on a console with no proxy — are
1163        // unaffected and still brake.
1164        let auth_configured =
1165            self.token.is_some() || self.credentials.is_some() || !self.sso_secrets.is_empty();
1166        let credential_presented = bearer.is_some() || proxy_secret_presented;
1167        if auth_configured && credential_presented && !proxy_proved {
1168            if let Ok(peer) = raw.peer_addr() {
1169                if self.auth_failure_count(peer.ip()) >= MAX_CONSECUTIVE_AUTH_FAILURES {
1170                    let payload = format!(
1171                        r#"{{"ok":false,"error":"too many failed authentications from this address - wait {} minutes, or restart the console to clear the counter"}}"#,
1172                        AUTH_FAILURE_IDLE_SECS / 60
1173                    );
1174                    drop(reader);
1175                    let mut out = conn;
1176                    write!(
1177                        out,
1178                        "HTTP/1.1 429 Too Many Requests\r\nContent-Type: application/json\r\nRetry-After: {}\r\nContent-Length: {}\r\nConnection: close\r\n\r\n",
1179                        AUTH_FAILURE_IDLE_SECS,
1180                        payload.len()
1181                    )?;
1182                    out.write_all(payload.as_bytes())?;
1183                    return out.flush();
1184                }
1185            }
1186        }
1187
1188        let (status, ctype, payload) = self.route_full(
1189            &method,
1190            &path,
1191            &body,
1192            bearer.as_deref(),
1193            sso_identity.as_deref(),
1194            sso_groups.as_deref(),
1195            oidc_principal.as_deref(),
1196        );
1197
1198        // Auth-failure count + log (issue #126). Only when this server has
1199        // SOME auth mechanism configured, AND a credential was actually
1200        // PRESENTED (a Bearer/Basic value, or a proxy secret) — a plain
1201        // unauthenticated request (a token-less console's read-only-write
1202        // refusal, a browser's very first Basic-auth probe with no
1203        // Authorization header at all) attempted nothing and has no failure
1204        // to count. The token itself is never logged, not even a prefix.
1205        //
1206        // Deliberately no in-process DELAY here — see the comment on
1207        // `note_auth_failure` for why an artificial delay would itself be the
1208        // more dangerous bug on this server. The cheap rejection above is the
1209        // lockout; this is the counter that feeds it.
1210        if auth_configured && credential_presented {
1211            if let Ok(peer) = raw.peer_addr() {
1212                let ip = peer.ip();
1213                if status.starts_with("401") {
1214                    let consecutive = self.note_auth_failure(ip);
1215                    eprintln!("areev: console auth FAILED from {ip} ({consecutive} consecutive)");
1216                } else {
1217                    self.reset_auth_failures(ip);
1218                }
1219            }
1220        }
1221
1222        // On a console-auth 401, challenge with Basic so browsers show the
1223        // native login prompt (any username; password = token).
1224        let auth_challenge = if self.auth_all && status.starts_with("401") {
1225            "WWW-Authenticate: Basic realm=\"Areev console\", charset=\"UTF-8\"\r\n"
1226        } else {
1227            ""
1228        };
1229        drop(reader);
1230        let mut out = conn;
1231        write!(
1232            out,
1233            "HTTP/1.1 {status}\r\nContent-Type: {ctype}\r\n{auth_challenge}Content-Length: {}\r\nConnection: close\r\n\r\n",
1234            payload.len()
1235        )?;
1236        out.write_all(&payload)?;
1237        out.flush()
1238    }
1239
1240    /// The runtime driver for the /api/run/* surface. The console never
1241    /// EXECUTES tools (list/inspect read; respond/cancel journal) — the
1242    /// executor is a refusing stub, present only to satisfy the driver
1243    /// shape.
1244    fn runner(&self, principal: &str) -> areev_run::Runner {
1245        struct NoExec;
1246        impl areev_run::HostToolExecutor for NoExec {
1247            fn execute(
1248                &self,
1249                tool_name: &str,
1250                _h: &str,
1251                _i: &Value,
1252                _k: &str,
1253            ) -> areev_run::ExecResult {
1254                areev_run::ExecResult::Err {
1255                    cause: areev_run::FailCause::ExecutorError,
1256                    detail: format!("the console does not execute tools ('{tool_name}')"),
1257                }
1258            }
1259        }
1260        areev_run::Runner {
1261            facade: std::sync::Arc::clone(&self.facade),
1262            clock: std::sync::Arc::new(areev_run::SystemClock),
1263            executor: std::sync::Arc::new(NoExec),
1264            llm: None,
1265            observer: None,
1266            ns: self
1267                .facade
1268                .default_namespace()
1269                .unwrap_or("shared")
1270                .to_string(),
1271            principal: principal.to_string(),
1272        }
1273    }
1274
1275    /// The no-groups spelling, for the tests written before A2 added the
1276    /// groups header. Production always goes through
1277    /// [`route_with_groups`](Self::route_with_groups); this exists only so
1278    /// two dozen assertions that never had a groups header do not each grow
1279    /// a trailing `None`.
1280    #[cfg(test)]
1281    fn route(
1282        &self,
1283        method: &str,
1284        path: &str,
1285        body: &[u8],
1286        bearer: Option<&str>,
1287        sso_identity: Option<&str>,
1288    ) -> (&'static str, &'static str, Vec<u8>) {
1289        self.route_full(method, path, body, bearer, sso_identity, None, None)
1290    }
1291
1292    /// [`route`](Self::route) with the A2 groups header but no OIDC session —
1293    /// the spelling the A2 tests use.
1294    #[cfg(test)]
1295    fn route_with_groups(
1296        &self,
1297        method: &str,
1298        path: &str,
1299        body: &[u8],
1300        bearer: Option<&str>,
1301        sso_identity: Option<&str>,
1302        sso_groups: Option<&str>,
1303    ) -> (&'static str, &'static str, Vec<u8>) {
1304        self.route_full(method, path, body, bearer, sso_identity, sso_groups, None)
1305    }
1306
1307    /// Route one request.
1308    ///
1309    /// `sso_groups` is the raw, proxy-proven groups header (A2) and
1310    /// `oidc_principal` the identity behind a valid session cookie (A3) —
1311    /// both already authenticated by the caller, which is why neither
1312    /// carries its proof this far down.
1313    #[allow(clippy::too_many_arguments)]
1314    fn route_full(
1315        &self,
1316        method: &str,
1317        path: &str,
1318        body: &[u8],
1319        bearer: Option<&str>,
1320        sso_identity: Option<&str>,
1321        sso_groups: Option<&str>,
1322        oidc_principal: Option<&str>,
1323    ) -> (&'static str, &'static str, Vec<u8>) {
1324        // Auth: console-auth mode (`auth_all`) guards every request;
1325        // otherwise the token guards only mutating endpoints. The credential
1326        // arrives as a Bearer token or an HTTP Basic password (see
1327        // `handle_request`).
1328        //
1329        // A credential map changes WHO a request is, not WHETHER the shared
1330        // secret is required. When both are configured, a map token
1331        // authenticates as its principal, the shared secret authenticates as
1332        // the implied admin (`docs/cal-all-you-need-proposal.md` §5.2b), and
1333        // anything else is refused — an attached map must never silently
1334        // downgrade a `--token-env` console to an open `anonymous` one.
1335        let mut shared_secret_ok = false;
1336        if let Some(tok) = &self.token {
1337            let guarded = self.auth_all || method == "POST";
1338            shared_secret_ok = bearer.is_some_and(|b| ct_eq(b.as_bytes(), tok.as_bytes()));
1339            let known = shared_secret_ok
1340                || sso_identity.is_some()
1341                || oidc_principal.is_some()
1342                || match (&self.credentials, bearer) {
1343                    (Some(map), Some(b)) => map.resolve_for_memory(b, &self.db_label).is_ok(),
1344                    _ => false,
1345                };
1346            if guarded && !known {
1347                return ("401 Unauthorized", "application/json",
1348                        br#"{"ok":false,"error":"authentication required"}"#.to_vec());
1349            }
1350        } else if self.credentials.is_none()
1351            && sso_identity.is_none()
1352            && oidc_principal.is_none()
1353            && method == "POST"
1354        {
1355            // §5.7: token-less `areev ui` is read-only. The ONLY POST allowed is
1356            // a read-only CAL statement; every write (any loop mutation, an
1357            // ADD/SUPERSEDE/FORGET CAL batch, etc.) requires --token-env. This
1358            // closes the bypass where a local process could execute a
1359            // proposal's CAL directly and skip the review queue.
1360            let base = path.split_once('?').map(|(p, _)| p).unwrap_or(path);
1361            let allowed = base == "/api/cal" && cal_body_is_read_only(body);
1362            if !allowed {
1363                return ("401 Unauthorized", "application/json",
1364                        br#"{"ok":false,"error":"read-only console: restart areev ui with --auth <map> (per-principal credentials, attributable writes and approvals) or --token-env VAR (one shared secret, writes only) to enable writes"}"#.to_vec());
1365            }
1366        }
1367
1368        // Credential-map mode: resolve this request's principal and bind
1369        // the facade for the request's duration (the guard restores the
1370        // owner default on drop). Every verb check downstream — CAL,
1371        // loop scopes, the run gate below — now answers for THIS caller.
1372        // A request authenticated by the shared secret keeps the owner
1373        // session that secret has always carried.
1374        let _session = match (&self.credentials, shared_secret_ok) {
1375            (Some(map), false) => Some(RequestBinding::bind(&self.facade, map, bearer, &self.db_label)),
1376            _ => None,
1377        };
1378        // The request's resolved PRINCIPAL identity — Some only when a
1379        // per-principal credential authenticated it. The shared secret and
1380        // anonymous access deliberately resolve to None: approvals need an
1381        // identity, and "whoever holds the console token" is not one.
1382        let mut request_principal: Option<String> = match (&self.credentials, shared_secret_ok, bearer)
1383        {
1384            (Some(map), false, Some(b)) => map.resolve_for_memory(b, &self.db_label).ok().map(str::to_string),
1385            _ => None,
1386        };
1387        // SSO v0: a proxy-proven identity becomes the request principal when
1388        // no bearer credential resolved one (machines keep tokens; SSO is
1389        // for humans behind the proxy). Rights come from the file's grants.
1390        //
1391        // `principal_from_sso` records the PROVENANCE of the identity, not
1392        // just its value: everything downstream treats a principal as a
1393        // principal, but one control (HITL approval) must distinguish an
1394        // identity proven by a per-principal credential from one asserted by
1395        // a proxy holding a shared secret. See `sso_approvals`.
1396        let mut principal_from_sso = false;
1397        // [A2] `principal_from_group` is a strictly stronger refusal than
1398        // `principal_from_sso`: a role name cannot be an approver's identity
1399        // under ANY setting, because there is no person behind it.
1400        let mut principal_from_group = false;
1401        // [A3] An OIDC session outranks a proxy-asserted header: its identity
1402        // was proven by a signature this process verified against the
1403        // issuer's key set, not by a shared secret whose holder can assert
1404        // anyone. That is the whole reason native OIDC exists here, so it is
1405        // resolved FIRST and the header path never overwrites it.
1406        let mut principal_from_oidc = false;
1407        let _oidc_session = match (&request_principal, oidc_principal, shared_secret_ok) {
1408            (None, Some(principal), false) => {
1409                principal_from_oidc = true;
1410                let binding = RequestBinding::bind_identity(&self.facade, principal);
1411                request_principal = Some(principal.to_string());
1412                Some(binding)
1413            }
1414            _ => None,
1415        };
1416        let _sso_session = match (&request_principal, sso_identity, shared_secret_ok) {
1417            (None, Some(identity), false) => {
1418                match self.resolve_sso_principal(identity, sso_groups) {
1419                    Some((principal, from_group)) => {
1420                        principal_from_sso = true;
1421                        principal_from_group = from_group;
1422                        let binding = RequestBinding::bind_identity(&self.facade, &principal);
1423                        request_principal = Some(principal);
1424                        Some(binding)
1425                    }
1426                    None => None,
1427                }
1428            }
1429            _ => None,
1430        };
1431
1432        let (path, query) = match path.split_once('?') {
1433            Some((p, q)) => (p, q),
1434            None => (path, ""),
1435        };
1436        let q = |key: &str| -> Option<String> {
1437            query.split('&').find_map(|kv| {
1438                let (k, v) = kv.split_once('=')?;
1439                (k == key).then(|| urldecode(v))
1440            })
1441        };
1442        match (method, path) {
1443            ("GET", "/") => (
1444                "200 OK",
1445                "text/html; charset=utf-8",
1446                CONSOLE_HTML
1447                    .replace("{{DB}}", &html_escape(&self.db_label_display))
1448                    .into_bytes(),
1449            ),
1450            ("POST", "/api/cal") => {
1451                let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1452                let queryt = req.get("query").and_then(|v| v.as_str()).unwrap_or("");
1453                match self.executor.execute(queryt, &*self.facade) {
1454                    Ok(res) => ok_json(json!({
1455                        "ok": true,
1456                        "result": res.result,
1457                        "warnings": res.warnings,
1458                        "statement": res.metadata.statement_type,
1459                        "elapsed_ms": res.metadata.execution_time_ms,
1460                    })),
1461                    Err(e) => {
1462                        // Structured error: code + span + hint let the console
1463                        // point at the offending token instead of just quoting.
1464                        let mut err = json!({
1465                            "ok": false,
1466                            "error": e.sanitize_for_client(),
1467                            "code": e.code(),
1468                        });
1469                        if let Some(sp) = e.span() {
1470                            err["span"] = json!({
1471                                "start": sp.start, "end": sp.end,
1472                                "line": sp.line, "col": sp.col,
1473                            });
1474                        }
1475                        if let Some(hint) = e.suggestion() {
1476                            err["suggestion"] = json!(hint);
1477                        }
1478                        // The store's code survives sanitizing: it is a
1479                        // bounded constant, and the one a host routes on.
1480                        if let Some(sc) = e.store_code() {
1481                            err["store_code"] = json!(sc);
1482                        }
1483                        ok_json(err)
1484                    }
1485                }
1486            }
1487            ("GET", "/api/stats") => {
1488                let stats = self.facade.with_store(|m| m.stats());
1489                match stats {
1490                    Ok(s) => ok_json(json!({
1491                        "db": self.db_label_display,
1492                        "grains": s.grains, "current": s.current,
1493                        "triples": s.triples, "terms": s.terms,
1494                        "ops": s.ops, "events_indexed": s.events_indexed,
1495                    })),
1496                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1497                }
1498            }
1499            ("GET", "/api/log") => {
1500                let since: i64 = q("since").and_then(|v| v.parse().ok()).unwrap_or(0);
1501                let limit: usize = q("limit").and_then(|v| v.parse().ok()).unwrap_or(100);
1502                match self.facade.with_store(|m| m.changes_since(since, limit)) {
1503                    Ok(ops) => {
1504                        let rows: Vec<Value> = ops
1505                            .iter()
1506                            .map(|o| {
1507                                json!({
1508                                    "op_seq": o.op_seq, "hlc": o.hlc,
1509                                    "op": match o.op { 1 => "add", 2 => "supersede", 3 => "forget", _ => "?" },
1510                                    "hash": o.hash.to_hex(),
1511                                })
1512                            })
1513                            .collect();
1514                        ok_json(json!(rows))
1515                    }
1516                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1517                }
1518            }
1519            // Who am I: the principal this request is bound to. In
1520            // credential-map mode this reflects the per-request binding;
1521            // read-only and mode-agnostic.
1522            ("GET", "/api/whoami") => {
1523                let a = self.facade.authz();
1524                ok_json(json!({
1525                    "ok": true,
1526                    "principal": a.principal(),
1527                    "owner": a.is_owner(),
1528                    "mode": if self.credentials.is_some() { "credentials" }
1529                            else if self.token.is_some() { "token" }
1530                            else { "open" },
1531                    // Provenance, not just value: the console shows an
1532                    // approver whether their identity is one this instance
1533                    // will accept on `run.respond` BEFORE they try it.
1534                    "identity_source": if principal_from_oidc { "oidc" }
1535                                       else if principal_from_group { "sso-group" }
1536                                       else if principal_from_sso { "sso" }
1537                                       else if request_principal.is_some() { "credential" }
1538                                       else { "none" },
1539                    "may_approve": request_principal.is_some()
1540                        && !principal_from_group
1541                        && (!principal_from_sso || self.sso_approvals),
1542                }))
1543            }
1544            ("GET", "/api/config") => {
1545                // Read-only observability: the *effective* per-process
1546                // configuration. Nothing here is persisted in the .db —
1547                // the file holds data only; these values are supplied by
1548                // the host at open time.
1549                let cfg = self.executor.config();
1550                let (index_text, embedder_dim, declared_embed, mut warnings) =
1551                    self.facade.with_store(|m| {
1552                        (
1553                            m.index_text_enabled(),
1554                            m.embedder_dim(),
1555                            m.declared_embedding().map(|(mm, d)| (mm.to_string(), d)),
1556                            m.open_warnings().to_vec(),
1557                        )
1558                    });
1559                if let Some((m, d)) = &declared_embed {
1560                    if embedder_dim.is_none() {
1561                        warnings.push(format!(
1562                            "vector leg dormant: file expects {m}@{d}, no embedding backend installed"
1563                        ));
1564                    }
1565                }
1566                // Saved queries and templates the file carries that this build
1567                // cannot load. Same class of thing as an open warning — the
1568                // file and the host disagree — so it belongs in the same list.
1569                warnings.extend(self.facade.meta_warnings());
1570                // Anonymization observability (REQ-ANON-5: no policy is
1571                // loud): declared per-ns modes, the host floor, and the
1572                // audit counters. Modes only — never mappings or values.
1573                let (anon_declared, anon_floor, anon_audit) = self.facade.with_store(|m| {
1574                    (
1575                        m.anon_declared(),
1576                        m.anonymize_egress_floor(),
1577                        m.anon_audit_counts(),
1578                    )
1579                });
1580                let anonymization = json!({
1581                    "policies": anon_declared
1582                        .iter()
1583                        .map(|(ns, mode)| json!({"ns": ns, "mode": mode}))
1584                        .collect::<Vec<_>>(),
1585                    "floor": anon_floor,
1586                    "audit_counts": anon_audit
1587                        .iter()
1588                        .map(|(ns, cat, n)| json!({"ns": ns, "category": cat, "count": n}))
1589                        .collect::<Vec<_>>(),
1590                });
1591                ok_json(json!({
1592                    "ok": true,
1593                    "db": self.db_label_display,
1594                    "warnings": warnings,
1595                    "anonymization": anonymization,
1596                    "file": {
1597                        "text_index": index_text,
1598                        "embedding": declared_embed.map(|(m, d)| json!({"model": m, "dim": d})),
1599                    },
1600                    "session": {
1601                        "namespace": self.facade.session_namespace(),
1602                        "mounts": self.facade.mount_aliases(),
1603                        // Gates the console's "All namespaces" picker option —
1604                        // same rule /api/browse?ns=* and /api/namespaces enforce.
1605                        "is_owner": self.facade.authz().is_owner(),
1606                    },
1607                    "store": {
1608                        "index_text": index_text,
1609                        "embedder": embedder_dim.map(|d| json!({"dim": d})),
1610                    },
1611                    "recall": {
1612                        "fusion": "rrf",
1613                        "rrf_k0": areev_store::RRF_K0,
1614                        "overfetch_factor": AreevFacade::RECALL_OVERFETCH,
1615                        "legs": {
1616                            "structural": true,
1617                            "bm25": index_text,
1618                            "vector": embedder_dim.is_some(),
1619                        },
1620                    },
1621                    "executor": {
1622                        "max_limit": cfg.max_limit,
1623                        "default_limit": cfg.default_limit,
1624                        "tier1_writes": cfg.tier1_enabled,
1625                        "namespace_override": cfg.namespace_override,
1626                        "user_id_override": cfg.user_id_override,
1627                    },
1628                    "server": {
1629                        "auth_required": self.token.is_some(),
1630                        // true = every request is authenticated (console-auth);
1631                        // false with auth_required = writes only.
1632                        "auth_all": self.auth_all,
1633                    },
1634                    "persistence": "per-process (host-supplied at open) — not stored in the .db",
1635                }))
1636            }
1637            ("GET", "/api/browse") => {
1638                // Browse-without-queries: the tail of the op-log joined with
1639                // grain summaries, newest first. Supersession and tombstone
1640                // status are resolved within the returned window so the
1641                // console can dim/strike them (grains are never mutated —
1642                // this reads the index + immutable blobs only).
1643                let limit: i64 = q("limit")
1644                    .and_then(|v| v.parse().ok())
1645                    .unwrap_or(500)
1646                    .clamp(1, 2000);
1647                let built = self.facade.with_store(|m| -> Result<(i64, Vec<Value>), String> {
1648                    let total = m.stats().map_err(|e| e.to_string())?.ops as i64;
1649                    let after = (total - limit).max(0);
1650                    let ops = m.changes_since(after, limit as usize).map_err(|e| e.to_string())?;
1651                    let mut rows: Vec<Value> = Vec::with_capacity(ops.len());
1652                    let mut idx: std::collections::HashMap<String, usize> =
1653                        std::collections::HashMap::new();
1654                    for o in &ops {
1655                        let hex = o.hash.to_hex();
1656                        let op_name = match o.op { 1 => "add", 2 => "supersede", 3 => "forget", _ => "?" };
1657                        if o.op == 3 {
1658                            // tombstone: flag the earlier row if it is in the
1659                            // window, else emit a stub (blob is already gone).
1660                            match idx.get(&hex) {
1661                                Some(&i) => { rows[i]["forgotten"] = json!(true); }
1662                                None => rows.push(json!({
1663                                    "hash": hex, "op_seq": o.op_seq, "hlc": o.hlc,
1664                                    "op": op_name, "forgotten": true,
1665                                })),
1666                            }
1667                            continue;
1668                        }
1669                        // A SUPERSEDE logs two ops for the new grain (add +
1670                        // supersede) — merge them into one row, keeping the
1671                        // later op label.
1672                        if let Some(&i) = idx.get(&hex) {
1673                            rows[i]["op"] = json!(op_name);
1674                            rows[i]["op_seq"] = json!(o.op_seq);
1675                            rows[i]["hlc"] = json!(o.hlc);
1676                            continue;
1677                        }
1678                        let mut row = json!({
1679                            "hash": hex, "op_seq": o.op_seq, "hlc": o.hlc, "op": op_name,
1680                        });
1681                        match m.get(&o.hash) {
1682                            Ok(g) => {
1683                                row["type"] = json!(format!("{:?}", g.grain_type).to_lowercase());
1684                                row["fields"] = serde_json::to_value(&g.fields).unwrap_or(Value::Null);
1685                            }
1686                            // erased since (forget op outside the window)
1687                            Err(_) => { row["missing"] = json!(true); }
1688                        }
1689                        idx.insert(hex, rows.len());
1690                        rows.push(row);
1691                    }
1692                    // A supersede op's grain points at its predecessor via
1693                    // derived_from; mark the predecessor if it is in-window.
1694                    let marks: Vec<(String, String)> = rows.iter()
1695                        .filter(|r| r["op"] == "supersede")
1696                        .filter_map(|r| {
1697                            let old = r["fields"].get("derived_from")?.as_str()?;
1698                            Some((old.to_string(), r["hash"].as_str()?.to_string()))
1699                        })
1700                        .collect();
1701                    for (old, newer) in marks {
1702                        if let Some(&i) = idx.get(&old) {
1703                            rows[i]["superseded_by"] = json!(newer);
1704                        }
1705                    }
1706                    rows.reverse();
1707                    Ok((total, rows))
1708                });
1709                // Scope the browse to a namespace, the way CAL already scopes
1710                // recall. Without this the rail lists entities (`john`, `bob`)
1711                // that every query on this console then reports as missing,
1712                // because the two disagree about what is in view.
1713                //
1714                // `?ns=<name>` lets the console browse a namespace other than
1715                // the one it was launched with, and `?ns=*` lifts the filter
1716                // entirely — both read-gated against the session's own
1717                // `AuthzSet` exactly as a `RECALL ... WHERE namespace = ...`
1718                // would be (`changes_since` itself reads the whole op-log
1719                // unscoped; this filter is the only enforcement point, so it
1720                // has to check what `AreevFacade::recall` checks, not less).
1721                // Omitting `?ns` keeps the original session-default behavior
1722                // byte-for-byte. Rows we cannot attribute — tombstone stubs,
1723                // grains erased since — are kept regardless: dropping them
1724                // would hide an erasure.
1725                let ns_param = q("ns");
1726                let built = built.and_then(|(total, rows)| {
1727                    let authz = self.facade.authz();
1728                    let filter_ns = match ns_param.as_deref() {
1729                        Some("*") => {
1730                            if !authz.is_owner() {
1731                                return Err("not authorized to browse all namespaces".to_string());
1732                            }
1733                            None
1734                        }
1735                        Some(ns) => {
1736                            authz
1737                                .check(areev_core::authz::Verb::Read, ns)
1738                                .map_err(|e| e.to_string())?;
1739                            Some(ns.to_string())
1740                        }
1741                        None => self.facade.session_namespace().map(|s| s.to_string()),
1742                    };
1743                    let Some(ns) = filter_ns else {
1744                        return Ok((total, rows));
1745                    };
1746                    let kept = rows
1747                        .into_iter()
1748                        .filter(|r| {
1749                            r["fields"]
1750                                .get("namespace")
1751                                .and_then(Value::as_str)
1752                                .is_none_or(|got| got == ns)
1753                        })
1754                        .collect();
1755                    Ok((total, kept))
1756                });
1757                match built {
1758                    Ok((total, rows)) => ok_json(json!({"ok": true, "total_ops": total, "grains": rows})),
1759                    Err(e) => ok_json(json!({"ok": false, "error": e})),
1760                }
1761            }
1762            ("GET", "/api/namespaces") => {
1763                // Every namespace present in the file, for the console's
1764                // namespace picker. A restricted principal only ever sees the
1765                // ones its own grants cover — read access to the *names* of
1766                // namespaces it cannot read from would itself be a disclosure.
1767                let result = self.facade.with_store(|m| m.namespaces());
1768                match result {
1769                    Ok(list) => {
1770                        let authz = self.facade.authz();
1771                        let owner = authz.is_owner();
1772                        let arr: Vec<Value> = list
1773                            .into_iter()
1774                            .filter(|(ns, _)| {
1775                                owner || authz.check(areev_core::authz::Verb::Read, ns).is_ok()
1776                            })
1777                            .map(|(ns, n)| json!({"namespace": ns, "count": n}))
1778                            .collect();
1779                        ok_json(json!({"ok": true, "namespaces": arr}))
1780                    }
1781                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1782                }
1783            }
1784            ("GET", "/api/grain") => {
1785                let hash = q("hash").unwrap_or_default();
1786                match areev_core::error::Hash::from_hex(&hash)
1787                    .and_then(|h| self.facade.get(&h))
1788                {
1789                    Ok(g) => ok_json(json!({
1790                        "hash": g.hash.to_hex(),
1791                        "type": format!("{:?}", g.grain_type).to_lowercase(),
1792                        "fields": g.fields,
1793                    })),
1794                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1795                }
1796            }
1797            ("GET", "/api/anon/preview") => {
1798                // "As the model sees it" (proposal §8.3): the grain rendered
1799                // the way an egress boundary would show it, policy or not.
1800                let hash = q("hash").unwrap_or_default();
1801                match areev_core::error::Hash::from_hex(&hash)
1802                    .and_then(|h| self.facade.with_store(|m| m.anon_preview(&h)))
1803                {
1804                    Ok(g) => ok_json(json!({
1805                        "hash": g.hash.to_hex(),
1806                        "type": format!("{:?}", g.grain_type).to_lowercase(),
1807                        "fields": g.fields,
1808                    })),
1809                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1810                }
1811            }
1812            ("GET", "/api/verify") => match self.facade.with_store(|m| m.verify()) {
1813                Ok(r) => ok_json(json!({
1814                    "integrity": r.integrity, "grains": r.grains,
1815                    "hash_mismatches": r.hash_mismatches, "undecodable": r.undecodable,
1816                })),
1817                Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
1818            },
1819            // ── Areev Loop API (§5.4) — GETs are reads (token-less OK); the POST
1820            //    mutations are guarded above (token-less → 401). ────────────
1821            // ── /api/run/*: the governed runtime's console surface ──
1822            ("GET", "/api/run/list") => {
1823                // Paged and namespace-scoped on the server (#165): the
1824                // console used to filter a fixed newest-50 page client-side,
1825                // so a quiet tenant's runs — open approvals included — were
1826                // simply absent behind a busier tenant's. `ns` takes an exact
1827                // name or an `org.*` prefix; `limit` is clamped so one
1828                // request cannot ask for the whole history; the response
1829                // says the total and whether it truncated.
1830                let runner = self.runner("console:read");
1831                let ns = q("ns").filter(|s| !s.is_empty() && s.as_str() != "*");
1832                let limit = q("limit")
1833                    .and_then(|v| v.parse::<usize>().ok())
1834                    .unwrap_or(50)
1835                    .clamp(1, 500);
1836                let offset = q("offset").and_then(|v| v.parse::<usize>().ok()).unwrap_or(0);
1837                match runner.list_runs(ns.as_deref(), offset, limit) {
1838                    Ok(page) => {
1839                        let rows: Vec<Value> = page
1840                            .runs
1841                            .iter()
1842                            .map(|r| {
1843                                serde_json::json!({
1844                                    "run_id": r.run_id,
1845                                    "ns": r.ns,
1846                                    "outcome": r.outcome,
1847                                })
1848                            })
1849                            .collect();
1850                        ok_json(serde_json::json!({
1851                            "ok": true,
1852                            "runs": rows,
1853                            "total": page.total,
1854                            "truncated": page.truncated,
1855                            "offset": page.offset,
1856                            "limit": page.limit,
1857                            "unattributed": page.unattributed,
1858                        }))
1859                    }
1860                    Err(e) => run_api_err(&e.to_string()),
1861                }
1862            }
1863            ("GET", "/api/run/inspect") => {
1864                let Some(run_id) = q("run_id") else {
1865                    return run_api_err("run_id required");
1866                };
1867                // Same Runner::inspect the CLI and bindings call (issue
1868                // #34) — this used to hand-roll a divergent, smaller subset
1869                // (no pinned/budgets/fork_of/superstep/phase).
1870                match self.runner("console:read").inspect(&run_id) {
1871                    Ok(report) => {
1872                        let mut v = serde_json::to_value(&report).unwrap_or(Value::Null);
1873                        if let Some(o) = v.as_object_mut() {
1874                            o.insert("ok".into(), Value::Bool(true));
1875                        }
1876                        ok_json(v)
1877                    }
1878                    Err(e) => run_api_err(&e.to_string()),
1879                }
1880            }
1881            // The plan-change rehearsal (`areev run shadow --plan`): a read,
1882            // guarded like the other run reads. GET names a stored plan;
1883            // POST additionally takes an unstored draft body — what the
1884            // Workflows canvas rehearses before Save.
1885            ("GET", "/api/run/shadow") => {
1886                let Some(runs) = q("runs") else { return run_api_err("runs required (comma-separated)") };
1887                let Some(plan) = q("plan") else { return run_api_err("plan required (a Workflow hash)") };
1888                let ids: Vec<String> = runs.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect();
1889                let h = match areev_core::error::Hash::from_hex(&plan) {
1890                    Ok(h) => h,
1891                    Err(e) => return run_api_err(&e.to_string()),
1892                };
1893                match self.runner("console:read").shadow_plan(&ids, &areev_run::PlanCandidate::Hash(h)) {
1894                    Ok(report) => {
1895                        let mut v = serde_json::to_value(&report).unwrap_or(Value::Null);
1896                        if let Some(o) = v.as_object_mut() {
1897                            o.insert("ok".into(), Value::Bool(true));
1898                        }
1899                        ok_json(v)
1900                    }
1901                    Err(e) => run_api_err(&e.to_string()),
1902                }
1903            }
1904            ("POST", "/api/run/shadow") => {
1905                let v: Value = match serde_json::from_slice(body) {
1906                    Ok(v) => v,
1907                    Err(e) => return run_api_err(&e.to_string()),
1908                };
1909                let ids: Vec<String> = match v.get("runs") {
1910                    Some(Value::Array(a)) => a.iter().filter_map(|x| x.as_str().map(str::to_string)).collect(),
1911                    Some(Value::String(csv)) => csv.split(',').map(|s| s.trim().to_string()).filter(|s| !s.is_empty()).collect(),
1912                    _ => return run_api_err("runs required (array or comma-separated)"),
1913                };
1914                let candidate = match (v.get("plan").and_then(Value::as_str), v.get("plan_body").and_then(Value::as_object)) {
1915                    (Some(_), Some(_)) => return run_api_err("give plan or plan_body, not both"),
1916                    (Some(h), None) => match areev_core::error::Hash::from_hex(h) {
1917                        Ok(h) => areev_run::PlanCandidate::Hash(h),
1918                        Err(e) => return run_api_err(&e.to_string()),
1919                    },
1920                    (None, Some(body)) => areev_run::PlanCandidate::Body(body.clone()),
1921                    (None, None) => return run_api_err("plan (hash) or plan_body (object) required"),
1922                };
1923                match self.runner("console:read").shadow_plan(&ids, &candidate) {
1924                    Ok(report) => {
1925                        let mut v = serde_json::to_value(&report).unwrap_or(Value::Null);
1926                        if let Some(o) = v.as_object_mut() {
1927                            o.insert("ok".into(), Value::Bool(true));
1928                        }
1929                        ok_json(v)
1930                    }
1931                    Err(e) => run_api_err(&e.to_string()),
1932                }
1933            }
1934            ("POST", "/api/run/respond") => {
1935                // [R3] SHARED-TOKEN APPROVALS ARE REFUSED: an approval whose
1936                // "identity" is a console-wide secret voids the
1937                // approver-identity edge of the provenance chain. Only a
1938                // per-principal credential (areev ui --auth) may respond.
1939                let Some(responder) = request_principal.clone() else {
1940                    return ("403 Forbidden", "application/json",
1941                        br#"{"ok":false,"error":"run.respond requires a per-principal credential (areev ui --auth <map>); shared-token or anonymous approvals are refused - the approver's identity IS the audit record"}"#.to_vec());
1942                };
1943                // [A0] PROXY-ASSERTED APPROVALS ARE REFUSED BY DEFAULT: an
1944                // SSO identity's only proof is a shared, static, fleet-wide
1945                // proxy secret — whoever holds it can assert any identity,
1946                // including this approver's. That is a weaker claim than the
1947                // per-principal credential [R3] demands, and the resulting
1948                // audit grain would be indistinguishable from a real
1949                // approval. Opt in with `areev ui --sso-approvals allow`.
1950                if principal_from_sso && !self.sso_approvals {
1951                    return ("403 Forbidden", "application/json",
1952                        br#"{"ok":false,"error":"run.respond refuses proxy-asserted SSO identities: the identity header is trusted only via a shared proxy secret, which whoever holds it can use to assert any approver. Approve with a per-principal credential (areev ui --auth <map>), or accept the trade-off explicitly with --sso-approvals allow"}"#.to_vec());
1953                }
1954                // [A2] A group-derived principal is a ROLE. It is refused
1955                // even under `--sso-approvals allow`, and deliberately has no
1956                // flag of its own: the audit record would read "role:eng
1957                // approved", which names no one who can be asked why.
1958                if principal_from_group {
1959                    return ("403 Forbidden", "application/json",
1960                        br#"{"ok":false,"error":"run.respond refuses group-derived principals: a role is not an approver, and an audit record naming one identifies nobody. Grant this person a principal of their own, or give them a per-principal credential"}"#.to_vec());
1961                }
1962                let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1963                let (Some(run_id), Some(ask)) = (
1964                    req.get("run_id").and_then(Value::as_str),
1965                    req.get("tool_call_id").and_then(Value::as_str),
1966                ) else {
1967                    return run_api_err("run_id and tool_call_id required");
1968                };
1969                let result = req.get("result").cloned().unwrap_or(Value::Null);
1970                let is_error = req.get("is_error").and_then(Value::as_bool).unwrap_or(false);
1971                let runner = self.runner(&responder);
1972                match runner.respond(run_id, ask, result, is_error, &responder) {
1973                    Ok(()) => ok_json(serde_json::json!({
1974                        "ok": true, "responded": ask, "responder": responder,
1975                        "note": "resume the run to spend the compute",
1976                    })),
1977                    Err(e) => run_api_err(&e.to_string()),
1978                }
1979            }
1980            ("POST", "/api/run/cancel") => {
1981                // The kill switch keeps its LOW bar: any authenticated POST
1982                // may brake a run (the identity label still records who).
1983                let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
1984                let Some(run_id) = req.get("run_id").and_then(Value::as_str) else {
1985                    return run_api_err("run_id required");
1986                };
1987                let because = req.get("because").and_then(Value::as_str).unwrap_or("canceled via console");
1988                let who = request_principal.clone().unwrap_or_else(|| "console:shared".into());
1989                let runner = self.runner(&who);
1990                match runner.cancel(run_id, &who, because) {
1991                    Ok(()) => ok_json(serde_json::json!({"ok": true, "canceled": run_id})),
1992                    Err(e) => run_api_err(&e.to_string()),
1993                }
1994            }
1995            ("GET", "/api/loop/recommendations") => {
1996                let status = q("status").and_then(|s| status_from_str(&s));
1997                let sub = BorrowedSubstrate::new(&self.facade);
1998                // Coverage-filtered (#312).
1999                match areev_loop_adapter::visible_recommendations(
2000                    &self.engine(),
2001                    &sub,
2002                    &self.facade.authz(),
2003                    status,
2004                ) {
2005                    Ok(recs) => ok_json(json!({
2006                        "ok": true,
2007                        "recommendations": recs.iter().map(rec_json).collect::<Vec<_>>(),
2008                    })),
2009                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2010                }
2011            }
2012            ("GET", "/api/loop/health") => {
2013                let sub = BorrowedSubstrate::new(&self.facade);
2014                match self.engine().health(&sub, now_ms()) {
2015                    Ok(h) => ok_json(json!({"ok": true, "health": h})),
2016                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2017                }
2018            }
2019            ("GET", "/api/loop/outcomes") => {
2020                let sub = BorrowedSubstrate::new(&self.facade);
2021                match self.engine().outcomes(&sub) {
2022                    Ok(o) => ok_json(json!({"ok": true, "outcomes": o})),
2023                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2024                }
2025            }
2026            ("GET", "/api/loop/analyzers") => {
2027                // Effective settings (manifest merged with the file-config), so
2028                // the Setup view renders accurate on/off state and floors.
2029                let sub = BorrowedSubstrate::new(&self.facade);
2030                match self.engine().analyzer_settings(&sub) {
2031                    Ok(list) => ok_json(json!({"ok": true, "analyzers": list})),
2032                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2033                }
2034            }
2035            ("GET", "/api/loop/telemetry") => {
2036                // Recall-telemetry rollups for the Sessions view. A read — open
2037                // in read-only mode like the other loop GETs.
2038                let mode = self.facade.with_store(|m| m.telemetry_mode());
2039                if mode == areev_store::TelemetryMode::Off {
2040                    ok_json(json!({"ok": true, "enabled": false}))
2041                } else {
2042                    let access = self.facade.with_store(|m| m.telemetry_access_stats(None));
2043                    let queries = self.facade.with_store(|m| m.telemetry_query_stats(None));
2044                    let budget = self.facade.with_store(|m| m.telemetry_budget_stats());
2045                    match (access, queries, budget) {
2046                        (Ok(mut a), Ok(mut q), Ok(b)) => {
2047                            // Most-recalled first; recurring-gap questions first.
2048                            a.sort_by_key(|x| std::cmp::Reverse(x.recall_count));
2049                            a.truncate(200);
2050                            q.sort_by_key(|x| std::cmp::Reverse(x.run_count));
2051                            q.truncate(200);
2052                            ok_json(json!({
2053                                "ok": true,
2054                                "enabled": true,
2055                                "mode": mode.as_str(),
2056                                "access": a.iter().map(|x| json!({
2057                                    "hash": x.hash, "recall_count": x.recall_count, "last_ms": x.last_ms,
2058                                })).collect::<Vec<_>>(),
2059                                "queries": q.iter().map(|x| json!({
2060                                    "sample": x.sample, "run_count": x.run_count,
2061                                    "empty_count": x.empty_count,
2062                                })).collect::<Vec<_>>(),
2063                                "budget": {
2064                                    "sample_count": b.sample_count,
2065                                    "overflow_count": b.overflow_count,
2066                                },
2067                            }))
2068                        }
2069                        _ => ok_json(json!({"ok": false, "error": "telemetry read failed"})),
2070                    }
2071                }
2072            }
2073            ("POST", "/api/loop/run") => {
2074                let authz = self.facade.authz();
2075                if !authz.is_owner()
2076                    && !authz.allows(areev_core::authz::Verb::LoopRun, areev_loop::LOOP_NS)
2077                {
2078                    return ("403 Forbidden", "application/json",
2079                            br#"{"ok":false,"error":"AUT-E001: this session lacks loop.run"}"#.to_vec());
2080                }
2081                let mut sub = BorrowedSubstrate::new(&self.facade);
2082                // The trigger is recorded as co-creator on LLM/external
2083                // findings, so the same principal cannot later approve them.
2084                // In credential-map mode the actor IS the bound principal —
2085                // a request cannot claim an identity.
2086                let actor = if self.credentials.is_some() {
2087                    authz.principal().to_string()
2088                } else {
2089                    serde_json::from_slice::<Value>(body)
2090                        .ok()
2091                        .and_then(|v| v.get("actor").and_then(Value::as_str).map(str::to_string))
2092                        .unwrap_or_else(|| "user:console".to_string())
2093                };
2094                let opts = RunOptions { triggering_actor: Some(actor), ..Default::default() };
2095                match self.engine().run(&mut sub, &opts, now_ms()) {
2096                    Ok(res) => ok_json(json!({"ok": true, "run": res})),
2097                    Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2098                }
2099            }
2100            ("POST", "/api/loop/review") => self.loop_review(body),
2101            ("POST", "/api/loop/apply") => self.loop_apply(body),
2102            ("POST", "/api/loop/rollback") => self.loop_rollback(body),
2103            ("POST", "/api/loop/config") => self.loop_config(body),
2104            ("POST", "/api/loop/replay") => self.loop_replay(body),
2105            ("POST", "/api/anon/config") => self.anon_config(body),
2106            _ => (
2107                "404 Not Found",
2108                "application/json",
2109                br#"{"ok":false,"error":"not found"}"#.to_vec(),
2110            ),
2111        }
2112    }
2113
2114    /// The console session's scopes derive from its grants (an owner
2115    /// session — today's only mode — holds all of them); actor
2116    /// `user:console` unless overridden, observer derived from the actor
2117    /// label, never from request text.
2118    fn loop_review(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
2119        let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
2120        let hash = req.get("hash").and_then(Value::as_str).unwrap_or("");
2121        let because = req.get("because").and_then(Value::as_str).unwrap_or("");
2122        let bound = self.facade.authz();
2123        let actor = if self.credentials.is_some() {
2124            bound.principal().to_string()
2125        } else {
2126            req.get("actor").and_then(Value::as_str).unwrap_or("user:console").to_string()
2127        };
2128        let actor = actor.as_str();
2129        let decision = if req.get("decision").and_then(Value::as_str) == Some("reject") {
2130            Decision::Reject
2131        } else {
2132            Decision::Approve
2133        };
2134        let mut sub = BorrowedSubstrate::new(&self.facade);
2135        let scopes = areev_loop_adapter::scopes_for(&self.facade.authz());
2136        let observer = areev_loop_adapter::observer_for_principal(actor);
2137        match self.engine().review(
2138            &mut sub, hash, decision, actor, observer, &scopes, because, now_ms(),
2139        ) {
2140            Ok(()) => ok_json(json!({"ok": true})),
2141            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2142        }
2143    }
2144
2145    fn loop_apply(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
2146        let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
2147        let hash = req.get("hash").and_then(Value::as_str).unwrap_or("");
2148        let because = req.get("because").and_then(Value::as_str).unwrap_or("");
2149        let bound = self.facade.authz();
2150        let actor = if self.credentials.is_some() {
2151            bound.principal().to_string()
2152        } else {
2153            req.get("actor").and_then(Value::as_str).unwrap_or("user:console").to_string()
2154        };
2155        let actor = actor.as_str();
2156        let allow_destructive = req.get("allow_destructive").and_then(Value::as_bool).unwrap_or(false);
2157        let mut sub = BorrowedSubstrate::new(&self.facade);
2158        let scopes = areev_loop_adapter::scopes_for(&self.facade.authz());
2159        let observer = areev_loop_adapter::observer_for_principal(actor);
2160        let engine = self.engine();
2161        // A code or adapter revision applies only through its recorded gating
2162        // edge: `gating_run` names an eval run whose journaled `mg:eval_run`
2163        // summary becomes the evidence — the stats never come from the client.
2164        let gating = match req.get("gating_run").and_then(Value::as_str) {
2165            Some(run_id) => match engine.gating_evidence(&sub, hash, run_id) {
2166                Ok(g) => Some(g),
2167                Err(e) => {
2168                    return ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()}))
2169                }
2170            },
2171            None => None,
2172        };
2173        let applied = match &gating {
2174            Some(g) => engine.apply_gated(
2175                &mut sub, hash, actor, observer, &scopes, because, allow_destructive, g, now_ms(),
2176            ),
2177            None => engine.apply(
2178                &mut sub, hash, actor, observer, &scopes, because, allow_destructive, now_ms(),
2179            ),
2180        };
2181        match applied {
2182            Ok(applied) => ok_json(json!({"ok": true, "rollbackable": applied.rollbackable})),
2183            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2184        }
2185    }
2186
2187    fn loop_rollback(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
2188        let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
2189        let hash = req.get("hash").and_then(Value::as_str).unwrap_or("");
2190        let because = req.get("because").and_then(Value::as_str).unwrap_or("");
2191        let bound = self.facade.authz();
2192        let actor = if self.credentials.is_some() {
2193            bound.principal().to_string()
2194        } else {
2195            req.get("actor").and_then(Value::as_str).unwrap_or("user:console").to_string()
2196        };
2197        let actor = actor.as_str();
2198        let mut sub = BorrowedSubstrate::new(&self.facade);
2199        let scopes = areev_loop_adapter::scopes_for(&self.facade.authz());
2200        let observer = areev_loop_adapter::observer_for_principal(actor);
2201        match self.engine().rollback(
2202            &mut sub, hash, actor, observer, &scopes, because, now_ms(),
2203        ) {
2204            Ok(()) => ok_json(json!({"ok": true})),
2205            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2206        }
2207    }
2208
2209    /// Edit one analyzer's file-config from the Setup view. The body is
2210    /// `{analyzer_id, enabled?, severity_floor?, clear_floor?, params?,
2211    /// namespaces?}`; absent fields are left unchanged. The console holds all
2212    /// scopes (local root of trust), so `Admin` is satisfied; an unknown
2213    /// analyzer or bad param is a structured `ok:false` (not a 500).
2214    fn loop_config(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
2215        let id = serde_json::from_slice::<Value>(body)
2216            .ok()
2217            .and_then(|v| v.get("analyzer_id").and_then(Value::as_str).map(str::to_string))
2218            .unwrap_or_default();
2219        // The update reads the same body; the extra `analyzer_id` key is ignored.
2220        let update: areev_loop::AnalyzerConfigUpdate = serde_json::from_slice(body).unwrap_or_default();
2221        let mut sub = BorrowedSubstrate::new(&self.facade);
2222        match self.engine().set_analyzer_config(&mut sub, &id, update, &areev_loop_adapter::scopes_for(&self.facade.authz())) {
2223            Ok(cfg) => ok_json(json!({"ok": true, "config": cfg})),
2224            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2225        }
2226    }
2227}
2228
2229impl UiServer {
2230    /// Score a candidate loop configuration against the recorded past
2231    /// (`areev loop replay`): a computation, not a write — but token-guarded
2232    /// like `/api/loop/config`, because it reads the whole review history
2233    /// and runs every analyzer over every step. Body = the candidate plus
2234    /// the window (`areev_loop::replay::ReplayRequest`).
2235    fn loop_replay(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
2236        let req = match serde_json::from_slice::<areev_loop::replay::ReplayRequest>(body) {
2237            Ok(r) => r,
2238            Err(e) => return ok_json(json!({"ok": false, "error": format!("replay request: {e}")})),
2239        };
2240        let (candidate, opts) = match req.resolve(now_ms()) {
2241            Ok(v) => v,
2242            Err(e) => return ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2243        };
2244        let sub = BorrowedSubstrate::new(&self.facade);
2245        match self.engine().replay(&sub, &candidate, &opts) {
2246            Ok(report) => ok_json(json!({"ok": true, "replay": report})),
2247            Err(e) => ok_json(json!({"ok": false, "error": e.to_string(), "code": e.code()})),
2248        }
2249    }
2250
2251    /// Declare or clear a per-namespace anonymization policy from the
2252    /// console (Connect → Settings). Rides the same write gate as every
2253    /// console POST (auth + Origin + body cap upstream); the policy itself
2254    /// validates fail-closed in the store.
2255    fn anon_config(&self, body: &[u8]) -> (&'static str, &'static str, Vec<u8>) {
2256        let v: Value = match serde_json::from_slice(body) {
2257            Ok(v) => v,
2258            Err(e) => return ok_json(json!({"ok": false, "error": e.to_string()})),
2259        };
2260        let Some(ns) = v.get("ns").and_then(Value::as_str).filter(|n| !n.trim().is_empty())
2261        else {
2262            return ok_json(json!({"ok": false, "error": "ns is required"}));
2263        };
2264        let result = if v.get("clear").and_then(Value::as_bool) == Some(true) {
2265            self.facade.with_store(|m| m.clear_anon_policy(ns))
2266        } else {
2267            match v.get("policy") {
2268                Some(policy) => {
2269                    let policy_json = policy.to_string();
2270                    self.facade.with_store(|m| m.set_anon_policy(ns, &policy_json))
2271                }
2272                None => {
2273                    return ok_json(json!({"ok": false, "error": "policy (object) or clear: true is required"}));
2274                }
2275            }
2276        };
2277        match result {
2278            Ok(()) => {
2279                let declared = self.facade.with_store(|m| m.anon_declared());
2280                ok_json(json!({"ok": true, "policies": declared
2281                    .iter()
2282                    .map(|(ns, mode)| json!({"ns": ns, "mode": mode}))
2283                    .collect::<Vec<_>>()}))
2284            }
2285            Err(e) => ok_json(json!({"ok": false, "error": e.to_string()})),
2286        }
2287    }
2288}
2289
2290/// True when a `host[:port]` (or `[ipv6]:port`) authority names a loopback
2291/// host. Shared by the Origin drive-by check and the Host-header
2292/// (DNS-rebinding) check.
2293fn host_is_local(host_port: &str) -> bool {
2294    let host = if let Some(h) = host_port.strip_prefix('[') {
2295        h.split(']').next().unwrap_or("")
2296    } else {
2297        host_port.split(':').next().unwrap_or("")
2298    };
2299    matches!(host, "127.0.0.1" | "localhost" | "::1")
2300}
2301
2302/// True when an Origin header names a loopback host (any port, http/https).
2303fn origin_is_local(origin: &str) -> bool {
2304    let rest = origin
2305        .strip_prefix("http://")
2306        .or_else(|| origin.strip_prefix("https://"));
2307    let Some(rest) = rest else { return false };
2308    let host_port = rest.split('/').next().unwrap_or("");
2309    host_is_local(host_port)
2310}
2311
2312/// Normalize an origin (an `Origin` header value, or an operator-configured
2313/// `--allow-origin` entry) for EXACT comparison: lowercase, with a single
2314/// trailing `/` dropped. Nothing else — this only makes two spellings of the
2315/// same origin compare equal; it does not validate shape (the CLI's
2316/// `--allow-origin` parser does that at startup).
2317fn normalize_origin(origin: &str) -> String {
2318    let trimmed = origin.trim();
2319    trimmed.strip_suffix('/').unwrap_or(trimmed).to_ascii_lowercase()
2320}
2321
2322/// Redact a DISPLAY copy of a connection-string / file-path DB label
2323/// (issue #124): if `label` has a `scheme://[user[:pass]@]host...` shape and
2324/// the userinfo carries a password, replace the password with `***`.
2325/// Anything else — a plain file path (no `://`), a DSN with no userinfo, or
2326/// userinfo with no password — is returned byte-for-byte unchanged.
2327///
2328/// **DISPLAY ONLY.** The raw label is still what
2329/// `CredentialMap::resolve_for_memory` (`areev-core/src/authz.rs`) matches a
2330/// token's `memories` scope against via an exact string compare — never swap
2331/// in this redacted form at those call sites, or every `--auth FILE`
2332/// deployment that scopes a token to a Postgres memory silently breaks.
2333///
2334/// Hand-rolled (no URL crate, per the workspace's dependency-light policy):
2335/// only the AUTHORITY segment — between `://` and the first `/`, `?`, or `#`
2336/// that follows — is considered, and within it the LAST `@` is taken as the
2337/// userinfo delimiter. RFC 3986 allows a percent-encoded `@` in a password
2338/// and real passwords contain literal `@` too, so an earlier `@` is presumed
2339/// to be *inside* the password rather than a second delimiter; and a `:` or
2340/// `@` appearing later — in a query string, say — is outside the authority
2341/// entirely and is never mistaken for userinfo.
2342pub fn redact_dsn(label: &str) -> String {
2343    let Some(scheme_end) = label.find("://") else {
2344        return label.to_string();
2345    };
2346    let authority_start = scheme_end + 3;
2347    let rest = &label[authority_start..];
2348    let authority_len = rest.find(['/', '?', '#']).unwrap_or(rest.len());
2349    let authority = &rest[..authority_len];
2350    let Some(at) = authority.rfind('@') else {
2351        return label.to_string();
2352    };
2353    let userinfo = &authority[..at];
2354    let Some(colon) = userinfo.find(':') else {
2355        return label.to_string();
2356    };
2357    let user = &userinfo[..colon];
2358    let mut out = String::with_capacity(label.len());
2359    out.push_str(&label[..authority_start]);
2360    out.push_str(user);
2361    out.push_str(":***@");
2362    out.push_str(&authority[at + 1..]);
2363    out.push_str(&rest[authority_len..]);
2364    out
2365}
2366
2367fn ok_json(v: Value) -> (&'static str, &'static str, Vec<u8>) {
2368    ("200 OK", "application/json", v.to_string().into_bytes())
2369}
2370
2371fn run_api_err(msg: &str) -> (&'static str, &'static str, Vec<u8>) {
2372    (
2373        "400 Bad Request",
2374        "application/json",
2375        serde_json::json!({"ok": false, "error": msg}).to_string().into_bytes(),
2376    )
2377}
2378
2379/// True when a `POST /api/cal` body is a read-only statement. Classification
2380/// is delegated to `areev-cal`'s exhaustive [`areev_cal::classify`] module —
2381/// the single source of truth — so grammar growth cannot desynchronize this
2382/// gate (a keyword sniff lived here before and would have silently
2383/// misclassified any new statement). Fail closed: unparseable text is not
2384/// read-only.
2385fn cal_body_is_read_only(body: &[u8]) -> bool {
2386    let req: Value = serde_json::from_slice(body).unwrap_or(Value::Null);
2387    let q = req.get("query").and_then(Value::as_str).unwrap_or("");
2388    areev_cal::classify::query_is_read_only(q)
2389}
2390
2391fn status_from_str(s: &str) -> Option<RecStatus> {
2392    match s {
2393        "pending" => Some(RecStatus::Pending),
2394        "approved" => Some(RecStatus::Approved),
2395        "rejected" => Some(RecStatus::Rejected),
2396        "applied" => Some(RecStatus::Applied),
2397        "rolled_back" => Some(RecStatus::RolledBack),
2398        "expired" => Some(RecStatus::Expired),
2399        "withdrawn" => Some(RecStatus::Withdrawn),
2400        _ => None, // includes "all"
2401    }
2402}
2403
2404fn rec_json(r: &areev_loop::Recommendation) -> Value {
2405    let mut v = json!({
2406        "hash": r.hash,
2407        "status": r.status.as_str(),
2408        "severity": r.severity.as_str(),
2409        "analyzer": r.analyzer,
2410        "summary": r.summary.render(),
2411        "target_ref": r.target_ref,
2412        "destructive": r.destructive,
2413        "rollbackable": r.rollbackable,
2414        "evidence": r.evidence,
2415        // The Rule E1 pin, when present — the console reads it to know this
2416        // apply needs a recorded gating run (and which evalset to run).
2417        "evalset_hash": r.evalset_hash,
2418    });
2419    // Live lessons this authored lesson restates — the console shows the
2420    // existing rule beside the proposal. Present only when there are any,
2421    // so every other row reads exactly as before.
2422    if !r.near_duplicate_of.is_empty() {
2423        v["near_duplicate_of"] = json!(r.near_duplicate_of);
2424    }
2425    // A plan revision's rehearsal report, when the runtime could run one.
2426    if let Some(replay) = &r.replay {
2427        v["replay"] = replay.clone();
2428    }
2429    v
2430}
2431
2432fn urldecode(s: &str) -> String {
2433    let mut out = Vec::new();
2434    let b = s.as_bytes();
2435    let mut i = 0;
2436    while i < b.len() {
2437        match b[i] {
2438            b'%' if i + 2 < b.len() + 1 && i + 2 < b.len() + 1 => {
2439                if i + 2 < b.len() + 1 && i + 2 < b.len() {
2440                    let hex = std::str::from_utf8(&b[i + 1..i + 3]).unwrap_or("");
2441                    if let Ok(v) = u8::from_str_radix(hex, 16) {
2442                        out.push(v);
2443                        i += 3;
2444                        continue;
2445                    }
2446                }
2447                out.push(b[i]);
2448                i += 1;
2449            }
2450            b'+' => {
2451                out.push(b' ');
2452                i += 1;
2453            }
2454            c => {
2455                out.push(c);
2456                i += 1;
2457            }
2458        }
2459    }
2460    String::from_utf8_lossy(&out).to_string()
2461}
2462
2463fn html_escape(s: &str) -> String {
2464    s.replace('&', "&amp;").replace('<', "&lt;").replace('>', "&gt;")
2465}
2466
2467/// Constant-time byte comparison — avoids leaking the bearer token through
2468/// response timing. A length mismatch fails fast (token length is not secret).
2469fn ct_eq(a: &[u8], b: &[u8]) -> bool {
2470    if a.len() != b.len() {
2471        return false;
2472    }
2473    let mut diff = 0u8;
2474    for (x, y) in a.iter().zip(b.iter()) {
2475        diff |= x ^ y;
2476    }
2477    diff == 0
2478}
2479
2480/// Decode standard base64 (RFC 4648), enough for HTTP Basic credentials.
2481/// The one hand-rolled decoder lives in `areev_core::b64` (the trigger
2482/// evaluator shares it for connector blobs, #93).
2483fn base64_decode(s: &str) -> Option<Vec<u8>> {
2484    areev_core::b64::decode(s)
2485}
2486
2487/// Extract the password from an HTTP Basic credential (`base64("user:pass")`).
2488/// The console ignores the username and treats the password as the token.
2489/// Returns `None` if the value is not valid base64/UTF-8 or has no `:`.
2490fn basic_auth_password(b64: &str) -> Option<String> {
2491    let text = String::from_utf8(base64_decode(b64)?).ok()?;
2492    text.split_once(':').map(|(_user, pass)| pass.to_string())
2493}
2494
2495#[cfg(test)]
2496mod loop_route_tests {
2497    use super::UiServer;
2498    use areev_cal::AreevFacade;
2499    use areev_core::types::{Fact, Grain};
2500    use areev_store::Areev;
2501
2502    fn server(auth: Option<&str>) -> UiServer {
2503        let dir = tempfile::tempdir().unwrap();
2504        let path = dir.path().join("t.db");
2505        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
2506        // Two identical facts → a duplicate-consolidation recommendation.
2507        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller")).unwrap();
2508        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller")).unwrap();
2509        std::mem::forget(dir); // keep the file alive for the server's lifetime
2510        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2511        let s = UiServer::new(facade, "test".into());
2512        match auth {
2513            Some(t) => s.with_auth(t.to_string()),
2514            None => s,
2515        }
2516    }
2517
2518    fn text(r: &(&str, &str, Vec<u8>)) -> String {
2519        String::from_utf8_lossy(&r.2).to_string()
2520    }
2521
2522    #[test]
2523    fn token_less_console_is_read_only() {
2524        let s = server(None);
2525        // A loop mutation (run) is a write → 401.
2526        assert!(s.route("POST", "/api/loop/run", b"{}", None, None).0.starts_with("401"));
2527        // A write CAL → 401.
2528        let w = s.route("POST", "/api/cal", br#"{"query":"ADD fact SET subject=\"x\""}"#, None, None);
2529        assert!(w.0.starts_with("401"), "write CAL must be 401: {}", text(&w));
2530        // A read CAL → 200.
2531        let r = s.route("POST", "/api/cal", br#"{"query":"RECALL facts WHERE subject = \"acme\""}"#, None, None);
2532        assert!(r.0.starts_with("200"), "read CAL allowed: {}", text(&r));
2533        // Areev Loop reads stay open token-less.
2534        assert!(s.route("GET", "/api/loop/recommendations", b"", None, None).0.starts_with("200"));
2535    }
2536
2537    #[test]
2538    fn telemetry_endpoint_is_an_open_read() {
2539        let s = server(None);
2540        let r = s.route("GET", "/api/loop/telemetry", b"", None, None);
2541        assert!(r.0.starts_with("200"), "telemetry read open: {}", text(&r));
2542        let body = text(&r);
2543        assert!(body.contains("\"ok\":true"), "{body}");
2544        // The test store opens bare (telemetry off) → enabled:false.
2545        assert!(body.contains("\"enabled\":false"), "{body}");
2546    }
2547
2548    #[test]
2549    fn authenticated_run_review_apply_roundtrip() {
2550        let s = server(Some("tok"));
2551        let run = s.route("POST", "/api/loop/run", b"{}", Some("tok"), None);
2552        assert!(run.0.starts_with("200"), "run: {}", text(&run));
2553        assert!(text(&run).contains("\"ran\""));
2554
2555        let list = s.route("GET", "/api/loop/recommendations?status=pending", b"", Some("tok"), None);
2556        let v: serde_json::Value = serde_json::from_slice(&list.2).unwrap();
2557        let recs = v["recommendations"].as_array().unwrap();
2558        assert!(!recs.is_empty(), "at least one recommendation");
2559        let hash = recs[0]["hash"].as_str().unwrap().to_string();
2560
2561        let rev = s.route(
2562            "POST",
2563            "/api/loop/review",
2564            format!(r#"{{"hash":"{hash}","decision":"approve","because":"ok"}}"#).as_bytes(),
2565            Some("tok"),
2566            None,
2567        );
2568        assert!(text(&rev).contains("\"ok\":true"), "review: {}", text(&rev));
2569
2570        let ap = s.route(
2571            "POST",
2572            "/api/loop/apply",
2573            format!(r#"{{"hash":"{hash}","because":"go"}}"#).as_bytes(),
2574            Some("tok"),
2575            None,
2576        );
2577        assert!(text(&ap).contains("\"ok\":true"), "apply: {}", text(&ap));
2578    }
2579
2580    #[test]
2581    fn config_edit_toggles_analyzer_via_console() {
2582        let s = server(Some("tok"));
2583        // Token-less write → 401 (guarded like every POST).
2584        assert!(s.route("POST", "/api/loop/config", b"{}", None, None).0.starts_with("401"));
2585        // Replay is a read, but it is guarded like config: it runs every
2586        // analyzer over the whole history on request.
2587        assert!(s.route("POST", "/api/loop/replay", b"{}", None, None).0.starts_with("401"));
2588        let (status, _ct, body) = s.route("POST", "/api/loop/replay", br#"{"window":"7d","step":"1d"}"#, Some("tok"), None);
2589        assert!(status.starts_with("200"), "{status}");
2590        let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
2591        assert_eq!(v["ok"], true, "{v}");
2592        for key in ["incumbent", "candidate", "steps", "not_replayed", "matching"] {
2593            assert!(v["replay"].get(key).is_some(), "replay report lacks {key}: {v}");
2594        }
2595        assert!(v["replay"]["incumbent"]["total"]["findings"].is_u64(), "{v}");
2596        let (_status, _ct, body) = s.route("POST", "/api/loop/replay", br#"{"analyzers": {}}"#, Some("tok"), None);
2597        let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
2598        assert_eq!(v["ok"], false, "unknown keys are refused: {v}");
2599
2600        // Read the analyzers; pick one that is on by default.
2601        let list = s.route("GET", "/api/loop/analyzers", b"", Some("tok"), None);
2602        let v: serde_json::Value = serde_json::from_slice(&list.2).unwrap();
2603        let id = v["analyzers"]
2604            .as_array()
2605            .unwrap()
2606            .iter()
2607            .find(|x| x["enabled"] == true)
2608            .expect("some analyzer is on")["id"]
2609            .as_str()
2610            .unwrap()
2611            .to_string();
2612
2613        // Disable it via the console endpoint.
2614        let post = s.route(
2615            "POST",
2616            "/api/loop/config",
2617            format!(r#"{{"analyzer_id":"{id}","enabled":false}}"#).as_bytes(),
2618            Some("tok"),
2619            None,
2620        );
2621        assert!(text(&post).contains("\"ok\":true"), "config: {}", text(&post));
2622
2623        // It reads back disabled.
2624        let list2 = s.route("GET", "/api/loop/analyzers", b"", Some("tok"), None);
2625        let v2: serde_json::Value = serde_json::from_slice(&list2.2).unwrap();
2626        let now = v2["analyzers"]
2627            .as_array()
2628            .unwrap()
2629            .iter()
2630            .find(|x| x["id"] == id.as_str())
2631            .unwrap();
2632        assert_eq!(now["enabled"], false, "toggled off and persisted");
2633    }
2634
2635    #[test]
2636    fn self_approval_surfaces_lop_code() {
2637        let s = server(Some("tok"));
2638        s.route("POST", "/api/loop/run", b"{}", Some("tok"), None);
2639        let list = s.route("GET", "/api/loop/recommendations?status=pending", b"", Some("tok"), None);
2640        let v: serde_json::Value = serde_json::from_slice(&list.2).unwrap();
2641        let rec = &v["recommendations"].as_array().unwrap()[0];
2642        let hash = rec["hash"].as_str().unwrap();
2643        let analyzer = rec["analyzer"].as_str().unwrap();
2644        let creator = format!("engine:{analyzer}");
2645        // The engine actor approving its own proposal is blocked.
2646        let rev = s.route(
2647            "POST",
2648            "/api/loop/review",
2649            format!(r#"{{"hash":"{hash}","decision":"approve","because":"self","actor":"{creator}"}}"#).as_bytes(),
2650            Some("tok"),
2651            None,
2652        );
2653        assert!(text(&rev).contains("LOP-E021"), "self-approval blocked: {}", text(&rev));
2654    }
2655}
2656
2657#[cfg(test)]
2658mod security_tests {
2659    use super::{base64_decode, basic_auth_password, ct_eq, normalize_origin, redact_dsn};
2660
2661    #[test]
2662    fn ct_eq_matches_only_equal() {
2663        assert!(ct_eq(b"secret-token", b"secret-token"));
2664        assert!(!ct_eq(b"secret-token", b"secret-toker"));
2665        assert!(!ct_eq(b"secret", b"secret-token")); // length mismatch
2666        assert!(ct_eq(b"", b""));
2667    }
2668
2669    #[test]
2670    fn base64_decode_roundtrips_known_vectors() {
2671        // RFC 4648 test vectors (with and without padding).
2672        assert_eq!(base64_decode("").unwrap(), b"");
2673        assert_eq!(base64_decode("Zg==").unwrap(), b"f");
2674        assert_eq!(base64_decode("Zm8=").unwrap(), b"fo");
2675        assert_eq!(base64_decode("Zm9v").unwrap(), b"foo");
2676        assert_eq!(base64_decode("Zm9vYmFy").unwrap(), b"foobar");
2677        // padding is optional for us
2678        assert_eq!(base64_decode("Zg").unwrap(), b"f");
2679        // invalid characters are rejected
2680        assert!(base64_decode("****").is_none());
2681    }
2682
2683    #[test]
2684    fn basic_auth_extracts_password_ignoring_username() {
2685        // base64("areev:s3cret") and base64(":s3cret") both yield the token.
2686        assert_eq!(basic_auth_password("ZGVqYTpzM2NyZXQ=").as_deref(), Some("s3cret"));
2687        assert_eq!(basic_auth_password("OnMzY3JldA==").as_deref(), Some("s3cret"));
2688        // a password may itself contain ':' — only the first ':' splits.
2689        // base64("u:a:b") -> "a:b"
2690        assert_eq!(basic_auth_password("dTphOmI=").as_deref(), Some("a:b"));
2691        // no ':' at all → not a valid Basic credential
2692        assert_eq!(basic_auth_password("bm9jb2xvbg=="), None); // "nocolon"
2693        assert_eq!(basic_auth_password("****"), None); // not base64
2694    }
2695
2696    // ── redact_dsn (issue #124) ─────────────────────────────────────────
2697
2698    #[test]
2699    fn redact_dsn_hides_a_password() {
2700        assert_eq!(
2701            redact_dsn("postgresql://rounic:SUPERSECRET@pg-x:5432/rounic?sslmode=verify-full&schema=desk_invoice"),
2702            "postgresql://rounic:***@pg-x:5432/rounic?sslmode=verify-full&schema=desk_invoice",
2703        );
2704    }
2705
2706    #[test]
2707    fn redact_dsn_leaves_no_password_alone() {
2708        assert_eq!(redact_dsn("postgres://user@host:5432/db"), "postgres://user@host:5432/db");
2709    }
2710
2711    #[test]
2712    fn redact_dsn_leaves_no_userinfo_alone() {
2713        assert_eq!(redact_dsn("postgres://host:5432/db"), "postgres://host:5432/db");
2714    }
2715
2716    #[test]
2717    fn redact_dsn_leaves_a_plain_file_path_alone() {
2718        assert_eq!(redact_dsn("demo.db"), "demo.db");
2719        assert_eq!(redact_dsn("/var/lib/areev/demo.db"), "/var/lib/areev/demo.db");
2720    }
2721
2722    #[test]
2723    fn redact_dsn_hides_a_percent_encoded_password() {
2724        // %40 is an encoded '@' inside the password — the LITERAL '@' that
2725        // ends the authority is the one after it.
2726        assert_eq!(
2727            redact_dsn("postgres://user:pa%40ss@host:5432/db"),
2728            "postgres://user:***@host:5432/db",
2729        );
2730    }
2731
2732    #[test]
2733    fn redact_dsn_hides_a_password_with_a_literal_at_sign() {
2734        // The LAST '@' in the authority is the userinfo/host boundary, so a
2735        // literal '@' inside the password is presumed to be part of it.
2736        assert_eq!(
2737            redact_dsn("postgres://user:p@ss@host:5432/db"),
2738            "postgres://user:***@host:5432/db",
2739        );
2740    }
2741
2742    #[test]
2743    fn redact_dsn_ignores_colon_and_at_in_the_query_string() {
2744        assert_eq!(
2745            redact_dsn("postgres://user:pass@host:5432/db?options=-c%20foo:bar@baz"),
2746            "postgres://user:***@host:5432/db?options=-c%20foo:bar@baz",
2747        );
2748    }
2749
2750    #[test]
2751    fn redact_dsn_handles_empty_string() {
2752        assert_eq!(redact_dsn(""), "");
2753    }
2754
2755    // ── normalize_origin (issue #125) ───────────────────────────────────
2756
2757    #[test]
2758    fn normalize_origin_lowercases_and_drops_one_trailing_slash() {
2759        assert_eq!(normalize_origin("https://Console.Example.com"), "https://console.example.com");
2760        assert_eq!(normalize_origin("https://console.example.com/"), "https://console.example.com");
2761        assert_eq!(
2762            normalize_origin("HTTPS://Console.Example.com/"),
2763            "https://console.example.com",
2764        );
2765    }
2766}
2767
2768#[cfg(test)]
2769mod credential_route_tests {
2770    use super::UiServer;
2771    use areev_cal::AreevFacade;
2772    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
2773    use areev_core::types::{Fact, Grain};
2774    use areev_store::Areev;
2775
2776    /// The tuning seam over HTTP: an adapter recommendation applies only
2777    /// with `gating_run` in the POST body — evidence loaded from the
2778    /// journaled eval summary, never from the client — and the rec row
2779    /// carries `evalset_hash` so the console knows the apply is gated.
2780    #[test]
2781    fn loop_apply_honors_a_gating_run() {
2782        let dir = tempfile::tempdir().unwrap();
2783        let path = dir.path().join("g.db");
2784        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
2785        // The pin and a clean recorded gate run, as `areev eval` writes them.
2786        let evalset = store
2787            .add(
2788                &Fact::new("evalset:gate", "mg:evalset", "{\"name\":\"gate\",\"cases\":[]}")
2789                    .namespace("agent:harness")
2790                    .created_at(1_000),
2791            )
2792            .unwrap()
2793            .to_hex();
2794        store
2795            .add(
2796                &Fact::new(
2797                    &format!("evalset:{evalset}"),
2798                    "mg:eval_run",
2799                    "{\"run_id\":\"eval-1\",\"passed\":3,\"failed\":0}",
2800                )
2801                .namespace("agent:harness")
2802                .created_at(2_000),
2803            )
2804            .unwrap();
2805        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(3_000)).unwrap();
2806        std::mem::forget(dir);
2807        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2808        // Register an adapter with real lineage (a recorded corpus export).
2809        let manifest = facade
2810            .record_corpus_export("RECALL facts", "train.jsonl", None, 4_000, &[], &[])
2811            .unwrap()
2812            .to_hex();
2813        facade
2814            .record_adapter(
2815                r#"{"adapter":{"uri":"file:///tmp/a","sha256":"feed"},"base_model":"qwen3-4b","serves_as":"acme-support"}"#,
2816                &manifest,
2817                &evalset,
2818                5_000,
2819            )
2820            .unwrap();
2821        // Token mode: the shared secret is the implied local owner — the
2822        // token-less console is read-only by design.
2823        let s = UiServer::new(facade, "test".into()).with_auth("gate-token".into());
2824        let tok = Some("gate-token");
2825
2826        // Propose, then find the adapter rec — its row must carry the pin.
2827        let run = s.route("POST", "/api/loop/run", b"{}", tok, None);
2828        assert!(run.0.starts_with("200"), "{}", text(&run));
2829        let list = s.route("GET", "/api/loop/recommendations?status=pending", b"", tok, None);
2830        let rows: serde_json::Value = serde_json::from_str(&text(&list)).unwrap();
2831        let rec = rows["recommendations"]
2832            .as_array()
2833            .unwrap()
2834            .iter()
2835            .find(|r| r["analyzer"].as_str().unwrap_or("").contains("adapter_intake"))
2836            .unwrap_or_else(|| panic!("adapter_intake must propose: {rows}"));
2837        assert_eq!(rec["evalset_hash"], evalset.as_str(), "the row must carry the pin");
2838        let hash = rec["hash"].as_str().unwrap().to_string();
2839
2840        // Approve (the console's first POST), then: ungated apply refused,
2841        // a bad run id refused, the recorded run admits.
2842        let approve = format!(r#"{{"hash":"{hash}","decision":"approve","because":"reviewed"}}"#);
2843        let r = s.route("POST", "/api/loop/review", approve.as_bytes(), tok, None);
2844        assert!(text(&r).contains("\"ok\":true"), "{}", text(&r));
2845        let bare = format!(r#"{{"hash":"{hash}","because":"ship it"}}"#);
2846        let r = s.route("POST", "/api/loop/apply", bare.as_bytes(), tok, None);
2847        assert!(text(&r).contains("gating run"), "ungated must refuse: {}", text(&r));
2848        let bad = format!(r#"{{"hash":"{hash}","because":"ship","gating_run":"eval-nope"}}"#);
2849        let r = s.route("POST", "/api/loop/apply", bad.as_bytes(), tok, None);
2850        assert!(text(&r).contains("no recorded gate run"), "{}", text(&r));
2851        let good = format!(r#"{{"hash":"{hash}","because":"gated and green","gating_run":"eval-1"}}"#);
2852        let r = s.route("POST", "/api/loop/apply", good.as_bytes(), tok, None);
2853        assert!(text(&r).contains("\"ok\":true"), "gated apply failed: {}", text(&r));
2854    }
2855
2856    /// A server in credential-map mode: two env-referenced tokens, rights
2857    /// seeded as grant grains in the file itself.
2858    fn server() -> UiServer {
2859        std::env::set_var("AREEV_TEST_READER_TOK", "reader-secret");
2860        std::env::set_var("AREEV_TEST_WRITER_TOK", "writer-secret");
2861        let dir = tempfile::tempdir().unwrap();
2862        let path = dir.path().join("t.db");
2863        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
2864        store
2865            .add(&Fact::new("user:reader", REL_PERMITS, "read ON *").namespace(AUTHZ_NS).created_at(1_000))
2866            .unwrap();
2867        store
2868            .add(
2869                &Fact::new("agent:writer", REL_PERMITS, "read,write ON caller")
2870                    .namespace(AUTHZ_NS)
2871                    .created_at(2_000),
2872            )
2873            .unwrap();
2874        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(3_000)).unwrap();
2875        std::mem::forget(dir);
2876        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
2877        let map = CredentialMap::from_json(
2878            r#"{"version":1,"tokens":[
2879                {"env":"AREEV_TEST_READER_TOK","principal":"user:reader"},
2880                {"env":"AREEV_TEST_WRITER_TOK","principal":"agent:writer"}
2881            ]}"#,
2882        )
2883        .unwrap();
2884        UiServer::new(facade, "test".into()).with_credentials(map)
2885    }
2886
2887    fn text(r: &(&str, &str, Vec<u8>)) -> String {
2888        String::from_utf8_lossy(&r.2).to_string()
2889    }
2890
2891    const READ: &[u8] = br#"{"query":"RECALL facts WHERE subject = \"acme\""}"#;
2892    const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
2893
2894    #[test]
2895    fn tokens_bind_principals_and_verbs_decide() {
2896        let s = server();
2897
2898        // The reader reads…
2899        let r = s.route("POST", "/api/cal", READ, Some("reader-secret"), None);
2900        assert!(r.0.starts_with("200"), "{}", text(&r));
2901        assert!(text(&r).contains("grains"));
2902        // …and cannot write.
2903        let w = s.route("POST", "/api/cal", WRITE, Some("reader-secret"), None);
2904        assert!(text(&w).contains("AUT-E001"), "reader write must be refused: {}", text(&w));
2905
2906        // The writer writes.
2907        let w = s.route("POST", "/api/cal", WRITE, Some("writer-secret"), None);
2908        assert!(text(&w).contains("added"), "writer write must land: {}", text(&w));
2909
2910        // Sequential isolation: the very next reader request is still
2911        // read-only — nothing leaked from the writer's binding.
2912        let w = s.route("POST", "/api/cal", WRITE, Some("reader-secret"), None);
2913        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
2914    }
2915
2916    #[test]
2917    fn anonymous_is_read_only_and_loop_run_is_verb_gated() {
2918        let s = server();
2919        // No token → anonymous: reads work…
2920        let r = s.route("POST", "/api/cal", READ, None, None);
2921        assert!(r.0.starts_with("200"), "{}", text(&r));
2922        assert!(text(&r).contains("grains"));
2923        // …writes are verb-refused…
2924        let w = s.route("POST", "/api/cal", WRITE, None, None);
2925        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
2926        // …and an unknown token is anonymous too, never an escalation.
2927        let w = s.route("POST", "/api/cal", WRITE, Some("stolen-guess"), None);
2928        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
2929
2930        // The analysis trigger needs loop.run — anonymous gets 403.
2931        let run = s.route("POST", "/api/loop/run", b"{}", None, None);
2932        assert!(run.0.starts_with("403"), "{}", text(&run));
2933    }
2934
2935    #[test]
2936    fn whoami_reports_the_bound_principal() {
2937        let s = server();
2938        let r = s.route("GET", "/api/whoami", b"", Some("reader-secret"), None);
2939        assert!(text(&r).contains("user:reader"), "{}", text(&r));
2940        assert!(text(&r).contains("credentials"));
2941        let r = s.route("GET", "/api/whoami", b"", None, None);
2942        assert!(text(&r).contains("anonymous"), "{}", text(&r));
2943    }
2944
2945    /// A credential map must not silently disable `--token-env`. Attaching
2946    /// one changes WHO a request is; the operator who also asked for a
2947    /// shared secret still gets authentication on every request.
2948    #[test]
2949    fn a_credential_map_does_not_disable_the_shared_secret() {
2950        let s = server().with_auth("console-token".into());
2951
2952        // No credential at all: refused, not dropped to `anonymous`.
2953        let r = s.route("POST", "/api/cal", READ, None, None);
2954        assert!(r.0.starts_with("401"), "{}", text(&r));
2955        let page = s.route("GET", "/", b"", None, None);
2956        assert!(page.0.starts_with("401"), "the console page is guarded too");
2957        // An unrecognized token is refused rather than downgraded.
2958        let r = s.route("POST", "/api/cal", READ, Some("stolen-guess"), None);
2959        assert!(r.0.starts_with("401"), "{}", text(&r));
2960
2961        // A map token still binds its principal and its verbs still decide.
2962        let r = s.route("POST", "/api/cal", READ, Some("reader-secret"), None);
2963        assert!(r.0.starts_with("200"), "{}", text(&r));
2964        let w = s.route("POST", "/api/cal", WRITE, Some("reader-secret"), None);
2965        assert!(text(&w).contains("AUT-E001"), "{}", text(&w));
2966
2967        // The shared secret is the implied admin (docs/cal-all-you-need
2968        // -proposal.md §5.2b) — it keeps the owner session it always had.
2969        let w = s.route("POST", "/api/cal", WRITE, Some("console-token"), None);
2970        assert!(text(&w).contains("added"), "{}", text(&w));
2971    }
2972
2973    /// `RUN LOOP` through CAL is gated exactly as `POST /api/loop/run` is —
2974    /// otherwise the CAL surface is a way around the HTTP gate.
2975    #[test]
2976    fn run_loop_via_cal_is_verb_gated_like_the_route() {
2977        let s = server();
2978        let run = s.route("POST", "/api/loop/run", b"{}", None, None);
2979        assert!(run.0.starts_with("403"), "{}", text(&run));
2980        let cal = s.route("POST", "/api/cal", br#"{"query":"RUN LOOP"}"#, None, None);
2981        assert!(
2982            text(&cal).contains("AUT-E001"),
2983            "anonymous must not run the loop through CAL either: {}",
2984            text(&cal)
2985        );
2986    }
2987}
2988
2989#[cfg(test)]
2990mod namespace_route_tests {
2991    use super::UiServer;
2992    use areev_cal::AreevFacade;
2993    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
2994    use areev_core::types::{Fact, Grain};
2995    use areev_store::Areev;
2996
2997    fn text(r: &(&str, &str, Vec<u8>)) -> String {
2998        String::from_utf8_lossy(&r.2).to_string()
2999    }
3000
3001    /// `agent:writer` is granted read+write on `caller` only — nothing on
3002    /// `other`, nothing on the audit namespace.
3003    fn restricted_server() -> UiServer {
3004        std::env::set_var("AREEV_TEST_NS_WRITER_TOK", "writer-secret");
3005        let dir = tempfile::tempdir().unwrap();
3006        let path = dir.path().join("t.db");
3007        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
3008        store
3009            .add(&Fact::new("agent:writer", REL_PERMITS, "read,write ON caller").namespace(AUTHZ_NS).created_at(1_000))
3010            .unwrap();
3011        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(2_000)).unwrap();
3012        store.add(&Fact::new("bob", "role", "admin").namespace("other").created_at(3_000)).unwrap();
3013        std::mem::forget(dir);
3014        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
3015        let map = CredentialMap::from_json(
3016            r#"{"version":1,"tokens":[{"env":"AREEV_TEST_NS_WRITER_TOK","principal":"agent:writer"}]}"#,
3017        )
3018        .unwrap();
3019        UiServer::new(facade, "test".into()).with_credentials(map)
3020    }
3021
3022    /// Unbound local open — the implicit owner, unrestricted on every
3023    /// namespace, same as a plain `areev ui` with no `--auth`.
3024    fn owner_server() -> UiServer {
3025        let dir = tempfile::tempdir().unwrap();
3026        let path = dir.path().join("t.db");
3027        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
3028        store.add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(1_000)).unwrap();
3029        store.add(&Fact::new("bob", "role", "admin").namespace("other").created_at(2_000)).unwrap();
3030        std::mem::forget(dir);
3031        UiServer::new(AreevFacade::with_session(store, Some("caller".into()), None), "t".into())
3032    }
3033
3034    #[test]
3035    fn namespaces_route_hides_what_the_principal_cannot_read() {
3036        let s = restricted_server();
3037        let r = s.route("GET", "/api/namespaces", b"", Some("writer-secret"), None);
3038        let body = text(&r);
3039        assert!(body.contains("\"caller\""), "granted namespace must be listed: {body}");
3040        assert!(!body.contains("\"other\""), "ungranted namespace must not be listed: {body}");
3041        assert!(!body.contains(AUTHZ_NS), "the audit namespace must not leak either: {body}");
3042    }
3043
3044    #[test]
3045    fn namespaces_route_lists_everything_for_the_owner() {
3046        let s = owner_server();
3047        let r = s.route("GET", "/api/namespaces", b"", None, None);
3048        let body = text(&r);
3049        assert!(body.contains("\"caller\"") && body.contains("\"other\""), "{body}");
3050    }
3051
3052    #[test]
3053    fn browse_ns_param_is_read_gated_for_a_restricted_principal() {
3054        let s = restricted_server();
3055        let ok = s.route("GET", "/api/browse?ns=caller", b"", Some("writer-secret"), None);
3056        assert!(text(&ok).contains("\"ok\":true"), "their own granted namespace must work: {}", text(&ok));
3057
3058        let denied = s.route("GET", "/api/browse?ns=other", b"", Some("writer-secret"), None);
3059        let body = text(&denied);
3060        assert!(body.contains("\"ok\":false"), "an ungranted namespace must be refused, not silently emptied: {body}");
3061    }
3062
3063    #[test]
3064    fn browse_ns_star_requires_owner() {
3065        let s = restricted_server();
3066        let r = s.route("GET", "/api/browse?ns=*", b"", Some("writer-secret"), None);
3067        assert!(text(&r).contains("\"ok\":false"), "a restricted principal must not get the unfiltered view: {}", text(&r));
3068
3069        let owner = owner_server();
3070        let r2 = owner.route("GET", "/api/browse?ns=*", b"", None, None);
3071        let body2 = text(&r2);
3072        assert!(body2.contains("\"ok\":true"), "the owner session may lift the filter: {body2}");
3073        assert!(body2.contains("\"caller\"") && body2.contains("\"other\""), "{body2}");
3074    }
3075
3076    #[test]
3077    fn browse_without_ns_param_keeps_original_session_default_behavior() {
3078        let s = owner_server();
3079        let r = s.route("GET", "/api/browse", b"", None, None);
3080        let body = text(&r);
3081        assert!(body.contains("\"caller\""), "{body}");
3082        assert!(!body.contains("\"other\""), "omitting ?ns must behave exactly as before this feature existed: {body}");
3083    }
3084}
3085
3086#[cfg(test)]
3087mod builder_tests {
3088    use super::UiServer;
3089    use areev_cal::AreevFacade;
3090    use areev_store::Areev;
3091
3092    fn server() -> UiServer {
3093        let dir = tempfile::tempdir().unwrap();
3094        let path = dir.path().join("t.db");
3095        let store = Areev::open(path.to_str().unwrap()).unwrap();
3096        std::mem::forget(dir);
3097        UiServer::new(AreevFacade::with_session(store, Some("caller".into()), None), "t".into())
3098    }
3099
3100    /// `areev ui --no-destructive-ops --policy FILE` calls these in this
3101    /// order. A later builder must not re-enable destruction the operator
3102    /// turned off — the console was announced as read-only.
3103    #[test]
3104    fn attaching_a_loop_policy_preserves_the_destructive_cap() {
3105        let s = server()
3106            .allow_destructive_ops(false)
3107            .with_loop_policy(areev_loop::Policy::default());
3108        let r = s.route(
3109            "POST",
3110            "/api/cal",
3111            br#"{"query":"FORGET sha256:684c6c9bda818630a870119d0726e4d242ed537af061658ef6f3acb158a2c67d"}"#,
3112            None,
3113            None,
3114        );
3115        let body = String::from_utf8_lossy(&r.2).to_string();
3116        assert!(
3117            body.contains("--no-destructive-ops") || r.0.starts_with("401"),
3118            "destruction must stay disabled: {body}"
3119        );
3120    }
3121}
3122
3123#[cfg(test)]
3124mod run_api_tests {
3125    use super::UiServer;
3126    use areev_cal::AreevFacade;
3127    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
3128    use areev_core::types::{Fact, Grain, Tool, ToolKind, Workflow};
3129    use areev_store::Areev;
3130    use std::sync::Arc;
3131
3132    /// A server whose file holds a PARKED run (Client-gated approve node) —
3133    /// the HITL queue's fixture. Returns (server, ask id).
3134    fn parked_server() -> (UiServer, String) {
3135        std::env::set_var("AREEV_RUN_OFFICER_TOK", "officer-secret");
3136        std::env::set_var("AREEV_RUN_STARTER_TOK", "starter-secret");
3137        let dir = tempfile::tempdir().unwrap();
3138        let path = dir.path().join("t.db");
3139        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
3140        for (who, what) in [
3141            ("user:officer", "read,run.respond ON ops"),
3142            ("user:starter", "read ON ops"),
3143        ] {
3144            store
3145                .add(&Fact::new(who, REL_PERMITS, what).namespace(AUTHZ_NS).created_at(900))
3146                .unwrap();
3147        }
3148        std::mem::forget(dir);
3149        let facade = Arc::new(AreevFacade::with_session(store, Some("ops".into()), None));
3150
3151        // Seed + run the plan to the park with a scripted runner.
3152        let (wf, _g, _a) = facade
3153            .with_store(|m| {
3154                let greet = Tool::new("greet")
3155                    .kind(ToolKind::Definition)
3156                    .tool_description("t")
3157                    .created_at(500)
3158                    .namespace("ops");
3159                let gh = m.add(&greet)?;
3160                let approve = Tool::new("approve")
3161                    .kind(ToolKind::Definition)
3162                    .tool_description("human approves")
3163                    .executor_kind(areev_core::types::ExecutorKind::Client)
3164                    .created_at(501)
3165                    .namespace("ops");
3166                let ah = m.add(&approve)?;
3167                let wf = Workflow::new(vec!["greet".into(), "approve".into()])
3168                    .edge("greet", "approve")
3169                    .bind("greet", &gh.to_hex())
3170                    .bind("approve", &ah.to_hex())
3171                    .created_at(502)
3172                    .namespace("ops");
3173                let wh = m.add(&wf)?;
3174                Ok::<_, areev_core::error::AreevError>((wh, gh, ah))
3175            })
3176            .unwrap();
3177        struct Ok1;
3178        impl areev_run::HostToolExecutor for Ok1 {
3179            fn execute(
3180                &self,
3181                t: &str,
3182                _h: &str,
3183                _i: &serde_json::Value,
3184                _k: &str,
3185            ) -> areev_run::ExecResult {
3186                areev_run::ExecResult::Ok(serde_json::json!({t.to_string(): true}))
3187            }
3188        }
3189        let runner = areev_run::Runner {
3190            facade: Arc::clone(&facade),
3191            clock: Arc::new(areev_run::ScriptedClock::new(
3192                (0..100).map(|i| 1_758_000_000_000 + i * 10).collect(),
3193            )),
3194            executor: Arc::new(Ok1),
3195            llm: None,
3196            observer: None,
3197            ns: "ops".into(),
3198            principal: "user:starter".into(),
3199        };
3200        let session = runner
3201            .start(&wf, "hitl-1", serde_json::json!({}), &areev_run::RunOptions {
3202                workers: 1,
3203                ..Default::default()
3204            })
3205            .unwrap();
3206        let areev_run::RunSession::Parked { envelope, .. } = session else {
3207            panic!("expected park")
3208        };
3209        let ask = envelope["asks"][0]["tool_call_id"].as_str().unwrap().to_string();
3210
3211        // Rebuild a facade-owning server over the SAME file is impossible
3212        // (single writer) — hand the server the same Arc'd facade via the
3213        // Arc-aware constructor path: UiServer::new takes AreevFacade, so
3214        // we build the server FIRST in real deployments; here we reuse the
3215        // internal Arc through a second UiServer field write. Instead:
3216        // drop the runner (facade Arc count drops) and unwrap.
3217        drop(runner);
3218        let facade = Arc::try_unwrap(facade).unwrap_or_else(|_| panic!("facade still shared"));
3219        let map = CredentialMap::from_json(
3220            r#"{"version":1,"tokens":[
3221                {"env":"AREEV_RUN_OFFICER_TOK","principal":"user:officer"},
3222                {"env":"AREEV_RUN_STARTER_TOK","principal":"user:starter"}
3223            ]}"#,
3224        )
3225        .unwrap();
3226        let server = UiServer::new(facade, "test".into())
3227            .with_auth("shared-console-secret".into())
3228            .with_credentials(map);
3229        (server, ask)
3230    }
3231
3232    fn body(run_id: &str, ask: &str) -> Vec<u8> {
3233        serde_json::json!({"run_id": run_id, "tool_call_id": ask, "result": {"approved": true}})
3234            .to_string()
3235            .into_bytes()
3236    }
3237
3238    /// #165: `/api/run/list` is scoped and paged on the server, and says
3239    /// its total and whether it truncated. Twenty older runs in a quiet
3240    /// namespace behind forty newer ones in a busy one is exactly the
3241    /// shape under which the old fixed newest-50 page, filtered
3242    /// client-side, showed the quiet tenant as having no runs.
3243    #[test]
3244    fn run_list_is_scoped_and_paged_server_side() {
3245        let dir = tempfile::tempdir().unwrap();
3246        let path = dir.path().join("t.db");
3247        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
3248        for i in 0..60u32 {
3249            let (id, ns) = if i < 20 {
3250                (format!("quiet-{i}"), "tenant.quiet")
3251            } else {
3252                (format!("busy-{i}"), "tenant.busy")
3253            };
3254            let mut f = Fact::new(&format!("run:{id}"), "mg:harness", "00")
3255                .namespace(areev_core::authz::HARNESS_NS)
3256                .created_at(1_000 + i64::from(i));
3257            f.common.extra_fields.insert("run_id".into(), serde_json::json!(id));
3258            f.common.extra_fields.insert("run_ns".into(), serde_json::json!(ns));
3259            store.add(&f).unwrap();
3260        }
3261        std::mem::forget(dir);
3262        let s = UiServer::new(AreevFacade::with_session(store, Some("ops".into()), None), "test".into());
3263        let page = |query: &str| -> serde_json::Value {
3264            let r = s.route("GET", &format!("/api/run/list{query}"), b"", None, None);
3265            assert!(r.0.starts_with("200"), "{} {}", r.0, String::from_utf8_lossy(&r.2));
3266            serde_json::from_slice(&r.2).unwrap()
3267        };
3268        let rows = |v: &serde_json::Value| v["runs"].as_array().unwrap().clone();
3269
3270        // The default page: newest fifty of sixty, and it SAYS so.
3271        let all = page("");
3272        assert_eq!(rows(&all).len(), 50);
3273        assert_eq!(all["total"], 60);
3274        assert_eq!(all["truncated"], true);
3275        assert_eq!(rows(&all).iter().filter(|r| r["ns"] == "tenant.quiet").count(), 10);
3276
3277        // Scoped: the whole quiet tenant, no truncation, nothing else.
3278        let quiet = page("?ns=tenant.quiet");
3279        assert_eq!(quiet["total"], 20);
3280        assert_eq!(quiet["truncated"], false);
3281        assert_eq!(rows(&quiet).len(), 20);
3282        assert!(rows(&quiet).iter().all(|r| r["ns"] == "tenant.quiet"), "{quiet}");
3283
3284        // A prefix scope, paged to the end.
3285        let tail = page("?ns=tenant.*&limit=10&offset=55");
3286        assert_eq!(tail["total"], 60);
3287        assert_eq!(rows(&tail).len(), 5);
3288        assert_eq!(tail["truncated"], false);
3289        assert_eq!((tail["offset"].as_u64(), tail["limit"].as_u64()), (Some(55), Some(10)));
3290
3291        // `*` is unscoped; an absurd limit is clamped rather than honored.
3292        assert_eq!(page("?ns=*")["total"], 60);
3293        assert_eq!(page("?limit=100000")["limit"], 500);
3294
3295        // A row from before the namespace stamp is not known to be in any
3296        // scope: excluded from a scoped read and COUNTED, present unscoped.
3297        s.facade
3298            .with_store(|m| {
3299                let mut f = Fact::new("run:old", "mg:harness", "00")
3300                    .namespace(areev_core::authz::HARNESS_NS)
3301                    .created_at(1);
3302                f.common.extra_fields.insert("run_id".into(), serde_json::json!("old"));
3303                m.add(&f)
3304            })
3305            .unwrap();
3306        let quiet = page("?ns=tenant.quiet");
3307        assert_eq!(quiet["total"], 20, "scoping stays exact: {quiet}");
3308        assert_eq!(quiet["unattributed"], 1, "and says what it left out: {quiet}");
3309        let unscoped = page("");
3310        assert_eq!(unscoped["total"], 61);
3311        assert_eq!(unscoped["unattributed"], 0);
3312    }
3313
3314    /// `/api/run/shadow` is the plan-change rehearsal as a read: GET names a
3315    /// stored plan, POST may carry an unstored draft. Pinned on the parked
3316    /// fixture under its own plan — the identity row — and on the refusals.
3317    #[test]
3318    fn run_shadow_route_rehearses_a_run_under_a_candidate_plan() {
3319        let (s, _ask) = parked_server();
3320        let (_st, _ct, body) = s.route("GET", "/api/run/inspect?run_id=hitl-1", b"", Some("officer-secret"), None);
3321        let inspect: serde_json::Value = serde_json::from_slice(&body).unwrap();
3322        let plan = inspect["plan_hash"].as_str().expect("the run's plan").to_string();
3323
3324        let (status, _ct, body) = s.route("GET", &format!("/api/run/shadow?runs=hitl-1&plan={plan}"), b"", Some("officer-secret"), None);
3325        assert!(status.starts_with("200"), "{status}");
3326        let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
3327        assert_eq!(v["ok"], true, "{v}");
3328        assert_eq!(v["candidate_plan"], plan.as_str());
3329        assert_eq!((v["effect_dispatches"].as_u64(), v["writes"].as_u64()), (Some(0), Some(0)));
3330        let row = &v["runs"][0];
3331        assert_eq!(row["run_id"], "hitl-1");
3332        assert!(row["identity"].is_object(), "the incumbent as candidate is a verify: {row}");
3333
3334        // A draft body over the same run; and the two refusals.
3335        let draft = serde_json::json!({"runs": ["hitl-1"], "plan_body": {"nodes": ["greet"], "edges": []}});
3336        let (status, _ct, body) = s.route("POST", "/api/run/shadow", draft.to_string().as_bytes(), Some("officer-secret"), None);
3337        assert!(status.starts_with("200"), "{status}");
3338        let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
3339        assert_eq!(v["ok"], true, "{v}");
3340        assert_eq!(v["candidate_is_draft"], true);
3341        let (_st, _ct, body) = s.route("POST", "/api/run/shadow", br#"{"runs": ["hitl-1"]}"#, Some("officer-secret"), None);
3342        let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
3343        assert_eq!(v["ok"], false, "{v}");
3344        let (_st, _ct, body) = s.route("GET", "/api/run/shadow?runs=hitl-1", b"", Some("officer-secret"), None);
3345        let v: serde_json::Value = serde_json::from_slice(&body).unwrap();
3346        assert_eq!(v["ok"], false, "plan required: {v}");
3347    }
3348
3349    #[test]
3350    fn shared_token_approvals_are_refused_and_principals_approve() {
3351        let (s, ask) = parked_server();
3352
3353        // The HITL queue is visible.
3354        let r = s.route("GET", "/api/run/list", b"", Some("shared-console-secret"), None);
3355        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
3356        let r = s.route(
3357            "GET",
3358            "/api/run/inspect?run_id=hitl-1",
3359            b"",
3360            Some("shared-console-secret"),
3361            None,
3362        );
3363        assert!(String::from_utf8_lossy(&r.2).contains(&ask), "ask visible in inspect");
3364
3365        // 1. The SHARED console secret cannot approve — identity is the point.
3366        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("shared-console-secret"), None);
3367        assert!(r.0.starts_with("403"), "shared-token approval must refuse: {}", r.0);
3368
3369        // 2. The STARTER's own credential cannot approve their own run
3370        //    (separation of duties, enforced by the runtime).
3371        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("starter-secret"), None);
3372        let text = String::from_utf8_lossy(&r.2).to_string();
3373        assert!(text.contains("responder") || text.contains("RUN-E012"), "{text}");
3374
3375        // 3. The officer's principal-bound credential approves.
3376        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("officer-secret"), None);
3377        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
3378        assert!(String::from_utf8_lossy(&r.2).contains("user:officer"));
3379
3380        // 4. The kill switch keeps its low bar: shared token may cancel.
3381        let r = s.route(
3382            "POST",
3383            "/api/run/cancel",
3384            br#"{"run_id":"hitl-1","because":"test brake"}"#,
3385            Some("shared-console-secret"),
3386            None,
3387        );
3388        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
3389    }
3390
3391    /// [A0] The approval trust floor. `user:officer` holds `run.respond ON
3392    /// ops` in the FILE, so the grants are identical whichever way the
3393    /// identity arrives — which is exactly the point. What differs is the
3394    /// PROOF: a credential-map token is a per-principal secret; an SSO header
3395    /// is trusted via one shared, fleet-wide proxy secret that lets its
3396    /// holder assert any approver. The default refuses the weaker proof.
3397    #[test]
3398    fn proxy_asserted_identities_cannot_approve_by_default() {
3399        let (s, ask) = parked_server();
3400        let s = s.with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>);
3401
3402        // The SSO identity is a GRANTED principal and still cannot approve.
3403        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), None, Some("user:officer"));
3404        assert!(
3405            r.0.starts_with("403"),
3406            "proxy-asserted approval must refuse by default: {} {}",
3407            r.0,
3408            String::from_utf8_lossy(&r.2)
3409        );
3410        let text = String::from_utf8_lossy(&r.2).to_string();
3411        assert!(
3412            text.contains("--sso-approvals"),
3413            "the refusal must name the flag that lifts it: {text}"
3414        );
3415
3416        // Reviewing is unaffected — SSO identities keep every read.
3417        let r = s.route("GET", "/api/run/list", b"", None, Some("user:officer"));
3418        assert!(r.0.starts_with("200"), "SSO identities may still review: {}", r.0);
3419
3420        // And the SAME principal, proven by its own credential, approves.
3421        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), Some("officer-secret"), None);
3422        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
3423    }
3424
3425    /// [A0] `--sso-approvals allow` is the operator accepting that trade-off
3426    /// explicitly. Nothing else changes: the same identity, the same file
3427    /// grants, the same runtime separation-of-duties check behind it.
3428    #[test]
3429    fn sso_approvals_allow_opts_in_to_proxy_asserted_approvals() {
3430        let (s, ask) = parked_server();
3431        let s = s
3432            .with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>)
3433            .with_sso_approvals(true);
3434
3435        let r = s.route("POST", "/api/run/respond", &body("hitl-1", &ask), None, Some("user:officer"));
3436        assert!(r.0.starts_with("200"), "{}", String::from_utf8_lossy(&r.2));
3437        assert!(String::from_utf8_lossy(&r.2).contains("user:officer"));
3438    }
3439
3440    /// [A2] Groups → principals, so SSO scales without a grant grain per
3441    /// person — and the precedence that makes it safe: an identity with its
3442    /// own grants outranks its group, so adding a groups header can never
3443    /// narrow someone who already had rights.
3444    #[test]
3445    fn groups_resolve_to_principals_but_never_outrank_an_identity() {
3446        let (s, _ask) = parked_server();
3447        // `user:officer` is granted in the file; `role:reviewer` is not a
3448        // credential, only a group target.
3449        let map = CredentialMap::from_json(
3450            r#"{"version":1,
3451                "tokens":[{"env":"AREEV_RUN_OFFICER_TOK","principal":"user:officer","id":"off"}],
3452                "groups":{"Engineering":"user:officer"}}"#,
3453        )
3454        .unwrap();
3455        let s = s
3456            .with_credentials(map)
3457            .with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>)
3458            .with_sso_groups_header("x-forwarded-groups");
3459
3460        // An UNGRANTED identity in a mapped group inherits the group's
3461        // principal — the whole point of the feature.
3462        let r = s.route_with_groups(
3463            "GET",
3464            "/api/whoami",
3465            b"",
3466            None,
3467            Some("user:newhire@example.com"),
3468            Some("platform,Engineering"),
3469        );
3470        let text = String::from_utf8_lossy(&r.2).to_string();
3471        assert!(text.contains("user:officer"), "group should resolve: {text}");
3472        assert!(text.contains(r#""identity_source":"sso-group""#), "{text}");
3473
3474        // Case-insensitive: directories are inconsistent, and a mapping that
3475        // misses on case would fail OPEN into the identity's own (empty)
3476        // grants.
3477        let r = s.route_with_groups(
3478            "GET", "/api/whoami", b"", None, Some("user:newhire@example.com"), Some("ENGINEERING"),
3479        );
3480        assert!(String::from_utf8_lossy(&r.2).contains("user:officer"));
3481
3482        // An identity that IS granted keeps its own principal, group or not.
3483        let r = s.route_with_groups(
3484            "GET", "/api/whoami", b"", None, Some("user:officer"), Some("Engineering"),
3485        );
3486        let text = String::from_utf8_lossy(&r.2).to_string();
3487        assert!(text.contains(r#""identity_source":"sso""#), "identity outranks group: {text}");
3488
3489        // An unmapped group resolves to nothing and the identity stands.
3490        let r = s.route_with_groups(
3491            "GET", "/api/whoami", b"", None, Some("user:newhire@example.com"), Some("sales"),
3492        );
3493        assert!(String::from_utf8_lossy(&r.2).contains("user:newhire@example.com"));
3494    }
3495
3496    /// [A2] A role is not an approver. This refusal has no flag of its own —
3497    /// `--sso-approvals allow` does not lift it — because an audit record
3498    /// reading "role:eng approved" names nobody who can be asked why.
3499    #[test]
3500    fn group_derived_principals_can_never_approve() {
3501        let (s, ask) = parked_server();
3502        let map = CredentialMap::from_json(
3503            r#"{"version":1,
3504                "tokens":[{"env":"AREEV_RUN_OFFICER_TOK","principal":"user:officer","id":"off"}],
3505                "groups":{"approvers":"user:officer"}}"#,
3506        )
3507        .unwrap();
3508        let s = s
3509            .with_credentials(map)
3510            .with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>)
3511            .with_sso_groups_header("x-forwarded-groups")
3512            // Even with the A0 opt-in explicitly granted.
3513            .with_sso_approvals(true);
3514
3515        let r = s.route_with_groups(
3516            "POST",
3517            "/api/run/respond",
3518            &body("hitl-1", &ask),
3519            None,
3520            Some("user:newhire@example.com"),
3521            Some("approvers"),
3522        );
3523        assert!(r.0.starts_with("403"), "{} {}", r.0, String::from_utf8_lossy(&r.2));
3524        let text = String::from_utf8_lossy(&r.2).to_string();
3525        assert!(text.contains("role"), "the refusal must say why: {text}");
3526    }
3527
3528    /// [A2] The prefix keeps IdP-sourced principals visibly distinct from
3529    /// local ones everywhere they land.
3530    #[test]
3531    fn the_principal_prefix_is_stamped_on_proxy_asserted_identities() {
3532        let (s, _ask) = parked_server();
3533        let s = s
3534            .with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>)
3535            .with_sso_principal_prefix("sso:");
3536
3537        let r = s.route("GET", "/api/whoami", b"", None, Some("pat@example.com"));
3538        assert!(String::from_utf8_lossy(&r.2).contains("sso:pat@example.com"), "{}", String::from_utf8_lossy(&r.2));
3539
3540        // Idempotent: an identity the proxy already prefixed is not
3541        // double-stamped into a different principal than the grant names.
3542        let r = s.route("GET", "/api/whoami", b"", None, Some("sso:pat@example.com"));
3543        let text = String::from_utf8_lossy(&r.2).to_string();
3544        assert!(text.contains("sso:pat@example.com"), "{text}");
3545        assert!(!text.contains("sso:sso:"), "must not double-stamp: {text}");
3546    }
3547
3548    /// [A0] `whoami` tells an approver where they stand BEFORE they try to
3549    /// approve — a console that only discovers this at the 403 is a console
3550    /// that discovers it mid-incident.
3551    #[test]
3552    fn whoami_reports_identity_provenance_and_approval_capability() {
3553        let (s, _ask) = parked_server();
3554        let s = s.with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>);
3555
3556        let r = s.route("GET", "/api/whoami", b"", None, Some("user:officer"));
3557        let text = String::from_utf8_lossy(&r.2).to_string();
3558        assert!(text.contains(r#""identity_source":"sso""#), "{text}");
3559        assert!(text.contains(r#""may_approve":false"#), "{text}");
3560
3561        let r = s.route("GET", "/api/whoami", b"", Some("officer-secret"), None);
3562        let text = String::from_utf8_lossy(&r.2).to_string();
3563        assert!(text.contains(r#""identity_source":"credential""#), "{text}");
3564        assert!(text.contains(r#""may_approve":true"#), "{text}");
3565    }
3566}
3567
3568#[cfg(test)]
3569mod memory_scoped_token_tests {
3570    use super::UiServer;
3571    use areev_cal::AreevFacade;
3572    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
3573    use areev_core::types::{Fact, Grain};
3574    use areev_store::Areev;
3575
3576    /// The enterprise-plane rule: one auth file, per-memory token
3577    /// scopes — a token listed for memory A is an UNKNOWN token on
3578    /// memory B's server.
3579    #[test]
3580    fn memory_scoped_tokens_do_not_cross_memories() {
3581        std::env::set_var("AREEV_SCOPED_TOK", "scoped-secret");
3582        let map_json = r#"{"version":1,"tokens":[
3583            {"env":"AREEV_SCOPED_TOK","principal":"agent:sync","memories":["memory-a"]}
3584        ]}"#;
3585        let server_for = |label: &str| {
3586            let dir = tempfile::tempdir().unwrap();
3587            let mut store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
3588            store
3589                .add(
3590                    &Fact::new("agent:sync", REL_PERMITS, "read,write ON caller")
3591                        .namespace(AUTHZ_NS)
3592                        .created_at(1_000),
3593                )
3594                .unwrap();
3595            store
3596                .add(&Fact::new("acme", "tier", "Enterprise").namespace("caller").created_at(2_000))
3597                .unwrap();
3598            std::mem::forget(dir);
3599            let facade = AreevFacade::with_session(store, Some("caller".into()), None);
3600            UiServer::new(facade, label.into())
3601                .with_credentials(CredentialMap::from_json(map_json).unwrap())
3602        };
3603        const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
3604
3605        // On memory-a the token authenticates and its grants let the write
3606        // through (no authorization refusal in the CAL payload).
3607        let a = server_for("memory-a");
3608        let r = a.route("POST", "/api/cal", WRITE, Some("scoped-secret"), None);
3609        let text = String::from_utf8_lossy(&r.2).to_string();
3610        assert!(r.0.starts_with("200") && !text.contains("AUT-E001"), "{text}");
3611
3612        // On memory-b the SAME token is an unknown credential: the request
3613        // runs as anonymous and the write is refused (AUT-E001 rides the
3614        // CAL payload — the console surfaces statement results in-band).
3615        let b = server_for("memory-b");
3616        let r = b.route("POST", "/api/cal", WRITE, Some("scoped-secret"), None);
3617        let text = String::from_utf8_lossy(&r.2).to_string();
3618        assert!(
3619            text.contains("AUT-E001") && text.contains("anonymous"),
3620            "scoped token must not cross memories: {text}"
3621        );
3622
3623        // An auth file with an EMPTY memories scope refuses to load.
3624        assert!(CredentialMap::from_json(
3625            r#"{"version":1,"tokens":[{"env":"AREEV_SCOPED_TOK","principal":"x","memories":[]}]}"#
3626        )
3627        .is_err());
3628    }
3629}
3630
3631#[cfg(all(test, feature = "tls"))]
3632mod tls_tests {
3633    use super::UiServer;
3634    use areev_cal::AreevFacade;
3635    use areev_store::Areev;
3636    use std::io::{Read, Write};
3637
3638    fn self_signed(dir: &std::path::Path) -> Option<(String, String)> {
3639        let cert = dir.join("cert.pem");
3640        let key = dir.join("key.pem");
3641        let status = std::process::Command::new("openssl")
3642            .args([
3643                "req", "-x509", "-newkey", "rsa:2048", "-nodes",
3644                "-keyout", key.to_str().unwrap(), "-out", cert.to_str().unwrap(),
3645                "-days", "1", "-subj", "/CN=localhost",
3646                "-addext", "subjectAltName=DNS:localhost,IP:127.0.0.1",
3647                "-addext", "basicConstraints=critical,CA:FALSE",
3648            ])
3649            .stdout(std::process::Stdio::null())
3650            .stderr(std::process::Stdio::null())
3651            .status();
3652        match status {
3653            Ok(s) if s.success() => Some((
3654                cert.to_str().unwrap().to_string(),
3655                key.to_str().unwrap().to_string(),
3656            )),
3657            _ => None, // no openssl on this host: the CI leg covers it
3658        }
3659    }
3660
3661    /// The §3.2 gate: a TLS round-trip succeeds, and a PLAINTEXT client on
3662    /// the same listener is refused (no silent downgrade).
3663    #[test]
3664    fn tls_round_trip_and_no_plaintext_downgrade() {
3665        let dir = tempfile::tempdir().unwrap();
3666        let Some((cert, key)) = self_signed(dir.path()) else {
3667            eprintln!("skipping: no openssl binary");
3668            return;
3669        };
3670        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
3671        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
3672        let server = UiServer::new(facade, "tls-test".into())
3673            .with_tls(&cert, &key)
3674            .unwrap();
3675        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
3676        let addr = listener.local_addr().unwrap();
3677        let handle = std::thread::spawn(move || {
3678            // Two connections: the TLS client, then the plaintext prober.
3679            for _ in 0..2 {
3680                if let Ok((s, _)) = listener.accept() {
3681                    let _ = server.handle(s);
3682                }
3683            }
3684        });
3685
3686        // 1. TLS round-trip: trust exactly our self-signed cert.
3687        let mut roots = rustls::RootCertStore::empty();
3688        {
3689            use rustls::pki_types::pem::PemObject;
3690            for c in rustls::pki_types::CertificateDer::pem_file_iter(&cert).unwrap() {
3691                roots.add(c.unwrap()).unwrap();
3692            }
3693        }
3694        let config = std::sync::Arc::new(
3695            rustls::ClientConfig::builder()
3696                .with_root_certificates(roots)
3697                .with_no_client_auth(),
3698        );
3699        let name = rustls::pki_types::ServerName::try_from("localhost").unwrap();
3700        let session = rustls::ClientConnection::new(config, name).unwrap();
3701        let tcp = std::net::TcpStream::connect(addr).unwrap();
3702        let mut tls = rustls::StreamOwned::new(session, tcp);
3703        tls.write_all(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n")
3704            .unwrap();
3705        let mut response = String::new();
3706        let _ = tls.read_to_string(&mut response);
3707        assert!(response.starts_with("HTTP/1.1 200"), "{}", &response[..response.len().min(120)]);
3708        assert!(response.contains("Areev"), "console served over TLS");
3709
3710        // 2. No downgrade: plaintext HTTP on the TLS listener gets a
3711        // handshake failure, never a page.
3712        let mut plain = std::net::TcpStream::connect(addr).unwrap();
3713        plain
3714            .write_all(b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\nConnection: close\r\n\r\n")
3715            .unwrap();
3716        let mut buf = Vec::new();
3717        let _ = plain.read_to_end(&mut buf);
3718        let text = String::from_utf8_lossy(&buf);
3719        assert!(
3720            !text.contains("HTTP/1.1 200"),
3721            "plaintext must not be served on a TLS listener: {text}"
3722        );
3723        handle.join().unwrap();
3724    }
3725
3726    /// `with_tls` composes with `with_auth` — `with_auth` only sets the
3727    /// token/auth_all fields, not the connection layer, so an authenticated
3728    /// console terminates TLS identically to an open one. Pins the CLI
3729    /// wiring that lets `areev ui --token-env VAR --tls-cert ... --tls-key
3730    /// ...` work, not just `areev ui --tls-cert ...`.
3731    #[test]
3732    fn an_authenticated_console_terminates_tls_the_same_way() {
3733        let dir = tempfile::tempdir().unwrap();
3734        let Some((cert, key)) = self_signed(dir.path()) else {
3735            eprintln!("skipping: no openssl binary");
3736            return;
3737        };
3738        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
3739        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
3740        let server = UiServer::new(facade, "auth-tls-test".into())
3741            .with_auth("console-token".into())
3742            .with_tls(&cert, &key)
3743            .unwrap();
3744        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
3745        let addr = listener.local_addr().unwrap();
3746        let handle = std::thread::spawn(move || {
3747            if let Ok((s, _)) = listener.accept() {
3748                let _ = server.handle(s);
3749            }
3750        });
3751
3752        let mut roots = rustls::RootCertStore::empty();
3753        {
3754            use rustls::pki_types::pem::PemObject;
3755            for c in rustls::pki_types::CertificateDer::pem_file_iter(&cert).unwrap() {
3756                roots.add(c.unwrap()).unwrap();
3757            }
3758        }
3759        let config = std::sync::Arc::new(
3760            rustls::ClientConfig::builder()
3761                .with_root_certificates(roots)
3762                .with_no_client_auth(),
3763        );
3764        let name = rustls::pki_types::ServerName::try_from("localhost").unwrap();
3765        let session = rustls::ClientConnection::new(config, name).unwrap();
3766        let tcp = std::net::TcpStream::connect(addr).unwrap();
3767        let mut tls = rustls::StreamOwned::new(session, tcp);
3768        tls.write_all(
3769            b"GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n\
3770              Authorization: Bearer console-token\r\nConnection: close\r\n\r\n",
3771        )
3772        .unwrap();
3773        let mut response = String::new();
3774        let _ = tls.read_to_string(&mut response);
3775        assert!(
3776            response.starts_with("HTTP/1.1 200"),
3777            "an authenticated console should terminate TLS like an open one: {}",
3778            &response[..response.len().min(120)]
3779        );
3780        handle.join().unwrap();
3781    }
3782}
3783
3784#[cfg(test)]
3785mod sso_tests {
3786    use super::UiServer;
3787    use areev_cal::AreevFacade;
3788    use areev_core::authz::{AUTHZ_NS, REL_PERMITS};
3789    use areev_core::types::{Fact, Grain};
3790    use areev_store::Areev;
3791    use std::io::{Read, Write};
3792
3793    const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
3794
3795    fn sso_server() -> UiServer {
3796        sso_server_with("proxy-secret", None)
3797    }
3798
3799    /// The same server mid-rotation: `proxy-secret` deployed, `next` incoming.
3800    fn sso_server_rotating(next: Option<&str>) -> UiServer {
3801        sso_server_with("proxy-secret", next)
3802    }
3803
3804    fn sso_server_with(secret: &str, next: Option<&str>) -> UiServer {
3805        let dir = tempfile::tempdir().unwrap();
3806        let mut store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
3807        store
3808            .add(
3809                &Fact::new("user:pat@example.com", REL_PERMITS, "read,write ON caller")
3810                    .namespace(AUTHZ_NS)
3811                    .created_at(1_000),
3812            )
3813            .unwrap();
3814        std::mem::forget(dir);
3815        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
3816        UiServer::new(facade, "sso-test".into())
3817            .with_sso_rotating("x-forwarded-user", secret, next)
3818    }
3819
3820    fn post(server: &UiServer, extra_headers: &str) -> String {
3821        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
3822        let addr = listener.local_addr().unwrap();
3823        std::thread::scope(|scope| {
3824            scope.spawn(|| {
3825                if let Ok((s, _)) = listener.accept() {
3826                    let _ = server.handle(s);
3827                }
3828            });
3829            let mut c = std::net::TcpStream::connect(addr).unwrap();
3830            let req = format!(
3831                "POST /api/cal HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\n{extra_headers}Content-Length: {}\r\nConnection: close\r\n\r\n",
3832                WRITE.len()
3833            );
3834            c.write_all(req.as_bytes()).unwrap();
3835            c.write_all(WRITE).unwrap();
3836            let mut out = String::new();
3837            let _ = c.read_to_string(&mut out);
3838            out
3839        })
3840    }
3841
3842    /// The §3.3 gate: a proxy-proven identity lands as a named actor whose
3843    /// FILE grants decide; a forged identity header without the proxy
3844    /// secret is ignored (the write is refused as anonymous).
3845    #[test]
3846    fn proxied_identity_works_and_forged_headers_are_ignored() {
3847        let server = sso_server();
3848
3849        // 1. Proxy-proven: identity + secret → the granted principal writes.
3850        let ok = post(
3851            &server,
3852            "X-Forwarded-User: user:pat@example.com\r\nX-Areev-Proxy-Secret: proxy-secret\r\n",
3853        );
3854        assert!(ok.contains("HTTP/1.1 200"), "{}", &ok[..ok.len().min(200)]);
3855        assert!(!ok.contains("AUT-E001"), "granted principal must write: {ok}");
3856
3857        // 2. Forged: the identity header WITHOUT the secret is ignored — the
3858        // request is anonymous and the write refuses.
3859        let forged = post(&server, "X-Forwarded-User: user:pat@example.com\r\n");
3860        assert!(
3861            forged.contains("anonymous") || forged.contains("401"),
3862            "forged identity must not authenticate: {forged}"
3863        );
3864
3865        // 3. Wrong secret: same refusal (constant-time compare, same path).
3866        let wrong = post(
3867            &server,
3868            "X-Forwarded-User: user:pat@example.com\r\nX-Areev-Proxy-Secret: guess\r\n",
3869        );
3870        assert!(
3871            wrong.contains("anonymous") || wrong.contains("401"),
3872            "wrong proxy secret must not authenticate: {wrong}"
3873        );
3874    }
3875
3876    /// [A2] The proxy secret proves the PROXY. It does not make whatever the
3877    /// proxy forwarded well-formed — and an identity lands in immutable,
3878    /// replicating audit grains, so a malformed one outlives its request.
3879    #[test]
3880    fn malformed_identities_are_rejected_even_with_a_valid_proxy_secret() {
3881        assert_eq!(
3882            super::sanitize_sso_identity("  user:pat@example.com  ").as_deref(),
3883            Some("user:pat@example.com"),
3884            "surrounding whitespace is trimmed, not rejected"
3885        );
3886
3887        // CR/LF would smuggle a second line into every log and audit record.
3888        assert!(super::sanitize_sso_identity("user:a\r\nX-Admin: true").is_none());
3889        assert!(super::sanitize_sso_identity("user:a\tb").is_none());
3890        // Internal whitespace would make "user: a" and "user:a" two audit
3891        // identities for one person.
3892        assert!(super::sanitize_sso_identity("user a").is_none());
3893        assert!(super::sanitize_sso_identity("").is_none());
3894        assert!(super::sanitize_sso_identity(&"x".repeat(129)).is_none());
3895        // Reserved: an audit line must never be ambiguous between a vouched
3896        // person and the console's own floor/ceiling principals.
3897        assert!(super::sanitize_sso_identity("anonymous").is_none());
3898        assert!(super::sanitize_sso_identity("user:console").is_none());
3899    }
3900
3901    /// [A2] A rejected identity is treated as an ABSENT one — the request
3902    /// degrades to anonymous rather than the console refusing to serve, so a
3903    /// misconfigured proxy cannot take the console down.
3904    #[test]
3905    fn a_rejected_identity_degrades_to_anonymous_rather_than_erroring() {
3906        let server = sso_server();
3907        let out = post(
3908            &server,
3909            "X-Forwarded-User: anonymous\r\nX-Areev-Proxy-Secret: proxy-secret\r\n",
3910        );
3911        assert!(
3912            out.contains("anonymous") || out.contains("401"),
3913            "a reserved name must not authenticate: {out}"
3914        );
3915        assert!(!out.contains("HTTP/1.1 500"), "and must not error: {out}");
3916    }
3917
3918    /// The rotation window (#79): while two secrets are configured, either
3919    /// proves the proxy — so the fleet moves over one node at a time instead
3920    /// of atomically, which is the only way an impersonation-grade credential
3921    /// gets rotated without an outage or a gap.
3922    #[test]
3923    fn both_secrets_authenticate_during_a_rotation_window() {
3924        let server = sso_server_rotating(Some("new-secret"));
3925        let id = "X-Forwarded-User: user:pat@example.com\r\n";
3926
3927        for secret in ["proxy-secret", "new-secret"] {
3928            let out = post(&server, &format!("{id}X-Areev-Proxy-Secret: {secret}\r\n"));
3929            assert!(out.contains("HTTP/1.1 200"), "{secret} rejected: {out}");
3930            assert!(!out.contains("AUT-E001"), "{secret} must authenticate: {out}");
3931        }
3932
3933        // The window widens what proves the proxy, never what a proved
3934        // identity may do, and never to a third value.
3935        let wrong = post(&server, &format!("{id}X-Areev-Proxy-Secret: guess\r\n"));
3936        assert!(
3937            wrong.contains("anonymous") || wrong.contains("401"),
3938            "an unrelated secret must still be refused mid-rotation: {wrong}"
3939        );
3940    }
3941
3942    /// Closing the window is what actually retires the old credential — the
3943    /// step the runbook exists to make sure nobody skips.
3944    #[test]
3945    fn the_retired_secret_stops_working_once_the_window_closes() {
3946        // Rotation complete: the new value is now the only one deployed.
3947        let server = sso_server_with("new-secret", None);
3948        let id = "X-Forwarded-User: user:pat@example.com\r\n";
3949
3950        let ok = post(&server, &format!("{id}X-Areev-Proxy-Secret: new-secret\r\n"));
3951        assert!(ok.contains("HTTP/1.1 200"), "{ok}");
3952        assert!(!ok.contains("AUT-E001"), "the promoted secret must authenticate: {ok}");
3953
3954        let old = post(&server, &format!("{id}X-Areev-Proxy-Secret: proxy-secret\r\n"));
3955        assert!(
3956            old.contains("anonymous") || old.contains("401"),
3957            "the retired secret must stop working: {old}"
3958        );
3959    }
3960
3961    #[test]
3962    #[should_panic(expected = "must differ")]
3963    fn rotating_to_the_same_value_is_refused() {
3964        // A rotation that deploys the same value twice did not happen, and it
3965        // would read as "both live" in every log and status line.
3966        let _ = sso_server_rotating(Some("proxy-secret"));
3967    }
3968
3969    use crate::CONSOLE_HTML;
3970    use std::collections::BTreeSet;
3971
3972    /// Every `<section class="page" id="page-X">` must be named in the one
3973    /// array in `render()` that clears `hidden`, and every sidebar
3974    /// `data-page="X"` must have such a section.
3975    ///
3976    /// The console's page sections ship `hidden` in the markup, so a page is
3977    /// visible only if `render()` un-hides it. The Triggers tab shipped
3978    /// missing from that array: `renderTriggers()` filled `#trgBody` on every
3979    /// render, the nav item highlighted, the hash routed — and the section
3980    /// stayed `hidden` while every other page was hidden too, so the tab
3981    /// showed a blank pane. Nothing else in the file could catch it: the
3982    /// omission is a *missing* string, not a wrong one.
3983    #[test]
3984    fn every_console_page_is_in_the_visibility_list() {
3985        fn all(hay: &str, open: &str) -> BTreeSet<String> {
3986            let mut out = BTreeSet::new();
3987            let mut rest = hay;
3988            while let Some(i) = rest.find(open) {
3989                rest = &rest[i + open.len()..];
3990                let end = rest.find('"').expect("unterminated attribute");
3991                out.insert(rest[..end].to_string());
3992                rest = &rest[end..];
3993            }
3994            out
3995        }
3996
3997        let sections = all(CONSOLE_HTML, "id=\"page-");
3998        let nav = all(CONSOLE_HTML, "data-page=\"");
3999        for want in ["workflows", "runs", "tools"] {
4000            assert!(sections.contains(want), "the {want} section itself went missing");
4001        }
4002        // Triggers deliberately have no page: a trigger points AT a plan and is
4003        // only legible next to it, so they render in the workflow canvas's own
4004        // lane. The old deep link must still land somewhere real.
4005        assert!(!sections.contains("triggers"), "triggers belong on the canvas, not on a page");
4006        assert!(
4007            CONSOLE_HTML.contains("if (head === 'triggers')"),
4008            "#triggers must still route somewhere — a removed page is not a dead bookmark"
4009        );
4010
4011        // The single array in render() that drives `.hidden`.
4012        let head = "for (const p of [";
4013        let i = CONSOLE_HTML.find(head).expect("render()'s page-visibility loop");
4014        let tail = &CONSOLE_HTML[i + head.len()..];
4015        let j = tail.find(']').expect("unterminated page array");
4016        let listed: BTreeSet<String> = tail[..j]
4017            .split(',')
4018            .map(|t| t.trim().trim_matches('\'').to_string())
4019            .filter(|t| !t.is_empty())
4020            .collect();
4021
4022        assert_eq!(
4023            sections, listed,
4024            "page sections and render()'s visibility list disagree; a section missing \
4025             from the list renders into a pane that never un-hides"
4026        );
4027        let orphan: Vec<_> = nav.difference(&sections).collect();
4028        assert!(orphan.is_empty(), "nav items with no page section: {orphan:?}");
4029    }
4030}
4031
4032/// Issue #124: the console must never disclose the password half of a
4033/// Postgres DSN, at any of its three display surfaces, while the RAW label
4034/// keeps working as the credential map's `memories` comparison key.
4035#[cfg(test)]
4036mod dsn_redaction_tests {
4037    use super::UiServer;
4038    use areev_cal::AreevFacade;
4039    use areev_core::authz::{CredentialMap, AUTHZ_NS, REL_PERMITS};
4040    use areev_core::types::{Fact, Grain};
4041    use areev_store::Areev;
4042
4043    const DSN: &str = "postgresql://u:SUPERSECRET@h:5432/d?sslmode=require";
4044
4045    fn server() -> UiServer {
4046        let dir = tempfile::tempdir().unwrap();
4047        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4048        std::mem::forget(dir);
4049        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
4050        UiServer::new(facade, DSN.to_string())
4051    }
4052
4053    fn text(r: &(&str, &str, Vec<u8>)) -> String {
4054        String::from_utf8_lossy(&r.2).to_string()
4055    }
4056
4057    #[test]
4058    fn stats_config_and_html_redact_the_dsn_password_but_keep_the_host() {
4059        let s = server();
4060        for (method, path) in [("GET", "/api/stats"), ("GET", "/api/config"), ("GET", "/")] {
4061            let r = s.route(method, path, b"", None, None);
4062            let body = text(&r);
4063            assert!(!body.contains("SUPERSECRET"), "{path} leaked the password: {body}");
4064            assert!(body.contains("h:5432"), "{path} redacted more than the password: {body}");
4065        }
4066    }
4067
4068    /// Redaction is DISPLAY ONLY: a credential map's `memories` entry is
4069    /// documented as the raw `--db` value, and it must still resolve —
4070    /// proving the redacted form never leaked into the auth-comparison
4071    /// path (`resolve_for_memory`'s exact string compare).
4072    #[test]
4073    fn credential_map_memories_entry_still_matches_the_raw_dsn() {
4074        std::env::set_var("AREEV_DSN_TOK", "dsn-scoped-secret");
4075        let map_json = format!(
4076            r#"{{"version":1,"tokens":[{{"env":"AREEV_DSN_TOK","principal":"agent:sync","memories":["{DSN}"]}}]}}"#
4077        );
4078        let dir = tempfile::tempdir().unwrap();
4079        let mut store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4080        store
4081            .add(
4082                &Fact::new("agent:sync", REL_PERMITS, "read,write ON caller")
4083                    .namespace(AUTHZ_NS)
4084                    .created_at(1_000),
4085            )
4086            .unwrap();
4087        std::mem::forget(dir);
4088        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
4089        let s = UiServer::new(facade, DSN.to_string())
4090            .with_credentials(CredentialMap::from_json(&map_json).unwrap());
4091
4092        const WRITE: &[u8] = br#"{"query":"ADD fact SET subject = \"x\" SET relation = \"r\" SET object = \"o\" SET namespace = \"caller\" REASON \"t\""}"#;
4093        let r = s.route("POST", "/api/cal", WRITE, Some("dsn-scoped-secret"), None);
4094        let body = text(&r);
4095        assert!(r.0.starts_with("200") && !body.contains("AUT-E001"), "{body}");
4096    }
4097}
4098
4099/// Issue #125: `--allow-origin` lets an exact origin's cross-origin POSTs
4100/// through, without weakening the check for anyone else — no wildcards, no
4101/// suffix/subdomain matching. The Origin header is parsed in
4102/// `handle_request` (not `route`), so these go over a real socket.
4103#[cfg(test)]
4104mod origin_tests {
4105    use super::UiServer;
4106    use areev_cal::AreevFacade;
4107    use areev_store::Areev;
4108    use std::io::{Read, Write};
4109
4110    fn server() -> UiServer {
4111        let dir = tempfile::tempdir().unwrap();
4112        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4113        std::mem::forget(dir);
4114        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
4115        UiServer::new(facade, "origin-test".into())
4116    }
4117
4118    /// POST a read-only CAL query (works token-less on an open console) with
4119    /// an optional `Origin` header, returning the raw HTTP response text.
4120    fn post_with_origin(server: &UiServer, origin: Option<&str>) -> String {
4121        let body: &[u8] = br#"{"query":"RECALL facts WHERE subject = \"acme\""}"#;
4122        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
4123        let addr = listener.local_addr().unwrap();
4124        std::thread::scope(|scope| {
4125            scope.spawn(|| {
4126                if let Ok((s, _)) = listener.accept() {
4127                    let _ = server.handle(s);
4128                }
4129            });
4130            let mut c = std::net::TcpStream::connect(addr).unwrap();
4131            let origin_header = origin.map(|o| format!("Origin: {o}\r\n")).unwrap_or_default();
4132            let req = format!(
4133                "POST /api/cal HTTP/1.1\r\nHost: 127.0.0.1\r\nContent-Type: application/json\r\n{origin_header}Content-Length: {}\r\nConnection: close\r\n\r\n",
4134                body.len()
4135            );
4136            c.write_all(req.as_bytes()).unwrap();
4137            c.write_all(body).unwrap();
4138            let mut out = String::new();
4139            let _ = c.read_to_string(&mut out);
4140            out
4141        })
4142    }
4143
4144    #[test]
4145    fn matching_allowed_origin_is_accepted() {
4146        let s = server().allow_origin("https://console.example.com");
4147        let r = post_with_origin(&s, Some("https://console.example.com"));
4148        assert!(r.contains("HTTP/1.1 200"), "{r}");
4149    }
4150
4151    #[test]
4152    fn non_matching_origin_is_still_rejected() {
4153        let s = server().allow_origin("https://console.example.com");
4154        let r = post_with_origin(&s, Some("https://not-allowed.example.com"));
4155        assert!(r.contains("403"), "{r}");
4156        assert!(r.contains("cross-origin request rejected"), "{r}");
4157    }
4158
4159    #[test]
4160    fn a_subdomain_of_an_allowed_origin_is_rejected_not_matched() {
4161        // The allowlist entry is the console's real origin; a lookalike
4162        // subdomain must not be accepted by suffix matching.
4163        let s = server().allow_origin("https://console.example.com");
4164        let r = post_with_origin(&s, Some("https://evil-console.example.com"));
4165        assert!(r.contains("403"), "{r}");
4166    }
4167
4168    #[test]
4169    fn loopback_origin_still_works_with_no_allowlist_set() {
4170        let s = server();
4171        let r = post_with_origin(&s, Some("http://127.0.0.1:7437"));
4172        assert!(r.contains("HTTP/1.1 200"), "{r}");
4173    }
4174
4175    #[test]
4176    fn no_origin_header_still_works() {
4177        let s = server();
4178        let r = post_with_origin(&s, None);
4179        assert!(r.contains("HTTP/1.1 200"), "{r}");
4180    }
4181
4182    #[test]
4183    fn matching_origin_differing_only_in_case_and_trailing_slash_is_accepted() {
4184        let s = server().allow_origin("https://console.example.com");
4185        let r = post_with_origin(&s, Some("HTTPS://Console.Example.com/"));
4186        assert!(r.contains("HTTP/1.1 200"), "{r}");
4187    }
4188}
4189
4190/// Issue #126: repeated bad credentials from one source IP are COUNTED and
4191/// LOGGED in a stable, greppable shape — but never delayed or locked out
4192/// in-process (`serve` is a strictly serial accept loop; see
4193/// `note_auth_failure`'s doc comment for why a sleep here would itself be a
4194/// denial-of-service lever). A request that never presented a credential at
4195/// all has nothing to fail and is never counted.
4196/// [A3] Native OIDC at the request level: cookies, the `/auth/*` endpoints,
4197/// and the approval rules an IdP-proven identity is subject to.
4198#[cfg(all(test, feature = "oidc"))]
4199mod oidc_route_tests {
4200    use super::{cookie_value, oidc, UiServer};
4201    use areev_cal::AreevFacade;
4202    use areev_core::authz::{AUTHZ_NS, REL_PERMITS};
4203    use areev_core::types::{Fact, Grain};
4204    use areev_store::Areev;
4205
4206    fn cfg() -> oidc::OidcConfig {
4207        oidc::OidcConfig {
4208            issuer: "https://idp.example.com".into(),
4209            client_id: "console".into(),
4210            client_secret: "s3cr3t".into(),
4211            redirect_uri: "https://console.example.com/auth/callback".into(),
4212            scopes: "openid email".into(),
4213            principal_claim: "email".into(),
4214            principal_prefix: None,
4215        }
4216    }
4217
4218    /// A cookie header is attacker-influenced: any script on any same-site
4219    /// origin can set cookies. A prefix or substring match would let a decoy
4220    /// shadow the real session.
4221    #[test]
4222    fn cookie_lookup_is_exact_on_the_name() {
4223        let h = "theme=dark; areev_session=real; other=x";
4224        assert_eq!(cookie_value(h, "areev_session").as_deref(), Some("real"));
4225        // A decoy whose name merely CONTAINS the real one must not match.
4226        let decoy = "areev_session_decoy=evil; areev_session=real";
4227        assert_eq!(cookie_value(decoy, "areev_session").as_deref(), Some("real"));
4228        let only_decoy = "xareev_session=evil; areev_sessionx=evil2";
4229        assert!(cookie_value(only_decoy, "areev_session").is_none());
4230        assert!(cookie_value("", "areev_session").is_none());
4231    }
4232
4233    /// `/auth/login` redirects to the IdP and stores nothing in the browser;
4234    /// `/auth/logout` clears the cookie AND drops the server-side session.
4235    #[test]
4236    fn the_auth_endpoints_redirect_and_manage_the_cookie() {
4237        let dir = tempfile::tempdir().unwrap();
4238        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4239        std::mem::forget(dir);
4240        let facade = AreevFacade::with_session(store, Some("ops".into()), None);
4241        let provider = oidc::OidcProvider::with_test_session(cfg(), "sid-1", "user:pat");
4242        let s = UiServer::new(facade, "oidc-test".into()).with_oidc(provider);
4243
4244        // login → 302 to the IdP, and no Set-Cookie (nothing to remember yet).
4245        let (status, extra, _) = s.oidc_route("GET", "/auth/login", None);
4246        assert!(status.starts_with("302"), "{status}");
4247        assert!(extra.contains("Location: https://idp.example.com/authorize"), "{extra}");
4248        assert!(!extra.contains("Set-Cookie"), "{extra}");
4249        assert!(extra.contains("Cache-Control: no-store"), "a redirect carrying state must not cache: {extra}");
4250
4251        // The session works before logout...
4252        assert!(s.oidc_principal(Some("areev_session=sid-1")).is_some());
4253
4254        // ...and logout clears the browser copy AND invalidates server-side.
4255        let (status, extra, _) =
4256            s.oidc_route("POST", "/auth/logout", Some("areev_session=sid-1"));
4257        assert!(status.starts_with("302"), "{status}");
4258        assert!(extra.contains("Max-Age=0"), "{extra}");
4259        assert!(extra.contains("HttpOnly"), "{extra}");
4260        assert!(extra.contains("SameSite=Strict"), "{extra}");
4261        assert!(
4262            s.oidc_principal(Some("areev_session=sid-1")).is_none(),
4263            "logout must invalidate the session, not just the browser's copy"
4264        );
4265
4266        // A callback with no code surfaces the IdP's own error.
4267        let (status, _, body) =
4268            s.oidc_route("GET", "/auth/callback?error=access_denied", None);
4269        assert!(status.starts_with("400"), "{status}");
4270        assert!(body.contains("access_denied"), "{body}");
4271    }
4272
4273    /// The cookie is `HttpOnly` + `SameSite=Strict`, and `Secure` exactly
4274    /// when the redirect URI is https — a loopback console over plain http
4275    /// must still be able to log in.
4276    #[test]
4277    fn the_session_cookie_is_httponly_strict_and_secure_only_over_https() {
4278        let mk = |redirect: &str| {
4279            let dir = tempfile::tempdir().unwrap();
4280            let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4281            std::mem::forget(dir);
4282            let facade = AreevFacade::with_session(store, Some("ops".into()), None);
4283            let mut c = cfg();
4284            c.redirect_uri = redirect.into();
4285            let p = oidc::OidcProvider::with_test_session(c, "sid", "user:pat");
4286            UiServer::new(facade, "t".into()).with_oidc(p)
4287        };
4288
4289        let https = mk("https://console.example.com/auth/callback");
4290        let (_, extra, _) = https.oidc_route("GET", "/auth/logout", None);
4291        assert!(extra.contains("; Secure"), "https deployment must set Secure: {extra}");
4292
4293        let http = mk("http://127.0.0.1:7437/auth/callback");
4294        let (_, extra, _) = http.oidc_route("GET", "/auth/logout", None);
4295        assert!(!extra.contains("; Secure"), "loopback http must not set Secure: {extra}");
4296        // The other two are unconditional.
4297        assert!(extra.contains("HttpOnly") && extra.contains("SameSite=Strict"), "{extra}");
4298    }
4299
4300    /// The point of the whole feature: an OIDC-proven identity MAY approve,
4301    /// and `--sso-approvals` (which governs the header path) does not touch
4302    /// it. A signature verified against the issuer's key set is a stronger
4303    /// claim than a shared proxy secret, so it gets the stronger right.
4304    #[test]
4305    fn an_oidc_principal_may_approve_regardless_of_sso_approvals() {
4306        let dir = tempfile::tempdir().unwrap();
4307        let path = dir.path().join("t.db");
4308        let mut store = Areev::open(path.to_str().unwrap()).unwrap();
4309        store
4310            .add(
4311                &Fact::new("user:pat", REL_PERMITS, "read,run.respond ON ops")
4312                    .namespace(AUTHZ_NS)
4313                    .created_at(900),
4314            )
4315            .unwrap();
4316        std::mem::forget(dir);
4317        let facade = AreevFacade::with_session(store, Some("ops".into()), None);
4318        let provider = oidc::OidcProvider::with_test_session(cfg(), "sid-1", "user:pat");
4319        let s = UiServer::new(facade, "oidc-test".into())
4320            .with_oidc(provider)
4321            // The A0 default, explicitly: SSO headers may NOT approve.
4322            .with_sso_approvals(false);
4323
4324        let r = s.route_full("GET", "/api/whoami", b"", None, None, None, Some("user:pat"));
4325        let text = String::from_utf8_lossy(&r.2).to_string();
4326        assert!(text.contains(r#""identity_source":"oidc""#), "{text}");
4327        assert!(
4328            text.contains(r#""may_approve":true"#),
4329            "an IdP-proven identity is exactly what --sso-approvals exists to \
4330             substitute for; it must not be caught by that refusal: {text}"
4331        );
4332        assert!(text.contains("user:pat"), "{text}");
4333    }
4334}
4335
4336#[cfg(test)]
4337mod auth_failure_tracking_tests {
4338    use super::UiServer;
4339    use areev_cal::AreevFacade;
4340    use areev_store::Areev;
4341    use std::io::{Read, Write};
4342    use std::net::IpAddr;
4343
4344    const LOCALHOST: IpAddr = IpAddr::V4(std::net::Ipv4Addr::LOCALHOST);
4345
4346    fn server() -> UiServer {
4347        let dir = tempfile::tempdir().unwrap();
4348        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4349        std::mem::forget(dir);
4350        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
4351        UiServer::new(facade, "auth-failure-test".into()).with_auth("right-token".into())
4352    }
4353
4354    /// GET `/` with an optional `Authorization: Bearer` header, over a real
4355    /// socket (the counter hooks `handle_request`, not `route`).
4356    fn get_root(server: &UiServer, bearer: Option<&str>) -> String {
4357        let auth_header = bearer
4358            .map(|t| format!("Authorization: Bearer {t}\r\n"))
4359            .unwrap_or_default();
4360        get_root_with(server, &auth_header)
4361    }
4362
4363    /// `GET /` with arbitrary extra headers, over a real socket (the counter
4364    /// hooks `handle_request`, not `route`).
4365    fn get_root_with(server: &UiServer, extra_headers: &str) -> String {
4366        let listener = std::net::TcpListener::bind("127.0.0.1:0").unwrap();
4367        let addr = listener.local_addr().unwrap();
4368        std::thread::scope(|scope| {
4369            scope.spawn(|| {
4370                if let Ok((s, _)) = listener.accept() {
4371                    let _ = server.handle(s);
4372                }
4373            });
4374            let mut c = std::net::TcpStream::connect(addr).unwrap();
4375            let req = format!("GET / HTTP/1.1\r\nHost: 127.0.0.1\r\n{extra_headers}Connection: close\r\n\r\n");
4376            c.write_all(req.as_bytes()).unwrap();
4377            let mut out = String::new();
4378            let _ = c.read_to_string(&mut out);
4379            out
4380        })
4381    }
4382
4383    #[test]
4384    fn a_bare_request_is_401_but_not_counted() {
4385        let s = server();
4386        // No credential presented at all: 401 (auth_all guards every
4387        // request), but nothing was attempted, so it must not be counted —
4388        // a token-less probe is not an attack attempt.
4389        let bare = get_root(&s, None);
4390        assert!(bare.contains("HTTP/1.1 401"), "{bare}");
4391        assert_eq!(s.auth_failure_count_for_test(LOCALHOST), 0, "a bare request has nothing to fail");
4392    }
4393
4394    #[test]
4395    fn a_wrong_token_is_401_and_is_counted() {
4396        let s = server();
4397        let wrong = get_root(&s, Some("guess"));
4398        assert!(wrong.contains("HTTP/1.1 401"), "{wrong}");
4399        assert_eq!(s.auth_failure_count_for_test(LOCALHOST), 1);
4400
4401        let wrong_again = get_root(&s, Some("guess"));
4402        assert!(wrong_again.contains("HTTP/1.1 401"), "{wrong_again}");
4403        assert_eq!(s.auth_failure_count_for_test(LOCALHOST), 2, "consecutive failures accumulate");
4404    }
4405
4406    #[test]
4407    fn a_right_token_after_a_streak_succeeds_immediately_and_resets_it() {
4408        let s = server();
4409        for _ in 0..5 {
4410            let r = get_root(&s, Some("guess"));
4411            assert!(r.contains("HTTP/1.1 401"), "{r}");
4412        }
4413        assert_eq!(s.auth_failure_count_for_test(LOCALHOST), 5);
4414
4415        // The right token succeeds on the very next request — there is no
4416        // in-process DELAY at any streak length, and below
4417        // MAX_CONSECUTIVE_AUTH_FAILURES no rejection either, so an operator
4418        // who fat-fingers a pasted token is never locked out.
4419        let ok = get_root(&s, Some("right-token"));
4420        assert!(ok.contains("HTTP/1.1 200"), "{ok}");
4421        assert_eq!(s.auth_failure_count_for_test(LOCALHOST), 0, "success resets the streak");
4422
4423        // The next wrong guess starts a fresh streak at 1, not 6.
4424        let r = get_root(&s, Some("guess"));
4425        assert!(r.contains("HTTP/1.1 401"), "{r}");
4426        assert_eq!(s.auth_failure_count_for_test(LOCALHOST), 1);
4427    }
4428
4429    /// [A1] Behind a trusted proxy every request shares ONE source address,
4430    /// so a per-IP lockout would let one attacker's bad guesses refuse every
4431    /// user behind it — a self-inflicted outage in the deployment we
4432    /// recommend. A proxy-proven request is exempt; throttling there belongs
4433    /// to the proxy, like TLS and the IdP handshake.
4434    #[test]
4435    fn a_proxy_proven_request_is_never_locked_out() {
4436        let dir = tempfile::tempdir().unwrap();
4437        let store = Areev::open(dir.path().join("t.db").to_str().unwrap()).unwrap();
4438        std::mem::forget(dir);
4439        let facade = AreevFacade::with_session(store, Some("caller".into()), None);
4440        let s = UiServer::new(facade, "proxy-test".into())
4441            .with_auth("right-token".into())
4442            .with_sso_rotating("x-forwarded-user", "proxy-secret", None::<String>);
4443
4444        // Burn well past the threshold with wrong bearer tokens, but all
4445        // arriving through the proxy (valid proxy secret on every request).
4446        for _ in 0..(super::MAX_CONSECUTIVE_AUTH_FAILURES + 5) {
4447            let _ = get_root_with(
4448                &s,
4449                "Authorization: Bearer guess\r\nX-Areev-Proxy-Secret: proxy-secret\r\n",
4450            );
4451        }
4452        // A legitimate user behind the same proxy is still served.
4453        let ok = get_root_with(
4454            &s,
4455            "Authorization: Bearer right-token\r\nX-Areev-Proxy-Secret: proxy-secret\r\n",
4456        );
4457        assert!(ok.contains("HTTP/1.1 200"), "proxy-borne traffic must not be locked out: {ok}");
4458    }
4459
4460    /// [A1] Past the threshold the console stops answering credential-bearing
4461    /// requests from that source — refusing, never sleeping, because a serial
4462    /// accept loop makes a sleep a denial-of-service lever for everyone else.
4463    #[test]
4464    fn a_long_streak_is_refused_with_429_until_it_goes_idle() {
4465        let s = server();
4466        for i in 0..super::MAX_CONSECUTIVE_AUTH_FAILURES {
4467            let r = get_root(&s, Some("guess"));
4468            assert!(r.contains("HTTP/1.1 401"), "attempt {i} should still be a 401: {r}");
4469        }
4470
4471        // The next credential-bearing request is refused before routing.
4472        let blocked = get_root(&s, Some("guess"));
4473        assert!(blocked.contains("HTTP/1.1 429"), "{blocked}");
4474        assert!(blocked.contains("Retry-After:"), "a 429 must say when: {blocked}");
4475
4476        // The brake is on the SOURCE, not on the credential: even the right
4477        // token is refused while the streak stands. That is the intended
4478        // trade — an attacker who has burned an IP cannot then race a guess
4479        // through — and it is why the streak expires on its own.
4480        let right = get_root(&s, Some("right-token"));
4481        assert!(right.contains("HTTP/1.1 429"), "{right}");
4482
4483        // A credential-LESS request is untouched: it was never counted, so it
4484        // must not be blocked either. The console page still serves its 401
4485        // challenge, which is what lets a browser prompt for a fresh login.
4486        let bare = get_root(&s, None);
4487        assert!(bare.contains("HTTP/1.1 401"), "{bare}");
4488    }
4489}