From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68]) by lore.proxmox.com (Postfix) with ESMTPS id 28BFA1FF15C for ; Fri, 28 Nov 2025 14:01:07 +0100 (CET) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 062A51A994; Fri, 28 Nov 2025 14:01:28 +0100 (CET) From: Dominik Csapak To: pdm-devel@lists.proxmox.com Date: Fri, 28 Nov 2025 13:59:55 +0100 Message-ID: <20251128130124.2738745-1-d.csapak@proxmox.com> X-Mailer: git-send-email 2.47.3 MIME-Version: 1.0 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.030 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment RCVD_IN_VALIDITY_CERTIFIED_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_RPBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. RCVD_IN_VALIDITY_SAFE_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to Validity was blocked. See https://knowledge.validity.com/hc/en-us/articles/20961730681243 for more information. SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [RFC PATCH proxmox] pve api types: percent encode string params in url X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" when we generate an url for the pve api from something like "/some/{foo}/thing/{bar}" then the url we generate will be wrapped in a `format!` and the variables `foo` and `bar` will be inserted into the url. Sometimes, these parameters can contain invalid characters for a url and we have to percent encode them. To do that, we convert the url to one with unnamed parameters and add them with percent encoding. The order of the url_params is the same as in the url, so we don't have to to a lookup or similar, simply iterating over that is enough. This fixes an issue with UPIDs that contain the character '>'. (e.g. move disk from one vm to another). Signed-off-by: Dominik Csapak --- sending as RFC, because there is a (not so generic) alternative: we could simply percent encode the upid ourselves in pdm, whenever we call the pve api. but since this wasn't too hard to do and everything seems to work in my (albeit short) tests, I wanted to send it and gather feedback. pve-api-types/generator-lib/Schema2Rust.pm | 29 +++++++++++++++++++++- pve-api-types/src/client/code.rs | 2 ++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/pve-api-types/generator-lib/Schema2Rust.pm b/pve-api-types/generator-lib/Schema2Rust.pm index ab1c1700..d12c1525 100644 --- a/pve-api-types/generator-lib/Schema2Rust.pm +++ b/pve-api-types/generator-lib/Schema2Rust.pm @@ -457,7 +457,27 @@ my sub return_expr : prototype($$) ($def, $expr) { my sub format_url : prototype($;$) ($def, $as_ref = 0) { if (defined($def->{url_params}) && $def->{url_params}->@*) { $as_ref = $as_ref ? '&' : ''; - return "${as_ref}format!(\"/api2/extjs$def->{url}\")"; + + my $url_with_unnamed_params = url_with_unnamed_params($def->{url}); + + my $url = "${as_ref}format!(\"/api2/extjs${url_with_unnamed_params}\""; + + # we have to percent encode string parameter in the url + for my $url_arg ($def->{url_params}->@*) { + my ($arg, $def) = @$url_arg; + my $name = $def->{rust_name}; + my $type = $def->{type}; + + if ($type eq '&str') { + $url .= ",percent_encode(${name}.as_bytes(), percent_encoding::NON_ALPHANUMERIC)"; + } elsif ($type =~ /^u8|u16|u32|u64|u128|i8|i16|i32|i64|i128|usize|isize|f32|f64$/) { + $url .= ",${name}"; + } else { + $url .= ",percent_encode(${name}.to_string().as_bytes(), percent_encoding::NON_ALPHANUMERIC)"; + } + } + + $url .= ")"; } else { return "\"/api2/extjs$def->{url}\""; } @@ -1617,6 +1637,13 @@ my sub url_parameters : prototype($) { return \@params; } +# remove named params from the url +sub url_with_unnamed_params : prototype($) { + my ($path) = @_; + $path =~ s/\{([^}]+)\}/\{\}/g; + return $path; +} + ### Extract method parameters and deal with path based parameters. # $api_method is the dumped schema's method definition. my sub method_parameters : prototype($$$$$) { diff --git a/pve-api-types/src/client/code.rs b/pve-api-types/src/client/code.rs index ff206ca0..bd6c8ff1 100644 --- a/pve-api-types/src/client/code.rs +++ b/pve-api-types/src/client/code.rs @@ -2,6 +2,8 @@ #![allow(unused)] //! The generated API client code. +use percent_encoding::percent_encode; + use proxmox_client::{ApiPathBuilder, ApiResponseData, Error, HttpApiClient}; use crate::types::*; -- 2.47.3 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel