use super::*;
#[derive(Clone)]
pub(crate) struct DeployAuditEnv {
pub rustango_env: Option<String>,
pub session_secret: Option<String>,
pub database_url: Result<String, String>,
pub database_style_conflict: Option<String>,
pub apex_domain: ApexDomain,
pub bind: Option<String>,
pub ungated_admin: bool,
}
#[derive(Default, Clone)]
pub(crate) enum ApexDomain {
#[default]
SingleTenant,
#[cfg_attr(not(feature = "tenancy"), allow(dead_code))]
Tenancy(Option<String>),
}
pub(super) fn deploy_audit_env(tenancy: bool) -> DeployAuditEnv {
DeployAuditEnv {
rustango_env: std::env::var("RUSTANGO_ENV").ok(),
session_secret: std::env::var("RUSTANGO_SESSION_SECRET").ok(),
database_url: crate::env::database_url_from_env().map_err(|e| e.to_string()),
database_style_conflict: crate::env::database_style_conflict(),
#[cfg(feature = "tenancy")]
apex_domain: if tenancy {
ApexDomain::Tenancy(crate::tenancy::server::configured_apex_domain())
} else {
ApexDomain::SingleTenant
},
#[cfg(not(feature = "tenancy"))]
apex_domain: {
let _ = tenancy;
ApexDomain::SingleTenant
},
bind: std::env::var("RUSTANGO_BIND").ok(),
#[cfg(feature = "admin")]
ungated_admin: crate::admin::ungated_admin_built(),
#[cfg(not(feature = "admin"))]
ungated_admin: false,
}
}
#[doc(hidden)]
#[derive(Debug, Default)]
pub struct DeployAuditFindings {
pub info: Vec<String>,
pub warnings: Vec<String>,
pub errors: Vec<String>,
}
pub(super) const APEX_UNSET: &str =
"RUSTANGO_APEX_DOMAIN is unset / `localhost` — tenancy projects need \
the public-facing apex (e.g. `app.example.com`) so subdomain resolution + cookie scoping \
work in production.";
#[cfg(feature = "tenancy")]
pub(super) const SIGNED_BY_SESSION_SECRET: &str =
"session cookies (operator and tenant ones included) and JWTs";
#[cfg(not(feature = "tenancy"))]
pub(super) const SIGNED_BY_SESSION_SECRET: &str = "session cookies and JWTs";
pub(crate) fn run_deploy_audit(env: &DeployAuditEnv, out: &mut DeployAuditFindings) {
if env.ungated_admin {
out.warnings.push(
"[admin] the admin router has no authentication — anyone can read and write \
every model. Call `admin::Builder::with_session_auth(secret)`, or gate the \
route yourself."
.into(),
);
}
match env.rustango_env.as_deref() {
Some(tier) if crate::env::is_prod_tier(tier) => {
out.info
.push("RUSTANGO_ENV is set to a production value".into());
}
Some(other) => {
out.warnings.push(format!(
"RUSTANGO_ENV is `{other}` — set to `prod` (or `production`) in deployed env"
));
}
None => {
out.warnings.push(
"RUSTANGO_ENV is unset — set to `prod` so config loaders pick the right tier"
.into(),
);
}
}
match env.session_secret.as_deref() {
None => {
out.errors.push(format!(
"RUSTANGO_SESSION_SECRET is unset — on the prod tier the server will not \
start; off prod, {SIGNED_BY_SESSION_SECRET} use the dev key in ./var/, which \
a redeploy loses. Set via `openssl rand -base64 32`."
));
}
Some(s) if s.contains("change-me") || s.contains("placeholder") => {
out.errors.push(
"RUSTANGO_SESSION_SECRET still contains the scaffolder placeholder \
(`change-me-...`) — replace with a real secret via `openssl rand -base64 32`."
.into(),
);
}
#[cfg(any(feature = "admin", feature = "tenancy"))]
Some(s) => match crate::session::SessionSecret::from_b64(s) {
Ok(_) => out
.info
.push("RUSTANGO_SESSION_SECRET decodes to a valid ≥32-byte key".into()),
Err(e) => out.errors.push(format!(
"{e}. This is the same check the runtime applies, so the app would \
fall back to an ephemeral key and sign everyone out on restart."
)),
},
#[cfg(not(any(feature = "admin", feature = "tenancy")))]
Some(_) => out.info.push(
"RUSTANGO_SESSION_SECRET is set but not validated — this build has \
neither `admin` nor `tenancy`, so nothing in it signs cookies or JWTs."
.into(),
),
}
match env.database_url.as_deref() {
Err(e) => out.errors.push(format!("no usable database URL: {e}")),
Ok(url) if url.contains("localhost") || url.contains("127.0.0.1") => {
out.warnings.push(
"DATABASE_URL points at localhost / 127.0.0.1 — verify this is intended in \
production (typically a managed service hostname)"
.into(),
);
}
Ok(_) => out.info.push("DATABASE_URL set".into()),
}
if let Some(conflict) = &env.database_style_conflict {
out.warnings.push(conflict.clone());
}
match &env.apex_domain {
ApexDomain::SingleTenant => {}
ApexDomain::Tenancy(None) => out.warnings.push(APEX_UNSET.into()),
ApexDomain::Tenancy(Some(d)) if d == "localhost" => out.warnings.push(APEX_UNSET.into()),
ApexDomain::Tenancy(Some(_)) => out
.info
.push("RUSTANGO_APEX_DOMAIN set to a non-localhost value".into()),
}
match env.bind.as_deref() {
Some(b) if b.starts_with("127.0.0.1") => {
out.warnings.push(format!(
"RUSTANGO_BIND={b} only listens on loopback — production usually wants \
`0.0.0.0:<port>` to accept external traffic"
));
}
Some(_) | None => {} }
}
#[cfg(feature = "cache")]
pub(crate) fn login_store_audit(
lockout: &crate::account_lockout::Lockout,
out: &mut DeployAuditFindings,
) {
if lockout.stores_nothing() {
out.warnings
.push(crate::account_lockout::NULL_STORE_WARNING.into());
} else if lockout.is_process_local() {
out.info
.push(crate::account_lockout::CHECK_DEPLOY_NOTE.into());
}
#[cfg(feature = "admin")]
out.info
.push(crate::login_throttle::PROCESS_LOCAL_NOTE.into());
}
#[cfg(feature = "config")]
pub(super) fn run_settings_audit(out: &mut DeployAuditFindings) {
use crate::config::Settings;
let env_tier = Settings::current_env_tier();
out.info
.push(format!("config tier resolved to `{env_tier}`"));
settings_audit_loaded(&env_tier, Settings::load_from_env(), out);
}
pub(super) fn check_settings_cmd<W: Write>(args: &[String], w: &mut W) -> Result<(), MigrateError> {
if args.len() != 1 {
return Err(MigrateError::Validation(
"check --settings takes no other arguments".into(),
));
}
#[cfg(feature = "config")]
{
let tier = crate::config::Settings::current_env_tier();
let loaded = crate::config::report(Path::new("config"), &tier);
write_settings_report(w, &tier, loaded, &|n| std::env::var(n).ok())
}
#[cfg(not(feature = "config"))]
{
let _ = w;
Err(MigrateError::Validation(
"check --settings needs the `config` feature".into(),
))
}
}
#[cfg(feature = "config")]
pub(super) fn write_settings_report<W: Write>(
w: &mut W,
tier: &str,
loaded: Result<crate::config::Loaded, crate::config::ConfigError>,
get: &dyn Fn(&str) -> Option<String>,
) -> Result<(), MigrateError> {
use crate::config::registry::{Reader, KEYS};
use crate::env::SettingSource;
let loaded = match loaded {
Ok(l) => Some(l),
Err(e) if e.is_missing_config() => None,
Err(e) => return Err(MigrateError::Validation(e.to_string())),
};
writeln!(w, "settings for tier `{tier}`:")?;
if loaded.is_none() {
writeln!(w, " (no config/default.toml: only env vars apply)")?;
}
let blank = |v: &String| v.trim().is_empty();
let file_source = |path: &str| -> SettingSource {
let Some(l) = &loaded else {
return SettingSource::Default;
};
l.sources
.iter()
.filter(|(k, _)| crate::config::registry::find(k).is_some_and(|r| r.path == path))
.map(|(_, s)| s.clone())
.max_by_key(|s| matches!(s, SettingSource::Override(_)))
.unwrap_or(SettingSource::Default)
};
for key in KEYS {
let mut missing = None;
let source = if key.path == "database.url" {
let file_url = loaded
.as_ref()
.and_then(|l| l.settings.database.url.clone());
match crate::env::resolve_database_url_from(get, file_url.as_deref()) {
Ok((_, SettingSource::File(_))) => file_source(key.path),
Ok((_, src)) => src,
Err(e) => {
let kind = match e {
crate::env::EnvError::MissingRequired { .. } => "MISSING (required)",
_ => "INVALID",
};
missing = Some(format!("{kind}: {e}"));
SettingSource::Default
}
}
} else {
match key.env.iter().find(|v| get(v).is_some_and(|x| !blank(&x))) {
Some(var) => SettingSource::Env(var),
None => file_source(key.path),
}
};
let set = source != SettingSource::Default;
let value = match (&source, set && key.secret) {
(_, true) => " = <hidden>".to_owned(),
(SettingSource::Env(var), false) => {
format!(" = {}", get(var).unwrap_or_default())
}
(SettingSource::Default, false) => String::new(),
(_, false) => loaded
.as_ref()
.and_then(|l| key.path.split('.').try_fold(&l.tree, |v, seg| v.get(seg)))
.map(|v| format!(" = {}", toml_text(v)))
.unwrap_or_default(),
};
let note = match (key.reader, set) {
(_, false) if missing.is_some() => format!(" {}", missing.unwrap_or_default()),
(Reader::App(by), true) => format!(" (read only by {by})"),
(Reader::Tenancy, true) => " (tenancy apps only)".to_owned(),
_ => String::new(),
};
writeln!(w, " {:<40} {source}{value}{note}", key.path)?;
}
Ok(())
}
#[cfg(feature = "config")]
pub(super) fn toml_text(v: &toml::Value) -> String {
match v {
toml::Value::String(s) => s.clone(),
toml::Value::Integer(i) => i.to_string(),
toml::Value::Float(f) => f.to_string(),
toml::Value::Boolean(b) => b.to_string(),
toml::Value::Datetime(d) => d.to_string(),
toml::Value::Array(a) => {
let items: Vec<String> = a.iter().map(toml_text).collect();
format!("[{}]", items.join(", "))
}
toml::Value::Table(_) => "{…}".to_owned(),
}
}
#[cfg(feature = "config")]
pub(super) fn settings_audit_loaded(
env_tier: &str,
loaded: Result<crate::config::Settings, crate::config::ConfigError>,
out: &mut DeployAuditFindings,
) {
let settings = match loaded {
Ok(s) => s,
Err(e) if e.is_missing_config() => {
if crate::env::is_prod_tier(env_tier) {
out.warnings.push(
"config/default.toml is missing in prod tier — no [security] settings \
(allowed_hosts, headers, SSL redirect) are applied"
.into(),
);
}
return;
}
Err(e) => {
out.warnings.push(format!(
"config: failed to load settings for audit: {e} — fix the file shape \
to enable the rest of the deploy audit"
));
return;
}
};
settings_audit_check(&env_tier, &settings, out);
}
#[cfg(feature = "config")]
#[doc(hidden)]
pub fn settings_audit_check(
env_tier: &str,
settings: &crate::config::Settings,
out: &mut DeployAuditFindings,
) {
let in_prod = crate::env::is_prod_tier(env_tier);
if !in_prod {
return;
}
match settings.security.headers_preset.as_deref() {
None | Some("strict" | "relaxed") => {}
Some(preset @ ("dev" | "none" | "empty")) => out.warnings.push(format!(
"[security] headers_preset = `{preset}` in prod tier — promote to `strict` \
so HSTS / X-Frame-Options / X-Content-Type-Options / Referrer-Policy are emitted"
)),
Some(preset) => out.warnings.push(format!(
"[security] headers_preset = `{preset}` is not `strict`, `relaxed`, `dev` or \
`none`; the layer falls back to `strict`. Fix the spelling."
)),
}
if settings.security.secure_cookies == Some(false) {
out.warnings.push(
"[security] secure_cookies = false in prod tier — session and CSRF cookies are \
sent over plain HTTP too. Remove it or set it to true."
.into(),
);
}
if matches!(settings.security.hsts_max_age_secs, Some(0)) {
out.warnings.push(
"[security] hsts_max_age_secs = 0 in prod tier — disables HSTS, leaving TLS-strip \
attacks viable on first request"
.into(),
);
}
if settings.security.allowed_hosts.is_empty() {
out.warnings.push(
"[security] allowed_hosts = [] in prod tier — `host_validation::AllowedHostsLayer` \
passes every Host header (no DisallowedHost protection). Populate with the canonical \
host + subdomain wildcards before deploying."
.into(),
);
} else if settings
.security
.allowed_hosts
.iter()
.any(|h| h.trim() == "*")
{
out.warnings.push(
"[security] allowed_hosts contains \"*\" in prod tier — every Host header passes, \
so host validation is off. List the real hosts instead."
.into(),
);
}
if settings
.security
.cors_allowed_origins
.iter()
.any(|o| o.trim() == "*")
{
out.warnings.push(
"[security] cors_allowed_origins contains \"*\" in prod tier — any site can read \
non-credentialed responses. List the real origins."
.into(),
);
}
if matches!(settings.server.request_timeout_secs, None | Some(0)) {
out.warnings.push(
"[server] request_timeout_secs unset or 0 in prod tier — no handler timeout, so \
stuck requests hold workers. Set about 30."
.into(),
);
}
if settings.server.max_body_bytes.is_none() {
out.info.push(
"[server] max_body_bytes unset in prod tier — no body-limit layer; only axum's \
2 MiB extractor default applies, and streamed bodies are unlimited."
.into(),
);
}
if settings.security.csrf_trusted_origins.is_empty() {
out.info.push(
"[security] csrf_trusted_origins = [] in prod tier — the CSRF Origin check accepts \
only the request's own Host. Add any SPA or other origin that POSTs cross-host, \
or its requests get 403."
.into(),
);
}
if matches!(settings.security.secure_ssl_redirect, Some(false) | None) {
out.info.push(
"[security] secure_ssl_redirect unset/false in prod tier — plain-HTTP requests \
aren't redirected. If your LB doesn't already enforce HTTPS, mount \
`ssl_redirect::SslRedirectLayer` and set this true (configure `secure_proxy_ssl_header` \
to avoid loops)."
.into(),
);
}
if !settings.security.secure_proxy_ssl_header.is_empty()
&& settings.security.secure_proxy_ssl_header.len() != 2
{
out.warnings.push(format!(
"[security] secure_proxy_ssl_header has {} entries — expected exactly 2 \
([header_name, expected_value]); ssl_redirect layer will drop the trusted-header \
pair and redirect-loop behind the LB",
settings.security.secure_proxy_ssl_header.len()
));
}
if let Some(kib) = settings.auth.argon2_memory_kib {
if kib < 19_456 {
out.warnings.push(format!(
"[auth] argon2_memory_kib = {kib} in prod tier — OWASP 2024 recommends ≥ 19456 \
for password hashing brute-force resistance"
));
}
}
if let Some(ttl) = settings.auth.jwt.access_ttl_secs {
if ttl > 3600 {
out.warnings.push(format!(
"[auth.jwt] access_ttl_secs = {ttl} in prod tier — access tokens > 1h widen \
the leaked-token blast radius. The refresh flow rotates them; keep this short."
));
}
}
if settings.audit.retention_days.is_none() {
out.info.push(
"[audit] retention_days unset — log grows forever; set it and schedule \
`manage audit-cleanup`, which prunes to it"
.into(),
);
}
if matches!(settings.routes.legacy_preset, Some(true)) {
out.info.push(
"[routes] legacy_preset = true — using the pre-v0.29 `__`-prefixed URLs \
(`/__login`, `/__admin`, …). Switch to the friendly defaults when bookmarks allow."
.into(),
);
}
if matches!(settings.mail.backend.as_deref(), None | Some("console")) {
out.warnings.push(
"[mail] backend unset or `console` in prod tier — mail bodies, including reset \
and magic-link URLs, are printed to stdout. Set `smtp` (or `null`)."
.into(),
);
}
if let Some(bind) = settings.server.bind.as_deref() {
if bind.starts_with("127.0.0.1") || bind.starts_with("localhost") {
out.warnings.push(format!(
"[server] bind = `{bind}` in prod tier — only listens on loopback. \
Production usually wants `0.0.0.0:<port>` to accept external traffic."
));
}
}
if let Some(backend) = settings.database.resolved_backend() {
let tenancy_on = crate::config::Settings::detected_features()
.iter()
.any(|f| *f == "tenancy");
if tenancy_on && backend != "postgres" {
out.warnings.push(format!(
"[database] backend = `{backend}` with `tenancy` feature on — \
schema-mode multi-tenancy is Postgres-only by language semantics \
(it relies on `SET search_path`). Either switch to a Postgres URL \
in prod, or use `crate::tenancy::DatabasePools<DB>` for the \
one-database-per-tenant model that works on sqlite/mysql."
));
}
}
let admin = &settings.admin;
if let Some(mode) = admin.theme_mode.as_deref() {
if !matches!(mode, "auto" | "light" | "dark") {
out.warnings.push(format!(
"[admin] theme_mode = `{mode}` is not one of `auto` / `light` / `dark` — \
the chrome will ignore it and fall back to `auto`."
));
}
}
if let Some(prefix) = admin.url_prefix.as_deref() {
if prefix.ends_with('/') && prefix.len() > 1 {
out.warnings.push(format!(
"[admin] url_prefix = `{prefix}` ends with a trailing slash — Builder will \
strip it, but config files should write the canonical form (no trailing slash) \
so reviewers can grep across deployments."
));
}
}
if crate::config::Settings::detected_features()
.iter()
.any(|f| *f == "mcp")
{
if settings.mcp.allowed_origins.is_empty() {
out.info.push(
"[mcp] allowed_origins = [] in prod tier — the MCP endpoint is same-origin only. \
If browser-based agents call it cross-origin, populate the CORS allow-list; \
server-to-server agents (no browser) don't need it."
.into(),
);
}
if matches!(settings.mcp.enable_sse, Some(false)) {
out.info.push(
"[mcp] enable_sse = false in prod tier — no server→client notifications \
(list_changed / progress); agents must re-list to observe capability changes."
.into(),
);
}
if settings.mcp.rate_limit().is_none() {
out.info.push(
"[mcp] rate_limit_per_minute unset or 0 in prod tier — the agent token + JSON-RPC \
endpoints are unthrottled. Set a per-IP cap to blunt credential-stuffing / abuse."
.into(),
);
}
}
}