Skip to content

fix(config): read environment variables that are not UTF-8 without a panic - #1352

Open
LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/977-config-env-non-utf8
Open

LeeroyHannigan wants to merge 1 commit into
mainfrom
fix/977-config-env-non-utf8

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

What

extenddb serve now starts when a variable of its environment holds bytes that are not UTF-8 and its name does not start with EXTENDDB__ 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 through load or load_builtin_defaults.

Both functions in crates/config/src/lib.rs gave the config crate's Environment source no map of its own. The source then read the process environment with std::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 with std::env::vars_os() when the config crate builds the configuration, at the same point as 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.
  • An EXTENDDB__ variable whose name or value is not UTF-8 makes load and load_builtin_defaults return the error below, and extenddb serve stops with exit status 1. The error names the variable and omits its value. On main, the panic message printed the bytes of the value.
  • Two names can set one setting, because the config crate lowercases a name and reads __ as .. For example, EXTENDDB__SERVER__PORT, extenddb__server__port and EXTENDDB__SERVER.PORT all set server.port. The later one in the environment wins, as it did when the config crate read the environment itself.
Error: environment variable "EXTENDDB__SERVER__REGION" is not valid UTF-8; every EXTENDDB__ variable is a setting, and a setting must be UTF-8 text. Unset it, or give it a UTF-8 name and value.

The config crate takes the variables as one HashMap, because this workspace builds it without its preserve_order feature. Enabling that feature would also enable preserve_order in toml and serde_json for the whole build. A HashMap of all the variables would lose the order of the environment. So ProcessEnvironment gives 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 load returns this error, extenddb stop reads the PID file at its default path, as it does for any other error from load. extenddb status --port does the same when the port is in use. Without --port, extenddb status reads the port from the config file, so it stops with the error. On main, load panicked in each of these cases.

The admin guide's "Environment Variable Overrides" section gets two sentences on the error and on stop and status. 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 --foreground on 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.

Environment main this branch
another variable whose value is the byte 0xFF exit 101, called Result::unwrap() on an Err value: "\xFF" serves: /health 200, ListTables 200
another variable whose name ends in the byte 0xFF exit 101 serves
EXTENDDB__SERVER__REGION holds 0xFF, with and without --config exit 101 exit 1 and the error above
extenddb__server__region holds 0xFF exit 101 exit 1, the error names "extenddb__server__region"
the name EXTENDDB__SERVER__ followed by the byte 0xFF exit 101 exit 1, the error names "EXTENDDB__SERVER__\xFF"
another variable holds 0xFF, and EXTENDDB__SERVER__PORT=18978 exit 101 serves on port 18978
EXTENDDB__SERVER__PORT and extenddb__server__port, in either order the later one sets the port the same
EXTENDDB__SERVER.PORT and EXTENDDB__SERVER__PORT, in either order the later one sets the port the same
a malformed config file, and EXTENDDB__SERVER__REGION holds 0xFF exit 1, TOML parse error at line 1, column 8 the same

Closes #977

Testing done

  • cargo +1.97.0 test -p extenddb-config: 26 passed. collect_vars_keeps_extenddb_variables_and_refuses_them_when_not_utf8 holds 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 extenddb passed with the PostgreSQL backend, as cargo test --workspace builds it, and with --no-default-features --features sqlite-memory,dev-mode. crates/bin/tests/process_environment.rs sets two variables that are not UTF-8 and EXTENDDB__SERVER__PORT in its own process. Then it calls load_builtin_defaults and load, and asserts the port that each one reads. The test sets the variables with std::env::set_var, which is unsafe in edition 2024, so it is the only test in its binary. It is in the bin crate because load_builtin_defaults needs a storage backend, and the config crate has none. With the crates/config sources of main, the test panics in std::env::vars().
  • A probe prints the AppConfig or the error that load_builtin_defaults and load return. 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.
  • Each of these changes to the code, made one at a time, makes a test fail:
    • load_builtin_defaults or load reads the environment through the config crate again. The process test panics, on both backends for load_builtin_defaults.
    • collect_vars reads other variables as settings, or stops at the first one (break in place of continue).
    • collect_vars skips an EXTENDDB__ variable that is not UTF-8, in place of the refusal.
    • The prefix test is case-sensitive, ignores the separator, or reads one byte more than the prefix.
    • The merge keeps the earlier of two variables for one setting, or keeps only the last variable.
    • A kept variable loses its value.
    • The source reads no variables.
  • cargo +1.97.0 fmt --all -- --check is clean. cargo +1.97.0 clippy -p extenddb-config -p extenddb --all-targets -- -D warnings is clean, also with the Cassandra and MongoDB test-hooks features and on the dev-mode SQLite build. cargo +1.91.1 check -p extenddb-config -p extenddb --all-targets is clean.
  • -W clippy::pedantic reports nothing on the changed lines. The error formats the name with Debug and an allow for unnecessary_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:

Not in this PR, #1350: two readers outside the configuration loader treat a value that is not UTF-8 as unset.

  • The dev-mode key pair, EXTENDDB_DEV_ACCESS_KEY_ID and EXTENDDB_DEV_SECRET_ACCESS_KEY.
  • extenddb healthcheck without a config file, for EXTENDDB__SERVER__PORT and EXTENDDB__SERVER__BIND_ADDR.

Also not in this PR, #1351: that healthcheck reader matches EXTENDDB__SERVER__PORT in upper case only, where serve reads it in any letter case.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • 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: 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.

…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 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.

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.

Comment thread crates/config/src/lib.rs
}

fn collect(&self) -> Result<config::Map<String, config::Value>, config::ConfigError> {
collect_vars(std::env::vars_os())

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.

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.

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.

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.

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.

extenddb serve panics at startup when an environment variable holds non-UTF-8 bytes

2 participants