| 1 |
#!/usr/bin/env perl |
| 2 |
|
| 3 |
# Regression test for the 2-arg open() in _make_filehandle. |
| 4 |
# |
| 5 |
# _make_filehandle is the open path behind the -file argument (new -> ReadConfig |
| 6 |
# and WriteConfig both reach it). A 2-arg open() there interprets shell-magic |
| 7 |
# prefixes, so a "cmd |" filename runs a command and a "> file" filename |
| 8 |
# truncates a file. These must be treated as plain pathnames. |
| 9 |
|
| 10 |
use strict; |
| 11 |
use warnings; |
| 12 |
|
| 13 |
use Config::IniFiles (); |
| 14 |
use File::Temp qw( tempdir ); |
| 15 |
use File::Spec (); |
| 16 |
use Test::More tests => 5; |
| 17 |
|
| 18 |
use lib "./t/lib"; |
| 19 |
use Config::IniFiles::Slurp qw( slurp utf8_slurp utf8_spew ); |
| 20 |
|
| 21 |
my $dir = tempdir( CLEANUP => 1 ); |
| 22 |
|
| 23 |
# A trailing-pipe payload must not run a command. |
| 24 |
{ |
| 25 |
my $marker = File::Spec->catfile( $dir, "pwned_read" ); |
| 26 |
my $fh = eval { Config::IniFiles->_make_filehandle("touch $marker |") }; |
| 27 |
close $fh if $fh; |
| 28 |
|
| 29 |
# TEST |
| 30 |
ok !-e $marker, "trailing-pipe payload does not execute a command"; |
| 31 |
} |
| 32 |
|
| 33 |
# A leading-pipe payload must not run a command. |
| 34 |
{ |
| 35 |
my $marker = File::Spec->catfile( $dir, "pwned_write" ); |
| 36 |
my $fh = eval { Config::IniFiles->_make_filehandle("| touch $marker") }; |
| 37 |
close $fh if $fh; |
| 38 |
|
| 39 |
# TEST |
| 40 |
ok !-e $marker, "leading-pipe payload does not execute a command"; |
| 41 |
} |
| 42 |
|
| 43 |
# A redirect payload must not truncate a file. |
| 44 |
{ |
| 45 |
my $victim = File::Spec->catfile( $dir, "victim" ); |
| 46 |
open my $fh, ">", $victim or die "$victim: $!"; |
| 47 |
print $fh "important data\n"; |
| 48 |
close $fh; |
| 49 |
my $made = eval { Config::IniFiles->_make_filehandle("> $victim") }; |
| 50 |
close $made if $made; |
| 51 |
|
| 52 |
# TEST |
| 53 |
my $contents = utf8_slurp($victim); |
| 54 |
like( |
| 55 |
$contents, |
| 56 |
qr#\Aimportant data[\r\n]+\z#ms, |
| 57 |
"redirect payload does not truncate a file", |
| 58 |
); |
| 59 |
} |
| 60 |
|
| 61 |
# A plain filename still opens as a file. |
| 62 |
{ |
| 63 |
my $real = File::Spec->catfile( $dir, "real.txt" ); |
| 64 |
open my $fh, ">", $real or die "$real: $!"; |
| 65 |
print $fh "x\n"; |
| 66 |
close $fh; |
| 67 |
my $opened = eval { Config::IniFiles->_make_filehandle($real) }; |
| 68 |
|
| 69 |
# TEST |
| 70 |
ok $opened, "plain filename still opens as a file"; |
| 71 |
} |
| 72 |
|
| 73 |
# 2-arg open() silently trimmed surrounding whitespace (including a trailing |
| 74 |
# newline); 3-arg open treats the argument literally, so an un-chomped name no |
| 75 |
# longer opens the trimmed file. |
| 76 |
{ |
| 77 |
my $real = File::Spec->catfile( $dir, "plain.txt" ); |
| 78 |
open my $fh, ">", $real or die "$real: $!"; |
| 79 |
print $fh "x\n"; |
| 80 |
close $fh; |
| 81 |
my $padded = eval { Config::IniFiles->_make_filehandle("$real\n") }; |
| 82 |
|
| 83 |
# TEST |
| 84 |
ok !$padded, "trailing whitespace is significant (filename not trimmed)"; |
| 85 |
} |