Answer#
Failing at start-up is the cheapest place to find a configuration mistake: the operator sees it at once, and no user is ever served by a half-configured service. The opposite, a silent default, turns a typing mistake into a security hole that nobody notices.
Write one validate function that turns the raw values into a Settings struct, or a list of everything that is wrong. Only the validated struct is passed on to the rest of the program.
const PLACEHOLDERS: [&str; 3] = ["changeme", "secret", "password"];
struct Settings {
signing_key: String,
}
/// Collect every problem, so one start-up attempt reports all of them.
fn validate(signing_key: Option<&str>) -> Result<Settings, Vec<String>> {
let mut problems = Vec::new();
match signing_key {
None => problems.push("SIGNING_KEY is not set".to_string()),
Some(k) => {
if PLACEHOLDERS.contains(&k.to_lowercase().as_str()) {
problems.push("SIGNING_KEY is a placeholder value".to_string());
}
if k.len() < 32 {
problems.push("SIGNING_KEY must be at least 32 bytes".to_string());
}
}
}
match signing_key {
Some(k) if problems.is_empty() => Ok(Settings { signing_key: k.to_string() }),
_ => Err(problems),
}
}
fn main() {
for input in [None, Some("changeme"), Some("0123456789abcdef0123456789abcdef")] {
match validate(input) {
Ok(s) => println!("ok, key is {} bytes", s.signing_key.len()),
Err(list) => println!("refusing to start: {}", list.join("; ")),
}
}
}refusing to start: SIGNING_KEY is not set
refusing to start: SIGNING_KEY is a placeholder value; SIGNING_KEY must be at least 32 bytes
ok, key is 32 bytesThe messages name the setting and the rule. They never print the value that was given.
How to do it#
- Decide minimum rules for each secret: present, minimum length, not on a short list of placeholders.
- Run the validation before opening any port or database connection.
- Print the problems to standard error and exit with a non-zero status (
std::process::exit(1)). - Generate real keys with a random generator, not by typing them; see the thread on random tokens and keys.
Common mistakes#
- Length checks only. A 40-character
aaaa...passes. A length rule catches typos, not weak choices; generate secrets, do not invent them. - Validating only some settings and reading others later with
unwrap. A missing value then panics in the middle of a request. - Logging the rejected value "to help". Name the setting; keep the value out of the log.
Related questions#
Should development and production use different rules?#
They can differ in where the value comes from, not in whether it is checked. A program that skips checks in development tends to ship that mode by accident.
Can a missing optional setting have a default?#
Yes, for settings that are not security relevant (a port, a page size). Secrets, signing keys and database passwords should never have a default.
Sources#
- OWASP Secrets Management Cheat Sheet: https://cheatsheetseries.owasp.org/cheatsheets/Secrets_Management_Cheat_Sheet.html
- The Twelve-Factor App, "Config": https://12factor.net/config