Skip to content

Commit 7e7a3b4

Browse files
authored
Merge pull request #2952 from ehuss/ignore-top-env-keys
Ignore invalid top-level environment variable config keys
2 parents 8385e75 + d39deca commit 7e7a3b4

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

crates/mdbook-core/src/config.rs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
//! # run().unwrap()
4444
//! ```
4545
46+
use crate::static_regex;
4647
use crate::utils::{TomlExt, fs, log_backtrace};
4748
use anyhow::{Context, Error, Result, bail};
4849
use serde::{Deserialize, Serialize};
@@ -149,15 +150,23 @@ impl Config {
149150
pub fn update_from_env(&mut self) -> Result<()> {
150151
debug!("Updating the config from environment variables");
151152

153+
static_regex!(
154+
VALID_KEY,
155+
r"^(:?book|build|rust|output|preprocessor)(:?$|\.)"
156+
);
157+
152158
let overrides =
153159
env::vars().filter_map(|(key, value)| parse_env(&key).map(|index| (index, value)));
154160

155161
for (key, value) in overrides {
156-
if key == "log" {
157-
// MDBOOK_LOG is used to control logging.
162+
trace!("{} => {}", key, value);
163+
if !VALID_KEY.is_match(&key) {
164+
// Ignore environment variables for other top-level things.
165+
// This allows users to set things like `MDBOOK_VERSION` or
166+
// `MDBOOK_DOWNLOAD_URL` for their own scripts and not
167+
// interfere with how the config is loaded.
158168
continue;
159169
}
160-
trace!("{} => {}", key, value);
161170
let parsed_value = serde_json::from_str(&value)
162171
.unwrap_or_else(|_| serde_json::Value::String(value.to_string()));
163172

tests/testsuite/config.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,11 @@ unknown field `title`, expected `edition`
213213
fn env_invalid_config_key() {
214214
BookTest::from_dir("config/empty").run("build", |cmd| {
215215
cmd.env("MDBOOK_FOO", "testing")
216-
.expect_failure()
217216
.expect_stdout(str![[""]])
218217
.expect_stderr(str![[r#"
219-
ERROR invalid key `foo`
218+
INFO Book building has started
219+
INFO Running the html backend
220+
INFO HTML book written to `[ROOT]/book`
220221
221222
"#]]);
222223
});

0 commit comments

Comments
 (0)