From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gate001.proxmox.com (gate001.proxmox.com [IPv6:2a0f:8001:1:32::40]) by lore.proxmox.com (Postfix) with ESMTPS id 28D801FF0B6 for ; Thu, 12 Nov 2026 11:17:48 +0100 (CET) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id B17CE21622; Thu, 12 Nov 2026 11:17:47 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=stuttgart.de; s=s1022; t=1794478244; bh=R4qUb9j7OiwDO0zj9/tENoxbH7CYGn1nnyN89nnG0x8=; h=From:To:Cc:Subject:Date:From; b=mMgWxLD8kNnMj4IRU0kA6VMVVQtVuuj8GYPIqb9ZirYkmU6wXfdI+tJsl2EH1Bu3F GhaCkbb9n8QprwbIHrFGvvatPppZPib9sjE7j90acd6Pk5aofC+VMDvKoTOGtacb7m iaOIwAUSQxzO35TA4tF7p5lXUZiQnHWSnTCFCjxKDajlQbbRdlrOY0AGzegqXhCIzK Do1UGweTI/RazhMhwJq8/rWfNjKq5SAxkFcHgFZlSR2rutrITWL9rmFYVj9k3gy20K CrsbHj4QHSThkBTNzOFgRXPRFeJb/eZU5XXrcl87zAubReZCn6JxOeh8fooGZSUyUV RzQpeL5qFs6bQ== From: Jakob-Tobias Winter To: pbs-devel@lists.proxmox.com Subject: [PATCH proxmox-backup v2] bin: debug: api: fix panic on parameters given more than once Date: Thu, 12 Nov 2026 11:10:30 +0100 Message-ID: <20261112101029.726398-2-tobias@stuttgart.de> X-Mailer: git-send-email 2.53.0 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-SPAM-LEVEL: Spam detection results: 0 AWL -0.125 Adjusted score from AWL reputation of From: address DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain DMARC_PASS -0.1 DMARC pass policy POISEN_SPAM_PILL 0.1 Meta: its spam POISEN_SPAM_PILL_1 0.1 random spam to be learned in bayes POISEN_SPAM_PILL_3 0.1 random spam to be learned in bayes RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_PASS -0.001 SPF: HELO matches SPF record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 3QL3ZHYHMFUBSR4ZWV4IA7ACT3CC7OFL X-Message-ID-Hash: 3QL3ZHYHMFUBSR4ZWV4IA7ACT3CC7OFL X-MailFrom: prvs=f560d3371a=tobias@stuttgart.de X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; loop; banned-address; emergency; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header CC: Jakob-Tobias Winter X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox Backup Server development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Array parameters, for example 'group-filter', are set by giving the parameter once per value. The command line parser collects the values of a repeated parameter in an array, while the 'api' subcommands expected a single string for each parameter and panicked with "entered unreachable code". So array parameters could only be passed with one value. Hand each value over to the parameter verification separately. Array parameters now accept more than one value, and other parameters that are given more than once get rejected as duplicate instead of causing a panic. Anything other than a string or an array of strings cannot come from the command line parser. Such a value was the other way to reach that panic, and parameters that are no object at all were ignored. Return an error for both, and add tests for all of these cases. Signed-off-by: Jakob-Tobias Winter --- changes since v1, thanks to Christian for the review: * return an error for a value that is no string, where unreachable!() was left, and for parameters that are no object, which were ignored * add regression tests. Without the fix they fail: on master with the panic, on v1 for parameters that are no object. v1: https://lore.proxmox.com/pbs-devel/20261109215938.903397-1-tobias@stuttgart.de/ Reproducer: proxmox-backup-debug api create /config/tape-backup-job --id two \ --store store1 --pool pool1 --drive drive1 \ --group-filter type:vm --group-filter group:ct/200 Tested with release builds of current master (68577e654) and proxmox-backup-api and proxmox-backup-proxy running, over HTTP and in-process (PROXMOX_DEBUG_API_CODE=1): without the patch each call with a repeated parameter panics. With it array parameters given more than once reach the API, as JSON body (create) and as query string (set, get), and calls without a repeated parameter print the same as before. cargo test --workspace passes, cargo fmt --check and clippy report nothing new. src/bin/proxmox_backup_debug/api.rs | 138 ++++++++++++++++++++++++++-- 1 file changed, 128 insertions(+), 10 deletions(-) diff --git a/src/bin/proxmox_backup_debug/api.rs b/src/bin/proxmox_backup_debug/api.rs index 61cd1b7db..ec6701357 100644 --- a/src/bin/proxmox_backup_debug/api.rs +++ b/src/bin/proxmox_backup_debug/api.rs @@ -152,16 +152,24 @@ fn merge_parameters( .map(|(k, v)| (k.clone(), v.clone())) .collect(); - if let Some(Value::Object(map)) = param { - param_list.extend(map.into_iter().map(|(k, v)| { - ( - k, - match v { - Value::String(s) => s, - _ => unreachable!(), // we're in the CLI - }, - ) - })); + if let Some(param) = param { + let Value::Object(map) = param else { + bail!("expected the parameters as an object, got '{param}'"); + }; + for (key, value) in map { + // the command line parser collects the values of a parameter that is given + // more than once in an array + let values = match value { + Value::Array(values) => values, + value => vec![value], + }; + for value in values { + let Value::String(text) = value else { + bail!("parameter '{key}': expected a string, got '{value}'"); + }; + param_list.push((key.clone(), text)); + } + } } let params = schema.parse_parameter_strings(¶m_list, true)?; @@ -505,3 +513,113 @@ pub fn api_commands() -> CommandLineInterface { cmd_def.into() } + +#[cfg(test)] +mod tests { + use super::*; + + use proxmox_schema::{ArraySchema, ObjectSchema, StringSchema}; + + const PARAMETERS: ObjectSchema = ObjectSchema::new( + "Parameters of a made up API call.", + &[ + ( + "id", + false, + &StringSchema::new("Part of the path.").schema(), + ), + ("name", true, &StringSchema::new("A value.").schema()), + ( + "tag", + true, + &ArraySchema::new("A list.", &StringSchema::new("An item.").schema()).schema(), + ), + ], + ); + + /// Parse a command line like the 'api' subcommands do and return what is left for + /// the called API method + fn cli_param(args: &[&str]) -> Value { + let fixed_param = HashMap::from([("method", "get".to_string())]); + let (mut param, remaining) = parse_arguments( + args, + &["api-path"], + &fixed_param, + API_METHOD_API_CALL.parameters, + ) + .expect("parsing the command line failed"); + assert!(remaining.is_empty()); + + let map = param.as_object_mut().unwrap(); + map.remove("method"); + map.remove("api-path"); + param + } + + fn merge(param: Option) -> Result { + let uri_param = HashMap::from([("id".to_string(), "one".to_string())]); + merge_parameters(&uri_param, param, ParameterSchema::from(&PARAMETERS)) + } + + #[test] + fn test_merge_parameters() -> Result<(), Error> { + assert_eq!(merge(None)?, json!({ "id": "one" })); + assert_eq!(merge(Some(cli_param(&["/path"])))?, json!({ "id": "one" })); + + let param = cli_param(&["/path", "--name", "a", "--tag", "b"]); + assert_eq!( + merge(Some(param))?, + json!({ "id": "one", "name": "a", "tag": ["b"] }) + ); + + Ok(()) + } + + #[test] + fn test_merge_parameters_given_more_than_once() -> Result<(), Error> { + let param = cli_param(&["/path", "--tag", "a", "--tag", "b", "--tag", "c"]); + assert_eq!(param, json!({ "tag": ["a", "b", "c"] })); + assert_eq!( + merge(Some(param))?, + json!({ "id": "one", "tag": ["a", "b", "c"] }) + ); + + // only an array can take more than one value + let param = cli_param(&["/path", "--name", "a", "--name", "b"]); + let err = merge(Some(param)).expect_err("second value for a string should fail"); + assert!( + err.to_string().contains("'name': duplicate parameter"), + "unexpected error: {err}" + ); + + Ok(()) + } + + #[test] + fn test_merge_parameters_unexpected_input() { + // the command line parser produces none of these, they have to fail without panic + for param in [json!(null), json!("a"), json!(["a"])] { + let err = merge(Some(param)).expect_err("parameters that are no object should fail"); + assert!( + err.to_string() + .contains("expected the parameters as an object"), + "unexpected error: {err}" + ); + } + + for (name, param) in [ + ("name", json!({ "name": 1 })), + ("name", json!({ "name": null })), + ("name", json!({ "name": { "a": "b" } })), + ("tag", json!({ "tag": ["a", 1] })), + ("tag", json!({ "tag": [["a"]] })), + ] { + let err = merge(Some(param)).expect_err("value that is no string should fail"); + assert!( + err.to_string() + .contains(&format!("parameter '{name}': expected a string")), + "unexpected error: {err}" + ); + } + } +} -- 2.53.0