« blib/lib/App/Project/Doctor/Check/CpanReadiness.pm
↑ Summary
lib/App/Project/Doctor/Check/Pod.pm »
| 1 | package App::Project::Doctor::Check::Security; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 5 | ▶ | use parent -norequire, 'App::Project::Doctor::Check::Base'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 5 | ▶ | use Carp qw(croak carp); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Readonly; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 5 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 5 | Readonly::Array my @SECRET_PATTERNS => ( | |||||||||||||||||||||||||||
| 15 | qr/(?:password|passwd|secret|api_?key|token)\s*=\s*['"][^'"]{4,}['"]/i, | ||||||||||||||||||||||||||||
| 16 | qr/-----BEGIN (?:RSA |EC )?PRIVATE KEY-----/, | ||||||||||||||||||||||||||||
| 17 | qr/(?:AKIA|ASIA)[A-Z0-9]{16}/, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 4 | ▶ | sub name { 'Security' } | ||||||||||||||||||||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 21 | 1 | ▶ | sub description { 'All modules declare strict/warnings; no hardcoded credentials.' } | ||||||||||||||||||||||||||
|
Subroutine
description: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 22 | 2 | ▶ | sub can_fix { 1 } | ||||||||||||||||||||||||||
|
Subroutine
can_fix: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 5 | ▶ | sub order { 60 } | ||||||||||||||||||||||||||
|
Subroutine
order: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | 23 | ▶ | sub check { | ||||||||||||||||||||||||||
|
Subroutine
check: called CC=11 SCAR=24.0
Pod
documented
| |||||||||||||||||||||||||||||
| 26 | 23 | my ($self, $ctx) = @_; | |||||||||||||||||||||||||||
| 27 | 23 | ▶ | croak 'check requires an App::Project::Doctor::Context' unless ref $ctx; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 23 | my @findings; | |||||||||||||||||||||||||||
| 30 | 23 | my $files = $ctx->perl_files('lib', 'script', 'bin'); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 23 | for my $rel (@{$files}) { | |||||||||||||||||||||||||||
| 33 | 22 | ▶ | my $content = eval { $ctx->slurp($rel) } // do { carp "Cannot slurp $rel"; next }; | ||||||||||||||||||||||||||
|
Condition66%2/3
eval {
do {
$ctx->slurp($rel)
}
} // do {
carp("Cannot slurp $rel");
next
}
MC/DC0%0/2
eval {
do {
$ctx->slurp($rel)
}
} // do {
carp("Cannot slurp $rel");
next
}
eval {
do {
$ctx->slurp($rel)
}
}
do {
carp("Cannot slurp $rel");
next
}
Truth table33%1/3
A // B
A= eval {
do {
$ctx->slurp($rel)
}
}B= do {
carp("Cannot slurp $rel");
next
}
| |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | # strict / warnings -- skip .t files (they inherit from test harness). | ||||||||||||||||||||||||||||
| 36 | 21 | ▶ | unless ($rel =~ /\.t$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 37 | 20 | ▶ | unless ($content =~ /^\s*use\s+strict\b/m) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | 3 | push @findings, _f( | |||||||||||||||||||||||||||
| 39 | severity => 'error', | ||||||||||||||||||||||||||||
| 40 | message => "Missing 'use strict' in $rel.", | ||||||||||||||||||||||||||||
| 41 | file => $rel, | ||||||||||||||||||||||||||||
| 42 | fix => _fix_pragma($ctx, $rel, 'strict'), | ||||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 20 | ▶ | unless ($content =~ /^\s*use\s+warnings\b/m) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 2 | push @findings, _f( | |||||||||||||||||||||||||||
| 47 | severity => 'error', | ||||||||||||||||||||||||||||
| 48 | message => "Missing 'use warnings' in $rel.", | ||||||||||||||||||||||||||||
| 49 | file => $rel, | ||||||||||||||||||||||||||||
| 50 | fix => _fix_pragma($ctx, $rel, 'warnings'), | ||||||||||||||||||||||||||||
| 51 | ); | ||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | # Credential scan. | ||||||||||||||||||||||||||||
| 56 | 21 | my @lines = split /\n/, $content; | |||||||||||||||||||||||||||
| 57 | 21 | for my $i (0 .. $#lines) { | |||||||||||||||||||||||||||
| 58 | 90 | for my $pat (@SECRET_PATTERNS) { | |||||||||||||||||||||||||||
| 59 | 261 | ▶ | if ($lines[$i] =~ $pat) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 8 | push @findings, _f( | |||||||||||||||||||||||||||
| 61 | severity => 'error', | ||||||||||||||||||||||||||||
| 62 | message => "Possible hardcoded credential in $rel at line " . ($i + 1) . '.', | ||||||||||||||||||||||||||||
| 63 | file => $rel, | ||||||||||||||||||||||||||||
| 64 | line => $i + 1, | ||||||||||||||||||||||||||||
| 65 | detail => 'Move secrets to environment variables or a config file.', | ||||||||||||||||||||||||||||
| 66 | ); | ||||||||||||||||||||||||||||
| 67 | 8 | last; | |||||||||||||||||||||||||||
| 68 | } | ||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 23 | ▶ | unless (@findings) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 11 | push @findings, _f( | |||||||||||||||||||||||||||
| 75 | severity => 'pass', | ||||||||||||||||||||||||||||
| 76 | message => 'All checked files use strict/warnings; no credential patterns found.', | ||||||||||||||||||||||||||||
| 77 | ); | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 23 | return @findings; | |||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 84 | # Private helpers | ||||||||||||||||||||||||||||
| 85 | # --------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 24 | ▶ | sub _f { | ||||||||||||||||||||||||||
|
Subroutine
_f: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 24 | require App::Project::Doctor::Finding; | |||||||||||||||||||||||||||
| 89 | 24 | return App::Project::Doctor::Finding->new(check_name => 'Security', @_); | |||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | 8 | ▶ | sub _fix_pragma { | ||||||||||||||||||||||||||
|
Subroutine
_fix_pragma: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 93 | 8 | my ($ctx, $rel, $pragma) = @_; | |||||||||||||||||||||||||||
| 94 | 4 | ▶ | return sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 95 | 4 | my $abs = $ctx->abs_path($rel); | |||||||||||||||||||||||||||
| 96 | 4 | open my $fh, '<', $abs; | |||||||||||||||||||||||||||
| 97 | 4 | my @lines = <$fh>; | |||||||||||||||||||||||||||
| 98 | 4 | close $fh; | |||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | # A shebang must remain the very first line of a script so the OS | ||||||||||||||||||||||||||||
| 101 | # can recognise it; preserve it by starting the search one line in. | ||||||||||||||||||||||||||||
| 102 | 4 | ▶ | my $insert_at = (@lines && $lines[0] =~ /^#!/) ? 1 : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@lines and $lines[0] =~ /^#!/
MC/DC50%1/2
@lines and $lines[0] =~ /^#!/
@lines
$lines[0] =~ /^#!/
Truth table66%2/3
A and B
A= @linesB= $lines[0] =~ /^#!/
| |||||||||||||||||||||||||||||
| 103 | 4 | for my $i ($insert_at .. $#lines) { | |||||||||||||||||||||||||||
| 104 | 5 | ▶ | if ($lines[$i] =~ /^\s*package\s+\S+/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | $insert_at = $i + 1; | |||||||||||||||||||||||||||
| 106 | 1 | last; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||||||||
| 109 | 4 | splice @lines, $insert_at, 0, "use $pragma;\n"; | |||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | 4 | open my $out, '>', $abs; | |||||||||||||||||||||||||||
| 112 | 4 | print {$out} @lines; | |||||||||||||||||||||||||||
| 113 | 4 | close $out; | |||||||||||||||||||||||||||
| 114 | 8 | }; | |||||||||||||||||||||||||||
| 115 | } | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | 5 | 1; | |||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | __END__ | ||||||||||||||||||||||||||||