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 ABF641FF0AB for ; Wed, 23 Sep 2026 17:09:12 +0200 (CEST) Received: from gate001.proxmox.com (localhost.localdomain [127.0.0.1]) by gate001.proxmox.com (Proxmox) with ESMTP id 47FE521834; Wed, 23 Sep 2026 17:07:08 +0200 (CEST) From: "Max R. Carrara" To: pve-devel@lists.proxmox.com Subject: [PATCH pve-storage v2 19/50] api: status: move up-/download file path parsing code into helper Date: Wed, 23 Sep 2026 17:05:33 +0200 Message-ID: <20260923150606.531239-20-m.carrara@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20260923150606.531239-1-m.carrara@proxmox.com> References: <20260923150606.531239-1-m.carrara@proxmox.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1790176006554 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.435 Adjusted score from AWL reputation of From: address DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment (newer systems) RCVD_IN_DNSWL_MED -2.3 Sender listed at https://www.dnswl.org/, medium trust SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Message-ID-Hash: 6KHKRBWVXOR6JT5O3C6K7E6MYJFD2WVF X-Message-ID-Hash: 6KHKRBWVXOR6JT5O3C6K7E6MYJFD2WVF X-MailFrom: m.carrara@proxmox.com 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 X-Mailman-Version: 3.3.10 Precedence: list List-Id: Proxmox VE development discussion List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: Create a separate helper for the common file path parsing parts of the upload / download_url API methods that returns the extension of the uploaded file. Break up the if-elsif chain along the way. Keep the inner if-clauses for the 'import' vtype inline for now for further simplifications later, but don't check whether we have the 'import' vtype before those anymore. This is fine because the 'ova' file extension is not used for files of any other vtype. Signed-off-by: Max R. Carrara --- src/PVE/API2/Storage/Status.pm | 85 ++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 40 deletions(-) diff --git a/src/PVE/API2/Storage/Status.pm b/src/PVE/API2/Storage/Status.pm index 09a37f4b..a9b4f798 100644 --- a/src/PVE/API2/Storage/Status.pm +++ b/src/PVE/API2/Storage/Status.pm @@ -69,6 +69,41 @@ my sub assert_can_transfer_files : prototype($$$) { return; } +my sub parse_transferred_file_path_extension : prototype($$) { + my ($path, $vtype) = @_; + + if ($vtype eq 'iso') { + if ($path !~ m![^/]+$PVE::Storage::ISO_EXT_RE_0$!) { + raise_param_exc({ filename => "wrong file extension" }); + } + + my $ext = $1; + return $ext; + } + + if ($vtype eq 'vztmpl') { + if ($path !~ m![^/]+$PVE::Storage::VZTMPL_EXT_RE_1$!) { + raise_param_exc({ filename => "wrong file extension" }); + } + + my $ext = $1; + return $ext; + } + + if ($vtype eq 'import') { + if ( + $path !~ m!${PVE::Storage::SAFE_CHAR_CLASS_RE}+$PVE::Storage::UPLOAD_IMPORT_EXT_RE_1$! + ) { + raise_param_exc({ filename => "invalid filename or wrong extension" }); + } + + my $ext = $1; + return $ext; + } + + die "upload / download: failed to parse '$path' - unhandled content type '$vtype'\n"; +} + my sub assert_ova_contents { my ($file) = @_; @@ -617,27 +652,12 @@ __PACKAGE__->register_method({ my $is_ova = 0; my $image_format; - if ($content eq 'iso') { - if ($filename !~ m![^/]+$PVE::Storage::ISO_EXT_RE_0$!) { - raise_param_exc({ filename => "wrong file extension" }); - } - } elsif ($content eq 'vztmpl') { - if ($filename !~ m![^/]+$PVE::Storage::VZTMPL_EXT_RE_1$!) { - raise_param_exc({ filename => "wrong file extension" }); - } - } elsif ($content eq 'import') { - if ($filename !~ - m!${PVE::Storage::SAFE_CHAR_CLASS_RE}+$PVE::Storage::UPLOAD_IMPORT_EXT_RE_1$! - ) { - raise_param_exc({ filename => "invalid filename or wrong extension" }); - } - my $format = $1; + my $extension = parse_transferred_file_path_extension($filename, $content); - if ($format eq 'ova') { - $is_ova = 1; - } else { - $image_format = $format; - } + if ($extension eq 'ova') { + $is_ova = 1; + } else { + $image_format = $extension; } my $path = plugin_get_vtype_subdir($scfg, $content); @@ -846,27 +866,12 @@ __PACKAGE__->register_method({ my $is_ova = 0; my $image_format; - if ($content eq 'iso') { - if ($filename !~ m![^/]+$PVE::Storage::ISO_EXT_RE_0$!) { - raise_param_exc({ filename => "wrong file extension" }); - } - } elsif ($content eq 'vztmpl') { - if ($filename !~ m![^/]+$PVE::Storage::VZTMPL_EXT_RE_1$!) { - raise_param_exc({ filename => "wrong file extension" }); - } - } elsif ($content eq 'import') { - if ($filename !~ - m!${PVE::Storage::SAFE_CHAR_CLASS_RE}+$PVE::Storage::UPLOAD_IMPORT_EXT_RE_1$! - ) { - raise_param_exc({ filename => "invalid filename or wrong extension" }); - } - my $format = $1; + my $extension = parse_transferred_file_path_extension($filename, $content); - if ($format eq 'ova') { - $is_ova = 1; - } else { - $image_format = $format; - } + if ($extension eq 'ova') { + $is_ova = 1; + } else { + $image_format = $extension; } my $path = plugin_get_vtype_subdir($scfg, $content); -- 2.47.3