Skip to content

feat(server/app): console tabs a thin binary adds - #557

Open
LeeroyHannigan wants to merge 21 commits into
feat/thin-bin-extensionsfrom
feat/console-tabs
Open

LeeroyHannigan wants to merge 21 commits into
feat/thin-bin-extensionsfrom
feat/console-tabs

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 25, 2026 •

Copy link
Copy Markdown
Collaborator

What

A thin binary could mount HTTP routes and observe requests (#549) but had no way to add a page to the web console. Extensions::with_console_tab(label, slug, router) adds one. The router is nested at /console/<slug> behind the console's session check. The label becomes a link in the navigation bar after the built-in ones, on every console page.

The check is console::require_console_session, a middleware on the tab's router. It is the session check of the built-in pages, which reads the catalog on every request. A request without a live session is redirected to /console/login before it reaches the router. So is a request whose principal no longer exists or whose password changed after login. A request that the catalog cannot check gets 503. A request with a session carries a ConsoleIdentity request extension. It holds the user's label, the session's CSRF token, the tab links and the version line. It also says whether the session is an admin login rather than an IAM user's. The built-in pages pass these values to html::nav_bar and html::layout_with_version_csrf. The bar takes the label and is_admin as an html::NavIdentity, with the tab links. The layout takes the CSRF token and the version line. A tab page makes the same two calls, so it renders inside the console's layout with the same bar and footer. ConsoleIdentity and the console's HTML helpers are re-exported from extenddb_app (console_html), so a tab needs that one dependency. Every path under the slug meets the check. The server installs a plain 404 fallback on the tab router inside the middleware, in place of any fallback the extension set. When a tab is mounted, the console's own fallback is explicit, so axum keeps the nested ones. With no tab, the console keeps axum's default fallback, as on main. /console/<slug>/ redirects to /console/<slug> behind the same check. Without a session, a mounted slug answers 303 to the login page and an unmounted slug answers 404. A built-in page such as /console/accounts and an unknown console path answer the same way.

One check stays with the tab author, as it does for the built-in pages. The check admits every console user, an admin or an IAM user with a console password, and identity.is_admin tells them apart. 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. The session check guards each request that may change state, which is any method but GET, HEAD, OPTIONS and TRACE. Its body must be an application/x-www-form-urlencoded form whose _csrf field is the session's token. Otherwise the check answers 403 before the router runs. The check reads the field with axum's Form and compares it with require_csrf, the constant-time check of the built-in pages. The router then gets the whole body. A body over axum's 2 MB default gets 413 before the router runs. A script in the layout adds the _csrf field to each POST form on the page when the page loads.

A slug is one segment of [a-z0-9-], not one of the console router's first segments (login, logout, docs, metrics, metrics-data, settings, cache, accounts), and not another tab's. A test reads the router's routes and fails when this list and their first segments differ. A label is not empty or only white space. A reserved slug, a repeated slug and an empty label each report as themselves. The check runs in cmd_serve before the backend connects or the daemon forks, and again in start_server, whether or not the console is mounted. ConsoleTab and ConsoleIdentity are #[non_exhaustive], and ConsoleIdentity::new builds one for a thin binary's own tests. ConsoleIdentity has no Debug, so a log line cannot print the CSRF token.

Why

A backend author who ships a status or control page for their backend wants it inside the console the operator already logs in to. There it has the same session, the same layout and the same CSRF protection, with no second port and no second login. The session check and the identity give the page everything the built-in pages have. The check also refuses a request that may change state without the CSRF token. The author writes the page and checks is_admin on a page meant for admins.

Stacking

This branch is built on #549, which stacks on #547, #536, #532 and #529. The change under review is the two commits this branch had on top of #549: the code and tests, and the documentation. After them come the merges of #549's current head, the follow-ups below and the changes since the 24 November review. Once #549 is on main this PR rebases onto main.

Merge of #549's head

786940be3 merges #549 at 6f5380c84, which carries #547, #536 and main at 95425f5e8. Seven files conflicted. main added html::NavIdentity, which leaves the admin links out of an IAM user's bar (#727). nav_bar now takes that identity and the tab links, and it lists the tabs after the built-in links for every console user. main's session check reads the catalog on every request (#704), and main no longer has the in-memory lookup that the tab middleware used. The other files keep both sides.

The follow-up commits:

  • 750c38f79 makes the tab middleware call main's session check, pages::require_session. It also gives the tab links to the navigation bars that main added.
  • df8eef026 merges feat(server/app): thin-bin Extensions: HTTP routers and a request observer #549's head again, for its test and comment changes. No file conflicted.
  • 0f4556d33 corrects the ConsoleIdentity::is_admin comment and the storage extension manual: on main, the built-in admin pages redirect an IAM-user session to /console, and /console/metrics-data answers 403.
  • c27e1d8ed takes the label and the admin flag from one identity_label call, and makes is_admin private to the pages module again.

Changes since the 24 November review

  • 494305509 adds a data table test of the tab session check. It moves the catalog store double of the account scope tests to console/test_catalog.rs, so both tests use one double.
  • 41a6c51fd builds the navigation bar of every built-in page with SessionData::nav_bar, so no page passes a tab list of its own.
  • 4068b6555 gives ConsoleIdentity no Debug.
  • 53ab8a349 makes the tab session check refuse a request that may change state without the CSRF token, and drops ConsoleIdentity::csrf_matches.
  • c7b7e20ca makes the console fallback explicit only when a tab is mounted.
  • 1a7562769 corrects the comment on how a slug answers without a session, and tests the built-in pages' answers.
  • 93275b337 cuts the reserved slug list to the console router's first segments, with a test that ties the two. It also refuses an empty tab label at startup.
  • d7fe18e34 adds a row for a tab POST with the token in a JSON body.
  • 9237e0f adds a PROPFIND row to that table. It fails when the check guards only POST, PUT, PATCH and DELETE. The storage extension manual now says that GET, HEAD, OPTIONS and TRACE reach a tab without the token. It also says that a body over 2 MB gets 413. The security model manual names the tab's CSRF check.
  • 62014f884 merges feat(server/app): thin-bin Extensions: HTTP routers and a request observer #549 at 1f8fa4256, which carries feat(server): a request scope carries the request id to the engine's fault log lines #547, feat(engine): serve a batch partially when a backend throttles some entries #536, feat(core): throttle errors carry the service's ThrottlingReasons #532, feat(storage): a StorageError::Service variant the engine passes through #529 and main at a417b2f82, so this branch's minimum Rust version is now 1.91.1. One file conflicted, crates/server/src/console/pages/account_scope_reads_tests.rs. It keeps this PR's side, and main's CountingStore edits go into console/test_catalog.rs.
  • 9cabee9f4 passes the storage argument to build_state_with in this PR's start_server refusal test.
  • At 9cabee9f4, fmt, cargo check --workspace --all-targets and clippy with -D warnings pass. The lib tests of app (105), engine (904) and server (568) pass, apart from the five known backup_store host failures. batch_partial, the WebAssembly adapter tests and cargo +1.91.1 check pass too.
  • 592fbad6a names GET, HEAD, OPTIONS and TRACE in the tab session check. Before, the check called Method::is_safe, whose list comes from the http crate, and http 1.5.0 adds QUERY to it. A new row sends a QUERY without the token and expects 403. With http 1.5.0 and is_safe, the row gets 200. The start_server comment now says that a tab path without a session answers 303 as a GET of a built-in page does.
  • ca128ba88 merges feat(server/app): thin-bin Extensions: HTTP routers and a request observer #549 at a24cfe9c4, which carries feat(server): a request scope carries the request id to the engine's fault log lines #547's test rename. No file conflicted.
  • 9f5749dca adds rows that send HEAD, OPTIONS and TRACE without the token and expect 200. A check that leaves out any one of the four methods fails a row.
  • At 592fbad6a, fmt, clippy with -D warnings on the server and cargo +1.91.1 check -p extenddb-server --all-targets pass. The server lib tests (568) pass, apart from the five known backup_store host failures. At 9f5749dca, the tab session table passes.

Other open PRs:

Testing done

Three unit tests cover the slug rule. They check the accepted forms, eight malformed forms and the eight reserved segments, each refused as reserved. They also check a repeated slug, an empty and a blank label, the list check and the builder. The third test reads the console router's routes and checks that the reserved list is their first segments.

One unit test runs require_console_session from a data table, with a catalog double. Its rows cover an admin, an IAM user, no cookie, a deleted principal, a changed password and an unreadable catalog. Other rows send a POST with the session's token, without a token, with another token, or with the token in a JSON body. Rows without a token send a DELETE, a PROPFIND and a QUERY, which get 403, and a HEAD, an OPTIONS and a TRACE, which get 200. Each row checks the status, the location, the body and the calls to the page and to the catalog.

Three tests run through start_server on a loopback socket. One mounts a tab whose label carries <, & and ", and checks these answers:

  • Without a session, the page, a non-route under it and the slash form redirect to the login page, as /console/accounts does. /console/nothere is 404.
  • After logging in as an admin seeded on the fixture's catalog, the page renders inside the console layout. It shows the admin's label, is_admin true, the escaped tab link and the logged-in pages' footer, and it carries the security headers.
  • The slash form redirects to the page, a non-route under the tab is 404, and the console home lists the escaped link.
  • A POST carrying the page's CSRF token reaches the route with its form. A POST without the token is 403 before the route, and one without a session is 303.

Without tabs, GET and HEAD /console/<slug> answer as /nope does, headers included, and the login page names no tab. start_server refuses a reserved slug and a reserved extension prefix before serving, each with its error text.

Negative controls at c27e1d8, each on a clean committed tree and restored after. Each one fails the test written for it.

  • the session gate deleted from the tab router, and the slash router unguarded
  • the tab links not passed to the bar
  • the reserved list emptied, and a reserved slug reported as repeated
  • the start_server slug check deleted
  • the console's explicit fallback deleted
  • csrf_matches made to return true
  • the label written unescaped

Negative controls at d7fe18e, each on a clean committed tree and restored after. Each one fails the test written for it.

  • is_admin set to true in the session check
  • the 503 turned into a login redirect
  • an empty tab list in SessionData::nav_bar
  • the console fallback made explicit with no tab
  • the reserved list without cache, and the old list of 13
  • the CSRF check skipped, and the session's token as the answer to a body that does not parse
  • no label check, and a label check that does not trim
cargo +1.97.0 fmt --all -- --check
cargo +1.97.0 clippy --all-targets -- -D warnings
cargo +1.97.0 test -p extenddb-server -p extenddb-app --lib   # 558 + 93 passed at d7fe18e34

At d7fe18e34, 5 more server tests fail on the build host only: the backup_store tests, because another user owns a directory above the spool directory.

A dev binary built at c27e1d8 answered GET and HEAD /console/nothere over HTTP/2, and HEAD over HTTP/1.1, with content-length: 0, where /nope has none. Built at d7fe18e and at 95425f5 of main, it gives the two paths the same eight answers.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace --lib; the integration suites run in CI)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy --all-targets -- -D warnings)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: no trait or auth model change; the console's session check is applied to the new routes as it is to the built-in ones. RFC-0002 names ConsoleTab and ConsoleIdentity under the field-addition policy. The architecture guide's Thin binary extensions subsection and the storage extension manual's Step 6 paragraph describe the tab, the identity and the layout call. They also describe the slug and label rules, the CSRF check and the is_admin check that the author makes.

