[pve-devel] [RFC pve-storage 17/36] plugin: dir: factor path validity check into helper methods
Max Carrara
m.carrara at proxmox.com
Wed Jul 17 11:40:15 CEST 2024
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 <m.carrara at proxmox.com>
---
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<undef> if C<$path> is C<undef>, 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<may contain almost any sequence of bytes|https://lwn.net/Articles/71472/>,
+I<allowing> almost any sequence of bytes can lead to many unforeseen issues.
+See L<this|https://dwheeler.com/essays/fixing-unix-linux-filenames.html> for more
+information.
+
+Valid characters are the letters C<a-z> as well as their uppercase variants
+C<A-Z>, the numbers C<0-9> and the symbols C<->, C</>, C<_>, C<.> and C<@>.
+
+Will return C<undef> if C<$path> is C<undef>, 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<L</path_is_absolute>> C<&&> C<L</path_contains_valid_chars>>.
+
+Will return C<undef> if C<$path> is C<undef>, 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
More information about the pve-devel
mailing list