From 3cb7b9377af4743d1024b438110910bd1dcae501 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 00:00:42 +0100 Subject: [PATCH 1/2] fix(manifest): redact map keys in decode paths even when they match a field name safe_decode_path kept any segment found in its field list, so a map key such as an environment variable named `image` stayed in decode error paths. It now stops after a map field (`args`, `environment`, `environments`, `variables`), so the next segment, a user-supplied key, is always redacted. The schema-driven test now also checks that every map property in the manifest schema redacts the key after it, so a new map can't leak. Fixes PQD-142 --- .../src/manifest/validation/mod.rs | 15 ++++-- crates/piqueld-core/tests/manifests.rs | 48 +++++++++++++++++-- 2 files changed, 54 insertions(+), 9 deletions(-) diff --git a/crates/piqueld-core/src/manifest/validation/mod.rs b/crates/piqueld-core/src/manifest/validation/mod.rs index 4df5f7c45..50fdb9129 100644 --- a/crates/piqueld-core/src/manifest/validation/mod.rs +++ b/crates/piqueld-core/src/manifest/validation/mod.rs @@ -72,12 +72,14 @@ pub fn parse_json(input: &str) -> Result /// Redacts map keys and unknown components from a serde decode path. /// /// Keeps the longest prefix made of known manifest field names with numeric -/// indices, so user-supplied map keys (e.g. environment names) never leak. +/// indices, ending at the first map field, so user-supplied map keys (e.g. +/// environment names) never leak, even when they match a field name. /// /// ```text -/// "spec.services[0].replicas" -> "spec.services[0].replicas" -/// "spec.services[0].environment.KEY" -> "spec.services[0].environment" -/// "bogus.field" -> "$" +/// "spec.services[0].replicas" -> "spec.services[0].replicas" +/// "spec.services[0].environment.KEY" -> "spec.services[0].environment" +/// "spec.services[0].environment.image" -> "spec.services[0].environment" +/// "bogus.field" -> "$" /// ``` #[must_use] pub fn safe_decode_path(path: &str) -> String { @@ -155,6 +157,8 @@ pub fn safe_decode_path(path: &str) -> String { "stop_service", "receives_telemetry", ]; + // Fields whose values are maps with user-supplied keys. + const MAPS: &[&str] = &["args", "environment", "environments", "variables"]; let mut safe = Vec::new(); for component in path.split('.') { let field_end = component.find('[').unwrap_or(component.len()); @@ -163,6 +167,9 @@ pub fn safe_decode_path(path: &str) -> String { break; } safe.push(component); + if MAPS.contains(&field) { + break; + } } if safe.is_empty() { "$".into() diff --git a/crates/piqueld-core/tests/manifests.rs b/crates/piqueld-core/tests/manifests.rs index af3b010f3..0c4215db7 100644 --- a/crates/piqueld-core/tests/manifests.rs +++ b/crates/piqueld-core/tests/manifests.rs @@ -254,14 +254,22 @@ image = "nginx:1.27" } /// Every property name in the manifest JSON schema survives decode path -/// redaction, so a new manifest field can't truncate its decode errors. +/// redaction, so a new manifest field can't truncate its decode errors, and +/// the key after every map property is redacted, so a new map can't leak its +/// keys. #[test] fn decode_paths_keep_every_manifest_field() { - fn property_names(schema: &serde_json::Value, names: &mut Vec) { + /// Collects each property's name and whether its value is a map. + fn property_names(schema: &serde_json::Value, names: &mut Vec<(String, bool)>) { match schema { serde_json::Value::Object(object) => { if let Some(serde_json::Value::Object(properties)) = object.get("properties") { - names.extend(properties.keys().cloned()); + names.extend(properties.iter().map(|(name, property)| { + let map = property + .get("additionalProperties") + .is_some_and(serde_json::Value::is_object); + (name.clone(), map) + })); } for value in object.values() { property_names(value, names); @@ -280,15 +288,27 @@ fn decode_paths_keep_every_manifest_field() { &piqueld_core::manifest::ApplicationManifest::json_schema(), &mut names, ); - assert!(names.iter().any(|name| name == "stop_service")); + assert!(names.iter().any(|(name, _)| name == "stop_service")); + assert!( + names + .iter() + .any(|(name, map)| name == "environment" && *map) + ); let redacted = names .iter() - .filter(|name| piqueld_core::manifest::safe_decode_path(name) != **name) + .filter(|(name, _)| piqueld_core::manifest::safe_decode_path(name) != *name) .collect::>(); assert!( redacted.is_empty(), "redacted manifest fields: {redacted:?}" ); + let leaking = names + .iter() + .filter(|(name, map)| { + *map && piqueld_core::manifest::safe_decode_path(&format!("{name}.image")) != *name + }) + .collect::>(); + assert!(leaking.is_empty(), "maps keeping their keys: {leaking:?}"); } #[test] @@ -312,6 +332,24 @@ stop_service = "yes" assert_eq!(error.path, "spec.jobs[0].stop_service"); } +/// A map key is redacted from decode errors even when it matches a field name. +#[test] +fn decode_errors_redact_map_keys_named_like_fields() { + let manifest = format!( + r#"{} +[spec.services.environment] +image = ["notes"] +"#, + valid_manifest("notes") + ); + let error = parse_toml(&manifest).unwrap_err(); + let [error] = error.0.as_slice() else { + panic!("expected one decode error: {error}"); + }; + assert_eq!(error.code, ApiErrorCode::ManifestDecodeFailed); + assert_eq!(error.path, "spec.services[0].environment"); +} + #[test] fn malformed_and_invalid_manifests_return_safe_validation_errors() { let error = parse_toml(include_str!("fixtures/manifests/invalid.toml")).unwrap_err(); From 4c8f42d9bf1ec3449262d0c5235b386edf4b5a10 Mon Sep 17 00:00:00 2001 From: piquel Date: Sun, 22 Nov 2026 00:32:17 +0100 Subject: [PATCH 2/2] test(manifest): count a map behind a schema reference as a map --- crates/piqueld-core/tests/manifests.rs | 56 ++++++++++++++++++++------ 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/crates/piqueld-core/tests/manifests.rs b/crates/piqueld-core/tests/manifests.rs index 0c4215db7..954a729e6 100644 --- a/crates/piqueld-core/tests/manifests.rs +++ b/crates/piqueld-core/tests/manifests.rs @@ -259,35 +259,65 @@ image = "nginx:1.27" /// keys. #[test] fn decode_paths_keep_every_manifest_field() { + /// Whether `schema` is a map, following references into `root` and + /// through `allOf`, `oneOf` and `anyOf`, so a map behind a named type + /// counts too. + fn is_map(schema: &serde_json::Value, root: &serde_json::Value) -> bool { + if schema + .get("additionalProperties") + .is_some_and(serde_json::Value::is_object) + { + return true; + } + if let Some(reference) = schema.get("$ref").and_then(serde_json::Value::as_str) { + return reference + .strip_prefix('#') + .and_then(|pointer| root.pointer(pointer)) + .is_some_and(|target| is_map(target, root)); + } + ["allOf", "oneOf", "anyOf"].iter().any(|key| { + schema + .get(key) + .and_then(serde_json::Value::as_array) + .is_some_and(|schemas| schemas.iter().any(|schema| is_map(schema, root))) + }) + } /// Collects each property's name and whether its value is a map. - fn property_names(schema: &serde_json::Value, names: &mut Vec<(String, bool)>) { + fn property_names( + schema: &serde_json::Value, + root: &serde_json::Value, + names: &mut Vec<(String, bool)>, + ) { match schema { serde_json::Value::Object(object) => { if let Some(serde_json::Value::Object(properties)) = object.get("properties") { - names.extend(properties.iter().map(|(name, property)| { - let map = property - .get("additionalProperties") - .is_some_and(serde_json::Value::is_object); - (name.clone(), map) - })); + names.extend( + properties + .iter() + .map(|(name, property)| (name.clone(), is_map(property, root))), + ); } for value in object.values() { - property_names(value, names); + property_names(value, root, names); } } serde_json::Value::Array(items) => { for item in items { - property_names(item, names); + property_names(item, root, names); } } _ => {} } } + // A map behind a reference, as a map newtype's schema is, counts. + let definitions = serde_json::json!({"defs": {"Map": {"additionalProperties": {}}}}); + assert!(is_map( + &serde_json::json!({"$ref": "#/defs/Map"}), + &definitions + )); + let schema = piqueld_core::manifest::ApplicationManifest::json_schema(); let mut names = Vec::new(); - property_names( - &piqueld_core::manifest::ApplicationManifest::json_schema(), - &mut names, - ); + property_names(&schema, &schema, &mut names); assert!(names.iter().any(|(name, _)| name == "stop_service")); assert!( names