Commit | Line | Data |
83542a7d |
1 | use strict; |
2 | use warnings; |
ade96c1f |
3 | use Config; |
4 | |
5 | # there is talk of possible perl compilations where -T is fatal or just |
6 | # doesn't work. We don't want to have the user deal with that. |
7 | BEGIN { unless ($INC{'t/lib/DBICTest/WithTaint.pm'}) { |
8 | |
a5741f5c |
9 | if ( $^O eq 'MSWin32' and $^X =~ /\x20/ ) { |
10 | print "1..0 # SKIP Running this test on Windows with spaces within the perl executable path (\$^X) is not possible due to https://rt.perl.org/Ticket/Display.html?id=123907\n"; |
11 | exit 0; |
12 | } |
13 | |
ade96c1f |
14 | # it is possible the test itself is initially invoked in taint mode |
15 | # and with relative paths *and* with a relative $^X and some other |
16 | # craziness... in short: just be proactive |
17 | require File::Spec; |
66a65d81 |
18 | |
19 | if (length $ENV{PATH}) { |
20 | ( $ENV{PATH} ) = join ( $Config{path_sep}, |
21 | map { length($_) ? File::Spec->rel2abs($_) : () } |
22 | split /\Q$Config{path_sep}/, $ENV{PATH} |
23 | ) =~ /\A(.+)\z/; |
24 | } |
25 | |
26 | my ($perl) = $^X =~ /\A(.+)\z/; |
ade96c1f |
27 | |
28 | { |
29 | local $ENV{PATH} = "/nosuchrootbindir"; |
30 | system( $perl => -T => -e => ' |
31 | use warnings; |
32 | use strict; |
33 | eval { my $x = $ENV{PATH} . (kill (0)); 1 } or exit 42; |
34 | exit 0; |
35 | '); |
36 | } |
37 | |
38 | if ( ($? >> 8) != 42 ) { |
39 | print "1..0 # SKIP Your perl does not seem to like/support -T...\n"; |
40 | exit 0; |
41 | } |
42 | |
43 | exec( $perl, qw( -I. -Mt::lib::DBICTest::WithTaint -T ), __FILE__ ); |
44 | }} |
83542a7d |
45 | |
9f72d93a |
46 | # When in taint mode, PERL5LIB is ignored (but *not* unset) |
47 | # Put it back in INC so that local-lib users can actually |
a8de639b |
48 | # run this test. Use lib.pm instead of an @INC unshift as |
49 | # it will correctly add any arch subdirs encountered |
a8de639b |
50 | |
51 | use lib ( |
52 | grep { length } |
53 | map { split /\Q$Config{path_sep}\E/, (/^(.*)$/)[0] } # untainting regex |
54 | grep { defined } |
55 | @ENV{qw(PERL5LIB PERLLIB)} # precedence preserved by lib |
56 | ); |
9f72d93a |
57 | |
6fc6d60c |
58 | # We need to specify 'lib' here as well because even if it was already in |
59 | # @INC, the above will have put our local::lib in front of it, so now an |
60 | # installed DBIx::Class will take precedence over the one we're trying to test. |
61 | # In some cases, prove will have supplied ./lib as an absolute path so it |
62 | # doesn't seem worth trying to remove the second copy since it won't hurt |
63 | # anything. |
64 | use lib qw(t/lib lib); |
65 | |
91b0ad0b |
66 | use Test::More; |
f54428ab |
67 | use Test::Exception; |
8d6b1478 |
68 | use DBICTest; |
83542a7d |
69 | |
f54428ab |
70 | throws_ok ( |
71 | sub { $ENV{PATH} . (kill (0)) }, |
72 | qr/Insecure dependency in kill/, |
73 | 'taint mode active' |
66a65d81 |
74 | ) if length $ENV{PATH}; |
83542a7d |
75 | |
f54428ab |
76 | { |
77 | package DBICTest::Taint::Classes; |
83542a7d |
78 | |
f54428ab |
79 | use Test::More; |
80 | use Test::Exception; |
91b0ad0b |
81 | |
f54428ab |
82 | use base qw/DBIx::Class::Schema/; |
91b0ad0b |
83 | |
f54428ab |
84 | lives_ok (sub { |
85 | __PACKAGE__->load_classes(qw/Manual/); |
86 | ok( __PACKAGE__->source('Manual'), 'The Classes::Manual source has been registered' ); |
87 | __PACKAGE__->_unregister_source (qw/Manual/); |
88 | }, 'Loading classes with explicit load_classes worked in taint mode' ); |
91b0ad0b |
89 | |
f54428ab |
90 | lives_ok (sub { |
91 | __PACKAGE__->load_classes(); |
92 | ok( __PACKAGE__->source('Auto'), 'The Classes::Auto source has been registered' ); |
93 | ok( __PACKAGE__->source('Auto'), 'The Classes::Manual source has been re-registered' ); |
94 | }, 'Loading classes with Module::Find/load_classes worked in taint mode' ); |
95 | } |
83542a7d |
96 | |
f54428ab |
97 | { |
98 | package DBICTest::Taint::Namespaces; |
83542a7d |
99 | |
f54428ab |
100 | use Test::More; |
101 | use Test::Exception; |
91b0ad0b |
102 | |
f54428ab |
103 | use base qw/DBIx::Class::Schema/; |
83542a7d |
104 | |
f54428ab |
105 | lives_ok (sub { |
106 | __PACKAGE__->load_namespaces(); |
107 | ok( __PACKAGE__->source('Test'), 'The Namespaces::Test source has been registered' ); |
108 | }, 'Loading classes with Module::Find/load_namespaces worked in taint mode' ); |
109 | } |
91b0ad0b |
110 | |
f2d11f4a |
111 | # check that we can create a database and all |
112 | { |
113 | my $s = DBICTest->init_schema( sqlite_use_file => 1 ); |
114 | my $art = $s->resultset('Artist')->search({}, { |
115 | prefetch => 'cds', order_by => 'artistid', |
116 | })->next; |
117 | is ($art->artistid, 1, 'got artist'); |
118 | } |
119 | |
f54428ab |
120 | done_testing; |