App-WebPerimeterScan

 view release on metacpan or  search on metacpan

t/scan.t  view on Meta::CPAN

mkdir "$docroot/.git";
for my $file ([ '.git/config', "[core]\n" ], [ '.env', "SECRET=1\n" ]) {
	open my $fh, '>', "$docroot/$file->[0]" or die $!;
	print {$fh} $file->[1];
	close $fh;
}
my $srcdir = tempdir(CLEANUP => 1);
open my $src, '>', "$srcdir/App.pm" or die $!;
print {$src} "\$r->get('/about');\n\$r->get('/hidden/:id');\n";
close $src;

my ($scope_fh, $scope_file) = tempfile(UNLINK => 1);
print {$scope_fh} "127.0.0.1:$port\n";
close $scope_fh;

my $scanner = App::WebPerimeterScan->new(
	scope       => App::WebPerimeterScan::Scope->new(allow_private => 1)->load_file($scope_file),
	ports       => [ $port ],
	delay       => 0,
	timeout     => 5,
	reflection  => 1,
	docroots    => [ $docroot ],
	source_dirs => [ $srcdir ],
);
my $findings = $scanner->run;
my %by_id = map { $_->{id} => $_ } @{ $findings->all };

my ($host) = @{ $scanner->host_reports };
is($host->{base}, "http://127.0.0.1:$port/", 'the root answered over HTTP');
is_deeply($host->{urls}, [ "http://127.0.0.1:$port/", "http://127.0.0.1:$port/about" ],
	'the crawl followed the in-scope link only: no asset, no other host, no other name');
is_deeply([ map { $_->{port} } @{ $host->{open_ports} } ], [ $port ], 'the port scan found the server');
ok(!$host->{tls}, 'no TLS inspection without a TLS port');
is($host->{response}{status}, 200, 'the main response is recorded');
is(scalar @{ $host->{responses} }, 2, 'the CORS and OPTIONS probes are recorded');

ok(!$by_id{'scan-error'}, 'the scan completed') or diag explain $by_id{'scan-error'};
ok($by_id{'csp-missing'}, 'header rules ran');
ok(!$by_id{'hsts-missing'}, 'no HSTS rule on plain HTTP');
ok($by_id{'cookie-flags'}, 'cookie rule ran');
ok($by_id{'server-version-disclosed'}, 'disclosure rule ran');
ok($by_id{'generator-disclosed'}, 'the generator meta tag was parsed');
ok($by_id{'git-exposed'}, 'the served .git/config was found');
# realpath, so compare the tail: on macOS the temporary directory is behind
# a /var -> /private/var symlink.
like($by_id{'git-exposed'}{file}, qr{\.git/config\z}, '... and resolved to the file on disk')
	or diag explain $by_id{'git-exposed'};
ok(-f $by_id{'git-exposed'}{file}, '... which exists');
ok($by_id{'robots-disclosure'}, 'robots.txt was read');
ok($by_id{'reflected-input'}, 'the reflection probe saw its marker come back');
ok(!$by_id{'catch-all-200'}, 'a 404 for unknown paths is not a catch-all');

my ($on_disk) = grep { $_->{id} eq 'source-file-in-docroot' } @{ $findings->all };
ok($on_disk, 'the unserved .env on disk is reported');
like($on_disk->{title}, qr/\.env/, '... by name');
ok(!(grep { $_->{id} eq 'source-file-in-docroot' && $_->{title} =~ /\.git/ } @{ $findings->all }),
	'the served file is not reported twice');

my $unvisited = $by_id{'source-routes-unvisited'};
ok($unvisited, 'the route nothing links to is reported');
like($unvisited->{title}, qr/\A1 endpoint defined in source was not scanned\z/, 'singular title for one route');
like($unvisited->{evidence}, qr{/hidden/:id}, 'the unvisited route is named');
unlike($unvisited->{evidence}, qr{/about}, 'the crawled route is not');
is($by_id{'csp-missing'}{source}, '', 'a host-level finding has no source line');

cmp_ok($scanner->http->requests, '>', 20, 'the request counter covers probes as well as pages');

# The same scope without --allow-private: refused, and loudly.
my $refused = App::WebPerimeterScan->new(
	scope => App::WebPerimeterScan::Scope->new->load_file($scope_file),
	ports => [ $port ],
	delay => 0,
)->run;
my ($error) = @{ $refused->all };
is($error->{id}, 'scan-error', 'a private target without allow_private is a scan error');
is($error->{severity}, 'high', '... at high severity, so --fail-on high exits 1');
like($error->{evidence}, qr/\ARefusing to scan '127\.0\.0\.1': it resolves into private address space/,
	'... with the reason, and no "at FILE line N"');
unlike($error->{evidence}, qr/ line \d+/, 'the location suffix is removed');

$server->stop;

done_testing();



( run in 0.970 second using v1.01-cache-2.11-cpan-eb134014566 )