Skip to content

Commit

Permalink
Item13028: rewriteshebang misses scripts using env
Browse files Browse the repository at this point in the history
rewriteshebang should rewrite all scripts, regardless of /usr/bin/perl
or /usr/bin/env perl.  Otherwise you end up with a mix of perl versions
in testing and web.
  • Loading branch information
gac410 committed Dec 20, 2014
1 parent dd7f1b5 commit 0f78371
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 11 deletions.
31 changes: 28 additions & 3 deletions UnitTestContrib/test/unit/ExtensionInstallerTests.pm
Expand Up @@ -690,9 +690,13 @@ sub test_Util_rewriteShebang {
);
_doRewriteTest(
$this, $tempdir, '#! /usr/bin/env perl ',
'/usr/bin/perl',
'#! /usr/bin/perl ',
'Not a perl script'
'/usr/bin/perl', '#! /usr/bin/perl ',
);
_doRewriteTest(
$this, $tempdir,
'#! /usr/bin/perl -wT ',
'/usr/bin/env perl',
'#! /usr/bin/env perl ',
);
_doRewriteTest( $this, $tempdir, '#! /usr/bin/perl -wT ',
'/my/bin/perl', '#! /my/bin/perl -wT ' );
Expand All @@ -702,6 +706,13 @@ sub test_Util_rewriteShebang {
'C:\Program Files\Active State\perl.exe',
'#! C:\Program Files\Active State\perl.exe -wT'
);
_doRewriteTest(
$this, $tempdir,
'#!/usr/bin/env perl',
'C:\Program Files\Active State\perl.exe',
'#! C:\Program Files\Active State\perl.exe -T',
'', 1
);
_doRewriteTest( $this, $tempdir,
'#! C:\Program Files\Active State\perl.exe -wT',
'/usr/bin/perl', '#! /usr/bin/perl -wT' );
Expand Down Expand Up @@ -760,6 +771,20 @@ sub test_Util_rewriteShebang {
_doRewriteTest( $this, $tempdir, '#!/usr/bin/perl',
'/usr/bin/perl', '#! /usr/bin/perl -T',
undef, 1 );
_doRewriteTest(
$this, $tempdir, '#!/usr/bin/env perl',
'/usr/bin/perl', '#! /usr/bin/perl -T',
undef, 1
);

# Even if Taint requested, don't set -T for env perl
_doRewriteTest(
$this, $tempdir,
'#!/usr/bin/perl -wT',
'/usr/bin/env perl',
'#! /usr/bin/env perl',
undef, 1
);
_doRewriteTest(
$this, $tempdir,
'#!/usr/bin/perl -wT',
Expand Down
21 changes: 13 additions & 8 deletions core/lib/Foswiki/Configure/FileUtil.pm
Expand Up @@ -849,10 +849,11 @@ sub getPerlLocation {

=begin TML
---++ StaticMethod rewriteShebang($file, $newShebang )
---++ StaticMethod rewriteShebang($file, $newShebang, $taint )
Rewrite the #! (shebang) line of the target script
with the specified script name.
with the specified script name. Clear any taint flag
by default, or set it if $taint is true.
This is used in 2 places:
- The Package installer - used when installing extensions
Expand All @@ -873,15 +874,20 @@ sub rewriteShebang {
my $contents = <$fh>;
close $fh;

# Pull out the first line, parse it into the script (match) and arguments
my $firstline = substr( $contents, 0, index( $contents, "\n" ) );
my ( $match, $args ) =
$firstline =~ m/^#\!\s*(.*?perl[^\s]*)(\s?-w?T?w?)?.*?$/ms;
$match = '' unless $match;
$match = '' if ( $match =~ m/env perl/ );
$args = '' unless $args;
$match ||= '';
$args ||= '';
my $newargs = $args;

if ( defined $taint ) {
return "Not a perl script" unless ($match);

if ( $newShebang =~ m/env perl/ ) {
$newargs = ''; # No arguments possible when using env perl
}
elsif ( defined $taint ) {
if ($args) {
if ($taint) {
$newargs .= 'T' unless ( $args =~ m/T/ );
Expand All @@ -893,8 +899,7 @@ sub rewriteShebang {
}
}

return "Not a perl script" unless ($match);

# Find position of existing args, and replace with new arguments
my $argsIdx = index( $contents, $args );
if ($argsIdx) {
substr( $contents, $argsIdx, length($args) ) = "$newargs";
Expand Down

0 comments on commit 0f78371

Please sign in to comment.