From: Jakob-Tobias Winter <tobias@stuttgart.de>
To: pbs-devel@lists.proxmox.com
Cc: Jakob-Tobias Winter <tobias@stuttgart.de>
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 [thread overview]
Message-ID: <20261112101029.726398-2-tobias@stuttgart.de> (raw)
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 <tobias@stuttgart.de>
---
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<Value>) -> Result<Value, Error> {
+ 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
next reply other threads:[~2026-11-12 10:17 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-12 10:10 Jakob-Tobias Winter [this message]
2026-11-13 8:56 ` [PATCH proxmox-backup v2] bin: debug: api: fix panic on parameters given more than once Christian Ebner
2026-11-14 21:27 ` applied: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261112101029.726398-2-tobias@stuttgart.de \
--to=tobias@stuttgart.de \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox