Repository navigation
fix(config): read environment variables that are not UTF-8 without a panic - #1352
LeeroyHannigan wants to merge 1 commit into
Conversation
…panic load and load_builtin_defaults gave the config crate's Environment source no map of its own, so it read the process environment with std::env::vars(). That call panics on the first name or value that is not UTF-8. Every command that loads the configuration exited 101 when any variable of the process held such bytes, whatever its name. Both loaders now add one source, ProcessEnvironment. It reads the environment with std::env::vars_os() when the config crate builds the configuration, at the point where the crate read it before. It gives each variable that ExtendDB reads to environment() as a map of one entry, and merges the results in environment order: - It skips a variable whose name does not start with EXTENDDB__, so its name and value can hold any bytes. The prefix test ignores letter case, as the config crate's test does. - For an EXTENDDB__ variable whose name or value is not UTF-8, the load returns an error. The error names the variable and omits its value. - Two names can set one setting when they differ in letter case, or in `.` against `__`. The config crate keys each variable by its own rule. The later variable in the environment wins, as it did when the crate read the environment itself. environment() now takes its map with no Option. With None, the config crate read the environment with std::env::vars(). Fixes #977
demohan
left a comment
There was a problem hiding this comment.
No command panics on a variable that is not UTF-8 at cd26248. Across 15 environments and 14 commands on main and this head (bad bytes in another variable's value or name, in an EXTENDDB__ value, name, lowercase name or the bare prefix, in a secret connection string, HOME, the XDG variables, AWS_, USER, RUST_LOG, TERM, LANG and single-underscore names), main exits 101 everywhere but version, and this head runs normally for variables outside the prefix (serve answers /health 200) and exits 1 with the documented message for a bad EXTENDDB__ variable, naming it with its bytes escaped and never printing the value, where main's panic printed the secret. init, destroy, status --port, the PostgreSQL binary with bad PG* variables and an S3 store with bad AWS_ variables behave the same way, UTF-8 environments answer as on main, and the one env::vars() call in the workspace and the binary's runtime dependencies is the one this replaces. 9 of 10 negative controls fail as they should, fmt, clippy and the 1.91.1 check are clean, CI is green on 58 of 58, and the branch merges with main at 222d80a, where the tests and the 1.94.0 check pass.
I will approve once a test holds the loaders' refusal.
| } | ||
|
|
||
| fn collect(&self) -> Result<config::Map<String, config::Value>, config::ConfigError> { | ||
| collect_vars(std::env::vars_os()) |
There was a problem hiding this comment.
No test holds this error reaching the caller: with it swallowed (Ok(collect_vars(..).unwrap_or_default())), all 26 config tests and the process test pass, and a refused variable would then drop every environment override without a word. Setting EXTENDDB__SERVER__REGION to \xff after the asserts in the existing test, and checking that load and load_builtin_defaults both return an error that names the variable and leaves out its value, would hold it.
|
|
||
| Precedence: CLI flags > environment variables > config file > defaults. | ||
|
|
||
| When the name or value of an `EXTENDDB__` variable is not UTF-8, `extenddb serve` and the other commands that load the configuration stop with an error that names the variable. `extenddb stop`, and `extenddb status --port` when that port is in use, read the default PID file in that case, as they do for any configuration error. |
There was a problem hiding this comment.
stop in that case also uses the default port, 18443, and prints nothing about the refused variable: with a config on port 27406 and EXTENDDB__SERVER__REGION=\xff, this head printed extenddb stopped (pid N), exited 0 and signalled the process named in the default port's PID file, where main panicked and signalled nothing. Main does the same for any configuration error (EXTENDDB__SERVER__PORT=abc), so this is older than the change, but the sentence could say that the default port is used and that no error is shown.
What
extenddb servenow starts when a variable of its environment holds bytes that are not UTF-8 and its name does not start withEXTENDDB__in any letter case. Before this change, such a variable stopped the command at startup with a panic and exit status 101. The same holds for every command that loads the configuration throughloadorload_builtin_defaults.Both functions in
crates/config/src/lib.rsgave the config crate'sEnvironmentsource no map of its own. The source then read the process environment withstd::env::vars(), which panics on the first name or value that is not UTF-8. Both functions now add one source,ProcessEnvironment. It reads the environment withstd::env::vars_os()when the config crate builds the configuration, at the same point as before. It gives each variable that ExtendDB reads toenvironment()as a map of one entry, and merges the results in environment order:EXTENDDB__, so its name and value can hold any bytes. The prefix test ignores letter case, as the config crate's test does.EXTENDDB__variable whose name or value is not UTF-8 makesloadandload_builtin_defaultsreturn the error below, andextenddb servestops with exit status 1. The error names the variable and omits its value. On main, the panic message printed the bytes of the value.__as.. For example,EXTENDDB__SERVER__PORT,extenddb__server__portandEXTENDDB__SERVER.PORTall setserver.port. The later one in the environment wins, as it did when the config crate read the environment itself.The config crate takes the variables as one
HashMap, because this workspace builds it without itspreserve_orderfeature. Enabling that feature would also enablepreserve_orderintomlandserde_jsonfor the whole build. AHashMapof all the variables would lose the order of the environment. SoProcessEnvironmentgives the crate one map per variable and merges the results in order. The crate applies its own key rule (lowercase, and__read as.) to each variable, and the later of two names for one setting wins. ExtendDB keeps no copy of that key rule.When
loadreturns this error,extenddb stopreads the PID file at its default path, as it does for any other error fromload.extenddb status --portdoes the same when the port is in use. Without--port,extenddb statusreads the port from the config file, so it stops with the error. On main,loadpanicked in each of these cases.The admin guide's "Environment Variable Overrides" section gets two sentences on the error and on
stopandstatus. The upgrade manual needs no entry. A deployment that starts on main has no variable that is not UTF-8, so it starts the same way with this change.Why
These are the answers of
serve --foregroundon 127.0.0.1, with the dev-mode SQLite binary (--features sqlite-memory,dev-mode) of main (8c64333) and of this branch (cd26248). Each case ran twice on main and once on this branch on 21 November 2026. The two runs on main gave the same answer.called Result::unwrap() on an Err value: "\xFF"/health200, ListTables 200EXTENDDB__SERVER__REGIONholds 0xFF, with and without--configextenddb__server__regionholds 0xFF"extenddb__server__region"EXTENDDB__SERVER__followed by the byte 0xFF"EXTENDDB__SERVER__\xFF"EXTENDDB__SERVER__PORT=18978EXTENDDB__SERVER__PORTandextenddb__server__port, in either orderEXTENDDB__SERVER.PORTandEXTENDDB__SERVER__PORT, in either orderEXTENDDB__SERVER__REGIONholds 0xFFTOML parse error at line 1, column 8Closes #977
Testing done
cargo +1.97.0 test -p extenddb-config: 26 passed.collect_vars_keeps_extenddb_variables_and_refuses_them_when_not_utf8holds 13 environments and compares the keys and values that the config crate reads from each. They include other variables with a name or value that is not UTF-8, and two names that are close to the prefix. They also include settings in upper and lower case, and the two refusals with their full error text. The last rows hold two settings, and two pairs of names for one setting.cargo +1.97.0 test -p extenddbpassed with the PostgreSQL backend, ascargo test --workspacebuilds it, and with--no-default-features --features sqlite-memory,dev-mode.crates/bin/tests/process_environment.rssets two variables that are not UTF-8 andEXTENDDB__SERVER__PORTin its own process. Then it callsload_builtin_defaultsandload, and asserts the port that each one reads. The test sets the variables withstd::env::set_var, which isunsafein edition 2024, so it is the only test in its binary. It is in the bin crate becauseload_builtin_defaultsneeds a storage backend, and the config crate has none. With thecrates/configsources of main, the test panics instd::env::vars().AppConfigor the error thatload_builtin_defaultsandloadreturn. It gave the same output on main and on this branch for 37 environments that are all UTF-8. They include unknown keys, three names for one setting, names with characters outside ASCII, and a malformed config file.load_builtin_defaultsorloadreads the environment through the config crate again. The process test panics, on both backends forload_builtin_defaults.collect_varsreads other variables as settings, or stops at the first one (breakin place ofcontinue).collect_varsskips anEXTENDDB__variable that is not UTF-8, in place of the refusal.cargo +1.97.0 fmt --all -- --checkis clean.cargo +1.97.0 clippy -p extenddb-config -p extenddb --all-targets -- -D warningsis clean, also with the Cassandra and MongoDBtest-hooksfeatures and on the dev-mode SQLite build.cargo +1.91.1 check -p extenddb-config -p extenddb --all-targetsis clean.-W clippy::pedanticreports nothing on the changed lines. The error formats the name withDebugand anallowforunnecessary_debug_formatting, because that form quotes the name and escapes its bytes that are not UTF-8.Line counts: production +69 -14, doc comments included. Tests +147 -1. Docs +2.
Other open PRs:
crates/config/src/lib.rstocrates/config/src/tests.rs, so the merge conflicts in themod testsblock. The new test moves to that file. fix(config): environment overrides for every backend key; Cassandra refuses unknown keys #546 also adds a callenvironment(Some(env))in that file. This change removes theOptionfromenvironment, because withNonethe config crate read the environment withstd::env::vars(), the call that panics. After the merge, that call becomesenvironment(env), in the file that the conflict resolution already edits.crates/config/src/lib.rsor the admin guide merge with this branch with no new conflict.Not in this PR, #1350: two readers outside the configuration loader treat a value that is not UTF-8 as unset.
EXTENDDB_DEV_ACCESS_KEY_IDandEXTENDDB_DEV_SECRET_ACCESS_KEY.extenddb healthcheckwithout a config file, forEXTENDDB__SERVER__PORTandEXTENDDB__SERVER__BIND_ADDR.Also not in this PR, #1351: that healthcheck reader matches
EXTENDDB__SERVER__PORTin upper case only, whereservereads it in any letter case.Checklist
cargo test --workspace)cargo fmt --check)cargo clippy -- -W clippy::pedantic)Storagetrait, auth model, on-diskformat, or public CLI surface, an RFC has been accepted or is linked
below. Otherwise, an ADR captures the decision (link below).
ADR / RFC: n/a
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.