Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
de03fe5
feat(server,app): console tabs a thin binary adds
LeeroyHannigan Oct 25, 2026
cffa41a
docs: describe console tabs
LeeroyHannigan Oct 25, 2026
786940b
Merge feat/thin-bin-extensions into feat/console-tabs
LeeroyHannigan Nov 17, 2026
750c38f
fix(console): run the catalog session check on console tabs
LeeroyHannigan Nov 17, 2026
df8eef0
Merge feat/thin-bin-extensions into feat/console-tabs
LeeroyHannigan Nov 17, 2026
0f4556d
docs(console): say how the built-in admin pages refuse an IAM-user se…
LeeroyHannigan Nov 17, 2026
c27e1d8
refactor(console): take the tab label and admin flag from one identit…
LeeroyHannigan Nov 17, 2026
4943055
test(console): run the tab session check for each session state
LeeroyHannigan Nov 25, 2026
41a6c51
fix(console): build each page's navigation bar from its session
LeeroyHannigan Nov 25, 2026
4068b65
fix(console): remove Debug from ConsoleIdentity
LeeroyHannigan Nov 25, 2026
53ab8a3
fix(console): refuse a tab request that may change state without the …
LeeroyHannigan Nov 25, 2026
c7b7e20
fix(server): keep axum's default console fallback when no tab is mounted
LeeroyHannigan Nov 25, 2026
1a75627
docs(server): say how a tab slug and a built-in page answer without a…
LeeroyHannigan Nov 25, 2026
93275b3
fix(server): reserve the console router's first segments and refuse a…
LeeroyHannigan Nov 25, 2026
d7fe18e
test(console): check that a tab POST with the token in a JSON body ge…
LeeroyHannigan Nov 25, 2026
9237e0f
test(console): check a PROPFIND without the token, and state which me…
LeeroyHannigan Nov 25, 2026
62014f8
Merge feat/thin-bin-extensions into feat/console-tabs
LeeroyHannigan Nov 25, 2026
9cabee9
test(server): pass the storage argument to build_state_with in the st…
LeeroyHannigan Nov 25, 2026
592fbad
fix(console): name the four methods that reach a tab without the token
LeeroyHannigan Nov 25, 2026
ca128ba
Merge feat/thin-bin-extensions into feat/console-tabs
LeeroyHannigan Nov 25, 2026
9f5749d
test(console): check that HEAD, OPTIONS and TRACE reach a tab without…
LeeroyHannigan Nov 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions crates/app/src/cmd_serve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ pub fn run(args: &ServeArgs, build: BuildInfo, extensions: Extensions) -> anyhow
// the backend connects or the daemon forks, puts the message on stderr.
extenddb_server::validate_extension_prefixes(&extensions.http)
.map_err(|e| anyhow::anyhow!("{e}"))?;
extenddb_server::validate_console_slugs(&extensions.console_tabs)
.map_err(|e| anyhow::anyhow!("{e}"))?;
// Load config early so bind address is known before fork. A dev-mode
// build serves with built-in defaults when no config file exists
// (zero-config local/CI use: loopback, in-memory storage, seeded dev
Expand Down
14 changes: 12 additions & 2 deletions crates/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
//! build,
//! extenddb_app::Extensions::default()
//! .with_router("/_mybackend/status", status_router())
//! .with_request_observer(my_observer),
//! .with_request_observer(my_observer)
//! .with_console_tab("My backend", "mybackend", page_router()),
//! )
//! ```

Expand Down Expand Up @@ -67,7 +68,16 @@ pub use extenddb_server::BuildInfo;
/// A thin binary's extension points, re-exported from `extenddb-server` so a
/// thin `main` needs only the `extenddb-app` dependency to use them. Pass an
/// [`Extensions`] to [`run_with`]; [`run`] uses the empty default.
pub use extenddb_server::{Extensions, RequestObserver, RequestRecord};
pub use extenddb_server::{
ConsoleIdentity, ConsoleTab, Extensions, RequestObserver, RequestRecord,
};

