btrbk: tidy safe_cmd; die if quoting leading dash for command

This should never happen, as all our filenames are checked to be
absolute.
pull/427/head
Axel Burri 2021-09-04 15:46:09 +02:00
parent d1247359f8
commit 1bda5fd978
1 changed files with 12 additions and 11 deletions

23
btrbk
View File

@ -781,24 +781,25 @@ sub quoteshell(@) {
join ' ', map { "'" . s/'/'\\''/gr . "'" } @_ join ' ', map { "'" . s/'/'\\''/gr . "'" } @_
} }
sub _safe_cmd($$) sub _safe_cmd($;$)
{ {
# NOTE: this function alters $aref: hashes of form: "{ unsafe => 'string' }" get translated to "'string'" # hashes of form: "{ unsafe => 'string' }" get translated to "'string'"
my $aref = shift; my $aref = shift;
my $offending = shift; my $offending = shift;
foreach(@$aref) { return join ' ', map {
if(ref($_) eq 'HASH') { if(ref($_)) {
my $prefix = $_->{prefix} // ""; my $prefix = $_->{prefix} // "";
my $postfix = $_->{postfix} // ""; my $postfix = $_->{postfix} // "";
$_ = $_->{unsafe}; # replace in-place $_ = $_->{unsafe};
# NOTE: all files must be absolute (if not, check for leading dash '-' here!) die "cannot quote leading dash for command: $_" if(/^-/);
unless(defined(check_file($_, { absolute => 1 }))) { # NOTE: all files must be absolute
push @$offending, "\"$_\""; if($offending && !defined(check_file($_, { absolute => 1 }))) {
push @$offending, $_;
} }
$_ = $prefix . quoteshell($_) . $postfix; $_ = $prefix . quoteshell($_) . $postfix;
} }
} $_
return join(' ', @$aref); } @$aref;
} }
sub run_cmd(@) sub run_cmd(@)
@ -925,7 +926,7 @@ sub run_cmd(@)
my $cmd = _piped_cmd_txt(\@cmd_pipe); my $cmd = _piped_cmd_txt(\@cmd_pipe);
if(scalar(@unsafe_cmd)) { if(scalar(@unsafe_cmd)) {
ERROR "Unsafe command `$cmd` (offending string: " . join(', ', @unsafe_cmd) . ')'; ERROR "Unsafe command `$cmd` (offending string: " . join(', ', map "\"$_\"", @unsafe_cmd) . ')';
return undef; return undef;
} }