| 1 | package Perl::Critic::Policy::ProhibitPipeOpen; | ||
| 2 | $Perl::Critic::Policy::ProhibitPipeOpen::VERSION = '1.000'; | ||
| 3 | # ABSTRACT: Don't let a pipe open be the quiet way to shell out. | ||
| 4 | |||
| 5 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 7 | |||
| 8 | 1 | ▶ | use 5.014; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 1 | ▶ | use re '/aa'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | 0 | ▶ | use Readonly; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | |||
| 14 | use Perl::Critic::Utils qw{ :severities :classification :ppi }; | ||
| 15 | use parent qw{Perl::Critic::Policy}; | ||
| 16 | |||
| 17 | |||
| 18 | # The mode argument of a three-or-more-arg open that makes it spawn something | ||
| 19 | # rather than open a file. | ||
| 20 | Readonly::Scalar my $PIPE_MODE_RX => qr/\A (?: -[|] | [|]- ) \z/xms; | ||
| 21 | |||
| 22 | # The same thing said the old way, with the pipe buried in a two-arg string: | ||
| 23 | # "cmd |" to read from it, "| cmd" to write to it. | ||
| 24 | Readonly::Scalar my $TWO_ARG_PIPE_RX => qr/ (?: \A \s* [|] | [|] \s* \z ) /xms; | ||
| 25 | |||
| 26 | Readonly::Scalar my $DESC => q{Pipe "open" used}; | ||
| 27 | Readonly::Scalar my $EXPL => q{Shelling out needs signing off on -- use system/exec/qx with a '## no critic' saying why}; | ||
| 28 | |||
| 29 | |||
| 30 | sub supported_parameters { return () } | ||
| 31 | sub default_severity { return $SEVERITY_HIGHEST } | ||
| 32 | sub default_themes { return qw(security performance) } | ||
| 33 | sub applies_to { return 'PPI::Token::Word' } | ||
| 34 | |||
| 35 | |||
| 36 | sub violates { | ||
| 37 | my ( $self, $elem, undef ) = @_; | ||
| 38 | |||
| 39 | return if $elem->content() ne 'open'; | ||
| 40 | return if !is_function_call($elem); | ||
| 41 | |||
| 42 | my @args = parse_arg_list($elem); | ||
| 43 | return if @args < 2; | ||
| 44 | |||
| 45 | my $mode = $args[1]->[0]; | ||
| 46 | return if !$mode->isa('PPI::Token::Quote'); | ||
| 47 | |||
| 48 | # open($fh, '-|', @cmd) and friends, including the bare two-arg fork form. | ||
| 49 | return $self->violation( $DESC, $EXPL, $elem ) if $mode->string() =~ $PIPE_MODE_RX; | ||
| 50 | |||
| 51 | # open($fh, "cmd |"). Only ever a pipe when there is no third argument -- | ||
| 52 | # with one, the second argument is a mode and a stray '|' is not our business. | ||
| 53 | return $self->violation( $DESC, $EXPL, $elem ) | ||
| 54 | if @args == 2 && $mode->string() =~ $TWO_ARG_PIPE_RX; | ||
| 55 | |||
| 56 | return; # ok! | ||
| 57 | } | ||
| 58 | |||
| 59 | 1; | ||
| 60 | |||
| 61 | __END__ | ||