Breaking changes

None for the wire or the console's routes. A binary with no tab answers an unmatched console path with axum's default 404, as main does. console::html::nav_bar gains a tabs parameter after main's &NavIdentity, and ConsoleState gains a tabs field. Before this PR console::html was private. The PR makes it public, with ConsoleIdentity and require_console_session, for the tab pages of a thin binary. start_server gains a parameter, and its one production caller is serve.rs.


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

Extensions gains console_tabs, built through with_console_tab(label, slug,
router): a page nested at /console/<slug> behind the console's session check
and a link in the navigation bar after the built-in ones. ConsoleTab is
re-exported from extenddb_app and is non_exhaustive.

The check is console::require_console_session, a middleware on the tab's
router: a request without a live session is redirected to /console/login
before it reaches the router; one with a session carries a ConsoleIdentity
request extension (the user's label, the CSRF token, whether the session is
an admin rather than an IAM user's, the tab links and the version line), which
is what the built-in pages pass to html::nav_bar and
html::layout_with_version_csrf, so the page renders inside the console's
layout. The check admits every console user; ConsoleIdentity::is_admin tells
an admin from an IAM user, and ConsoleIdentity::csrf_matches is the
constant-time check a tab's POST handler makes on the _csrf form field, the
one the built-in pages make. ConsoleIdentity and the console's HTML helpers
are re-exported from extenddb_app (console_html), so a tab needs that one
dependency. ConsoleIdentity is non_exhaustive with a constructor for a thin
binary's tests. A reserved or repeated slug reports as itself (ReservedSlug,
RepeatedSlug), not as an extension prefix collision. Every path under the slug meets
the check: the server installs a plain 404 fallback on the tab router inside
the middleware, replacing any fallback the extension set, and the console's
own fallback is made explicit so axum keeps the nested ones. /console/<slug>/
redirects to /console/<slug> behind the same check, so a client without a
session cannot tell a mounted slug from an unmounted one.