/// The console's HTML helpers (`nav_bar`, `layout_with_version_csrf`, `escape`),
/// so a console tab's page renders the console's layout from its
/// [`ConsoleIdentity`] with the one `extenddb-app` dependency.
pub mod console_html {
pub use extenddb_server::console::html::*;
}

/// The `axum` the server is built with, so a thin binary builds its
/// [`Extensions::http`] routers against the same `Router` type without naming
Expand Down
11 changes: 8 additions & 3 deletions crates/server/src/console/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,14 +182,19 @@ const ADMIN_LINKS: &str = concat!(
/// Build the navigation bar for an authenticated session. The Accounts,
/// Metrics, Settings and Cache links are left out for IAM users, because
/// those pages are admin only. An IAM-user session reaches its own user page
/// from the console dashboard.
pub fn nav_bar(identity: &NavIdentity) -> String {
/// from the console dashboard. `tabs` are the extension links shown after
/// the built-in ones, as `(label, href)`.
pub fn nav_bar(identity: &NavIdentity, tabs: &[(String, String)]) -> String {
let admin_links = if identity.is_admin { ADMIN_LINKS } else { "" };
let mut extra = String::new();
for (label, href) in tabs {
let _ = writeln!(extra, r#"<a href="{}">{}</a>"#, escape(href), escape(label));
}
format!(
r#"<nav>
<span class="brand">extenddb Console</span>
<a href="/console">Dashboard</a>{admin_links}
<span class="spacer"></span>
{extra}<span class="spacer"></span>
<span>{identity}</span>
<form class="inline" method="post" action="/console/logout">
<button class="btn btn-sm" type="submit">Logout</button>
Expand Down
109 changes: 108 additions & 1 deletion crates/server/src/console/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,30 @@
//! rendering.

pub mod docs_embed;
mod html;
pub mod html;
mod pages;
pub mod session;
#[cfg(test)]
mod tab_gate_tests;
#[cfg(test)]
mod test_catalog;

use std::sync::Arc;

use axum::Router;
use axum::http::Method;
use axum::routing::{get, post};

use session::SessionStore;

/// Shared state for console handlers.
pub struct ConsoleState {
pub sessions: SessionStore,
/// Links the navigation bar shows after the built-in ones: `(label,
/// href)`, one per extension tab (see
/// [`ConsoleTab`](crate::extensions::ConsoleTab)). Shared by pointer with
/// every [`ConsoleIdentity`] the session check builds.
pub tabs: Arc<[(String, String)]>,
/// Version string displayed in the console footer (e.g. "0.1.0 · catalog 2.1.0 · abc1234").
pub version_info: Arc<str>,
/// The URL the server is listening on (e.g. "<https://127.0.0.1:18443>").
Expand Down Expand Up @@ -60,6 +70,103 @@ pub struct ConsoleState {
pub secure_cookies: bool,
}

/// The console user behind a request to an extension tab, and what the page
/// needs to render the console's layout the way the built-in pages do.
/// Inserted as a request extension by [`require_console_session`]. `label` is
/// what the navigation bar shows for the user; `tabs` and `version_info` are
/// the arguments the built-in pages pass to [`html::nav_bar`] and
/// [`html::layout_with_version_csrf`]. Every console user reaches a tab, an
/// admin or an IAM user with a console password; `is_admin` tells them apart.
/// A request that may change state reaches the tab only with the session's
/// CSRF token in its `_csrf` form field (see [`require_console_session`]).
/// Read by field; the struct may gain fields in a minor release, so build one
/// for a test with [`ConsoleIdentity::new`]. It has no `Debug`, so that no log
/// line prints the token.
#[derive(Clone)]
#[non_exhaustive]
pub struct ConsoleIdentity {
/// What the navigation bar shows for the user (`Admin: name` or
/// `account/user`).
pub label: String,
/// The session's CSRF token, which the layout writes into the page's
/// `csrf-token` meta tag. A script in the layout adds it as the `_csrf`
/// field to each POST form on the page when the page loads.
pub csrf_token: String,
/// Whether the session is an admin login (full management access) rather
/// than an IAM user's self-service session. The session check admits
/// both. A page meant for admins refuses an IAM-user session as the
/// built-in admin pages do: `/console/settings`, `/console/cache` and
/// `/console/metrics` redirect it to `/console`, and
/// `/console/metrics-data` answers 403.
pub is_admin: bool,
/// Every extension tab as `(label, href)`, for the navigation bar.
pub tabs: Arc<[(String, String)]>,
/// The version line the footer shows.
pub version_info: Arc<str>,
}

impl ConsoleIdentity {
/// An identity for a test of a tab page. The server builds the real one.
#[must_use]
pub fn new(
label: impl Into<String>,
csrf_token: impl Into<String>,
is_admin: bool,
tabs: Arc<[(String, String)]>,
version_info: Arc<str>,
) -> Self {
Self {
label: label.into(),
csrf_token: csrf_token.into(),
is_admin,
tabs,
version_info,
}
}
}

/// Middleware for extension tabs: the session check of the built-in pages. A
/// request without a live console session is redirected to the login page,
/// and one whose session the catalog cannot check gets 503. A request that
/// may change state (any method but GET, HEAD, OPTIONS and TRACE) gets 403
/// unless its body is a form whose `_csrf` field is the session's CSRF token,
/// the check a built-in page makes on its own forms. A request that passes
/// carries a [`ConsoleIdentity`] extension for the page to render the
/// navigation bar.
pub async fn require_console_session(
axum::extract::State(state): axum::extract::State<Arc<ConsoleState>>,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optional. CSRF is checked by each tab's handler, and a POST with a valid cookie and no _csrf reaches it. The cookie is SameSite=Strict, which stops cross-site requests and not same-site ones from another port on the host. The PR's Why section says the same CSRF protection applies, and the manual (12-extending-extenddb-storage.md:367) says the layout adds _csrf to every form, where a script adds it to POST forms.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the check into the tab mount. A check in each handler lets a tab that forgets it accept a same-site POST. In 53ab8a3, require_console_session reads the body of each request whose method is not GET, HEAD, OPTIONS or TRACE. It answers 403 "CSRF token mismatch" unless the _csrf form field is the session's token. It reads the field with axum's Form, as the built-in pages do, and calls their require_csrf. The tab's router gets the whole body. A body over axum's 2 MB default gets 413 before the router. A body that is not an application/x-www-form-urlencoded form, such as JSON or multipart, gets 403.

The table has rows for no token, another token, a DELETE and the token in a JSON body (d7fe18e), each 403 before the page runs. 9237e0f adds a PROPFIND row, which fails when the check guards only POST, PUT, PATCH and DELETE. 53ab8a3 also deletes csrf_matches. The Why section and the manual now describe the check and the script that adds _csrf to POST forms. The manual also says that GET, HEAD, OPTIONS and TRACE reach the tab without the token, so a tab changes state only on another method.

req: axum::extract::Request,
next: axum::middleware::Next,
) -> axum::response::Response {
let session = match pages::require_session(req.headers(), &state).await {
Ok(session) => session,
Err(response) => return response,
};
// The four methods are named here because `Method::is_safe` follows the
// `http` crate's list, and http 1.5.0 adds QUERY to it.
let read_only = matches!(
*req.method(),
Method::GET | Method::HEAD | Method::OPTIONS | Method::TRACE
);
let mut req = if read_only {
req
} else {
match pages::require_form_csrf(req, &session).await {
Ok(req) => req,
Err(response) => return response,
}
};
let nav = pages::identity_label(&session.identity);
req.extensions_mut().insert(ConsoleIdentity {
label: nav.label,
csrf_token: session.csrf_token,
is_admin: nav.is_admin,
tabs: session.tabs,
version_info: Arc::clone(&state.version_info),
});
next.run(req).await
}

/// Build the console router.
pub fn router() -> Router<Arc<ConsoleState>> {
Router::new()
Expand Down
6 changes: 3 additions & 3 deletions crates/server/src/console/pages/access_key_pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::console::html;

use super::account_scope::require_user_session;
use super::user_pages::user_breadcrumb;
use super::{CsrfOnly, identity_label, op_error_message, require_csrf};
use super::{CsrfOnly, op_error_message, require_csrf};

/// POST /console/accounts/{id}/users/{name}/access-keys/new
pub async fn create_access_key(
Expand All @@ -33,7 +33,7 @@ pub async fn create_access_key(
return r;
}

let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let eid = html::escape(&account_id);
let eun = html::escape(&user_name);

Expand Down Expand Up @@ -125,7 +125,7 @@ pub async fn delete_access_key(
.into_response()
}
Err(e) => {
let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let content = format!("<h1>Error</h1>{}", html::alert_error(&op_error_message(e)));
Html(html::layout_csrf(
"Error",
Expand Down
19 changes: 8 additions & 11 deletions crates/server/src/console/pages/account_pages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@ use crate::console::html;
use crate::management::CallerIdentity;

use super::account_scope::require_account_session;
use super::{
CsrfOnly, SessionData, identity_label, is_admin, op_error_message, require_csrf,
require_session,
};
use super::{CsrfOnly, SessionData, is_admin, op_error_message, require_csrf, require_session};

/// GET /console/: the dashboard with quick links. An admin session also sees
/// the deployment's account and admin counts.
Expand Down Expand Up @@ -54,7 +51,7 @@ fn render_dashboard(
counts: Option<(i64, i64)>,
version_info: &str,
) -> Response {
let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();

let cards = match &session.identity {
CallerIdentity::Admin(_) => {
Expand Down Expand Up @@ -116,7 +113,7 @@ pub async fn list_accounts(State(state): State<Arc<ConsoleState>>, headers: Head
return Redirect::to("/console").into_response();
}

let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let crumbs = html::breadcrumb(&[("Dashboard", Some("/console")), ("Accounts", None)]);

let rows: Vec<(String, String)> = state
Expand Down Expand Up @@ -167,7 +164,7 @@ pub async fn new_account_form(
return Redirect::to("/console").into_response();
}

let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let crumbs = html::breadcrumb(&[
("Dashboard", Some("/console")),
("Accounts", Some("/console/accounts")),
Expand Down Expand Up @@ -235,7 +232,7 @@ pub async fn create_account(

// Validate account ID format (must be 12-digit numeric string).
if account_id.len() != 12 || !account_id.chars().all(|c| c.is_ascii_digit()) {
let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let content = format!(
"{}<h1>Create Account</h1>{}",
html::breadcrumb(&[
Expand All @@ -262,7 +259,7 @@ pub async fn create_account(
match result {
Ok(()) => Redirect::to(&format!("/console/accounts/{account_id}")).into_response(),
Err(e) => {
let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let msg = op_error_message(e);
let content = format!(
"{}<h1>Create Account</h1>{}",
Expand Down Expand Up @@ -300,7 +297,7 @@ pub async fn account_detail(
return Redirect::to("/console").into_response();
}

let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let eid = html::escape(&account_id);
let crumbs = html::breadcrumb(&[
("Dashboard", Some("/console")),
Expand Down Expand Up @@ -434,7 +431,7 @@ pub async fn delete_account(
Redirect::to("/console/accounts").into_response()
}
Err(e) => {
let nav = html::nav_bar(&identity_label(&session.identity));
let nav = session.nav_bar();
let msg = op_error_message(e);
let content = format!("<h1>Delete Account</h1>{}", html::alert_error(&msg));
Html(html::layout_csrf(
Expand Down
1 change: 1 addition & 0 deletions crates/server/src/console/pages/account_pages_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ fn session(identity: CallerIdentity) -> SessionData {
SessionData {
identity,
csrf_token: "csrf-token".to_owned(),
tabs: std::sync::Arc::from(Vec::new()),
}
}

Expand Down
Loading
Loading