From 31213d61d348d74bb8af9ff30f3b7b6b6a8355b6 Mon Sep 17 00:00:00 2001 From: Fabian Ebner Date: Wed, 27 Apr 2022 17:41:16 +0200 Subject: [PATCH] vzdump: generate notes: die upon unexpected escape character or variable Signed-off-by: Fabian Ebner --- PVE/VZDump.pm | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/PVE/VZDump.pm b/PVE/VZDump.pm index 5f78746d..fcbd87d5 100644 --- a/PVE/VZDump.pm +++ b/PVE/VZDump.pm @@ -80,16 +80,20 @@ my $generate_notes = sub { vmid => $task->{vmid}, }; - my $unescape = { - '\\\\' => '\\', # replace \\ by \ - '\n' => "\n", # turn literal \n into real newline + my $unescape = sub { + my ($char) = @_; + return '\\' if $char eq '\\'; + return "\n" if $char eq 'n'; + die "unexpected escape character '$char'\n"; }; - $notes_template =~ s/(\Q\\\E|\Q\n\E)/$unescape->{$1}/g; + $notes_template =~ s/\\(.)/$unescape->($1)/eg; my $vars = join('|', keys $info->%*); $notes_template =~ s/\{\{($vars)\}\}/\Q$info->{$1}\E/g; + die "unexpected variable name '$1'" if $notes_template =~ m/\{\{([^\s]+)\}\}/; + return $notes_template; }; @@ -1042,9 +1046,13 @@ sub exec_backup_task { if ($opts->{'notes-template'} && $opts->{'notes-template'} ne '') { debugmsg('info', "adding notes to backup", $logfd); - my $notes = $generate_notes->($opts->{'notes-template'}, $task); - eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'notes', $notes) }; - debugmsg('warn', "unable to add notes - $@", $logfd) if $@; + my $notes = eval { $generate_notes->($opts->{'notes-template'}, $task); }; + if (my $err = $@) { + debugmsg('warn', "unable to add notes - $err", $logfd); + } else { + eval { PVE::Storage::update_volume_attribute($cfg, $volid, 'notes', $notes) }; + debugmsg('warn', "unable to add notes - $@", $logfd) if $@; + } } if ($opts->{protected}) {