From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 669B31FF2C8 for ; Wed, 17 Jul 2024 11:42:26 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 9E7C238659; Wed, 17 Jul 2024 11:42:36 +0200 (CEST) From: Max Carrara To: pve-devel@lists.proxmox.com Date: Wed, 17 Jul 2024 11:40:15 +0200 Message-Id: <20240717094034.124857-18-m.carrara@proxmox.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20240717094034.124857-1-m.carrara@proxmox.com> References: <20240717094034.124857-1-m.carrara@proxmox.com> 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 SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pve-devel] [RFC pve-storage 17/36] plugin: dir: factor path validity check into helper methods X-BeenThere: pve-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox VE development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox VE development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pve-devel-bounces@lists.proxmox.com Sender: "pve-devel" Whether a directory-based storage's path is valid or not should not be solely decided within a method of the directoy plugin, but should instead be available to other plugins, possibly third-party plugins, as well. Therefore, factor that check into three different helper functions in `Common::Path`, so that they may be re-used by other plugins in the future. Document the helper functions as well. Signed-off-by: Max Carrara --- src/PVE/Storage/Common/Path.pm | 73 ++++++++++++++++++++++++++++++++++ src/PVE/Storage/DirPlugin.pm | 4 +- 2 files changed, 76 insertions(+), 1 deletion(-) diff --git a/src/PVE/Storage/Common/Path.pm b/src/PVE/Storage/Common/Path.pm index 7535dda..b9072bf 100644 --- a/src/PVE/Storage/Common/Path.pm +++ b/src/PVE/Storage/Common/Path.pm @@ -11,6 +11,9 @@ use parent qw(Exporter); our @EXPORT_OK = qw( path_is_mounted + path_is_absolute + path_contains_valid_chars + path_is_storage_dir ); =pod @@ -48,4 +51,74 @@ sub path_is_mounted { return undef; } +=pod + +=head3 path_is_absolute + + $result = path_is_absolute($path) + +Checks whether a C<$path> is absolute. + +Will return C if C<$path> is C, or a boolean otherwise. + +=cut + +sub path_is_absolute : prototype($) { + my ($path) = @_; + + return undef if !defined($path); + + return ($path =~ m|^/|) + 0; # convert to number +} + +=pod + +=head3 path_contains_valid_chars + + $result = path_contains_valid_chars($path) + +Checks whether a C<$path> contains only valid characters. + +"Valid" in this context means "the characters that we allow". While Unix/Linux/POSIX +paths L, +I almost any sequence of bytes can lead to many unforeseen issues. +See L for more +information. + +Valid characters are the letters C as well as their uppercase variants +C, the numbers C<0-9> and the symbols C<->, C, C<_>, C<.> and C<@>. + +Will return C if C<$path> is C, or a boolean otherwise. + +=cut + +sub path_contains_valid_chars : prototype($) { + my ($path) = @_; + + return undef if !defined($path); + + return ($path =~ m|[-/a-zA-Z0-9_.@]+|) + 0; # convert to number +} + + +=pod + +=head3 path_is_storage_dir + + $result = path_is_storage_dir($path) + +Shorthand for C> C<&&> C>. + +Will return C if C<$path> is C, or a boolean otherwise. + +=cut + +sub path_is_storage_dir : prototype($) { + my ($path) = @_; + + return undef if !defined($path); + + return path_is_absolute($path) && path_contains_valid_chars($path); +} + 1; diff --git a/src/PVE/Storage/DirPlugin.pm b/src/PVE/Storage/DirPlugin.pm index f6e1d73..4be39f9 100644 --- a/src/PVE/Storage/DirPlugin.pm +++ b/src/PVE/Storage/DirPlugin.pm @@ -187,9 +187,11 @@ sub check_config { my ($self, $sectionId, $config, $create, $skipSchemaCheck) = @_; my $opts = PVE::SectionConfig::check_config($self, $sectionId, $config, $create, $skipSchemaCheck); return $opts if !$create; - if ($opts->{path} !~ m|^/[-/a-zA-Z0-9_.@]+$|) { + + if (!PVE::Storage::Common::Path::path_is_storage_dir($opts->{path})) { die "illegal path for directory storage: $opts->{path}\n"; } + return $opts; } -- 2.39.2 _______________________________________________ pve-devel mailing list pve-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pve-devel