ConsoleState carries the tab links; html::nav_bar takes them and writes one
escaped link per tab; the 38 built-in call sites pass state.tabs. The console
html module is pub for the tab pages.

A slug is one segment of [a-z0-9-], not one of the console's own first
segments (login, logout, docs, metrics, metrics-data, settings, cache,
accounts, users, groups, roles, policies, access-keys), and not another tab's.
validate_console_slugs runs in cmd_serve before the backend connects or the
daemon forks, and again in start_server, whether or not the console is
mounted.

Tests: two unit tests on the slug rule (accepted forms, eight bad forms, the
thirteen reserved segments with their own error, a repeat with its own, the
list check, the builder). Three through start_server on a loopback socket:
with a tab mounted whose label carries <, & and ", the page, a non-route under
it and the slash form redirect to the login page without a session; after
logging in as an admin seeded on the fixture's catalog, the page renders
inside the console layout with the admin's label, is_admin true, the escaped
tab link and the logged-in pages' footer, carries the security headers, the
slash form redirects to the page, a non-route under the tab is 404, the
console home lists the escaped link, a POST with the page's CSRF token acts,
one without it is 403 and one without a session is 303; without tabs,
/console/<slug> is a plain 404 and the login page names no tab; start_server
refuses a reserved slug and a reserved extension prefix before serving, each
with its error text.
Architecture guide and storage extension manual: what a console tab is, the session check and the ConsoleIdentity it inserts, how a page renders the console layout, the slug rule and the reserved segments. RFC-0002: ConsoleTab is built through the builder; ConsoleIdentity is read by field.
crates/server/src/console/html.rs keeps NavIdentity and the admin-only links of feat/thin-bin-extensions. nav_bar takes that identity and the tabs parameter of this branch, and lists the tabs after the built-in links for every console user.

