Skip to content

Commit

Permalink
WIP filter out non-utf8 env key/values
Browse files Browse the repository at this point in the history
  • Loading branch information
Jongy committed Feb 5, 2025
1 parent 370c939 commit aebb717
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/env.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::env;
use std::ffi::OsString;

#[cfg(feature = "convert-case")]
use convert_case::{Case, Casing};
Expand Down Expand Up @@ -243,6 +244,21 @@ impl Source for Environment {
.as_ref()
.map(|prefix| format!("{prefix}{prefix_separator}").to_lowercase());

let filter_utf8 = |(key, value): (OsString, OsString)| {
let key = if let Ok(key) = key.into_string() {
key
} else {
return None;
};
let value = if let Ok(value) = value.into_string() {
value
} else {
return None;
};

Some((key, value))
};

let collector = |(key, value): (String, String)| {
// Treat empty environment variables as unset
if self.ignore_empty && value.is_empty() {
Expand Down Expand Up @@ -312,7 +328,7 @@ impl Source for Environment {

match &self.source {
Some(source) => source.clone().into_iter().for_each(collector),
None => env::vars().for_each(collector),
None => env::vars_os().filter_map(filter_utf8).for_each(collector),
}

Ok(m)
Expand Down

0 comments on commit aebb717

Please sign in to comment.