lib/App/WebPerimeterScan.pm

filter: Statementstmt 95.4% 375 / 393 Branchbran 78.2% 72 / 92 Conditioncond 59.1% 58 / 98 MC/DCmcdc 38.6% 22 / 57 Subroutinesub 97.1% 68 / 70 Podpod 100% 5 / 5 totaltotal 83.9% 600 / 715 CC 92 SCAR 19.3
CC
92
Cov
87%
_check_plaintext_hop
42.0
_links
14.7
_crawl
12.5
CRAP 6.9
« lib/App/WebPerimeterScan/Scope.pm ↑ Summary lib/App/WebPerimeterScan/Checks.pm »
1 package App::WebPerimeterScan;
2  
3 27 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 27 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 27 ▶use autodie qw(:all);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 27 ▶use HTML::SimpleLinkExtor;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 27 ▶use Object::Configure;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 27 ▶use Params::Get qw(get_params);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 27 ▶use Params::Validate::Strict qw(validate_strict);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 27 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 27 ▶use Sub::Private { mode => 'enforce' };
Subroutine BEGIN: called CC=1 SCAR=0.0
13 27 ▶use URI;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 27 ▶use App::WebPerimeterScan::I18N ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16 27 ▶use App::WebPerimeterScan::Checks;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 27 ▶use App::WebPerimeterScan::Findings;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 27 ▶use App::WebPerimeterScan::HTTP;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 27 ▶use App::WebPerimeterScan::Ports;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 27 ▶use App::WebPerimeterScan::Source;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 27 ▶use App::WebPerimeterScan::TLS;
Subroutine BEGIN: called CC=1 SCAR=0.0
22 27 ▶use parent -norequire, 'App::WebPerimeterScan::I18N';
Subroutine BEGIN: called CC=1 SCAR=0.0
23  
24 27 ▶use version; our $VERSION = version->declare('0.001.0');
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 # Defaults for new(). A plain hash, keyed like the constructor's
27 # arguments, so Object::Configure or a caller can change them for a run.
28 27 our %config = (
29 timeout => 10, # seconds per HTTP request
30 connect_timeout => 3, # seconds for the port and TLS handshakes
31 delay => 0.5, # seconds between requests to one host
32 depth => 1, # link-following depth from the root
33 max_urls => 40, # pages crawled per host
34 max_size => 2_000_000, # bytes of each body kept
35 path_probes => 1, # request the well-known sensitive paths
36 reflection => 0, # send the reflected-input marker
37 max_links_per_page => 60, # links taken from one page
38 unvisited_evidence_limit => 10, # routes listed in source-routes-unvisited
39 token_length => 10, # characters in a random probe token
40 );
41  
42 # Crawling is for finding pages that share the site's header policy, not for
43 # mirroring the site. These are skipped so a scan does not waste requests on
44 # assets.
45 27 Readonly::Hash my %SKIP_EXTENSION => map { $_ => 1 } qw(
46 jpg jpeg png gif svg webp ico bmp tif tiff css js mjs json map
47 pdf zip gz tgz bz2 xz rar 7z tar mp3 mp4 m4a avi mov wmv wav
48 woff woff2 ttf eot otf exe dmg iso apk jar deb rpm
49 );
50  
51 # Only pages of these types are parsed for links.
52 27 Readonly::Scalar my $HTML_TYPE => qr{text/html|application/xhtml}i;
53  
54 # Ports where TLS is expected, in the order they are tried.
55 27 Readonly::Array my @TLS_PORTS => (443, 8443);
56 27 Readonly::Hash my %TLS_PORT => map { $_ => 1 } @TLS_PORTS;
57  
58 # Every redirect status that keeps the visitor's request working. 307 was
59 # missing from the earlier list, so a 307 to HTTPS was reported as no redirect.
60 27 Readonly::Hash my %REDIRECT => map { $_ => 1 } (301, 302, 303, 307, 308);
61 27 Readonly::Scalar my $HTTP_OK => 200;
62  
63 27 Readonly::Scalar my $HTTPS_URL => qr{\Ahttps://}i;
64 27 Readonly::Hash my %WEB_SCHEME => (http => 1, https => 1);
65  
66 # A sensitive file on disk but not served is a hazard waiting for one
67 # configuration change, so it ranks below the same file being served.
68 27 Readonly::Hash my %DOCROOT_SEVERITY => (critical => 'medium', high => 'low');
69  
70 # The reflected-input marker: a parameter name, and a closing tag that is
71 # only returned as-is if the page does not encode its output.
72 27 Readonly::Scalar my $REFLECTION_PARAMETER => 'wsprobe';
73 27 Readonly::Scalar my $REFLECTION_RAW => '</x>';
74 27 Readonly::Scalar my $REFLECTION_ENCODED => '%3C%2Fx%3E';
75  
76 # Probe-path token alphabet.
77 27 Readonly::Array my @TOKEN_CHARS => ('a' .. 'z', 0 .. 9);
78  
79 # The runner's own rules: default severity and reference. Text is in the
80 # message catalogue under the same ids.
81 27 Readonly::Hash my %RULE => (
82 'scan-error' => { severity => 'high' },
83 'host-unreachable' => { severity => 'high' },
84 'no-https-redirect' => { severity => 'medium', reference => 'https://developer.mozilla.org/en-US/docs/Web/HTTP/Redirections' },
85 'catch-all-200' => { severity => 'info' },
86 'reflected-input' => { severity => 'medium', reference => 'https://cwe.mitre.org/data/definitions/79.html' },
87 'source-file-in-docroot' => { severity => 'medium' },
88 'source-routes-unvisited' => { severity => 'info' },
89 );
90  
91 =head1 NAME
92  
93 App::WebPerimeterScan - check your own web perimeter the way an outsider sees it
94  
95 =head1 VERSION
96  
97 Version 0.001.0
98  
99 =head1 SYNOPSIS
100  
101 From the command line, scan the hosts listed in F<scope.txt>:
102  
103 webperimeter --scope scope.txt --authorized
104  
105 From Perl, scan and print the findings, worst first:
106  
107 use App::WebPerimeterScan;
108 use App::WebPerimeterScan::Scope;
109  
110 my $scope = App::WebPerimeterScan::Scope->new->load_file('scope.txt');
111 my $scanner = App::WebPerimeterScan->new(scope => $scope);
112 my $findings = $scanner->run;
113  
114 print "$_->{severity} $_->{title}\n" for @{ $findings->sorted };
115  
116 Stop a deployment when the scan finds anything high or critical:
117  
118 my $serious = App::WebPerimeterScan->new(scope => $scope)->run->at_or_above('high');
119 die scalar(@{$serious}) . " serious finding(s)\n" if @{$serious};
120  
121 A fast scan of only the web ports, without the sensitive-path probes:
122  
123 my $quick = App::WebPerimeterScan->new(
124 scope => $scope,
125 ports => [ 80, 443 ],
126 max_urls => 1,
127 path_probes => 0,
128 );
129  
130 Scan your own site and point each finding at the file and the code behind
131 it. Run this on the machine that serves the site:
132  
133 my $full = App::WebPerimeterScan->new(
134 scope => $scope,
135 docroots => [ '/srv/www/example' ],
136 source_dirs => [ '/home/me/src/example/lib' ],
137 );
138 for my $finding (@{ $full->run->sorted }) {
139 print "$finding->{title}\n";
140 print " file: $finding->{file}\n" if length $finding->{file};
141 print " source: $finding->{source}\n" if length $finding->{source};
142 }
143  
144 Write an HTML report:
145  
146 use App::WebPerimeterScan::Report;
147  
148 open my $out, '>', 'report.html';
149 print $out App::WebPerimeterScan::Report::as_html($scanner->findings,
150 version => $App::WebPerimeterScan::VERSION,
151 hosts => $scanner->host_reports,
152 );
153 close $out;
154  
155 =head1 DESCRIPTION
156  
157 This tool looks at your own web servers from the outside, the way a
158 stranger on the internet sees them. It does not log in. It answers these
159 questions for each host:
160  
161 =over 4
162  
163 =item * Which TCP ports can be reached?
164  
165 =item * Is the TLS certificate valid, and are old protocol versions still
166 accepted?
167  
168 =item * Which security headers are missing, and are the session cookies
169 protected?
170  
171 =item * Has a sensitive file, such as F<.git/config> or F<.env>, been left
172 where the web server will send it?
173  
174 =back
175  
176 The tool does not attack the site. It does not inject code, does not guess
177 passwords, and does not change any data: it sends only C<GET> requests,
178 plus one C<OPTIONS> request for each host. So it is safe to run against a
179 production site. (C<--reflection-probe> adds one more C<GET> with a
180 harmless marker in the query string.)
181  
182 F<README.md> is generated from this POD by F<pod2github>: change the POD and
183 regenerate, rather than editing the README by hand.
184  
185 The scope file is the authorisation boundary: a host that does not appear in
186 it is never contacted, including hosts discovered by following links, and a
187 host that resolves into private address space is refused unless
188 C<--allow-private> is given.
189  
190 Every message the tool prints comes from a catalogue
191 (L<App::WebPerimeterScan::Messages>), so the text can be translated without
192 touching the code; see L<App::WebPerimeterScan::I18N>.
193  
194 =head1 REQUIREMENTS
195  
196 Perl 5.26.0 or later, with the modules listed in F<Makefile.PL>. The floor
197 is set by L<Params::Validate::Strict>, which needs 5.26; the code in this
198 distribution would run on 5.14. C<cpan> installs anything that is missing, so these
199 floors matter mainly on a machine that already has older copies of them:
200  
201 =over 4
202  
203 =item * C<Socket> 2.020
204  
205 The port scan reads C<SO_ERROR> from it to tell a refused connection from an
206 open one, and the private-address check uses its C<getaddrinfo>.
207  
208 =item * C<Net::SSLeay> 1.92
209  
210 The first release with OpenSSL 3 support. Earlier releases are not
211 supported: 1.46 does not compile against OpenSSL 3 at all.
212  
213 =item * C<IO::Socket::SSL> 2.000
214  
215 =item * C<IO::Select> 1.17 and C<IO::Socket::INET> 1.31
216  
217 They are stated because the port scan relies on how they behave, not only
218 on what they provide.
219  
220 =item * C<Sub::Private> 0.09 and C<Sub::Protected> 0.03
221  
222 Every internal routine is wrapped so that a call from outside its package
223 croaks. Tests run under C<prove> are exempt (C<HARNESS_ACTIVE>).
224  
225 =item * C<Params::Get> 0.17 and C<Params::Validate::Strict> 0.41
226  
227 Every public routine validates its arguments against the schema in its
228 documentation.
229  
230 =item * C<Data::Validate::IP> 0.31
231  
232 Classifies resolved addresses against the IANA special-purpose registries.
233  
234 =back
235  
236 Both SSL modules need the OpenSSL headers at install time, which the
237 installer cannot supply for you; install your distribution's C<openssl>
238 development package first if the build fails.
239  
240 The remaining prerequisites are C<autodie> with C<IPC::System::Simple>,
241 C<Getopt::Long> 2.50, C<HTML::Parser> 3.71, C<HTML::SimpleLinkExtor> 1.20,
242 C<JSON::PP> 4.00, C<Readonly> 2.05, C<Time::HiRes> 1.97 and C<URI> 1.70, plus
243 C<Test::More> 1.302 for the test suite.
244  
245 =head1 USAGE
246  
247 bin/webperimeter --scope scope.txt --authorized
248  
249 The scope file lists the hosts you are allowed to test, one per line. Without
250 C<--authorized> the tool refuses to send any traffic. Add C<--docroot DIR> and
251 C<--source DIR> to resolve findings back to the file on disk and the code that
252 serves them.
253  
254 =head1 QUICK TEST
255  
256 To check the tool works where you installed it, scan example.com. RFC 2606
257 reserves the name for documentation and it is always available, so it makes a
258 stable target. It is not a licence to scan other people's hosts: the scope
259 file is still the boundary.
260  
261 $ printf 'example.com\n' > scope.txt
262 $ bin/webperimeter --scope scope.txt --authorized --ports 80,443 \
263 --max-urls 1 --no-path-probes --no-color
264  
265 webperimeter 0.001.0 2026-11-06T01:05:02Z
266 Scope: scope.txt
267 Requests: 4
268  
269 ============================================================
270 example.com
271 ------------------------------------------------------------
272 TLS: TLSv1_3, TLS_AES_256_GCM_SHA384, expires Dec 25 22:56:35 2026 GMT (49 days)
273 Certificate: /CN=example.com issued by /C=US/O=SSL Corporation/CN=Cloudflare TLS Issuing ECC CA 3
274 Open ports: 80, 443
275 URLs scanned: 1
276  
277  
278 MEDIUM (4)
279 ----------
280 [csp-missing] Content-Security-Policy is missing
281 Evidence: No Content-Security-Policy header on https://example.com/
282  
283 [...]
284  
285 Summary: 0 critical, 0 high, 4 medium, 2 low, 1 info
286  
287 C<--max-urls 1> holds the crawl to the front page and C<--no-path-probes>
288 skips the sensitive path checks, so the run costs four requests. Leave the
289 probes on when the target is a deployment of your own.
290  
291 The findings are the point of the exercise. example.com is a good smoke test
292 because it is not hardened: a run reporting missing CSP, HSTS and nosniff
293 shows the checks are wired up, and a run that reports nothing - or a critical
294 finding - means the scan is broken, not the site.
295  
296 =head1 EXAMPLE
297  
298 A run against a local application, with its source tree supplied so the
299 report can name the endpoints it never reached. The output is trimmed in the
300 middle for length.
301  
302 $ echo localhost:3000 > scope.txt
303 $ bin/webperimeter --scope scope.txt --authorized --allow-private --ports 3000 \
304 --source ~/src/njh/Database-BI --no-path-probes --max-urls 6 --delay 0.2 --no-color
305  
306 webperimeter 0.001.0 2026-11-06T01:05:47Z
307 Scope: scope.txt
308 Requests: 8
309  
310 ============================================================
311 localhost
312 ------------------------------------------------------------
313 Open ports: 3000
314 URLs scanned: 6
315  
316  
317 LOW (1)
318 -------
319 [port-open-3000] Port 3000 accepts connections from outside the host
320 Evidence: TCP connect succeeded, no banner
321  
322  
323 INFO (1)
324 --------
325 [source-routes-unvisited] 71 endpoints defined in source were not scanned
326 Evidence: ~/src/njh/Database-BI/lib/Database/BI.pm:2713 GET /login; ~/src/njh/Database-BI/lib/Database/BI.pm:2714 POST /login; ~/src/njh/Database-BI/lib/Database/BI.pm:2715 POST /logout; [...]
327 Fix: Name them explicitly, raise --max-urls, or review them by hand.
328  
329 Summary: 0 critical, 0 high, 0 medium, 1 low, 1 info
330  
331 That application sends C<style-src 'self' 'unsafe-inline'> with a
332 nonce-based C<script-src>. Only the script sources are judged, so the
333 inline-style allowance is not reported as a cross-site scripting gap.
334  
335 C<--allow-private> is required for a loopback or private target; without it
336 the host is refused before any traffic is sent, and the refusal is reported
337 as a high-severity C<scan-error> so the exit status shows that nothing was
338 scanned. The exit status is 0 here because C<--fail-on> defaults to
339 C<high> and nothing reached it.
340  
341 C<--no-path-probes> skips the well-known sensitive path checks, which ask
342 whether a deployed document root exposes files such as C</.git/config>. The
343 target here is local, so those paths can be inspected directly on disk - with
344 C<--docroot>, or by reading the tree - and the probes would only add requests.
345 Against a real deployment, leave them on.
346  
347 C<source-routes-unvisited> is the reason to pass C<--source>: 71 routes exist in
348 the code that no crawled page linked to, so none of the checks above saw how
349 they behave. That is exactly the surface a link-following scan cannot reach.
350 Raise C<--max-urls>, or review them by hand.
351  
352 Findings also carry C<File:> and C<In source:> lines when C<--docroot> or
353 C<--source> lets the scan resolve them.
354  
355 =head1 REPORT FORMAT
356  
357 C<--format json> records the response header block behind each header finding,
358 so a report can be audited later without repeating the requests. Abridged to
359 one header:
360  
361 "response": {
362 "headers" : {
363 "content-security-policy" : [ "default-src 'self'; script-src 'self' 'nonce-...' https://cdnjs.cloudflare.com https://unpkg.com; ..." ]
364 },
365 "http_version" : "HTTP/1.1",
366 "method" : "GET",
367 "note" : "main page",
368 "raw" : "HTTP/1.1 200 OK\r\nServer: ...",
369 "reason" : "OK",
370 "status" : 200,
371 "url" : "http://localhost:3000/"
372 },
373 "responses" : [ ... ]
374  
375 A header appears once per value, so a second C<Content-Security-Policy> or
376 C<Set-Cookie> is not lost. C<raw> is the block exactly as it arrived.
377 C<responses> holds the same structure for the CORS and C<OPTIONS> probes, which
378 is where the CORS and method findings come from.
379  
380 =head1 SUBROUTINES/METHODS
381  
382 =head2 new
383  
384 Purpose: configure a scan.
385  
386 Args: named, all optional except that C<run> needs C<scope>:
387  
388 scope an App::WebPerimeterScan::Scope (or anything with
389 hosts, covers, allows and assert_target_allowed)
390 timeout seconds per HTTP request (10)
391 connect_timeout seconds for port and TLS handshakes (3)
392 delay seconds between requests to one host (0.5)
393 depth link-following depth from each root URL (1)
394 max_urls pages crawled per host (40)
395 max_size bytes of each response body kept (2000000)
396 path_probes request the well-known sensitive paths (1)
397 reflection send the reflected-input marker (0)
398 verbose progress on STDERR (0)
399 ports array reference of TCP ports (the Ports default list)
400 docroots array reference of document roots on this machine
401 source_dirs array reference of source trees on this machine
402  
403 Defaults come from C<%App::WebPerimeterScan::config>.
404  
405 Returns: the new object.
406  
407 Side Effects: none; no traffic is sent until C<run>.
408  
409 Usage: C<< my $scanner = App::WebPerimeterScan->new(scope => $scope); >>
410  
411 =head3 EXAMPLE
412  
413 my $scanner = App::WebPerimeterScan->new(
414 scope => $scope,
415 ports => [ 80, 443 ],
416 max_urls => 1,
417 path_probes => 0,
418 source_dirs => [ "$ENV{HOME}/src/my-app" ],
419 );
420  
421 =head3 API SPECIFICATION
422  
423 =head4 INPUT
424  
425 {
426 scope => { type => 'object', can => [qw(hosts covers allows assert_target_allowed)], optional => 1 },
427 timeout => { type => 'number', min => 0, optional => 1 },
428 connect_timeout => { type => 'number', min => 0, optional => 1 },
429 delay => { type => 'number', min => 0, optional => 1 },
430 depth => { type => 'integer', min => 0, optional => 1 },
431 max_urls => { type => 'integer', min => 1, optional => 1 },
432 max_size => { type => 'integer', min => 1, optional => 1 },
433 path_probes => { type => 'boolean', optional => 1 },
434 reflection => { type => 'boolean', optional => 1 },
435 verbose => { type => 'boolean', optional => 1 },
436 ports => { type => 'arrayref', element_type => 'integer', optional => 1 },
437 docroots => { type => 'arrayref', optional => 1 },
438 source_dirs => { type => 'arrayref', optional => 1 },
439 }
440  
441 =head4 OUTPUT
442  
443 { type => 'object', isa => 'App::WebPerimeterScan' }
444  
445 =head3 MESSAGES
446  
447 Message Meaning / resolution
448 ---------------------------------------- ------------------------------
449 Unknown parameter 'X' (fatal) A misspelt argument name.
450 Parameter 'X' ... must be ... (fatal) A value of the wrong type or
451 out of range, e.g. depth => -1.
452 Parameter 'scope' must be an object that Pass an App::WebPerimeterScan::
453 understands the ... method (fatal) Scope.
454  
455 =head3 PSEUDOCODE
456  
457 validate the arguments
458 overlay them on %config
459 create the findings collector and the HTTP client
460 create the source index if any docroot or source tree was given
461  
462 =cut
463  
464 sub new
465 16 ▶{
Subroutine new: called CC=7 SCAR=19.5
Pod documented
466 16 my $class = shift;
467  
468 16 ▶ my $args = validate_strict(
Condition100%2/2
get_params(undef, \@_) // {}
l!l
115
MC/DC100%1/1
get_params(undef, \@_) // {}
get_params(undef, \@_)
Truth table100%2/2
A
A= get_params(undef, \@_)
Aresult
11
00
469 schema => {
470 scope => { type => 'object', can => [qw(hosts covers allows assert_target_allowed)], optional => 1 },
471 timeout => { type => 'number', min => 0, optional => 1 },
472 connect_timeout => { type => 'number', min => 0, optional => 1 },
473 delay => { type => 'number', min => 0, optional => 1 },
474 depth => { type => 'integer', min => 0, optional => 1 },
475 max_urls => { type => 'integer', min => 1, optional => 1 },
476 max_size => { type => 'integer', min => 1, optional => 1 },
477 path_probes => { type => 'boolean', optional => 1 },
478 reflection => { type => 'boolean', optional => 1 },
479 verbose => { type => 'boolean', optional => 1 },
480 ports => { type => 'arrayref', element_type => 'integer', optional => 1 },
481 docroots => { type => 'arrayref', optional => 1 },
482 source_dirs => { type => 'arrayref', optional => 1 },
483 },
484 input => get_params(undef, \@_) // {},
485 );
486  
487 13 $ args = Object::Configure::configure($class, $args);
488  
489 # Explicit arguments win; anything not given comes from %config.
490 my $self = bless {
491 %config,
492 13 (map { $_ => $args->{$_} } grep { defined $args->{$_} } keys %{$args}),
493 hosts => {},
494 findings => App::WebPerimeterScan::Findings->new,
495 }, $class;
496  
497 13 ▶ $self->{ports} //= [ @App::WebPerimeterScan::Ports::DEFAULT_PORTS ];
Condition100%2/2
$self->{"ports"} //= [@App::WebPerimeterScan::Ports::DEFAULT_PORTS]
l!l
85
MC/DC100%1/1
$self->{"ports"} //= [@App::WebPerimeterScan::Ports::DEFAULT_PORTS]
$self->{"ports"}
Truth table100%2/2
A
A= $self->{"ports"}
Aresult
11
00
498 13 ▶ $self->{docroots} //= [];
Condition100%2/2
$self->{"docroots"} //= []
l!l
112
MC/DC100%1/1
$self->{"docroots"} //= []
$self->{"docroots"}
Truth table100%2/2
A
A= $self->{"docroots"}
Aresult
11
00
499 13 ▶ $self->{source_dirs} //= [];
Condition100%2/2
$self->{"source_dirs"} //= []
l!l
112
MC/DC100%1/1
$self->{"source_dirs"} //= []
$self->{"source_dirs"}
Truth table100%2/2
A
A= $self->{"source_dirs"}
Aresult
11
00
500  
501 $self->{http} = App::WebPerimeterScan::HTTP->new(
502 13 map { $_ => $self->{$_} } qw(timeout delay max_size)
503 );
504  
505 # The source-aware pass only runs when there is something to map to.
506 $self->{source} = App::WebPerimeterScan::Source->new(
507 docroots => $self->{docroots},
508 source_dirs => $self->{source_dirs},
509 13 ▶ ) if @{ $self->{docroots} } || @{ $self->{source_dirs} };
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"docroots"};} or @{$self->{"source_dirs"};} 1 12 13
Condition66%2/3
@{$self->{"docroots"};} or @{$self->{"source_dirs"};}
l!l&&r!l&&!r
1012
MC/DC50%1/2
@{$self->{"docroots"};} or @{$self->{"source_dirs"};}
@{$self->{"docroots"};} @{$self->{"source_dirs"};}
Truth table66%2/3
A or B
A= @{$self->{"docroots"};}B= @{$self->{"source_dirs"};}
ABresult
1X1
011
000
510  
511 13 return $self;
512 }
513  
514 =head2 findings
515  
516 Purpose: the findings collector the scan writes to.
517  
518 Args: none.
519  
520 Returns: an L<App::WebPerimeterScan::Findings>.
521  
522 Side Effects: none.
523  
524 Usage: C<< my $count = $scanner->findings->count; >>
525  
526 =head3 EXAMPLE
527  
528 $scanner->run;
529 printf "%d finding(s)\n", $scanner->findings->count;
530  
531 =head3 API SPECIFICATION
532  
533 =head4 INPUT
534  
535 {}
536  
537 =head4 OUTPUT
538  
539 { type => 'object', isa => 'App::WebPerimeterScan::Findings' }
540  
541 =head3 MESSAGES
542  
543 None.
544  
545 =cut
546  
547 sub findings
548 0 ▶{
Subroutine findings: not called CC=1 SCAR=6.9
Pod documented
549 0 my ($self) = @_;
550  
551 0 return $self->{findings};
552 }
553  
554 =head2 http
555  
556 Purpose: the HTTP client, for its request count or to make further
557 requests under the same throttle.
558  
559 Args: none.
560  
561 Returns: an L<App::WebPerimeterScan::HTTP>.
562  
563 Side Effects: none.
564  
565 Usage: C<< printf "Requests: %d\n", $scanner->http->requests; >>
566  
567 =head3 EXAMPLE
568  
569 $scanner->run;
570 print $scanner->http->requests, " requests sent\n";
571  
572 =head3 API SPECIFICATION
573  
574 =head4 INPUT
575  
576 {}
577  
578 =head4 OUTPUT
579  
580 { type => 'object', isa => 'App::WebPerimeterScan::HTTP' }
581  
582 =head3 MESSAGES
583  
584 None.
585  
586 =cut
587  
588 sub http
589 7 ▶{
Subroutine http: called CC=1 SCAR=0.0
Pod documented
590 7 my ($self) = @_;
591  
592 7 return $self->{http};
593 }
594  
595 =head2 host_reports
596  
597 Purpose: what the scan learned about each host, for the report formats.
598  
599 Args: none.
600  
601 Returns: an array reference, sorted by host, of hash references with
602 C<host>, C<entry> (the scope entry), C<urls> (pages crawled),
603 C<open_ports>, C<base> (the root URL that answered), C<tls> (the
604 L<App::WebPerimeterScan::TLS/inspect> result, when TLS was inspected),
605 C<response> (the main page's header block) and C<responses> (the probes'
606 header blocks). See L</REPORT FORMAT>.
607  
608 Side Effects: none.
609  
610 Usage: C<< my $hosts = $scanner->host_reports; >>
611  
612 =head3 EXAMPLE
613  
614 for my $host (@{ $scanner->host_reports }) {
615 printf "%s: %d page(s)\n", $host->{host}, scalar @{ $host->{urls} };
616 }
617  
618 =head3 API SPECIFICATION
619  
620 =head4 INPUT
621  
622 {}
623  
624 =head4 OUTPUT
625  
626 { type => 'arrayref', element_type => 'hashref' }
627  
628 =head3 MESSAGES
629  
630 None.
631  
632 =cut
633  
634 sub host_reports
635 7 ▶{
Subroutine host_reports: called CC=1 SCAR=0.0
Pod documented
636 7 my ($self) = @_;
637  
638 7 return [ map { $self->{hosts}{$_} } sort keys %{ $self->{hosts} } ];
639 }
640  
641 =head2 run
642  
643 Purpose: scan every concrete host in the scope.
644  
645 Args: none.
646  
647 Returns: the L<App::WebPerimeterScan::Findings> collector.
648  
649 Side Effects: network traffic to the hosts in scope, and only to them. A
650 host that cannot be scanned (refused by the scope, or an unexpected error)
651 becomes a high-severity C<scan-error> finding rather than stopping the run,
652 so the other hosts are still scanned and the exit status shows the gap.
653  
654 Usage: C<< my $findings = $scanner->run; >>
655  
656 =head3 EXAMPLE
657  
658 my $findings = App::WebPerimeterScan->new(scope => $scope)->run;
659 exit(@{ $findings->at_or_above('high') } ? 1 : 0);
660  
661 =head3 API SPECIFICATION
662  
663 =head4 INPUT
664  
665 {}
666  
667 =head4 OUTPUT
668  
669 { type => 'object', isa => 'App::WebPerimeterScan::Findings' }
670  
671 =head3 MESSAGES
672  
673 Message Meaning / resolution
674 ------------------------------------------ ------------------------------
675 No scope was given; a scan needs one Pass scope => ... to new().
676 before it can send any traffic (fatal)
677 No concrete hosts in scope (only The scope file lists only
678 wildcards?) (fatal) *.domain lines; add a host.
679  
680 =head3 PSEUDOCODE
681  
682 croak without a scope or without concrete hosts
683 for each host in scope
684 refuse it if it is out of scope or private (and not allowed)
685 port scan; fetch the root over HTTPS then HTTP
686 inspect TLS where it is served
687 send the CORS, OPTIONS and plaintext-redirect probes
688 crawl same-scope links breadth-first within depth and budget
689 probe the well-known sensitive paths against a not-found baseline
690 optionally send the reflection marker
691 list source routes the crawl never reached
692 on any error, record a scan-error finding and go on
693 map findings to files and source lines; report sensitive files on disk
694  
695 =cut
696  
697 sub run
698 9 ▶{
Subroutine run: called CC=5 SCAR=16.1
Pod documented
699 9 my ($self) = @_;
700  
701 9 my $scope = $self->{scope};
702 9 ▶ $self->_throw('scan.no_scope') unless $scope;
Branch: 1 branch
BranchTrueFalseTotal
unless $scope 1 8 9
703  
704 8 my $hosts = $scope->hosts;
705 8 ▶ $self->_throw('scan.no_hosts') unless @{$hosts};
Branch: 1 branch
BranchTrueFalseTotal
unless @{$hosts;} 0 8 8
706  
707 # One host failing must not stop the others; the failure is a finding.
708 8 for my $entry (@{$hosts}) {
709 8 ▶ next if eval { $self->_scan_host($entry); 1 };
Branch: 1 branch
BranchTrueFalseTotal
if eval { do { $self->_scan_host($entry); 1 } } 6 2 8
710 $self->_report('scan-error',
711 host => $entry->{host},
712 evidence => App::WebPerimeterScan::I18N::clean_error($@),
713 vars => { host => $entry->{host} },
714 2 );
715 }
716  
717 8 $self->_source_pass;
718  
719 8 return $self->{findings};
720 }
721  
722 # _report
723 # Purpose: record one of the runner's own findings, with the rule
724 # table's defaults.
725 # Entry: $id is a key of %RULE with catalogue text.
726 # Exit: what Findings::report returns.
727 # Side effects: adds a finding.
728 5 ▶sub _report :Private
Subroutine _report: called CC=1 SCAR=0.0
729 {
730 5 my ($self, $id, %args) = @_;
731  
732 5 return $self->{findings}->report($id, rule => $RULE{$id}, %args);
733 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
734  
735 # _record_response
736 # Purpose: the parsed headers of a response plus the raw block exactly as
737 # it arrived, so a finding can be checked after the fact - by
738 # another person, or on a machine that cannot reach the host.
739 # Entry: $res is a fetch() result.
740 # Exit: a hashref, or undef for a failed request.
741 # Side effects: none. Duplicate headers are preserved: a second CSP or
742 # Set-Cookie header is part of the evidence.
743 20 ▶sub _record_response :Private
Subroutine _record_response: called CC=7 SCAR=19.9
744 {
745 20 my ($self, $url, $method, $res, $note) = @_;
746  
747 20 ▶ return undef unless $res && !$res->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $res and not $res->{"error"} 1 19 20
Condition66%2/3
$res and not $res->{"error"}
!ll&&!rl&&r
0119
MC/DC50%1/2
$res and not $res->{"error"}
$res not $res->{"error"}
Truth table66%2/3
A and not(B)
A= $resB= not $res->{"error"}
ABresult
0X0
100
111
748  
749 my %record = (
750 url => $url,
751 method => $method || 'GET',
752 status => $res->{status},
753 http_version => $res->{http_version},
754 reason => $res->{reason},
755 # A copy, so later changes to the response cannot alter the record.
756 77 ▶ headers => { map { $_ => [ @{ $res->{headers}{$_} } ] } keys %{ $res->{headers} || {} } },
Condition50%1/2
$res->{'headers'} || {}
l!l
190
MC/DC0%0/1
$res->{'headers'} || {}
$res->{'headers'}
Truth table50%1/2
A
A= $res->{'headers'}
Aresult
11
00
757 raw => $res->{raw_headers},
758 19 ▶ );
Condition50%1/2
$method || 'GET'
l!l
190
MC/DC0%0/1
$method || 'GET'
$method
Truth table50%1/2
A
A= $method
Aresult
11
00
759 19 ▶ $record{note} = $note if defined $note && length $note;
Branch: 1 branch
BranchTrueFalseTotal
if defined $note and length $note 12 7 19
Condition66%2/3
defined $note and length $note
!ll&&!rl&&r
7012
MC/DC50%1/2
defined $note and length $note
defined $note length $note
Truth table66%2/3
A and B
A= defined $noteB= length $note
ABresult
0X0
100
111
760  
761 19 return \%record;
762 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
763  
764 # _keep_response
765 # Purpose: record a probe's response in the host report.
766 # Entry: $report is a host report.
767 # Exit: $self.
768 # Side effects: appends to $report->{responses} for a successful request.
769 12 ▶sub _keep_response :Private
Subroutine _keep_response: called CC=2 SCAR=7.0
770 {
771 12 my ($self, $report, $url, $method, $res, $note) = @_;
772  
773 12 my $record = $self->_record_response($url, $method, $res, $note);
774 12 ▶ push @{ $report->{responses} }, $record if $record;
Branch: 1 branch
BranchTrueFalseTotal
if $record 12 0 12
775  
776 12 return $self;
777 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
778  
779 # _say
780 # Purpose: progress output in verbose mode.
781 # Entry: $key is a catalogue key; $args its placeholders.
782 # Exit: $self.
783 # Side effects: prints one line to STDERR when verbose.
784 18 ▶sub _say :Private
Subroutine _say: called CC=2 SCAR=7.1
785 {
786 18 my ($self, $key, $args) = @_;
787  
788 print STDERR $self->i18n('scan.progress', { message => $self->i18n($key, $args) }), "\n"
789 18 ▶ if $self->{verbose};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"verbose"} 0 18 18
790  
791 18 return $self;
792 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
793  
794 # _scan_host
795 # Purpose: every check against one scope entry.
796 # Entry: $entry is a concrete scope entry.
797 # Exit: $self.
798 # Side effects: network traffic; adds findings; records the host report.
799 # Croaks if the scope refuses the host.
800 8 ▶sub _scan_host :Private
Subroutine _scan_host: called CC=8 SCAR=21.5
801 {
802 8 my ($self, $entry) = @_;
803  
804 8 my $host = $entry->{host};
805 8 $self->{scope}->assert_target_allowed($host);
806 6 $self->_say('scan.progress.host', { host => $host });
807  
808 6 my $report = { host => $host, urls => [], responses => [], entry => $entry };
809 6 $self->{hosts}{$host} = $report;
810  
811 6 my $port_scan = $self->_scan_ports($report);
812  
813 6 ▶ my ($base, $root) = @{ $self->_fetch_root($entry) || [] };
Condition50%1/2
$self->_fetch_root($entry) || []
l!l
60
MC/DC0%0/1
$self->_fetch_root($entry) || []
$self->_fetch_root($entry)
Truth table50%1/2
A
A= $self->_fetch_root($entry)
Aresult
11
00
814 6 ▶ unless($base) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($base) 0 6 6
815 $self->_report('host-unreachable', host => $host,
816 0 vars => { tried => join(', ', @{ $self->_root_candidates($entry) }) });
817 0 return $self;
818 }
819  
820 6 $report->{base} = $base;
821 6 $self->_say('scan.progress.base', { base => $base });
822  
823 # The exact block the header checks were run against, so the report can
824 # be audited later without sending the request again.
825 6 $report->{response} = $self->_record_response($base, 'GET', $root, $self->i18n('scan.note.main'));
826  
827 6 $self->_inspect_tls($report, $port_scan);
828 6 $self->_probe_same_origin($report);
829 6 ▶ $self->_check_plaintext_hop($report) if $base =~ $HTTPS_URL && !$entry->{port};
Branch: 1 branch
BranchTrueFalseTotal
if $base =~ /$HTTPS_URL/ and not $entry->{"port"} 0 6 6
Condition33%1/3
$base =~ /$HTTPS_URL/ and not $entry->{"port"}
!ll&&!rl&&r
600
MC/DC0%0/2
$base =~ /$HTTPS_URL/ and not $entry->{"port"}
$base =~ /$HTTPS_URL/ not $entry->{"port"}
Truth table33%1/3
A and not(B)
A= $base =~ /$HTTPS_URL/B= not $entry->{"port"}
ABresult
0X0
100
111
830 6 $self->_crawl($report, $root);
831 6 ▶ $self->_path_probes($report) if $self->{path_probes};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"path_probes"} 1 5 6
832 6 ▶ $self->_reflection_probe($report) if $self->{reflection};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"reflection"} 1 5 6
833 6 ▶ $self->_unvisited_routes($report) if $self->{source};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"source"} 1 5 6
834  
835 6 return $self;
836 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
837  
838 # _scan_ports
839 # Purpose: the port scan and its findings.
840 # Entry: $report is the host report.
841 # Exit: the scan result.
842 # Side effects: TCP connections; adds findings; sets open_ports.
843 6 ▶sub _scan_ports :Private
Subroutine _scan_ports: called CC=1 SCAR=0.0
844 {
845 6 my ($self, $report) = @_;
846  
847 my $scan = App::WebPerimeterScan::Ports->scan(
848 host => $report->{host},
849 ports => $self->{ports},
850 timeout => $self->{connect_timeout},
851 6 );
852 6 $report->{open_ports} = $scan->{open};
853 6 App::WebPerimeterScan::Checks::check_ports($self->{findings}, host => $report->{host}, scan => $scan);
854  
855 6 return $scan;
856 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
857  
858 # _tls_port
859 # Purpose: which port to inspect TLS on, if any.
860 # Entry: $base is the URL that answered; $scan the port scan.
861 # Exit: the base URL's port when it is HTTPS; otherwise the first
862 # open TLS port (443, then 8443); otherwise undef.
863 # Side effects: none.
864 #
865 # Inspecting a port that answered the scan, rather than always 443, is the
866 # fix for a host with only 8443 open being reported as tls-unavailable. A
867 # failed handshake on an open TLS port is still inspected: it is a finding.
868 10 ▶sub _tls_port :Private
Subroutine _tls_port: called CC=3 SCAR=11.0
869 {
870 10 my ($self, $base, $scan) = @_;
871  
872 10 ▶ my %open = map { $_->{port} => 1 } @{ $scan->{open} || [] };
Condition50%1/2
$scan->{'open'} || []
l!l
100
MC/DC0%0/1
$scan->{'open'} || []
$scan->{'open'}
Truth table50%1/2
A
A= $scan->{'open'}
Aresult
11
00
873 10 my ($open_tls) = grep { $open{$_} } @TLS_PORTS;
874  
875 10 ▶ return $base =~ $HTTPS_URL ? URI->new($base)->port : $open_tls;
Branch: 1 branch
BranchTrueFalseTotal
$base =~ /$HTTPS_URL/ ? : 1 9 10
876 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
877  
878 # _inspect_tls
879 # Purpose: TLS inspection and its findings.
880 # Entry: $report has a base URL.
881 # Exit: $self.
882 # Side effects: TLS handshakes; adds findings; sets $report->{tls}.
883 6 ▶sub _inspect_tls :Private
Subroutine _inspect_tls: called CC=2 SCAR=9.9
884 {
885 6 my ($self, $report, $port_scan) = @_;
886  
887 6 my $port = $self->_tls_port($report->{base}, $port_scan);
888 6 ▶ return $self unless $port;
Branch: 1 branch
BranchTrueFalseTotal
unless $port 6 0 6
889  
890 my $tls = App::WebPerimeterScan::TLS->inspect(
891 host => $report->{host}, port => $port, timeout => $self->{connect_timeout},
892 0 );
893 0 $report->{tls} = $tls;
894 0 App::WebPerimeterScan::Checks::check_tls($self->{findings}, host => $report->{host}, tls => $tls);
895  
896 0 return $self;
897 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
898  
899 # _probe_same_origin
900 # Purpose: the CORS probe (a foreign Origin) and the OPTIONS probe.
901 # Entry: $report has a base URL.
902 # Exit: $self.
903 # Side effects: two requests; adds findings; records both responses.
904 6 ▶sub _probe_same_origin :Private
Subroutine _probe_same_origin: called CC=1 SCAR=0.0
905 {
906 6 my ($self, $report) = @_;
907  
908 6 my ($host, $base) = @{$report}{qw(host base)};
909 6 my $origin = App::WebPerimeterScan::Checks::probe_origin();
910  
911 6 my $cors = $self->{http}->fetch($base, headers => [ "Origin: $origin" ]);
912 6 App::WebPerimeterScan::Checks::check_cors($self->{findings}, host => $host, url => $base, res => $cors);
913 6 $self->_keep_response($report, $base, 'GET', $cors, $self->i18n('scan.note.origin', { origin => $origin }));
914  
915 6 my $options = $self->{http}->fetch($base, method => 'OPTIONS');
916 6 App::WebPerimeterScan::Checks::check_methods($self->{findings}, host => $host, url => $base, res => $options);
917 6 $self->_keep_response($report, $base, 'OPTIONS', $options);
918  
919 6 return $self;
920 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
921  
922 # _check_plaintext_hop
923 # Purpose: does plain HTTP redirect to HTTPS? The first request a
924 # visitor makes is plaintext, and HSTS cannot protect it.
925 # Entry: $report's base is HTTPS on the default port.
926 # Exit: $self.
927 # Side effects: one request; may add no-https-redirect.
928 0 ▶sub _check_plaintext_hop :Private
Subroutine _check_plaintext_hop: not called CC=6 SCAR=37.4
929 {
930 0 my ($self, $report) = @_;
931  
932 0 my $url = "http://$report->{host}/";
933 0 my $plain = $self->{http}->fetch($url);
934 0 $self->_keep_response($report, $url, 'GET', $plain, $self->i18n('scan.note.plaintext'));
935  
936 # Port 80 closed is fine: there is no plaintext hop to protect.
937 0 ▶ return $self if $plain->{error} || _redirects_to_https($plain);
Branch: 1 branch
BranchTrueFalseTotal
if $plain->{"error"} or _redirects_to_https($plain) 0 0 0
Condition0%0/3
$plain->{"error"} or _redirects_to_https($plain)
l!l&&r!l&&!r
000
MC/DC0%0/2
$plain->{"error"} or _redirects_to_https($plain)
$plain->{"error"} _redirects_to_https($plain)
Truth table0%0/3
A or B
A= $plain->{"error"}B= _redirects_to_https($plain)
ABresult
1X1
011
000
938  
939 0 ▶ my $location = $self->{http}->header($plain, 'location') // '';
Condition0%0/2
$self->{"http"}->header($plain, "location") // ""
l!l
00
MC/DC0%0/1
$self->{"http"}->header($plain, "location") // ""
$self->{"http"}->header($plain, "location")
Truth table0%0/2
A
A= $self->{"http"}->header($plain, "location")
Aresult
11
00
940 $self->_report('no-https-redirect', host => $report->{host}, vars => {
941 url => $url,
942 0 ▶ status => $plain->{status} // 0,
Branch: 1 branch
BranchTrueFalseTotal
length $location ? : 0 0 0
Condition0%0/2
$plain->{"status"} // 0
l!l
00
MC/DC0%0/1
$plain->{"status"} // 0
$plain->{"status"}
Truth table0%0/2
A
A= $plain->{"status"}
Aresult
11
00
943 location => $location,
944 redirect => length $location ? 'elsewhere' : 'none',
945 });
946  
947 0 return $self;
948 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
949  
950 # _redirects_to_https
951 # Purpose: is this response a redirect to an https:// URL?
952 # Entry: $res is a successful fetch() result.
953 # Exit: 1 or 0.
954 # Side effects: none.
955 4 ▶sub _redirects_to_https :Private
Subroutine _redirects_to_https: called CC=6 SCAR=18.3
956 {
957 4 my ($res) = @_;
958  
959 4 ▶ my $locations = $res->{headers}{location} || [];
Condition50%1/2
$res->{'headers'}{'location'} || []
l!l
40
MC/DC0%0/1
$res->{'headers'}{'location'} || []
$res->{'headers'}{'location'}
Truth table50%1/2
A
A= $res->{'headers'}{'location'}
Aresult
11
00
960 4 ▶ my $location = @{$locations} ? $locations->[-1] : '';
Branch: 1 branch
BranchTrueFalseTotal
@{$locations;} ? : 4 0 4
961  
962 4 ▶ return ($REDIRECT{ $res->{status} // 0 } && $location =~ $HTTPS_URL) ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$REDIRECT{$res->{'status'} // 0} && $location =~ /$HTTPS_URL/ ? : 2 2 4
Condition80%4/5
$REDIRECT{$res->{"status"} // 0} and $location =~ /$HTTPS_URL/
!ll&&!rl&&r
112
$res->{"status"} // 0
l!l
40
MC/DC100%2/2
$REDIRECT{$res->{"status"} // 0} and $location =~ /$HTTPS_URL/
$REDIRECT{$res->{"status"} // 0} $location =~ /$HTTPS_URL/
MC/DC0%0/1
$res->{"status"} // 0
$res->{"status"}
Truth table100%3/3
A and B
A= $REDIRECT{$res->{"status"} // 0}B= $location =~ /$HTTPS_URL/
ABresult
0X0
100
111
Truth table50%1/2
A
A= $res->{"status"}
Aresult
11
00
963 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
964  
965 # _crawl
966 # Purpose: check the site's own pages, breadth-first from the root,
967 # within depth and page budget.
968 # Entry: $root is the response already fetched for the base URL.
969 # Exit: $self.
970 # Side effects: requests; adds findings; fills $report->{urls}.
971 6 ▶sub _crawl :Private
Subroutine _crawl: called CC=12 SCAR=25.2
972 {
973 6 my ($self, $report, $root) = @_;
974  
975 6 my $base = $report->{base};
976 6 my @queue = ([ $base, 0 ]);
977 6 my %seen = ($base => 1);
978 6 my %allowed; # host => 1/0, so each host is resolved once
979 6 my $fetched = 0;
980  
981 6 ▶ while(@queue && $fetched < $self->{max_urls}) {
Branch: 1 branch
BranchTrueFalseTotal
if (@queue and $fetched < $self->{"max_urls"}) 7 6 13
Condition66%2/3
@queue and $fetched < $self->{"max_urls"}
!ll&&!rl&&r
607
MC/DC50%1/2
@queue and $fetched < $self->{"max_urls"}
@queue $fetched < $self->{"max_urls"}
Truth table66%2/3
A and B
A= @queueB= $fetched < $self->{"max_urls"}
ABresult
0X0
100
111
982 7 my ($url, $depth) = @{ shift @queue };
983  
984 7 ▶ my $res = $url eq $base ? $root : $self->{http}->fetch($url);
Branch: 1 branch
BranchTrueFalseTotal
$url eq $base ? : 6 1 7
985 7 ▶ next if $res->{error};
Branch: 1 branch
BranchTrueFalseTotal
if $res->{"error"} 0 7 7
986  
987 7 $fetched++;
988 7 push @{ $report->{urls} }, $url;
989 7 App::WebPerimeterScan::Checks::check_page($self->{findings}, host => $report->{host}, url => $url, res => $res);
990  
991 7 ▶ next if $depth >= $self->{depth} || ($res->{content_type} // '') !~ $HTML_TYPE;
Branch: 1 branch
BranchTrueFalseTotal
if $depth >= $self->{"depth"} or not +($res->{"content_type"} // "") =~ /$HTML_TYPE/ 1 6 7
Condition60%3/5
$depth >= $self->{"depth"} or not +($res->{"content_type"} // "") =~ /$HTML_TYPE/
l!l&&r!l&&!r
106
$res->{"content_type"} // ""
l!l
60
MC/DC50%1/2
$depth >= $self->{"depth"} or not +($res->{"content_type"} // "") =~ /$HTML_TYPE/
$depth >= $self->{"depth"} $res->{"content_type"}
Truth table66%2/3
A or not(B)
A= $depth >= $self->{"depth"}B= $res->{"content_type"}
ABresult
1X1
001
010
992  
993 # allows(), not just covers(): a wildcard entry covers names nobody
994 # listed, and one resolving to an internal address must be refused.
995 6 for my $link (@{ $self->_links($res->{body}, $url) }) {
996 3 my $host = URI->new($link)->host;
997 3 ▶ next if $seen{$link}++ || !($allowed{$host} //= $self->{scope}->allows($host));
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$link}++ or not $allowed{$host} //= $self->{"scope"}->allows($host) 2 1 3
Condition50%3/6
$seen{$link}++ or not $allowed{$host} //= $self->{"scope"}->allows($host)
l!l&&r!l&&!r
021
$allowed{$host} //= $self->{"scope"}->allows($host)
l!l&&r!l&&!r
030
MC/DC50%1/2
$seen{$link}++ or not $allowed{$host} //= $self->{"scope"}->allows($host)
$seen{$link}++ not $allowed{$host} //= $self->{"scope"}->allows($host)
MC/DC0%0/2
$allowed{$host} //= $self->{"scope"}->allows($host)
$allowed{$host} $self->{"scope"}->allows($host)
Truth table66%2/3
A or not(B)
A= $seen{$link}++B= not $allowed{$host} //= $self->{"scope"}->allows($host)
ABresult
1X1
011
000
Truth table33%1/3
A //= B
A= $allowed{$host}B= $self->{"scope"}->allows($host)
ABresult
1X1
011
000
998 1 push @queue, [ $link, $depth + 1 ];
999 }
1000 }
1001  
1002 6 $self->_say('scan.progress.urls', { count => $fetched, host => $report->{host} });
1003  
1004 6 return $self;
1005 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1006  
1007 # _path_probes
1008 # Purpose: the sensitive-path probes, against a not-found baseline.
1009 # Entry: $report has a base URL.
1010 # Exit: $self.
1011 # Side effects: requests; adds findings.
1012 1 ▶sub _path_probes :Private
Subroutine _path_probes: called CC=4 SCAR=14.6
1013 {
1014 1 my ($self, $report) = @_;
1015  
1016 1 my ($host, $base) = @{$report}{qw(host base)};
1017  
1018 # A path that certainly does not exist. Its fingerprint tells us whether
1019 # this application answers 200 for everything, in which case a 200 on
1020 # /.git/config proves nothing.
1021 1 my $path = '/ws-baseline-' . _random_token() . '.html';
1022 1 my $miss = $self->{http}->fetch(($base =~ s{/+\z}{}r) . $path);
1023  
1024 $self->_report('catch-all-200', host => $host, vars => { path => $path })
1025 1 ▶ if !$miss->{error} && ($miss->{status} // 0) == $HTTP_OK;
Branch: 1 branch
BranchTrueFalseTotal
if not $miss->{"error"} and ($miss->{"status"} // 0) == $HTTP_OK 0 1 1
Condition40%2/5
not $miss->{"error"} and ($miss->{"status"} // 0) == $HTTP_OK
!ll&&!rl&&r
010
$miss->{"status"} // 0
l!l
10
MC/DC0%0/2
not $miss->{"error"} and ($miss->{"status"} // 0) == $HTTP_OK
not $miss->{"error"} $miss->{"status"}
Truth table33%1/3
not(A) and B
A= not $miss->{"error"}B= $miss->{"status"}
ABresult
0X0
100
111
1026  
1027 App::WebPerimeterScan::Checks::check_paths($self->{findings},
1028 http => $self->{http},
1029 1 host => $host,
1030 base => $base,
1031 baseline => App::WebPerimeterScan::Checks::fingerprint($miss),
1032 );
1033  
1034 1 return $self;
1035 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1036  
1037 # _reflection_probe
1038 # Purpose: opt-in: send one harmless marker in a query parameter and see
1039 # whether it comes back unencoded. A hint for a human, not a
1040 # proof of cross-site scripting.
1041 # Entry: $report has a base URL.
1042 # Exit: $self.
1043 # Side effects: one request; may add reflected-input.
1044 1 ▶sub _reflection_probe :Private
Subroutine _reflection_probe: called CC=5 SCAR=16.8
1045 {
1046 1 my ($self, $report) = @_;
1047  
1048 1 my $marker = 'wsp' . _random_token();
1049 1 my $sent = "$REFLECTION_PARAMETER=$marker$REFLECTION_ENCODED";
1050  
1051 1 my $uri = URI->new($report->{base});
1052 1 ▶ $uri->query(join '&', grep { defined && length } $uri->query, $sent);
Condition66%2/3
defined $_ && length $_
!ll&&!rl&&r
101
MC/DC50%1/2
defined $_ && length $_
defined $_ length $_
Truth table66%2/3
A && B
A= defined $_B= length $_
ABresult
0X0
100
111
1053  
1054 1 my $res = $self->{http}->fetch($uri->as_string);
1055 1 ▶ my $reflected = !$res->{error} && index($res->{body} // '', $marker . $REFLECTION_RAW) >= 0;
Condition40%2/5
!$res->{'error'} && index($res->{'body'} // '', $marker . $REFLECTION_RAW) != -1
!ll&&!rl&&r
001
$res->{"body"} // ""
l!l
10
MC/DC0%0/2
!$res->{'error'} && index($res->{'body'} // '', $marker . $REFLECTION_RAW) != -1
!$res->{'error'} index($res->{'body'} // '', $marker . $REFLECTION_RAW) != -1
MC/DC0%0/1
$res->{"body"} // ""
$res->{"body"}
Truth table33%1/3
not(A) && B
A= !$res->{'error'}B= index($res->{'body'} // '', $marker . $REFLECTION_RAW) != -1
ABresult
0X0
100
111
Truth table50%1/2
A
A= $res->{"body"}
Aresult
11
00
1056  
1057 $self->_report('reflected-input',
1058 1 ▶ host => $report->{host}, url => $uri->as_string, vars => { parameter => $sent },
Branch: 1 branch
BranchTrueFalseTotal
if $reflected 1 0 1
1059 ) if $reflected;
1060  
1061 1 return $self;
1062 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1063  
1064 # _source_pass
1065 # Purpose: the source-aware pass. A black-box scan can only say that a
1066 # URL answered; given a docroot and a source tree it can also
1067 # say which file is served and which line defines the endpoint.
1068 # Entry: the scan has finished.
1069 # Exit: $self.
1070 # Side effects: annotates findings; may add source-file-in-docroot.
1071 8 ▶sub _source_pass :Private
Subroutine _source_pass: called CC=6 SCAR=17.9
1072 {
1073 8 my ($self) = @_;
1074  
1075 8 my $source = $self->{source};
1076 8 ▶ return $self unless $source;
Branch: 1 branch
BranchTrueFalseTotal
unless $source 7 1 8
1077  
1078 1 for my $finding (@{ $self->{findings}->all }) {
1079 13 ▶ my $path = _url_path($finding->{url}) // next;
Condition100%2/2
_url_path($finding->{"url"}) // (next)
l!l
49
MC/DC100%1/1
_url_path($finding->{"url"}) // (next)
_url_path($finding->{"url"})
Truth table100%2/2
A
A= _url_path($finding->{"url"})
Aresult
11
00
1080 4 my $file = $source->file_for($path);
1081 4 my $route = $source->route_for($path);
1082  
1083 $self->{findings}->annotate($finding,
1084 4 ▶ ($file ? (file => $file->{path}) : ()),
Branch: 2 branches
BranchTrueFalseTotal
$file ? : 1 3 4
$route ? : 0 4 4
1085 ($route ? (source => "$route->{file}:$route->{line}") : ()),
1086 );
1087 }
1088  
1089 1 return $self->_docroot_files($source);
1090 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1091  
1092 # _docroot_files
1093 # Purpose: sensitive files present under a docroot but not served.
1094 # Entry: $source has docroots.
1095 # Exit: $self.
1096 # Side effects: may add source-file-in-docroot, keyed on the file so
1097 # several probes cannot repeat it.
1098 1 ▶sub _docroot_files :Private
Subroutine _docroot_files: called CC=3 SCAR=11.0
1099 {
1100 1 my ($self, $source) = @_;
1101  
1102 # Only the serious probes, and only those the wire did not already find.
1103 1 my %served = map { $_->{id} => 1 } @{ $self->{findings}->all };
1104 18 ▶ my @wanted = grep { $DOCROOT_SEVERITY{ $_->{severity} } && !$served{ $_->{id} } }
Condition100%3/3
$DOCROOT_SEVERITY{$_->{'severity'}} && !$served{$_->{'id'}}
!ll&&!rl&&r
7110
MC/DC100%2/2
$DOCROOT_SEVERITY{$_->{'severity'}} && !$served{$_->{'id'}}
$DOCROOT_SEVERITY{$_->{'severity'}} !$served{$_->{'id'}}
Truth table100%3/3
A && not(B)
A= $DOCROOT_SEVERITY{$_->{'severity'}}B= !$served{$_->{'id'}}
ABresult
0X0
100
111
1105 1 @{ App::WebPerimeterScan::Checks::sensitive_probes() };
1106  
1107 1 for my $hit (@{ $source->sensitive_on_disk(\@wanted) }) {
1108 $self->_report('source-file-in-docroot',
1109 key => $hit->{path},
1110 severity => $DOCROOT_SEVERITY{ $hit->{probe}{severity} },
1111 evidence => $hit->{path},
1112 vars => { file => $hit->{relative} },
1113 1 );
1114 }
1115  
1116 1 return $self;
1117 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1118  
1119 # _unvisited_routes
1120 # Purpose: the routes in the source that the scan never requested: the
1121 # coverage gap a link-following scan cannot see.
1122 # Entry: $self->{source} is set; the crawl and probes are done.
1123 # Exit: $self.
1124 # Side effects: may add source-routes-unvisited.
1125 1 ▶sub _unvisited_routes :Private
Subroutine _unvisited_routes: called CC=3 SCAR=11.0
1126 {
1127 1 my ($self, $report) = @_;
1128  
1129 1 my $visited = $self->_visited_paths($report);
1130 my @unvisited = grep {
1131 2 my $re = $_->{re};
1132 2 !grep { $_ =~ $re } @{$visited};
1133 1 } @{ $self->{source}->routes };
1134 1 ▶ return $self unless @unvisited;
Branch: 1 branch
BranchTrueFalseTotal
unless @unvisited 0 1 1
1135  
1136 # The first few as evidence. The slice stops at the end of the list: a
1137 # slice past the end, aliased by map or grep, would extend the array.
1138 1 my $limit = $self->{unvisited_evidence_limit};
1139 1 ▶ my @shown = @unvisited > $limit ? @unvisited[0 .. $limit - 1] : @unvisited;
Branch: 1 branch
BranchTrueFalseTotal
@unvisited > $limit ? : 0 1 1
1140  
1141 $self->_report('source-routes-unvisited',
1142 host => $report->{host},
1143 1 evidence => join('; ', map { "$_->{file}:$_->{line} $_->{method} $_->{pattern}" } @shown),
1144 vars => { count => scalar @unvisited },
1145 );
1146  
1147 1 return $self;
1148 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1149  
1150 # _visited_paths
1151 # Purpose: every URL path the scan is known to have requested.
1152 # Entry: $report is the host report.
1153 # Exit: arrayref of distinct paths.
1154 # Side effects: none.
1155 #
1156 # Path probes are not part of the crawl, but a finding that names a URL
1157 # proves that URL was requested, so a route serving it is not a gap.
1158 1 ▶sub _visited_paths :Private
Subroutine _visited_paths: called CC=1 SCAR=0.0
1159 {
1160 1 my ($self, $report) = @_;
1161  
1162 6 my %visited = map { $_ => 1 } grep { defined }
1163 1 map { _url_path($_) } @{ $report->{urls} }, map { $_->{url} } @{ $self->{findings}->all };
1164  
1165 1 return [ keys %visited ];
1166 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1167  
1168 # _url_path
1169 # Purpose: the path of a URL.
1170 # Entry: $url may be undef or empty.
1171 # Exit: a non-empty path, or undef.
1172 # Side effects: none.
1173 27 ▶sub _url_path :Private
Subroutine _url_path: called CC=7 SCAR=19.7
1174 {
1175 27 my ($url) = @_;
1176  
1177 27 ▶ my $uri = (defined $url && length $url) ? eval { URI->new($url) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $url && length $url ? : 10 17 27
Condition66%2/3
defined $url and length $url
!ll&&!rl&&r
01710
MC/DC50%1/2
defined $url and length $url
defined $url length $url
Truth table66%2/3
A and B
A= defined $urlB= length $url
ABresult
0X0
100
111
1178 27 ▶ my $path = ($uri && $uri->can('path')) ? $uri->path : undef;
Branch: 1 branch
BranchTrueFalseTotal
$uri && $uri->can('path') ? : 10 17 27
Condition66%2/3
$uri and $uri->can("path")
!ll&&!rl&&r
17010
MC/DC50%1/2
$uri and $uri->can("path")
$uri $uri->can("path")
Truth table66%2/3
A and B
A= $uriB= $uri->can("path")
ABresult
0X0
100
111
1179  
1180 27 ▶ return (defined $path && length $path) ? $path : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $path && length $path ? : 10 17 27
Condition66%2/3
defined $path and length $path
!ll&&!rl&&r
17010
MC/DC50%1/2
defined $path and length $path
defined $path length $path
Truth table66%2/3
A and B
A= defined $pathB= length $path
ABresult
0X0
100
111
1181 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1182  
1183 # _root_candidates
1184 # Purpose: the root URLs to try for a scope entry, in order.
1185 # Entry: $entry is a concrete scope entry.
1186 # Exit: arrayref of URLs.
1187 # Side effects: none.
1188 10 ▶sub _root_candidates :Private
Subroutine _root_candidates: called CC=6 SCAR=17.9
1189 {
1190 10 my ($self, $entry) = @_;
1191  
1192 10 my ($host, $port, $scheme) = @{$entry}{qw(host port scheme)};
1193  
1194 # An explicit port implies its scheme unless one was given; with
1195 # neither, HTTPS is tried first and HTTP second.
1196 10 ▶ $scheme //= $TLS_PORT{$port} ? 'https' : 'http' if $port;
Branch: 2 branches
BranchTrueFalseTotal
if $port 8 2 10
$TLS_PORT{$port} ? : 1 7 8
Condition50%1/2
$scheme //= $TLS_PORT{$port} ? "https" : "http"
l!l
08
MC/DC0%0/1
$scheme //= $TLS_PORT{$port} ? "https" : "http"
$scheme
Truth table50%1/2
A
A= $scheme
Aresult
11
00
1197 10 ▶ my $authority = $port ? "$host:$port" : $host;
Branch: 1 branch
BranchTrueFalseTotal
$port ? : 8 2 10
1198  
1199 10 ▶ return [ map { "$_://$authority/" } $scheme ? ($scheme) : qw(https http) ];
Branch: 1 branch
BranchTrueFalseTotal
$scheme ? : 9 1 10
1200 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1201  
1202 # _fetch_root
1203 # Purpose: the first root URL that answers.
1204 # Entry: $entry is a concrete scope entry.
1205 # Exit: [base URL, response], or undef if none answered.
1206 # Side effects: requests.
1207 6 ▶sub _fetch_root :Private
Subroutine _fetch_root: called CC=3 SCAR=11.4
1208 {
1209 6 my ($self, $entry) = @_;
1210  
1211 6 for my $base (@{ $self->_root_candidates($entry) }) {
1212 6 my $res = $self->{http}->fetch($base);
1213 6 ▶ return [ $base, $res ] unless $res->{error};
Branch: 1 branch
BranchTrueFalseTotal
unless $res->{"error"} 6 0 6
1214 }
1215  
1216 0 return undef;
1217 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1218  
1219 # _links
1220 # Purpose: the crawlable links on a page.
1221 # Entry: $body is HTML; $base is the page URL.
1222 # Exit: arrayref of absolute http(s) URLs without fragments, assets
1223 # skipped, at most max_links_per_page.
1224 # Side effects: none.
1225 8 ▶sub _links :Private
Subroutine _links: called CC=14 SCAR=26.9
1226 {
1227 8 my ($self, $body, $base) = @_;
1228  
1229 8 ▶ my $extor = (defined $body && length $body) ? eval { HTML::SimpleLinkExtor->new($base) } : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $body && length $body ? : 7 1 8
Condition66%2/3
defined $body and length $body
!ll&&!rl&&r
107
MC/DC50%1/2
defined $body and length $body
defined $body length $body
Truth table66%2/3
A and B
A= defined $bodyB= length $body
ABresult
0X0
100
111
1230 8 ▶ return [] unless $extor && eval { $extor->parse($body); 1 };
Branch: 1 branch
BranchTrueFalseTotal
unless $extor and eval { do { $extor->parse($body); 1 } } 1 7 8
Condition66%2/3
$extor and eval { do { $extor->parse($body); 1 } }
!ll&&!rl&&r
107
MC/DC50%1/2
$extor and eval { do { $extor->parse($body); 1 } }
$extor eval { do { $extor->parse($body); 1 } }
Truth table66%2/3
A and B
A= $extorB= eval { do { $extor->parse($body); 1 } }
ABresult
0X0
100
111
1231  
1232 7 my (@links, %seen);
1233 7 for my $found ($extor->links) {
1234 9 ▶ my $uri = eval { URI->new_abs($found, $base) } or next;
Branch: 1 branch
BranchTrueFalseTotal
my $uri = eval { do { "URI"->new_abs($found, $base) } } or next 9 0 9
1235 9 ▶ next unless $WEB_SCHEME{ lc($uri->scheme // '') };
Branch: 1 branch
BranchTrueFalseTotal
unless $WEB_SCHEME{lc($uri->scheme // "")} 1 8 9
Condition50%1/2
$uri->scheme // ""
l!l
90
MC/DC0%0/1
$uri->scheme // ""
$uri->scheme
Truth table50%1/2
A
A= $uri->scheme
Aresult
11
00
1236  
1237 8 $uri->fragment(undef);
1238 8 ▶ my ($extension) = ($uri->path // '') =~ /\.([A-Za-z0-9]+)\z/;
Condition50%1/2
$uri->path // ""
l!l
80
MC/DC0%0/1
$uri->path // ""
$uri->path
Truth table50%1/2
A
A= $uri->path
Aresult
11
00
1239 8 ▶ next if defined $extension && $SKIP_EXTENSION{ lc $extension };
Branch: 1 branch
BranchTrueFalseTotal
if defined $extension and $SKIP_EXTENSION{lc $extension} 2 6 8
Condition66%2/3
defined $extension and $SKIP_EXTENSION{lc $extension}
!ll&&!rl&&r
602
MC/DC50%1/2
defined $extension and $SKIP_EXTENSION{lc $extension}
defined $extension $SKIP_EXTENSION{lc $extension}
Truth table66%2/3
A and B
A= defined $extensionB= $SKIP_EXTENSION{lc $extension}
ABresult
0X0
100
111
1240  
1241 6 my $string = $uri->as_string;
1242 6 ▶ next if $seen{$string}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$string}++ 1 5 6
1243 5 push @links, $string;
1244 5 ▶ last if @links >= $self->{max_links_per_page};
Branch: 1 branch
BranchTrueFalseTotal
if @links >= $self->{"max_links_per_page"} 0 5 5
1245 }
1246  
1247 7 return \@links;
1248 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1249  
1250 # _random_token
1251 # Purpose: a short random string for probe paths and markers.
1252 # Entry: none.
1253 # Exit: token_length characters of [a-z0-9].
1254 # Side effects: consumes rand(). Not cryptographic: it only has to be
1255 # unlikely to exist on the server.
1256 2 ▶sub _random_token :Private
Subroutine _random_token: called CC=1 SCAR=0.0
1257 {
1258 2 return join '', map { $TOKEN_CHARS[ int rand @TOKEN_CHARS ] } 1 .. $config{token_length};
1259 27 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
1260  
1261 =head1 RULES
1262  
1263 These rules come from the scan itself. The page, TLS, port and path rules
1264 are listed in L<App::WebPerimeterScan::Checks/RULES>.
1265  
1266 Rule id Severity Reported when
1267 ------------------------ -------- ---------------------------------------
1268 scan-error high a host was refused (not listed, or
1269 private without allow_private), or its
1270 scan failed part way
1271 host-unreachable high neither https:// nor http:// answered
1272 at the root of the host
1273 no-https-redirect medium the site is served over HTTPS, but
1274 http:// does not answer with a 301,
1275 302, 303, 307 or 308 to https://
1276 (checked only when the scope line names
1277 no port; port 80 closed is fine)
1278 catch-all-200 info with path probes on: a random path
1279 that cannot exist
1280 answers 200, so the server answers
1281 every path alike
1282 reflected-input medium with reflection on: the marker sent
1283 in the query came back unescaped
1284 source-file-in-docroot medium with docroots: a file that a critical
1285 path probe looks for is on disk
1286 low ... a file that a high path probe
1287 looks for is on disk
1288 (files for medium and low probes are
1289 not looked for on disk)
1290 source-routes-unvisited info with source_dirs: routes in the code
1291 that the crawl never reached
1292  
1293 =head1 COMMON PITFALLS
1294  
1295 =over 4
1296  
1297 =item * B<Use a new object for each scan.> C<run> adds to the same
1298 findings collection every time it is called, and C<host_reports> keeps the
1299 last details for each host. A second C<run> on the same object gives the
1300 findings of both runs together.
1301  
1302 =item * B<A scope with only wildcard lines has nothing to scan.>
1303 C<*.example.com> lets the crawler follow links to those hosts, but is not
1304 a starting point. C<run> dies with "No concrete hosts in scope" when the
1305 scope lists no concrete host.
1306  
1307 =item * B<C<run> dies only for a missing scope or an empty one.> A host
1308 that is refused (not listed, or private), cannot be reached or fails
1309 during the scan does not stop the scan. It is recorded as a C<scan-error>
1310 or C<host-unreachable> finding, at high severity, and the next host is
1311 scanned. Look for those findings before you trust a clean result.
1312  
1313 =item * B<C<undef> means "use the default".> C<< depth => undef >> is the
1314 same as leaving C<depth> out. To turn something off, pass 0, for example
1315 C<< path_probes => 0 >>.
1316  
1317 =item * B<C<delay> protects the site.> The default waits half a second
1318 between two requests to the same host. Do not set it to 0 for a real
1319 site.
1320  
1321 =item * B<C<docroots> and C<source_dirs> are paths on this machine.> They
1322 help only when this machine holds the files of the site being scanned.
1323 A path that does not exist is ignored without a warning.
1324  
1325 =item * B<Private addresses are refused by default.> To scan a test server
1326 on C<127.0.0.1> or on your local network, create the scope with
1327 C<< App::WebPerimeterScan::Scope->new(allow_private => 1) >>, or pass
1328 C<--allow-private> to F<webperimeter>.
1329  
1330 =item * B<No findings is not proof of safety.> The tool checks a fixed
1331 list of things. See L</LIMITATIONS>.
1332  
1333 =item * B<Progress messages go to STDERR.> With C<< verbose => 1 >>, the
1334 progress lines are printed to STDERR, so they never mix with a report on
1335 STDOUT.
1336  
1337 =back
1338  
1339 =head1 ENCODING
1340  
1341 The scanner works with bytes from start to end, and never decodes or
1342 encodes text itself. In practice:
1343  
1344 =over 4
1345  
1346 =item * B<Web pages in any language are fine.> Page text, titles and
1347 header values are kept as the bytes the server sent. A UTF-8 page,
1348 including one with emoji, appears correctly in all three report formats.
1349  
1350 =item * B<Links with non-ASCII characters are followed correctly.> A link
1351 written as raw UTF-8 and the same link written with C<%> escapes are the
1352 same URL to the crawler.
1353  
1354 =item * B<International host names work in the scope file.> Save the file
1355 as UTF-8 and write the names as they are; they are scanned under their
1356 ASCII (Punycode) form, which is also the form shown in the reports. See
1357 L<App::WebPerimeterScan::Scope/ENCODING>.
1358  
1359 =item * B<File and directory names are bytes.> C<docroots>,
1360 C<source_dirs> and the paths in the findings are used exactly as the
1361 operating system gives them.
1362  
1363 =item * B<Messages come from the catalogue.> They are English and ASCII
1364 today. Error messages may contain the operating system's own text, in
1365 the language of your locale.
1366  
1367 =item * B<If you add your own findings or metadata, add bytes.> Mixing
1368 Perl character strings with the scanner's byte strings encodes the bytes a
1369 second time. See L<App::WebPerimeterScan::Findings/ENCODING>.
1370  
1371 =back
1372  
1373 =head1 LIMITATIONS
1374  
1375 These are the design weaknesses a reviewer should know about before relying
1376 on a clean report. Each is a reason the tool can miss something, or report
1377 something that is not there.
1378  
1379 =over 4
1380  
1381 =item * B<A clean report is not a clean site.> The checks are passive and
1382 unauthenticated: no form is submitted, no login is attempted and no
1383 parameter other than the opt-in reflection marker is altered. Anything
1384 behind a login, or reachable only by a POST, is outside what this tool can
1385 see.
1386  
1387 =item * B<DNS can change between the check and the request.> The scope is
1388 checked against the name, and each connection resolves the name again, so a
1389 name that resolves to a public address for the check and a private one for
1390 the request (DNS rebinding) defeats C<--allow-private>. The complete fix is
1391 to resolve once and connect to that address, carrying the name in SNI and
1392 the C<Host> header; the HTTP, TLS and port layers would all need to take an
1393 address.
1394  
1395 =item * B<IPv4 only for the port scan.> The private-address check
1396 considers IPv6 addresses, but the port scan connects over IPv4 only.
1397  
1398 =item * B<The crawl follows links, not the application.> Script-built
1399 navigation, forms and API calls are not followed. C<--source> mitigates
1400 this by listing the routes the crawl never reached, but only for
1401 Mojolicious-style route declarations.
1402  
1403 =item * B<Header rules are host-wide.> A missing header is reported once
1404 per host, with the first page that lacked it as evidence. A site whose
1405 pages differ in policy shows only the first gap.
1406  
1407 =item * B<Multiple CSP headers are read as one policy.> Browsers apply each
1408 policy separately, so a strict second policy can make a lax first one safe;
1409 the lax one is still reported.
1410  
1411 =item * B<Version rules trust banners.> PHP2Shell and the OpenSSH check use
1412 the version the server announces. Back-ported fixes (as distributions
1413 ship) are reported as vulnerable; a hidden or faked banner is not reported
1414 at all.
1415  
1416 =item * B<Path probes are a fixed list.> Eighteen well-known files are
1417 requested; a secret under any other name is only found by C<--docroot>.
1418  
1419 =item * B<The docroot check is not tied to a host.> A document root is a
1420 directory on this machine, and nothing says which host in the scope serves
1421 it. So a sensitive file that any host already served over the network is
1422 not reported again from the disk, even when the scope holds several sites
1423 and the docroot belongs to a different one. Scan one site at a time when
1424 you give C<--docroot>.
1425  
1426 =item * B<Only English is catalogued.> The message layer is ready for
1427 other languages (see L<App::WebPerimeterScan::I18N>), but none ships.
1428  
1429 =item * B<Perl 5.26 is required> because L<Params::Validate::Strict> needs it.
1430 The code in this distribution would run on 5.14.
1431  
1432 =back
1433  
1434 =encoding utf8
1435  
1436 =head1 FORMAL SPECIFICATION
1437  
1438 This section states exactly what each method does, in the Z notation. You
1439 do not need it to use the module: the method descriptions above say the same
1440 thing in words. It is here for reviewers and for anyone who writes tests.
1441  
1442 How to read a schema box:
1443  
1444 =over 4
1445  
1446 =item * The part above the middle line declares names. A name that ends in
1447 C<?> is an input. A name that ends in C<!> is an output.
1448  
1449 =item * The part below the middle line lists facts that are true after the
1450 call.
1451  
1452 =item * A name with a dash after it, such as C<items'>, is the value after
1453 the call. The same name without the dash is the value before the call.
1454  
1455 =item * C<Delta X> (written with the Greek capital delta) means the call
1456 may change the state C<X>. C<Xi X> (the Greek capital xi) means the call
1457 does not change it.
1458  
1459 =back
1460  
1461 The symbols used are:
1462  
1463 Δ may change Ξ does not change • such that
1464 ∀ for all ∃ there is ¬ not
1465 ∧ and ∨ or ⇒ implies
1466 ⇔ if and only if ≠ not equal ≤ ≥ at most, at least
1467 ∈ is in ∉ is not in ⊆ is a subset of
1468 ∪ union ∩ intersection ∅ the empty set
1469 ℙ X sets of X ⊥ undefined (undef) … and so on
1470 ⟨⟩ the empty sequence ⁀ join sequences ⇸ partial mapping
1471 ↦ maps to ⊕ override a mapping ↾ keep only these keys
1472 ∘ compose → function × ÷ − times, divide, minus
1473 ℕ 0, 1, 2, ... ℕ₁ 1, 2, 3, ... ℝ real numbers
1474 𝔹 true or false ⌊x⌋ x rounded down
1475 dom M the keys of M ran M the values of M #S the size of S
1476  
1477  
1478 =head2 new
1479  
1480 ┌─ InitScanner ───────────────────────────────────────
1481 │ Scanner'; args? : NAME ⇸ VALUE
1482 ├─────────────────────────────────────────────────────
1483 │ dom args? ⊆ dom config
1484 │ ∀ k ∈ dom config • setting'(k) = (config ⊕ args?)(k)
1485 │ findings' = ∅ ∧ hosts' = ∅
1486 │ source' ≠ ⊥ ⇔ docroots? ≠ ⟨⟩ ∨ source_dirs? ≠ ⟨⟩
1487 └─────────────────────────────────────────────────────
1488  
1489 =head2 findings
1490  
1491 ┌─ FindingsOf ────────────────────────────────────────
1492 │ ΞScanner; result! : FINDINGS
1493 ├─────────────────────────────────────────────────────
1494 │ result! = findings
1495 └─────────────────────────────────────────────────────
1496  
1497 =head2 http
1498  
1499 ┌─ HttpOf ────────────────────────────────────────────
1500 │ ΞScanner; result! : HTTP
1501 ├─────────────────────────────────────────────────────
1502 │ result! = http
1503 └─────────────────────────────────────────────────────
1504  
1505 =head2 host_reports
1506  
1507 ┌─ HostReports ───────────────────────────────────────
1508 │ ΞScanner; result! : seq HOSTREPORT
1509 ├─────────────────────────────────────────────────────
1510 │ ran result! = ran hosts
1511 │ ∀ i : 1 .. #result! − 1 • result!(i).host < result!(i + 1).host
1512 └─────────────────────────────────────────────────────
1513  
1514 =head2 run
1515  
1516 ┌─ Run ───────────────────────────────────────────────
1517 │ ΔScanner; result! : FINDINGS
1518 ├─────────────────────────────────────────────────────
1519 │ scope ≠ ⊥ ∧ hosts(scope) ≠ ⟨⟩
1520 │ contacted' ⊆ { h | allows(scope, h) }
1521 │ ∀ e ∈ ran hosts(scope) •
1522 │ scanned(e) ∨ (scan-error, e.host) ∈ keys(findings')
1523 │ result! = findings'
1524 └─────────────────────────────────────────────────────
1525  
1526 =head1 STATE DIAGRAM
1527  
1528 A scanner object is created READY. C<run> takes it through the steps
1529 below for each host, then to DONE. C<findings>, C<http> and
1530 C<host_reports> can be called in any state and change nothing.
1531  
1532 The object:
1533  
1534 new(scope, options)
1535 | unknown option or bad value -> croak (no object)
1536 v
1537 +-------------+
1538 | READY | findings empty; no traffic sent
1539 +-------------+
1540 |
1541 | run()
1542 | no scope -> croak "No scope was given"
1543 | no concrete host -> croak "No concrete hosts in scope"
1544 v
1545 +-------------+
1546 | SCANNING | each host in scope->hosts, in turn (below)
1547 +-------------+
1548 |
1549 | every host done
1550 v
1551 +-------------+
1552 | SOURCE PASS | only with docroots or source_dirs: add file and
1553 +-------------+ source to findings; report sensitive files on disk
1554 |
1555 v
1556 +-------------+
1557 | DONE | run() returns the findings collection
1558 +-------------+
1559 |
1560 | run() again: SCANNING again; new findings are added to
1561 | the same collection (see COMMON PITFALLS)
1562 v
1563 (SCANNING)
1564  
1565 One host, inside SCANNING. Any croak inside these steps is caught, and
1566 becomes a C<scan-error> finding for that host:
1567  
1568 +-------------------+
1569 | AUTHORISE |--- not listed, or private and not allowed
1570 +-------------------+ -> scan-error (high); next host
1571 |
1572 v
1573 +-------------------+
1574 | PORT SCAN | open ports and banners -> port findings
1575 +-------------------+
1576 |
1577 v
1578 +-------------------+
1579 | FETCH ROOT |--- neither https:// nor http:// answered
1580 +-------------------+ -> host-unreachable (high); next host
1581 |
1582 | the first that answered becomes the base URL
1583 v
1584 +-------------------+
1585 | INSPECT TLS | on the HTTPS port, if one is open
1586 +-------------------+
1587 |
1588 v
1589 +-------------------+
1590 | SAME-ORIGIN | CORS probe and OPTIONS probe
1591 | PROBES |
1592 +-------------------+
1593 |
1594 v
1595 +-------------------+
1596 | PLAIN HTTP HOP | HTTPS base and no port in scope only: does
1597 +-------------------+ http:// redirect to https://?
1598 |
1599 v
1600 +-------------------+
1601 | CRAWL | breadth first, up to 'depth' links deep and
1602 +-------------------+ 'max_urls' pages; only links that
1603 | scope->allows; page rules on each page
1604 v
1605 +-------------------+
1606 | PATH PROBES | if path_probes: the sensitive paths,
1607 +-------------------+ compared with a not-found baseline
1608 |
1609 v
1610 +-------------------+
1611 | REFLECTION PROBE | if reflection: one GET with a marker
1612 +-------------------+
1613 |
1614 v
1615 +-------------------+
1616 | UNVISITED ROUTES | if source_dirs: routes the crawl never
1617 +-------------------+ reached
1618 |
1619 v
1620 next host
1621  
1622 =head1 AUTHOR
1623  
1624 Nigel Horne, C<< <njh at nigelhorne.com> >>
1625  
1626 =head1 LICENCE AND COPYRIGHT
1627  
1628 Copyright 2026 Nigel Horne.
1629  
1630 Usage is subject to the GPL2 licence terms.
1631 If you use it,
1632 please let me know.
1633  
1634 =cut
1635  
1636 27 1;
« lib/App/WebPerimeterScan/Scope.pm ↑ Summary lib/App/WebPerimeterScan/Checks.pm »