crates/server/src/console/pages/account_pages.rs keeps the admin-only counts and render_dashboard of feat/thin-bin-extensions. render_dashboard takes the tabs for its nav bar, as the other pages of this branch do.

crates/server/src/console/pages/mod.rs keeps the catalog session check of feat/thin-bin-extensions and its identity_label, which returns NavIdentity, made pub(super) as this branch needs. session_for_headers goes: it read SessionStore::get, which feat/thin-bin-extensions removed.

crates/server/src/console/pages/user_pages.rs keeps render_user_page of feat/thin-bin-extensions and gives it the tabs for its nav bar, as this branch does on every page.

crates/server/src/lib.rs keeps the shutdown paragraph of feat/thin-bin-extensions and the console tab paragraph of this branch in the start_server comment.

docs/manuals/01-architecture-guide.md and docs/manuals/12-extending-extenddb-storage.md keep the text on route-wide layers from feat/thin-bin-extensions and the console tab text of this branch. The manual's nav_bar call passes a NavIdentity built from the ConsoleIdentity.
feat/thin-bin-extensions brings two console changes from main. The session check reads the catalog on every request. A session goes to the login page when its principal is gone or has a new password, and an unreadable catalog answers 503. The navigation bar takes a NavIdentity and leaves the admin-only links out for IAM users.

require_console_session now calls require_session, the check of the built-in pages, in place of session_for_headers, which the merge took out. It takes the label from identity_label, which returns a NavIdentity now. require_session becomes pub(super) for this.

The nav bars that main added on the group, policy and role name errors get the tabs, as every other page does. Main moved the bar of the metrics page into render_metrics_page, which now takes the tabs. Main's tests of the bar, the dashboard, the metrics page and the user page pass no tabs. The ConsoleState that main's account scope tests build has an empty tab list. The tab page of the start_server tests builds its bar from a NavIdentity made from the ConsoleIdentity.
The merge brings the new commits of #549 and #547. #547 adds a second subscriber to the dispatch entry test and a guide sentence on the TTL index backfill. #549 adds the recovery refusal to the observer comment and the repeated Host check on an extension route. No file conflicted.
…ssion

The ConsoleIdentity::is_admin comment and the storage extension manual said that a page meant for admins answers 403, as /console/settings and /console/cache do. Since #727, those two pages and /console/metrics redirect an IAM-user session to /console, and /console/metrics-data answers 403. Both texts now say so.
…y_label call

require_console_session called identity_label for the label and then is_admin for the flag, which identity_label also returns. It now reads both from the one NavIdentity. is_admin is private to the pages module again, as on main.

@demohan demohan left a comment

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.

Extension tabs go through the console's login check. Without a session, every path under a tab redirects to login, including the slash form, encoded and .. paths, wrong methods, HEAD, OPTIONS and /{*rest} routes. An extension's own fallback becomes a plain 404. Tab labels are escaped, an HTTP extension prefix under /console is refused, and startup refuses 11 bad slugs, each with its own error, before any backend line. On a thin binary built from the Step 6 text, an IAM user gets the tab with is_admin false, a POST with the right CSRF token gets 200 and a missing, altered or other user's token gets 403, and a deleted user, a changed password and logout each send the old session back to login.

