From dab1e90947873b79a29e58bd9ed523d95ebbd1da Mon Sep 17 00:00:00 2001 From: D3SOX Date: Tue, 3 Nov 2026 13:47:31 +0100 Subject: [PATCH 1/3] fix(config): read nested options such as OIDC__CLIENT_ID from the environment The environment source had no separator, so OIDC__* variables never reached the oidc section. Use `__` as the separator, and accept a comma-separated string for oidc.allowed_redirect_urls so it can be set from the environment too. --- README.md | 10 +++-- src/config.rs | 115 ++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 119 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index a32b6a4..36af712 100644 --- a/README.md +++ b/README.md @@ -65,6 +65,8 @@ The public instance's privacy policy is in [PRIVACY.md](./PRIVACY.md). Set options in a `config.toml` in the server's working directory, or as environment variables (case-insensitive, e.g. `ALLOW_REGISTRATION=false`). +Environment variables override `config.toml`. For options inside a section, join +the names with `__`, e.g. `OIDC__CLIENT_ID` for `client_id` in `[oidc]`. [config.dev.toml](./config.dev.toml) is an annotated example. | Option | Description | Default | @@ -156,9 +158,11 @@ The server never migrates an existing database on its own. To upgrade: ### OIDC -Add an `[oidc]` section to `config.toml` to let users sign in through an -OpenID Connect provider. All options except `allowed_redirect_urls` are -required. +Add an `[oidc]` section to `config.toml`, or set the matching `OIDC__*` +environment variables, to let users sign in through an OpenID Connect provider. +All options except `allowed_redirect_urls` are required. As an environment +variable, `allowed_redirect_urls` takes a comma-separated list, e.g. +`OIDC__ALLOWED_REDIRECT_URLS=myapp://oidc,https://app.example.com/oidc`. | Option | Description | Example | | ----------------------- | -------------------------------------------------------- | -------------------------- | diff --git a/src/config.rs b/src/config.rs index 5ef0f4d..5cf5abd 100644 --- a/src/config.rs +++ b/src/config.rs @@ -32,10 +32,31 @@ pub struct OidcConfig { pub app_url: String, /// Client URLs the OIDC flows may redirect back to. Login passes the session /// token to this URL, so only list clients you trust. - #[serde(default)] + #[serde(default, deserialize_with = "string_list")] pub allowed_redirect_urls: Vec, } +/// Accepts a list, or a comma-separated string so that lists can be set +/// through environment variables. +fn string_list<'de, D: serde::Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + #[derive(serde::Deserialize)] + #[serde(untagged)] + enum StringList { + List(Vec), + Joined(String), + } + + Ok(match serde::Deserialize::deserialize(deserializer)? { + StringList::List(list) => list, + StringList::Joined(joined) => joined + .split(',') + .map(str::trim) + .filter(|item| !item.is_empty()) + .map(str::to_owned) + .collect(), + }) +} + #[derive(serde::Deserialize, Clone)] pub struct Config { #[serde(rename = "secret_key")] @@ -81,7 +102,7 @@ impl Config { pub fn build() -> Result { config::Config::builder() .add_source(config::File::with_name("config").required(false)) - .add_source(config::Environment::with_convert_case(config::Case::Snake)) + .add_source(environment()) .build()? .try_deserialize() } @@ -160,6 +181,12 @@ impl Config { } } +/// Environment variable source. Nested options use `__` as the separator, +/// e.g. `OIDC__CLIENT_ID` for `oidc.client_id`. +fn environment() -> config::Environment { + config::Environment::with_convert_case(config::Case::Snake).separator("__") +} + /// Invalid config errors. #[derive(thiserror::Error, Debug, PartialEq, Eq)] pub enum ConfigValidationError { @@ -201,7 +228,7 @@ fn validate_secret(name: &'static str, secret: &str) -> Result<(), ConfigValidat #[cfg(test)] mod tests { - use super::{Config, ConfigValidationError, OidcConfig, validate_secret}; + use super::{Config, ConfigValidationError, OidcConfig, environment, validate_secret}; fn config_with(secret: &str, username_secret: Option<&str>) -> Config { Config { @@ -334,4 +361,86 @@ mod tests { }); assert!(config.validate().is_ok()); } + + /// Builds a config from the given variables instead of the process + /// environment, so tests cannot race on shared state. + fn config_from_env(vars: &[(&str, &str)]) -> Config { + let vars = vars + .iter() + .map(|(key, value)| ((*key).to_owned(), (*value).to_owned())) + .collect(); + config::Config::builder() + .add_source(environment().source(Some(vars))) + .build() + .unwrap() + .try_deserialize() + .unwrap() + } + + #[test] + fn environment_variables_configure_nested_oidc_options() { + let config = config_from_env(&[ + ("SECRET_KEY", STRONG), + ("DATABASE_URL", "./data/db.sqlite"), + ("ALLOW_REGISTRATION", "false"), + ("TRUSTED_PROXY_HOPS", "2"), + ("OIDC__PROVIDER_URL", "https://auth.example.com"), + ("OIDC__CLIENT_ID", "sync-server"), + ("OIDC__CLIENT_SECRET", STRONG), + ("OIDC__APP_URL", "https://sync.example.com"), + ( + "OIDC__ALLOWED_REDIRECT_URLS", + "myapp://oidc, https://app.example.com/oidc", + ), + ]); + + assert_eq!(config.secret, STRONG); + assert_eq!(config.database_url, "./data/db.sqlite"); + assert!(!config.allow_registration); + assert_eq!(config.trusted_proxy_hops, 2); + let oidc = config.oidc.expect("OIDC__* variables should set `oidc`"); + assert_eq!(oidc.provider_url, "https://auth.example.com"); + assert_eq!(oidc.client_id, "sync-server"); + assert_eq!(oidc.client_secret, STRONG); + assert_eq!(oidc.app_url, "https://sync.example.com"); + assert_eq!( + oidc.allowed_redirect_urls, + ["myapp://oidc", "https://app.example.com/oidc"] + ); + } + + /// Variables override single options of a `config.toml` section, and the + /// file's TOML array form for lists keeps working. + #[test] + fn environment_variables_override_the_config_file() { + let file = r#" + secret_key = "from-file" + database_url = "./db.sqlite" + + [oidc] + provider_url = "https://auth.example.com" + client_id = "from-file" + client_secret = "from-file" + app_url = "https://sync.example.com" + allowed_redirect_urls = ["myapp://oidc", "https://app.example.com/oidc"] + "#; + let vars = [("OIDC__CLIENT_ID".to_owned(), "from-env".to_owned())] + .into_iter() + .collect(); + let config: Config = config::Config::builder() + .add_source(config::File::from_str(file, config::FileFormat::Toml)) + .add_source(environment().source(Some(vars))) + .build() + .unwrap() + .try_deserialize() + .unwrap(); + + let oidc = config.oidc.unwrap(); + assert_eq!(oidc.client_id, "from-env"); + assert_eq!(oidc.client_secret, "from-file"); + assert_eq!( + oidc.allowed_redirect_urls, + ["myapp://oidc", "https://app.example.com/oidc"] + ); + } } From 849b0476b3ae6d5711c20ac0b174e9178f8716d7 Mon Sep 17 00:00:00 2001 From: D3SOX Date: Tue, 3 Nov 2026 13:47:32 +0100 Subject: [PATCH 2/3] docs(rate-limit): note that failed attempts count toward the limit --- src/rate_limit.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/rate_limit.rs b/src/rate_limit.rs index 1d9fc7f..fdd2a12 100644 --- a/src/rate_limit.rs +++ b/src/rate_limit.rs @@ -104,6 +104,9 @@ impl RateLimiter { } /// Record a request from `client` and report whether it is permitted. + /// + /// Every attempt counts, including failed ones. Counting only successful + /// requests would leave failed logins, and so password guessing, unlimited. pub fn check(&self, client: IpAddr) -> bool { let now = Instant::now(); let mut windows = self.windows.lock().expect("rate limiter mutex poisoned"); From 5b008a8dfff949df2e0e30824f1c162bec507d5e Mon Sep 17 00:00:00 2001 From: D3SOX Date: Tue, 3 Nov 2026 14:15:27 +0100 Subject: [PATCH 3/3] docs(readme): explain how to pass OIDC variables with Compose --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 36af712..12e8dfb 100644 --- a/README.md +++ b/README.md @@ -163,6 +163,8 @@ environment variables, to let users sign in through an OpenID Connect provider. All options except `allowed_redirect_urls` are required. As an environment variable, `allowed_redirect_urls` takes a comma-separated list, e.g. `OIDC__ALLOWED_REDIRECT_URLS=myapp://oidc,https://app.example.com/oidc`. +With the Compose quick start, add the variables to the service's `environment` +list. Compose doesn't pass entries in `.env` to the container on its own. | Option | Description | Example | | ----------------------- | -------------------------------------------------------- | -------------------------- |