Debian Codegraph @

t/38security-open.t

raw symbols SCIP
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 my $dir = tempdir( CLEANUP => 1 );
19
20 # A trailing-pipe payload must not run a command.
21 {
22 my $marker = File::Spec->catfile( $dir, "pwned_read" );
23 my $fh = eval { Config::IniFiles->_make_filehandle("touch $marker |") };
24 close $fh if $fh;
25
26 # TEST
27 ok !-e $marker, "trailing-pipe payload does not execute a command";
28 }
29
30 # A leading-pipe payload must not run a command.
31 {
32 my $marker = File::Spec->catfile( $dir, "pwned_write" );
33 my $fh = eval { Config::IniFiles->_make_filehandle("| touch $marker") };
34 close $fh if $fh;
35
36 # TEST
37 ok !-e $marker, "leading-pipe payload does not execute a command";
38 }
39
40 # A redirect payload must not truncate a file.
41 {
42 my $victim = File::Spec->catfile( $dir, "victim" );
43 open my $fh, ">", $victim or die "$victim: $!";
44 print $fh "important data\n";
45 close $fh;
46 my $made = eval { Config::IniFiles->_make_filehandle("> $victim") };
47 close $made if $made;
48
49 # TEST
50 is -s $victim, 15, "redirect payload does not truncate a file";
51 }
52
53 # A plain filename still opens as a file.
54 {
55 my $real = File::Spec->catfile( $dir, "real.txt" );
56 open my $fh, ">", $real or die "$real: $!";
57 print $fh "x\n";
58 close $fh;
59 my $opened = eval { Config::IniFiles->_make_filehandle($real) };
60
61 # TEST
62 ok $opened, "plain filename still opens as a file";
63 }
64
65 # 2-arg open() silently trimmed surrounding whitespace (including a trailing
66 # newline); 3-arg open treats the argument literally, so an un-chomped name no
67 # longer opens the trimmed file.
68 {
69 my $real = File::Spec->catfile( $dir, "plain.txt" );
70 open my $fh, ">", $real or die "$real: $!";
71 print $fh "x\n";
72 close $fh;
73 my $padded = eval { Config::IniFiles->_make_filehandle("$real\n") };
74
75 # TEST
76 ok !$padded, "trailing whitespace is significant (filename not trimmed)";
77 }