Skip to content

The fastest, cheapest, most efficient open-source agent harness. Run more than 500 agents on a $10 VPS.

fix(config): reject non-finite floats before deserializing

Steven Enamakeld51e8e25e074
diff --git a/crates/openhuman-core/src/config/schema/load/parse.rs b/crates/openhuman-core/src/config/schema/load/parse.rs
index abd963d2f6..b812a9630b 100644
--- a/crates/openhuman-core/src/config/schema/load/parse.rs
+++ b/crates/openhuman-core/src/config/schema/load/parse.rs
@@ -14,6 +14,7 @@ use crate::config::Config;
#[inline(never)]
pub(crate) fn config_from_toml_str(contents: &str) -> anyhow::Result<Config> {
let document: toml::Value = toml::from_str(contents)?;
+ reject_non_finite_floats(&document, &mut String::new())?;
let json = serde_json::to_value(document)?;
serde_path_to_error::deserialize(json).map_err(|err| {
let path = err.path().to_string();
@@ -21,6 +22,38 @@ pub(crate) fn config_from_toml_str(contents: &str) -> anyhow::Result<Config> {
})
}
+/// JSON has no NaN or infinity: `serde_json::to_value` turns them into null,
+/// which an `Option<f64>` field would silently read as unset. No config field
+/// has a meaningful non-finite value, so refuse them up front with their path.
+fn reject_non_finite_floats(value: &toml::Value, path: &mut String) -> anyhow::Result<()> {
+ match value {
+ toml::Value::Float(f) if !f.is_finite() => {
+ anyhow::bail!("{path}: non-finite float {f} is not a valid config value")
+ }
+ toml::Value::Table(table) => {
+ for (key, child) in table {
+ let len = path.len();
+ if !path.is_empty() {
+ path.push('.');
+ }
+ path.push_str(key);
+ reject_non_finite_floats(child, path)?;
+ path.truncate(len);
+ }
+ }
+ toml::Value::Array(items) => {
+ for (index, child) in items.iter().enumerate() {
+ let len = path.len();
+ path.push_str(&format!("[{index}]"));
+ reject_non_finite_floats(child, path)?;
+ path.truncate(len);
+ }
+ }
+ _ => {}
+ }
+ Ok(())
+}
+
#[cfg(test)]
#[path = "parse_tests.rs"]
mod tests;
diff --git a/crates/openhuman-core/src/config/schema/load/parse_tests.rs b/crates/openhuman-core/src/config/schema/load/parse_tests.rs
index 27543d6f93..fba8043a02 100644
--- a/crates/openhuman-core/src/config/schema/load/parse_tests.rs
+++ b/crates/openhuman-core/src/config/schema/load/parse_tests.rs
@@ -57,3 +57,12 @@ fn a_non_finite_float_is_rejected_with_its_field_path() {
"{err}"
);
}
+
+#[test]
+fn a_non_finite_optional_float_is_rejected_not_unset() {
+ let err = config_from_toml_str("[agent]\nnested = { values = [1.0, inf] }").unwrap_err();
+ assert_eq!(
+ err.to_string(),
+ "agent.nested.values[1]: non-finite float inf is not a valid config value"
+ );
+}