With no tabs, the default binary answers 352 console requests, the data plane and the management API as main does, apart from the header below. Gates are clean, 5,082 tests pass, and CI is green on 54 of 54 checks.

I will approve once the two test items are done.

Comment thread crates/server/src/console/mod.rs Outdated
Ok(session) => {
let nav = pages::identity_label(&session.identity);
req.extensions_mut().insert(ConsoleIdentity {
label: nav.label,

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.

The tab session check is tested for an admin and for a request with no cookie. Forcing is_admin: true, or turning the 503 into a login redirect, leaves the 561 server tests passing. No test covers an IAM user, a deleted principal or a changed password on a tab; this head answers each correctly, and a test with an IAM user fails under the is_admin change.

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 added the_tab_session_check_answers_for_each_session_state in crates/server/src/console/tab_gate_tests.rs (4943055, with one more row in d7fe18e). It is one data table that runs require_console_session against a catalog double. The rows cover an admin, an IAM user, no cookie, a deleted principal, a changed password and an unreadable catalog, plus the POST rows. Each row checks the status, the location, the page body (the is_admin the page got) and the calls to the page and to the catalog.

With is_admin: true, the IAM user row fails: the page got admin=true where the row expects admin=false. With the 503 turned into the login redirect, the unreadable catalog row fails with 303 where it expects 503. The session rows pass on the check as it was at c27e1d8.

}

let nav = html::nav_bar(&identity_label(&session.identity));
let nav = html::nav_bar(&identity_label(&session.identity), &state.tabs);

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.

The tab links are tested on the dashboard only, and about 45 nav_bar calls each pass &state.tabs. Passing &[] here leaves every test passing. A test that walks the pages showing the logged-in bar would hold it.

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 chose one helper over a walk of 45 pages. In 41a6c51, SessionData holds the tab list, and SessionData::nav_bar builds the bar from the session's identity and tabs. Every built-in page calls it. The dashboard, metrics, user and cache renderers lose their tabs parameter. No page names a tab list, so a page has no place to pass &[], and this line now reads session.nav_bar(). A walk would need catalog data for each page and a failing POST for each error page that draws the bar.

With &[] inside SessionData::nav_bar, a_console_tab_is_behind_the_session_check_and_listed_in_the_nav_bar fails at its dashboard check, because the dashboard builds its bar through the same method.

Comment thread crates/server/src/lib.rs
.router
.fallback(|| async { StatusCode::NOT_FOUND })
.layer(gate.clone());
// The trailing-slash form redirects to the page, as /console/ does

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.

Without a session, /console/mytab answers 303 to login and /console/nothere answers 404, so a client can tell a mounted slug from an unmounted one. The PR's tests at start_server_tests.rs:432 and :512 assert both. The built-in pages answer the same way, so nothing new is exposed, and the comment and PR body can say so.

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 checked the built-in pages on a server with a tab, without a session. /console/accounts answers 303 to /console/login and /console/nothere answers 404, the same pair as /console/chaos and an unmounted slug. 1a75627 rewrites the comment to say so, in place of the claim that a client without a session cannot tell the two apart. It also adds both paths to the no-session loop of a_console_tab_is_behind_the_session_check_and_listed_in_the_nav_bar. The PR body sentence changes the same way.

Comment thread crates/server/src/lib.rs Outdated
// below it (each tab's, inside its session layer) are kept when the
// console is nested into the app; axum drops a child's fallback when
// its parent still has the default one.
let mut console_router = console::router()

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.

With no tabs, 404s for unmatched /console/* paths now carry content-length: 0 (HEAD over HTTP/1.1, GET and HEAD over HTTP/2), and main sends none. HEAD /nope still has none. The PR body says nothing changes on the wire.

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 reproduced it with the dev binary, asking /console/nothere and /nope with GET and HEAD over HTTP/1.1 and over HTTP/2 with prior knowledge. At c27e1d8, /console/nothere carried content-length: 0 on HEAD over HTTP/1.1 and on GET and HEAD over HTTP/2, and /nope carried none. At 95425f5 the two paths answered alike, with content-length: 0 on GET over HTTP/1.1 only.

c7b7e20 makes the console's fallback explicit only when a tab is mounted. A binary with no tab keeps axum's default 404, and its eight answers now equal those of 95425f5. without_tabs_the_console_nav_bar_and_routes_are_unchanged compares GET and HEAD /console/chaos with /nope, headers included, and fails with the old fallback. With a tab mounted the console's fallback is explicit again, so an unmatched console path carries the header as it did at c27e1d8.

Comment thread crates/server/src/extensions.rs Outdated
pub router: Router,
}

/// Console paths the server owns at the first segment under `/console/`; a

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.

The reserved list is a copy of the console router's first segments, and no test ties them. With "cache" removed from it, a tab at cache makes start_server panic with Overlapping method route, which in serve comes after the fork. users, groups, roles, policies and access-keys are on the list and are not router segments, though the error calls them built-in routes.

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.

93275b3 cuts the list to the router's eight first segments: login, logout, docs, metrics, metrics-data, settings, cache and accounts. Each is a route under /console, so the error text is true for every entry. the_reserved_slugs_are_the_console_routers_first_segments in extensions/tests.rs reads the route paths of console::router() and fails when the list and the first segments differ in either direction. axum has no method that lists a router's routes, so the test reads them from the router's Debug output. A change to that output fails the test. The test fails with cache removed and with the old list of 13. The slug test now loops over the list, so the code keeps one copy of it.

Comment thread crates/server/src/console/mod.rs Outdated
/// [`csrf_matches`](Self::csrf_matches) before acting. Read by field; the
/// struct may gain fields in a minor release, so build one for a test with
/// [`ConsoleIdentity::new`].
#[derive(Debug, Clone)]

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. ConsoleIdentity derives Debug, so logging it prints the session's CSRF token. The built-in SessionData has no Debug.

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.

4068b65 removes the Debug derive from ConsoleIdentity, so a log line cannot print the token, the same as for SessionData. The struct's comment says it has no Debug and why. No code in the server crate, the app crate or their tests formatted an identity with {:?}, and both crates build without it.

/// session 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.

/// Add a console tab: `label` is the link text, `slug` one path segment
/// of `[a-z0-9-]`, `router` the page at `/console/<slug>`.
#[must_use]
pub fn with_console_tab(

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. An empty label is accepted and renders an empty link.

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.

93275b3 refuses a tab whose label is empty or only white space. The check is in validate_console_slugs. cmd_serve calls it before the backend connects and before the daemon forks, and start_server calls it again before it serves. The error is "console tab 'blank' has an empty label, which would show as an empty link in the navigation bar". validate_console_slugs_checks_the_list_in_order has rows for "" and " \t". With the check removed the "" row fails, and without the trim the " \t" row fails.

The tab session check had tests for an admin and for a request with no cookie only. A data table now runs require_console_session for an admin, an IAM user, no cookie, a deleted principal, a changed password and an unreadable catalog. It also runs it for a POST with the session's token, a POST with no cookie and a POST for a deleted principal. Each row checks the status, the location, the body of the tab page and the calls to the page and to the catalog. The page answers with the admin flag the check gave it.

With is_admin set to true in the check, the IAM user rows fail. With the 503 changed to the login redirect, the unreadable catalog row fails.

The catalog store of the account scope tests moves to console/test_catalog.rs, with a setting for its answer to the password hash read. Both tests use it.
About 45 built-in pages passed &state.tabs to html::nav_bar. One page could pass another list, or an empty one, and no test would fail. SessionData now holds the tab list, and its nav_bar method builds the bar from the session's identity and tabs. Every built-in page calls it. The dashboard, metrics, user and cache renderers lose their tabs parameter, so no page passes a tab list of its own.

With an empty list in nav_bar, the tab test that reads the dashboard's bar fails.
ConsoleIdentity derived Debug, so a log line that printed an identity printed the session's CSRF token. It now has no Debug, like the built-in SessionData.
…CSRF token

The tab session check left the CSRF check to each tab handler. A POST with a valid session cookie and no _csrf field reached the handler. The cookie is SameSite=Strict, so a browser does not send it on a cross-site request. A page on another port of the same host is the same site, and its requests carry the cookie.

The check now reads the form body of each request whose method is not GET, HEAD, OPTIONS or TRACE. It answers 403 "CSRF token mismatch" unless the _csrf field is the session's token. It uses require_csrf, the check of the built-in pages. The check reads the body up to axum's 2 MB default and passes it to the tab's router.

ConsoleIdentity::csrf_matches had no other caller than a tab handler, so this commit removes it. The manual and the architecture guide say what the check refuses. They also say that a script in the layout adds the _csrf field to the POST forms of a page.
The console router got an explicit fallback so that axum keeps the tab fallbacks nested below it. With no tab mounted, that handler answered an unmatched console path with a 404 that carries content-length: 0, and HEAD over HTTP/1.1 shows the header. axum's default fallback, which main uses there, sends no content-length. The fallback is now explicit only when a tab is mounted. A binary with no tab answers an unmatched console path with axum's default 404, as main does.

A test on a server with no tab compares GET and HEAD /console/chaos with /nope, status and headers included.
… session

The comment on the tab's slash route said that a client without a session cannot tell a mounted slug from an unmounted one. But /console/<slug> answers 303 to the login page, and an unmounted slug answers 404. The built-in pages answer the same way: /console/accounts answers 303 and an unknown console path answers 404. A tab shows a client nothing more than the built-in pages show. The comment now says this, and the tab test checks /console/accounts and /console/nothere next to the tab's paths.
…n empty tab label

The reserved slug list was a copy of the console router's first segments, and no test tied the two. A segment missing from the list let a tab collide with a route, and axum panicked in start_server after the fork in serve. Five entries (users, groups, roles, policies and access-keys) were not router segments, but the error called them built-in routes.

The list is now the router's eight first segments. A test reads the route paths from the router's Debug output and fails when the list and the segments differ.

A tab with an empty label showed an empty link in the navigation bar. validate_console_slugs now refuses a label that is empty or only white space, with its own error, before the backend connects.
…ts 403

The tab session check reads the _csrf field the way a built-in page's form does. A body that is not an application/x-www-form-urlencoded form gives no field, so the request gets 403. A new row sends the session's token in a JSON body and expects the 403. With the session's token as the answer to a body that does not parse, the row fails.
…thods reach a tab

The tab session check guards every method but GET, HEAD, OPTIONS and TRACE,
but the table sent only POST and DELETE. A check that guarded only POST, PUT,
PATCH and DELETE passed it. A new row sends a PROPFIND without the token and
expects 403. It fails under that narrower check.

The storage extension manual now says that GET, HEAD, OPTIONS and TRACE reach
the tab's router without the token. So a tab changes state only on another
method, as the built-in pages change state only on POST. The manual also says
that a body over 2 MB gets 413. The security model manual's CSRF line now says
that a script in the layout adds the token to each POST form. It also says
that the session check guards an extension tab.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

Both test items are done. the_tab_session_check_answers_for_each_session_state in crates/server/src/console/tab_gate_tests.rs (4943055 and d7fe18e) runs the tab session check from one data table. Its rows cover an admin, an IAM user, no cookie, a deleted principal, a changed password and an unreadable catalog. It fails with is_admin: true, and it fails with the 503 turned into a login redirect.

For the tab links, 41a6c51 builds the bar of every built-in page with SessionData::nav_bar, so no page passes a tab list. With &[] in that method, a_console_tab_is_behind_the_session_check_and_listed_in_the_nav_bar fails at the dashboard.

I answered the other six threads inline, each with its commit.

@demohan demohan left a comment

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.

The two test items are done at 9237e0f, and the other items from the earlier review are resolved; one change in the CSRF gate needs a fix.

  • The tab session tests hold: forcing is_admin, turning the catalog 503 into a redirect, ignoring the password stamp, or accepting a missing principal each fails its row. Every page builds its bar through SessionData::nav_bar, and passing no tabs there fails the dashboard check. On a thin binary written from the manual's Step 6, an admin and an IAM user get the right links and routes, a deleted user, a changed password and a logged-out session get 303, and with PostgreSQL stopped the tab answers 503 and recovers after a restart.
  • Without the token, POST, PUT, PATCH, DELETE, PROPFIND, MKCOL and an unknown method get 403 before the handler, and with it the handler gets the whole body; 12 malformed token forms get 403, a 2,097,152-byte body passes and one byte more gets 413, and with no session the answer is 303 before the body is read. The 8 reserved segments are exactly the router's first segments and each is refused at startup, cache no longer panics, the five freed slugs mount and the account pages still render, and an empty or blank label is refused. ConsoleIdentity no longer formats with {:?}. With no tab, console 404s carry the same headers as the merge base on 36 rows.
  • fmt, clippy and the 1.88 check are clean, and 656 server and app tests pass.

I will approve once the CSRF gate names its four methods.

Comment thread crates/server/src/console/mod.rs Outdated
Ok(session) => session,
Err(response) => return response,
};
let mut req = if req.method().is_safe() {

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.

is_safe() follows the http crate's list, not the four methods the comment, both manuals and the body name. This lockfile has http 1.4.0, where the list is GET, HEAD, OPTIONS and TRACE, but 1.5.0 adds QUERY, and a thin binary built with its own lockfile resolves http = "1" to 1.5.0. Built that way, a QUERY with body x=1, a valid session and no token answers 200 and the tab handler runs, on both runs; the PROPFIND row does not catch it. A browser cannot send QUERY cross-origin without a preflight the console does not answer, which limits the reach. Matching GET | HEAD | OPTIONS | TRACE here and adding a QUERY row to the method table would hold it.

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.

Fixed in 592fbad. require_console_session now matches Method::GET | Method::HEAD | Method::OPTIONS | Method::TRACE and no longer calls is_safe(). A comment on the match says why it names the four methods. The method table has a QUERY row that expects 403.

I ran the table under both versions of http. With 1.5.0 in the lockfile and the is_safe() check, the QUERY row gets 200 and fails. With the new match, it gets 403 under 1.4.0 and under 1.5.0. A match that also lets QUERY through fails the row with 200.

9f5749d adds HEAD, OPTIONS and TRACE rows that expect 200, so a check that leaves out one of the four methods also fails a row.

Comment thread crates/server/src/lib.rs
.fallback(|| async { StatusCode::NOT_FOUND })
.layer(gate.clone());
// The trailing-slash form redirects to the page, as /console/ does
// below, behind the same check. Without a session every path under

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. Without a session, /console/accounts answers 303 only on GET and HEAD and 405 on POST, PUT, DELETE, PATCH, OPTIONS and PROPFIND, while a tab path answers 303 on all eight. "as a GET of a built-in page does" would match.

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.

Changed in 592fbad to "as a GET of a built-in page does". The router gives /console/accounts only a get route, so the other methods get 405 there, as you measured.

…art_server refusal test

The merge of feat/thin-bin-extensions brings build_state_with with three
parameters: the storage, the auth provider and the request observer.
start_server_refuses_a_reserved_slug_and_a_reserved_prefix_before_serving
still passed the auth provider and the observer only, and the server's
test build failed.

The test now passes None for the storage, as the other callers do.
The tab session check let a request through without the CSRF token when
Method::is_safe was true. That list comes from the http crate. In http 1.4.0,
the version in Cargo.lock, it is GET, HEAD, OPTIONS and TRACE. http 1.5.0 adds
QUERY, and a thin binary built with its own lockfile can resolve http 1.5.0.

The check now matches GET, HEAD, OPTIONS and TRACE by name, the four methods
that the doc comment and both manuals give. A new row sends a QUERY without
the token and expects 403. With http 1.5.0 and the old check, the row gets
200 and fails. With http 1.5.0 and the new check, it passes.

The comment in start_server said that a tab path without a session answers
303 as /console/accounts does. That page answers 303 only on GET and HEAD, so
the comment now says "as a GET of a built-in page does".
… the token

The method table sent GET as the only method that passes without the token.
A session check that named GET alone passed it, and so did one that left out
HEAD, OPTIONS or TRACE. Three new rows send HEAD, OPTIONS and TRACE without
the token and expect 200 from the tab. Each of those narrower checks fails a
row. The HEAD row expects an empty body, because the router drops the body of
a HEAD answer.

@demohan demohan left a comment

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.

The CSRF gate names its four methods at 9f5749d. On a thin binary of this head built with http 1.5.0, a QUERY with body x=1 and no token answers 403 CSRF token mismatch without reaching the handler, and 200 with the token; GET, HEAD, OPTIONS and TRACE answer 200 without it; POST, PUT, PATCH, DELETE, PROPFIND, MKCOL and an unknown method answer 403 without it and 200 with it; with no session all twelve answer 303. Each probe ran twice, and the same binary with is_safe() put back answers the QUERY with 200.

Dropping any of the four methods, matching GET alone, or letting QUERY through each fails a row, and the lib.rs comment now matches the answers: without a session /console/accounts answers 303 on GET and HEAD and 405 on the other six methods, a tab path 303 on all eight. The method check still runs after the session check, fmt, clippy and the 1.91.1 check are clean, and 678 server and app tests pass.

Row { name: "PROPFIND without a token", holder: Holder::Admin, stored: StoredHash::LoginHash, method: "PROPFIND", csrf: Csrf::Absent, status: StatusCode::FORBIDDEN, location: None, body: "CSRF token mismatch" },
// With http 1.5.0, `Method::is_safe` is true for QUERY, and a gate that
// uses it fails this row.
Row { name: "QUERY without a token", holder: Holder::Admin, stored: StoredHash::LoginHash, method: "QUERY", csrf: Csrf::Absent, status: StatusCode::FORBIDDEN, location: None, body: "CSRF token mismatch" },

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. Cargo.lock pins http 1.4.0, where is_safe() and the named match agree on every row, so with is_safe() put back this table passes under the lockfile CI uses; the row fails only once the lock moves to 1.5.0, as the comment above it says. Moving the lock to http 1.5.0 (its rust-version is 1.57.0, and the table passes under it) would let CI hold it now.

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.

This stays as it is in this PR. A move of the lock to http 1.5.0 changes the http entry of every crate in Cargo.lock that uses it. Main's lock still holds 1.4.0, so the change belongs to a dependency update on main. Once the lock holds 1.5.0, the QUERY row holds the gate in CI, as its comment says.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants