| 1 | package App::WebPerimeterScan::Source; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 28 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 28 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 28 | ▶ | use autodie qw(:all); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||||||
| 7 | 28 | ▶ | use Cwd qw(realpath); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 28 | ▶ | use File::Find qw(find); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 28 | ▶ | use File::Spec; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 28 | ▶ | use Params::Get qw(get_params); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 28 | ▶ | use Params::Validate::Strict qw(validate_strict); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 28 | ▶ | use Readonly; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 28 | ▶ | use Sub::Private { mode => 'enforce' }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 28 | ▶ | use URI::Escape qw(uri_unescape); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | 28 | ▶ | use App::WebPerimeterScan::I18N (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | 28 | ▶ | use parent -norequire, 'App::WebPerimeterScan::I18N'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||||||
| 19 | 28 | our $VERSION = '0.001.0'; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Route declarations this understands. Mojolicious is the common Perl web | ||||||||||||||||||||||||||||||||||||||||||
| 22 | # framework, so its $r->get('/path') form is what the indexer reads. The | ||||||||||||||||||||||||||||||||||||||||||
| 23 | # pattern must start with "/": without that, $cache->get('key') and | ||||||||||||||||||||||||||||||||||||||||||
| 24 | # $ua->get('http://...') in the same tree were indexed as routes. A route | ||||||||||||||||||||||||||||||||||||||||||
| 25 | # written as a regexp is left alone rather than approximated. | ||||||||||||||||||||||||||||||||||||||||||
| 26 | 28 | Readonly::Scalar my $ROUTE_RE => qr/->(get|post|put|patch|delete|any|under|websocket)\s*\(\s*(['"])(\/[^'"]*)\2/i; | |||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | # Files worth reading for routes, and directories never worth walking. | ||||||||||||||||||||||||||||||||||||||||||
| 29 | 28 | Readonly::Scalar my $SOURCE_FILE => qr/\.(?:pm|pl|psgi|cgi)\z/; | |||||||||||||||||||||||||||||||||||||||||
| 30 | 28 | Readonly::Hash my %SKIP_DIRECTORY => map { $_ => 1 } qw(.git .svn blib node_modules); | |||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | # Path traversal: a ".." segment anywhere in the decoded path. | ||||||||||||||||||||||||||||||||||||||||||
| 33 | 28 | Readonly::Scalar my $TRAVERSAL => qr{(?:\A|/)\.\.(?:/|\z)}; | |||||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||||||
| 35 | # Route placeholders: ':name' is one segment, '*name' the rest of the path. | ||||||||||||||||||||||||||||||||||||||||||
| 36 | 28 | Readonly::Scalar my $ROUTE_TOKEN => qr/\G(?::([A-Za-z_]\w*)|(\*)([A-Za-z_]\w*)|(.))/s; | |||||||||||||||||||||||||||||||||||||||||
| 37 | 28 | Readonly::Scalar my $ONE_SEGMENT => '[^/]+'; | |||||||||||||||||||||||||||||||||||||||||
| 38 | 28 | Readonly::Scalar my $REST_OF_PATH => '.*'; | |||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||||||||||||||||
| 42 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | App::WebPerimeterScan::Source - map scan results onto files and code | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | Version 0.001.0 | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||
| 52 | Find the file on disk behind a URL: | ||||||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||||||
| 54 | use App::WebPerimeterScan::Source; | ||||||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||||||
| 56 | my $source = App::WebPerimeterScan::Source->new( | ||||||||||||||||||||||||||||||||||||||||||
| 57 | docroots => ['/srv/www/example'], | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||||||
| 60 | my $hit = $source->file_for('/.git/config'); | ||||||||||||||||||||||||||||||||||||||||||
| 61 | print "served from $hit->{path} ($hit->{size} bytes)\n" if $hit; | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | Find the line of code that defines an endpoint: | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | my $app = App::WebPerimeterScan::Source->new( | ||||||||||||||||||||||||||||||||||||||||||
| 66 | source_dirs => ['/home/me/src/example/lib'], | ||||||||||||||||||||||||||||||||||||||||||
| 67 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | my $route = $app->route_for('/view/dogs?page=2'); | ||||||||||||||||||||||||||||||||||||||||||
| 70 | print "$route->{method} $route->{pattern} at $route->{file} line $route->{line}\n" | ||||||||||||||||||||||||||||||||||||||||||
| 71 | if $route; | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | List every endpoint the source defines: | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | for my $route (@{ $app->routes }) { | ||||||||||||||||||||||||||||||||||||||||||
| 76 | print "$route->{pattern}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | Look for secret files that exist on disk, even if the server did not serve | ||||||||||||||||||||||||||||||||||||||||||
| 80 | them: | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | my $probes = App::WebPerimeterScan::Checks::sensitive_probes(); | ||||||||||||||||||||||||||||||||||||||||||
| 83 | for my $hit (@{ $source->sensitive_on_disk($probes) }) { | ||||||||||||||||||||||||||||||||||||||||||
| 84 | print "on disk: $hit->{relative}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | A scan from outside can only say "this URL answered". This module adds | ||||||||||||||||||||||||||||||||||||||||||
| 90 | two facts that tell you what to change: | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =item * B<The file.> Given a document root (the directory the web server | ||||||||||||||||||||||||||||||||||||||||||
| 95 | serves files from), it finds the file on disk that a URL path would be | ||||||||||||||||||||||||||||||||||||||||||
| 96 | served from. | ||||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||||
| 98 | =item * B<The route.> Given your application's source code, it finds the | ||||||||||||||||||||||||||||||||||||||||||
| 99 | line that declares the endpoint, for example | ||||||||||||||||||||||||||||||||||||||||||
| 100 | C<< $r->get('/view/:name') >>. | ||||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||||
| 102 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||||||
| 104 | The module only reads files. It never sends network traffic and never | ||||||||||||||||||||||||||||||||||||||||||
| 105 | changes a file. | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | A path is only accepted if it is inside the document root after every | ||||||||||||||||||||||||||||||||||||||||||
| 108 | symbolic link is followed. So C<..> in the path, a symbolic link that | ||||||||||||||||||||||||||||||||||||||||||
| 109 | points out of the tree, and C<%2e%2e> (an encoded C<..>) cannot reach a | ||||||||||||||||||||||||||||||||||||||||||
| 110 | file outside it. | ||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||
| 112 | =head2 Route patterns | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | Routes are found in F<.pm>, F<.pl>, F<.psgi> and F<.cgi> files by looking | ||||||||||||||||||||||||||||||||||||||||||
| 115 | for calls such as C<< ->get('/path') >>, C<< ->post('/path') >> and | ||||||||||||||||||||||||||||||||||||||||||
| 116 | C<< ->any('/path') >> whose pattern starts with C</>. In a pattern, | ||||||||||||||||||||||||||||||||||||||||||
| 117 | C<:name> matches one path segment, C<*name> matches the rest of the path, | ||||||||||||||||||||||||||||||||||||||||||
| 118 | and every other character must match exactly. A C</> at the end of the | ||||||||||||||||||||||||||||||||||||||||||
| 119 | path is optional. The directories F<.git>, F<.svn>, F<blib> and | ||||||||||||||||||||||||||||||||||||||||||
| 120 | F<node_modules> are not searched. | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | =head1 SUBROUTINES/METHODS | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =head2 new | ||||||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||||||
| 126 | Purpose: describe where the site's files and code are. | ||||||||||||||||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||||||||||||||||
| 128 | Args: named C<docroots> and C<source_dirs>, each an array reference of | ||||||||||||||||||||||||||||||||||||||||||
| 129 | directory names. Either may be empty. | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | Returns: the new object. | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | Side Effects: none; directories are read lazily. | ||||||||||||||||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||||||||||||||||
| 135 | Usage: C<< my $source = App::WebPerimeterScan::Source->new(docroots => [$dir]); >> | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||
| 139 | my $source = App::WebPerimeterScan::Source->new(source_dirs => ['lib']); | ||||||||||||||||||||||||||||||||||||||||||
| 140 | printf "%d route(s)\n", scalar @{ $source->routes }; | ||||||||||||||||||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||||||||||||||||
| 142 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||
| 144 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | { | ||||||||||||||||||||||||||||||||||||||||||
| 147 | docroots => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 148 | source_dirs => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 149 | } | ||||||||||||||||||||||||||||||||||||||||||
| 150 | |||||||||||||||||||||||||||||||||||||||||||
| 151 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | { type => 'object', isa => 'App::WebPerimeterScan::Source' } | ||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||
| 155 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||
| 157 | Message Meaning / resolution | ||||||||||||||||||||||||||||||||||||||||||
| 158 | ---------------------------- --------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 159 | Unknown parameter 'X' A misspelt argument name. | ||||||||||||||||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||||||||||||||||
| 161 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||
| 163 | sub new | ||||||||||||||||||||||||||||||||||||||||||
| 164 | 5 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 165 | 5 | my $class = shift; | |||||||||||||||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||||||||||||||||
| 167 | 5 | ▶ | my $args = validate_strict( | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
get_params(undef, \@_) // {}
MC/DC0%0/1
get_params(undef, \@_) // {}
get_params(undef, \@_)
Truth table50%1/2
A
A= get_params(undef, \@_)
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 169 | docroots => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 170 | source_dirs => { type => 'arrayref', optional => 1 }, | ||||||||||||||||||||||||||||||||||||||||||
| 171 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 172 | input => get_params(undef, \@_) // {}, | ||||||||||||||||||||||||||||||||||||||||||
| 173 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 174 | |||||||||||||||||||||||||||||||||||||||||||
| 175 | return bless { | ||||||||||||||||||||||||||||||||||||||||||
| 176 | 4 | ▶ | map { $_ => [ grep { defined } @{ $args->{$_} || [] } ] } qw(docroots source_dirs) | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$args->{$_} || []
MC/DC100%1/1
$args->{$_} || []
$args->{$_}
Truth table100%2/2
A
A= $args->{$_}
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | }, $class; | ||||||||||||||||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||
| 180 | =head2 docroots | ||||||||||||||||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||||||||||||||||
| 182 | Purpose: the document roots that exist, resolved once. | ||||||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||||
| 184 | Args: none. | ||||||||||||||||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||||||||||||||||
| 186 | Returns: an array reference of real paths, without duplicates. A docroot | ||||||||||||||||||||||||||||||||||||||||||
| 187 | that does not exist is dropped rather than fatal: the scan is still worth | ||||||||||||||||||||||||||||||||||||||||||
| 188 | running without it. | ||||||||||||||||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||||||||||||||||
| 190 | Side Effects: caches the result. | ||||||||||||||||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||||||||||||||||
| 192 | Usage: C<< for my $dir (@{ $source->docroots }) { ... } >> | ||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||
| 194 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | print "serving from $_\n" for @{ $source->docroots }; | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||
| 202 | {} | ||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||
| 204 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||
| 206 | { type => 'arrayref', element_type => 'string' } | ||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||
| 208 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||
| 210 | None. | ||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||
| 212 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 213 | |||||||||||||||||||||||||||||||||||||||||||
| 214 | sub docroots | ||||||||||||||||||||||||||||||||||||||||||
| 215 | 21 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
docroots: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 216 | 21 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||||||||||||||||
| 218 | 21 | ▶ | $self->{_docroots} //= do { | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"_docroots"} //= do {
my %seen;
[grep({defined $_ and not $seen{$_}++;} map({-d $_ ? scalar eval {
do {
realpath($_)
}
} : undef;} @{$$self{"docroots"};}))]
}
MC/DC0%0/2
$self->{"_docroots"} //= do {
my %seen;
[grep({defined $_ and not $seen{$_}++;} map({-d $_ ? scalar eval {
do {
realpath($_)
}
} : undef;} @{$$self{"docroots"};}))]
}
$self->{"_docroots"}
do {
my %seen;
[grep({defined $_ and not $seen{$_}++;} map({-d $_ ? scalar eval {
do {
realpath($_)
}
} : undef;} @{$$self{"docroots"};}))]
}
Truth table66%2/3
A //= B
A= $self->{"_docroots"}B= do {
my %seen;
[grep({defined $_ and not $seen{$_}++;} map({-d $_ ? scalar eval {
do {
realpath($_)
}
} : undef;} @{$$self{"docroots"};}))]
}
| |||||||||||||||||||||||||||||||||||||||||||
| 219 | 3 | my %seen; | |||||||||||||||||||||||||||||||||||||||||
| 220 | 3 | ▶ | [ grep { defined && !$seen{$_}++ } map { -d $_ ? scalar eval { realpath($_) } : undef } @{ $self->{docroots} } ]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $_ && !$seen{$_}++
MC/DC50%1/2
defined $_ && !$seen{$_}++
defined $_
!$seen{$_}++
Truth table66%2/3
A && not(B)
A= defined $_B= !$seen{$_}++
| |||||||||||||||||||||||||||||||||||||||||||
| 221 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 222 | |||||||||||||||||||||||||||||||||||||||||||
| 223 | 21 | return [ @{ $self->{_docroots} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||||||||||||||||
| 226 | =head2 file_for | ||||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||||
| 228 | Purpose: the file on disk a URL path would be served from. | ||||||||||||||||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||||||||||||||||
| 230 | Args: C<$url_path>, the path part of a URL (a query or fragment is ignored; | ||||||||||||||||||||||||||||||||||||||||||
| 231 | percent-encoding is decoded first, as a server would). | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | Returns: a hash reference of C<docroot>, C<path> (the real path), | ||||||||||||||||||||||||||||||||||||||||||
| 234 | C<relative> and C<size>; or C<undef> when the file is not under any docroot. | ||||||||||||||||||||||||||||||||||||||||||
| 235 | |||||||||||||||||||||||||||||||||||||||||||
| 236 | Side Effects: file system reads. | ||||||||||||||||||||||||||||||||||||||||||
| 237 | |||||||||||||||||||||||||||||||||||||||||||
| 238 | Usage: C<< my $hit = $source->file_for('/.env'); >> | ||||||||||||||||||||||||||||||||||||||||||
| 239 | |||||||||||||||||||||||||||||||||||||||||||
| 240 | Decoding first is deliberate: C<%2e%2e%2f> becomes C<../> and is then | ||||||||||||||||||||||||||||||||||||||||||
| 241 | refused, rather than slipping through as an opaque name. | ||||||||||||||||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||||||||||||||||
| 243 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 244 | |||||||||||||||||||||||||||||||||||||||||||
| 245 | if(my $hit = $source->file_for('/.git/config')) { | ||||||||||||||||||||||||||||||||||||||||||
| 246 | print "served from $hit->{path} ($hit->{size} bytes)\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 252 | |||||||||||||||||||||||||||||||||||||||||||
| 253 | { url_path => { type => 'string', optional => 1, position => 0 } } | ||||||||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||||||||
| 255 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||||||||||||||||
| 257 | { | ||||||||||||||||||||||||||||||||||||||||||
| 258 | type => 'hashref', optional => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 259 | schema => { | ||||||||||||||||||||||||||||||||||||||||||
| 260 | docroot => { type => 'string' }, | ||||||||||||||||||||||||||||||||||||||||||
| 261 | path => { type => 'string' }, | ||||||||||||||||||||||||||||||||||||||||||
| 262 | relative => { type => 'string' }, | ||||||||||||||||||||||||||||||||||||||||||
| 263 | size => { type => 'integer', min => 0 }, | ||||||||||||||||||||||||||||||||||||||||||
| 264 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 265 | } | ||||||||||||||||||||||||||||||||||||||||||
| 266 | |||||||||||||||||||||||||||||||||||||||||||
| 267 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 268 | |||||||||||||||||||||||||||||||||||||||||||
| 269 | None. | ||||||||||||||||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||||||||||||||||
| 271 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||||||||
| 273 | sub file_for | ||||||||||||||||||||||||||||||||||||||||||
| 274 | 27 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
file_for: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 275 | 27 | my ($self, $url_path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||||||||||||||||
| 277 | 27 | my $relative = _url_to_relative($url_path); | |||||||||||||||||||||||||||||||||||||||||
| 278 | 27 | ▶ | return undef unless defined $relative && $relative !~ $TRAVERSAL; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $relative and not $relative =~ /$TRAVERSAL/
MC/DC100%2/2
defined $relative and not $relative =~ /$TRAVERSAL/
defined $relative
not $relative =~ /$TRAVERSAL/
Truth table100%3/3
A and not(B)
A= defined $relativeB= not $relative =~ /$TRAVERSAL/
| |||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | 20 | for my $docroot (@{ $self->docroots }) { | |||||||||||||||||||||||||||||||||||||||||
| 281 | 20 | my $candidate = File::Spec->catfile($docroot, split m{/+}, $relative); | |||||||||||||||||||||||||||||||||||||||||
| 282 | 20 | ▶ | next unless -e $candidate; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | # realpath follows symlinks, so a link out of the tree resolves | ||||||||||||||||||||||||||||||||||||||||||
| 285 | # outside the docroot and is refused here. | ||||||||||||||||||||||||||||||||||||||||||
| 286 | 8 | my $real = eval { realpath($candidate) }; | |||||||||||||||||||||||||||||||||||||||||
| 287 | 8 | ▶ | next unless defined $real && -f $real && _inside($real, $docroot); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $real and -f $real and _inside($real, $docroot)
defined $real and -f $real
MC/DC66%2/3
defined $real and -f $real and _inside($real, $docroot)
defined $real
-f $real
_inside($real, $docroot)
Truth table75%3/4
A and B and C
A= defined $realB= -f $realC= _inside($real, $docroot)
| |||||||||||||||||||||||||||||||||||||||||||
| 288 | |||||||||||||||||||||||||||||||||||||||||||
| 289 | return { | ||||||||||||||||||||||||||||||||||||||||||
| 290 | 6 | ▶ | docroot => $docroot, | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
-s $real // 0
MC/DC0%0/1
-s $real // 0
-s $real
Truth table50%1/2
A
A= -s $real
| |||||||||||||||||||||||||||||||||||||||||||
| 291 | path => $real, | ||||||||||||||||||||||||||||||||||||||||||
| 292 | relative => $relative, | ||||||||||||||||||||||||||||||||||||||||||
| 293 | size => (-s $real) // 0, | ||||||||||||||||||||||||||||||||||||||||||
| 294 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | 14 | return undef; | |||||||||||||||||||||||||||||||||||||||||
| 298 | } | ||||||||||||||||||||||||||||||||||||||||||
| 299 | |||||||||||||||||||||||||||||||||||||||||||
| 300 | =head2 sensitive_on_disk | ||||||||||||||||||||||||||||||||||||||||||
| 301 | |||||||||||||||||||||||||||||||||||||||||||
| 302 | Purpose: which of the sensitive paths the scan probes actually exist under a | ||||||||||||||||||||||||||||||||||||||||||
| 303 | docroot. The caller passes the probe table so that what is looked for on | ||||||||||||||||||||||||||||||||||||||||||
| 304 | disk is exactly what was requested over the wire. | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | Args: C<$probes>, an array reference of hash references each with a C<path>. | ||||||||||||||||||||||||||||||||||||||||||
| 307 | |||||||||||||||||||||||||||||||||||||||||||
| 308 | Returns: an array reference of C<file_for> results, each with a C<probe> | ||||||||||||||||||||||||||||||||||||||||||
| 309 | key holding the probe it matched. | ||||||||||||||||||||||||||||||||||||||||||
| 310 | |||||||||||||||||||||||||||||||||||||||||||
| 311 | Side Effects: file system reads. | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | Usage: C<< my $hits = $source->sensitive_on_disk(App::WebPerimeterScan::Checks::sensitive_probes()); >> | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | for my $hit (@{ $source->sensitive_on_disk([ { id => 'env-exposed', path => '/.env' } ]) }) { | ||||||||||||||||||||||||||||||||||||||||||
| 318 | print "$hit->{probe}{id}: $hit->{path}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 319 | } | ||||||||||||||||||||||||||||||||||||||||||
| 320 | |||||||||||||||||||||||||||||||||||||||||||
| 321 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||||||||||||||||
| 323 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||||||||
| 325 | { probes => { type => 'arrayref', element_type => 'hashref', position => 0 } } | ||||||||||||||||||||||||||||||||||||||||||
| 326 | |||||||||||||||||||||||||||||||||||||||||||
| 327 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | { type => 'arrayref', element_type => 'hashref' } | ||||||||||||||||||||||||||||||||||||||||||
| 330 | |||||||||||||||||||||||||||||||||||||||||||
| 331 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 332 | |||||||||||||||||||||||||||||||||||||||||||
| 333 | None. | ||||||||||||||||||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||||||||||||||||
| 335 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||||||||||||||||
| 337 | sub sensitive_on_disk | ||||||||||||||||||||||||||||||||||||||||||
| 338 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
sensitive_on_disk: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 339 | 2 | my ($self, $probes) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 340 | |||||||||||||||||||||||||||||||||||||||||||
| 341 | return [ | ||||||||||||||||||||||||||||||||||||||||||
| 342 | 13 | ▶ | map { my $hit = $self->file_for($_->{path}); $hit ? { %{$hit}, probe => $_ } : () } | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 343 | 2 | ▶ | @{ $probes || [] } | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$probes || []
MC/DC0%0/1
$probes || []
$probes
Truth table50%1/2
A
A= $probes
| |||||||||||||||||||||||||||||||||||||||||||
| 344 | ]; | ||||||||||||||||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | =head2 routes | ||||||||||||||||||||||||||||||||||||||||||
| 348 | |||||||||||||||||||||||||||||||||||||||||||
| 349 | Purpose: every route declaration under the source directories. | ||||||||||||||||||||||||||||||||||||||||||
| 350 | |||||||||||||||||||||||||||||||||||||||||||
| 351 | Args: none. | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | Returns: an array reference of hash references: C<method>, C<pattern>, | ||||||||||||||||||||||||||||||||||||||||||
| 354 | C<file>, C<line> and C<re> (a compiled regexp matching the paths the route | ||||||||||||||||||||||||||||||||||||||||||
| 355 | serves). | ||||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||||
| 357 | Side Effects: walks the source directories the first time and caches the | ||||||||||||||||||||||||||||||||||||||||||
| 358 | result. An unreadable file is skipped with a warning. | ||||||||||||||||||||||||||||||||||||||||||
| 359 | |||||||||||||||||||||||||||||||||||||||||||
| 360 | Usage: C<< printf "%d route(s)\n", scalar @{ $source->routes }; >> | ||||||||||||||||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||||||||||||||||
| 362 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 363 | |||||||||||||||||||||||||||||||||||||||||||
| 364 | for my $route (@{ $source->routes }) { | ||||||||||||||||||||||||||||||||||||||||||
| 365 | print "$route->{method} $route->{pattern} at $route->{file}:$route->{line}\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||||||||||||||||
| 367 | |||||||||||||||||||||||||||||||||||||||||||
| 368 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 371 | |||||||||||||||||||||||||||||||||||||||||||
| 372 | {} | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||||||||
| 376 | { type => 'arrayref', element_type => 'hashref' } | ||||||||||||||||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||||||||||||||||
| 378 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 379 | |||||||||||||||||||||||||||||||||||||||||||
| 380 | Message Meaning / resolution | ||||||||||||||||||||||||||||||||||||||||||
| 381 | ------------------------------------- --------------------------------- | ||||||||||||||||||||||||||||||||||||||||||
| 382 | Skipping source file 'F': E (warning) F could not be opened; check its | ||||||||||||||||||||||||||||||||||||||||||
| 383 | permissions. | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | =head3 PSEUDOCODE | ||||||||||||||||||||||||||||||||||||||||||
| 386 | |||||||||||||||||||||||||||||||||||||||||||
| 387 | if not yet indexed | ||||||||||||||||||||||||||||||||||||||||||
| 388 | walk each source directory, pruning .git, .svn, blib, node_modules | ||||||||||||||||||||||||||||||||||||||||||
| 389 | for each .pm, .pl, .psgi or .cgi file | ||||||||||||||||||||||||||||||||||||||||||
| 390 | record every ->get('/...')-style declaration with its line | ||||||||||||||||||||||||||||||||||||||||||
| 391 | return a copy of the index | ||||||||||||||||||||||||||||||||||||||||||
| 392 | |||||||||||||||||||||||||||||||||||||||||||
| 393 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 394 | |||||||||||||||||||||||||||||||||||||||||||
| 395 | sub routes | ||||||||||||||||||||||||||||||||||||||||||
| 396 | 18 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
routes: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 397 | 18 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||||
| 399 | 18 | ▶ | $self->{routes} //= $self->_index_routes; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$self->{"routes"} //= $self->_index_routes
MC/DC0%0/2
$self->{"routes"} //= $self->_index_routes
$self->{"routes"}
$self->_index_routes
Truth table66%2/3
A //= B
A= $self->{"routes"}B= $self->_index_routes
| |||||||||||||||||||||||||||||||||||||||||||
| 400 | |||||||||||||||||||||||||||||||||||||||||||
| 401 | 18 | return [ @{ $self->{routes} } ]; | |||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | =head2 route_for | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | Purpose: the route that would serve a path, in declaration order. | ||||||||||||||||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||||||||||||||||
| 408 | Args: C<$url_path> (a query or fragment is ignored). | ||||||||||||||||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||||||||||||||||
| 410 | Returns: a route hash reference as in C<routes>, or C<undef>. | ||||||||||||||||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||||||||||||||||
| 412 | Side Effects: indexes the source on first use. | ||||||||||||||||||||||||||||||||||||||||||
| 413 | |||||||||||||||||||||||||||||||||||||||||||
| 414 | Usage: C<< my $route = $source->route_for('/view/dogs'); >> | ||||||||||||||||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||||||||||||||||
| 416 | =head3 EXAMPLE | ||||||||||||||||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||||||||||||||||
| 418 | if(my $route = $source->route_for('/api/dirs')) { | ||||||||||||||||||||||||||||||||||||||||||
| 419 | print "$route->{method} $route->{pattern} ($route->{file}:$route->{line})\n"; | ||||||||||||||||||||||||||||||||||||||||||
| 420 | } | ||||||||||||||||||||||||||||||||||||||||||
| 421 | |||||||||||||||||||||||||||||||||||||||||||
| 422 | =head3 API SPECIFICATION | ||||||||||||||||||||||||||||||||||||||||||
| 423 | |||||||||||||||||||||||||||||||||||||||||||
| 424 | =head4 INPUT | ||||||||||||||||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||||||||||||||||
| 426 | { url_path => { type => 'string', optional => 1, position => 0 } } | ||||||||||||||||||||||||||||||||||||||||||
| 427 | |||||||||||||||||||||||||||||||||||||||||||
| 428 | =head4 OUTPUT | ||||||||||||||||||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||||||||||||||||
| 430 | { type => 'hashref', optional => 1 } | ||||||||||||||||||||||||||||||||||||||||||
| 431 | |||||||||||||||||||||||||||||||||||||||||||
| 432 | =head3 MESSAGES | ||||||||||||||||||||||||||||||||||||||||||
| 433 | |||||||||||||||||||||||||||||||||||||||||||
| 434 | As C<routes>. | ||||||||||||||||||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||||||||||||||||
| 436 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 437 | |||||||||||||||||||||||||||||||||||||||||||
| 438 | sub route_for | ||||||||||||||||||||||||||||||||||||||||||
| 439 | 14 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
route_for: called CC=3 SCAR=11.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 440 | 14 | my ($self, $url_path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 441 | |||||||||||||||||||||||||||||||||||||||||||
| 442 | 14 | my $path = _strip_query($url_path); | |||||||||||||||||||||||||||||||||||||||||
| 443 | 14 | my $route; | |||||||||||||||||||||||||||||||||||||||||
| 444 | |||||||||||||||||||||||||||||||||||||||||||
| 445 | 14 | ▶ | if(defined $path) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 446 | 14 | ▶ | $path = "/$path" unless $path =~ m{\A/}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 447 | 14 | ($route) = grep { $path =~ $_->{re} } @{ $self->routes }; | |||||||||||||||||||||||||||||||||||||||||
| 448 | } | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | 14 | return $route; | |||||||||||||||||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||||||||||||||||
| 452 | |||||||||||||||||||||||||||||||||||||||||||
| 453 | # _index_routes | ||||||||||||||||||||||||||||||||||||||||||
| 454 | # Purpose: build the route index. | ||||||||||||||||||||||||||||||||||||||||||
| 455 | # Entry: $self->{source_dirs} holds directory names. | ||||||||||||||||||||||||||||||||||||||||||
| 456 | # Exit: arrayref of route records. | ||||||||||||||||||||||||||||||||||||||||||
| 457 | # Side effects: file system walk; warns on unreadable files. | ||||||||||||||||||||||||||||||||||||||||||
| 458 | 2 | ▶ | sub _index_routes :Private | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_index_routes: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 459 | { | ||||||||||||||||||||||||||||||||||||||||||
| 460 | 2 | my ($self) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | 2 | my @routes; | |||||||||||||||||||||||||||||||||||||||||
| 463 | 2 | for my $dir (grep { -d } @{ $self->{source_dirs} }) { | |||||||||||||||||||||||||||||||||||||||||
| 464 | find({ | ||||||||||||||||||||||||||||||||||||||||||
| 465 | no_chdir => 1, | ||||||||||||||||||||||||||||||||||||||||||
| 466 | 6 | ▶ | wanted => sub { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 467 | 6 | my $name = $File::Find::name; | |||||||||||||||||||||||||||||||||||||||||
| 468 | 6 | my (undef, undef, $leaf) = File::Spec->splitpath($name); | |||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | # Prune, rather than filter, so a large .git is not walked. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | 6 | ▶ | if(-d $name) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 472 | 4 | ▶ | $File::Find::prune = 1 if $SKIP_DIRECTORY{$leaf}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 473 | 4 | return; | |||||||||||||||||||||||||||||||||||||||||
| 474 | } | ||||||||||||||||||||||||||||||||||||||||||
| 475 | 2 | ▶ | push @routes, @{ $self->_routes_in_file($name) } if $name =~ $SOURCE_FILE; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 476 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 477 | 2 | }, $dir); | |||||||||||||||||||||||||||||||||||||||||
| 478 | } | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | 2 | return \@routes; | |||||||||||||||||||||||||||||||||||||||||
| 481 | 28 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||||||||||||||||
| 483 | # _routes_in_file | ||||||||||||||||||||||||||||||||||||||||||
| 484 | # Purpose: every route declaration in one file. | ||||||||||||||||||||||||||||||||||||||||||
| 485 | # Entry: $file is a regular file. | ||||||||||||||||||||||||||||||||||||||||||
| 486 | # Exit: arrayref of route records (empty if unreadable). | ||||||||||||||||||||||||||||||||||||||||||
| 487 | # Side effects: reads the file; warns when it cannot be opened. | ||||||||||||||||||||||||||||||||||||||||||
| 488 | 2 | ▶ | sub _routes_in_file :Private | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_routes_in_file: called CC=4 SCAR=14.0
| |||||||||||||||||||||||||||||||||||||||||||
| 489 | { | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 2 | my ($self, $file) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 491 | |||||||||||||||||||||||||||||||||||||||||||
| 492 | 2 | my $fh = eval { open my $handle, '<', $file; $handle }; | |||||||||||||||||||||||||||||||||||||||||
| 493 | 2 | ▶ | unless($fh) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 494 | 0 | $self->_warn('source.unreadable', { file => $file, error => App::WebPerimeterScan::I18N::clean_error($@) }); | |||||||||||||||||||||||||||||||||||||||||
| 495 | 0 | return []; | |||||||||||||||||||||||||||||||||||||||||
| 496 | } | ||||||||||||||||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||||||||||||||||
| 498 | 2 | my @routes; | |||||||||||||||||||||||||||||||||||||||||
| 499 | 2 | ▶ | while(my $line = <$fh>) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 500 | 10 | ▶ | while($line =~ /$ROUTE_RE/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 501 | 7 | push @routes, { | |||||||||||||||||||||||||||||||||||||||||
| 502 | method => uc $1, | ||||||||||||||||||||||||||||||||||||||||||
| 503 | pattern => $3, | ||||||||||||||||||||||||||||||||||||||||||
| 504 | file => $file, | ||||||||||||||||||||||||||||||||||||||||||
| 505 | line => $., | ||||||||||||||||||||||||||||||||||||||||||
| 506 | re => _pattern_re($3), | ||||||||||||||||||||||||||||||||||||||||||
| 507 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 508 | } | ||||||||||||||||||||||||||||||||||||||||||
| 509 | } | ||||||||||||||||||||||||||||||||||||||||||
| 510 | 2 | close $fh; | |||||||||||||||||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||||||||||||||||
| 512 | 2 | return \@routes; | |||||||||||||||||||||||||||||||||||||||||
| 513 | 28 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 514 | |||||||||||||||||||||||||||||||||||||||||||
| 515 | # _pattern_re | ||||||||||||||||||||||||||||||||||||||||||
| 516 | # Purpose: compile a route pattern into a regexp over URL paths. | ||||||||||||||||||||||||||||||||||||||||||
| 517 | # Entry: $pattern is the quoted string from the declaration. | ||||||||||||||||||||||||||||||||||||||||||
| 518 | # Exit: a qr// anchored at both ends, allowing one trailing "/". | ||||||||||||||||||||||||||||||||||||||||||
| 519 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||
| 520 | # | ||||||||||||||||||||||||||||||||||||||||||
| 521 | # '/view/:table' matches /view/dogs but not /view/a/b; '/files/*path' matches | ||||||||||||||||||||||||||||||||||||||||||
| 522 | # the rest of the path. Everything else is matched literally, so a dot in a | ||||||||||||||||||||||||||||||||||||||||||
| 523 | # route is a dot. | ||||||||||||||||||||||||||||||||||||||||||
| 524 | 7 | ▶ | sub _pattern_re :Private | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_pattern_re: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||||||||
| 525 | { | ||||||||||||||||||||||||||||||||||||||||||
| 526 | 7 | my ($pattern) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 527 | |||||||||||||||||||||||||||||||||||||||||||
| 528 | 7 | my $re = ''; | |||||||||||||||||||||||||||||||||||||||||
| 529 | 7 | ▶ | while($pattern =~ /$ROUTE_TOKEN/g) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 530 | 49 | ▶ | $re .= defined $1 ? $ONE_SEGMENT : defined $2 ? $REST_OF_PATH : quotemeta $4; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 531 | } | ||||||||||||||||||||||||||||||||||||||||||
| 532 | |||||||||||||||||||||||||||||||||||||||||||
| 533 | 7 | return qr{\A$re/?\z}; | |||||||||||||||||||||||||||||||||||||||||
| 534 | 28 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 535 | |||||||||||||||||||||||||||||||||||||||||||
| 536 | # _strip_query | ||||||||||||||||||||||||||||||||||||||||||
| 537 | # Purpose: the path part of a URL path, without query or fragment. | ||||||||||||||||||||||||||||||||||||||||||
| 538 | # Entry: $url_path may be undef. | ||||||||||||||||||||||||||||||||||||||||||
| 539 | # Exit: the path, or undef when there is nothing left. | ||||||||||||||||||||||||||||||||||||||||||
| 540 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||
| 541 | 41 | ▶ | sub _strip_query :Private | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_strip_query: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||||||||||||||||
| 542 | { | ||||||||||||||||||||||||||||||||||||||||||
| 543 | 41 | my ($url_path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 544 | |||||||||||||||||||||||||||||||||||||||||||
| 545 | 41 | ▶ | my $path = defined $url_path ? $url_path =~ s/[?#].*//sr : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | |||||||||||||||||||||||||||||||||||||||||||
| 547 | 41 | ▶ | return length $path ? $path : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 548 | 28 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | # _url_to_relative | ||||||||||||||||||||||||||||||||||||||||||
| 551 | # Purpose: decode a URL path the way a server would before it looks | ||||||||||||||||||||||||||||||||||||||||||
| 552 | # for a file, and make it relative. | ||||||||||||||||||||||||||||||||||||||||||
| 553 | # Entry: $url_path may be undef. | ||||||||||||||||||||||||||||||||||||||||||
| 554 | # Exit: the relative path, or undef for the root or nothing. | ||||||||||||||||||||||||||||||||||||||||||
| 555 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||
| 556 | 27 | ▶ | sub _url_to_relative :Private | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_url_to_relative: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 557 | { | ||||||||||||||||||||||||||||||||||||||||||
| 558 | 27 | my ($url_path) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 559 | |||||||||||||||||||||||||||||||||||||||||||
| 560 | 27 | my $path = _strip_query($url_path); | |||||||||||||||||||||||||||||||||||||||||
| 561 | 27 | ▶ | $path = uri_unescape($path) =~ s{\A/+}{}r if defined $path; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 562 | |||||||||||||||||||||||||||||||||||||||||||
| 563 | 27 | ▶ | return (defined $path && $path ne '' && $path ne '.') ? $path : undef; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
defined $path and $path ne "" and $path ne "."
defined $path and $path ne ""
MC/DC66%2/3
defined $path and $path ne "" and $path ne "."
defined $path
$path ne ""
$path ne "."
Truth table75%3/4
A and B and C
A= defined $pathB= $path ne ""C= $path ne "."
| |||||||||||||||||||||||||||||||||||||||||||
| 564 | 28 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 565 | |||||||||||||||||||||||||||||||||||||||||||
| 566 | # _inside | ||||||||||||||||||||||||||||||||||||||||||
| 567 | # Purpose: is a real path the docroot itself or below it? | ||||||||||||||||||||||||||||||||||||||||||
| 568 | # Entry: both arguments are realpath() results. | ||||||||||||||||||||||||||||||||||||||||||
| 569 | # Exit: 1 or 0. | ||||||||||||||||||||||||||||||||||||||||||
| 570 | # Side effects: none. | ||||||||||||||||||||||||||||||||||||||||||
| 571 | 7 | ▶ | sub _inside :Private | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_inside: called CC=3 SCAR=11.1
| |||||||||||||||||||||||||||||||||||||||||||
| 572 | { | ||||||||||||||||||||||||||||||||||||||||||
| 573 | 7 | my ($path, $docroot) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 574 | |||||||||||||||||||||||||||||||||||||||||||
| 575 | 7 | ▶ | return ($path eq $docroot || index($path, "$docroot/") == 0) ? 1 : 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$path eq $docroot or index($path, "$docroot/") == 0
MC/DC50%1/2
$path eq $docroot or index($path, "$docroot/") == 0
$path eq $docroot
index($path, "$docroot/") == 0
Truth table66%2/3
A or B
A= $path eq $docrootB= index($path, "$docroot/") == 0
| |||||||||||||||||||||||||||||||||||||||||||
| 576 | 28 | ▶ | } | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 577 | |||||||||||||||||||||||||||||||||||||||||||
| 578 | 28 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 579 | |||||||||||||||||||||||||||||||||||||||||||
| 580 | __END__ | ||||||||||||||||||||||||||||||||||||||||||