App-WebPerimeterScan

 view release on metacpan or  search on metacpan

lib/App/WebPerimeterScan/Source.pm  view on Meta::CPAN

use strict;
use warnings;
use autodie qw(:all);

use Cwd qw(realpath);
use File::Find qw(find);
use File::Spec;
use Params::Get qw(get_params);
use Params::Validate::Strict qw(validate_strict);
use Readonly;
use Sub::Private { mode => 'enforce' };
use URI::Escape qw(uri_unescape);

use App::WebPerimeterScan::I18N ();
use parent -norequire, 'App::WebPerimeterScan::I18N';

our $VERSION = '0.001.0';

# Route declarations this understands.  Mojolicious is the common Perl web
# framework, so its $r->get('/path') form is what the indexer reads.  The
# pattern must start with "/": without that, $cache->get('key') and
# $ua->get('http://...') in the same tree were indexed as routes.  A route
# written as a regexp is left alone rather than approximated.
Readonly::Scalar my $ROUTE_RE => qr/->(get|post|put|patch|delete|any|under|websocket)\s*\(\s*(['"])(\/[^'"]*)\2/i;

# Files worth reading for routes, and directories never worth walking.
Readonly::Scalar my $SOURCE_FILE => qr/\.(?:pm|pl|psgi|cgi)\z/;
Readonly::Hash my %SKIP_DIRECTORY => map { $_ => 1 } qw(.git .svn blib node_modules);

# Path traversal: a ".." segment anywhere in the decoded path.
Readonly::Scalar my $TRAVERSAL => qr{(?:\A|/)\.\.(?:/|\z)};

# Route placeholders: ':name' is one segment, '*name' the rest of the path.
Readonly::Scalar my $ROUTE_TOKEN => qr/\G(?::([A-Za-z_]\w*)|(\*)([A-Za-z_]\w*)|(.))/s;
Readonly::Scalar my $ONE_SEGMENT => '[^/]+';
Readonly::Scalar my $REST_OF_PATH => '.*';

=encoding utf8

=head1 NAME

App::WebPerimeterScan::Source - map scan results onto files and code

=head1 VERSION

Version 0.001.0

=head1 SYNOPSIS

Find the file on disk behind a URL:

	use App::WebPerimeterScan::Source;

	my $source = App::WebPerimeterScan::Source->new(
		docroots => ['/srv/www/example'],
	);

	my $hit = $source->file_for('/.git/config');
	print "served from $hit->{path} ($hit->{size} bytes)\n" if $hit;

Find the line of code that defines an endpoint:

	my $app = App::WebPerimeterScan::Source->new(
		source_dirs => ['/home/me/src/example/lib'],
	);

	my $route = $app->route_for('/view/dogs?page=2');
	print "$route->{method} $route->{pattern} at $route->{file} line $route->{line}\n"
		if $route;

List every endpoint the source defines:

	for my $route (@{ $app->routes }) {
		print "$route->{pattern}\n";
	}

Look for secret files that exist on disk, even if the server did not serve
them:

	my $probes = App::WebPerimeterScan::Checks::sensitive_probes();
	for my $hit (@{ $source->sensitive_on_disk($probes) }) {
		print "on disk: $hit->{relative}\n";
	}

=head1 DESCRIPTION

A scan from outside can only say "this URL answered".  This module adds
two facts that tell you what to change:

=over 4

=item * B<The file.>  Given a document root (the directory the web server
serves files from), it finds the file on disk that a URL path would be
served from.

=item * B<The route.>  Given your application's source code, it finds the
line that declares the endpoint, for example
C<< $r->get('/view/:name') >>.

=back

The module only reads files.  It never sends network traffic and never
changes a file.

A path is only accepted if it is inside the document root after every
symbolic link is followed.  So C<..> in the path, a symbolic link that
points out of the tree, and C<%2e%2e> (an encoded C<..>) cannot reach a
file outside it.

=head2 Route patterns

Routes are found in F<.pm>, F<.pl>, F<.psgi> and F<.cgi> files by looking
for calls such as C<< ->get('/path') >>, C<< ->post('/path') >> and
C<< ->any('/path') >> whose pattern starts with C</>.  In a pattern,
C<:name> matches one path segment, C<*name> matches the rest of the path,
and every other character must match exactly.  A C</> at the end of the
path is optional.  The directories F<.git>, F<.svn>, F<blib> and
F<node_modules> are not searched.

=head1 SUBROUTINES/METHODS

=head2 new

Purpose: describe where the site's files and code are.

Args: named C<docroots> and C<source_dirs>, each an array reference of
directory names.  Either may be empty.

Returns: the new object.

Side Effects: none; directories are read lazily.

Usage: C<< my $source = App::WebPerimeterScan::Source->new(docroots => [$dir]); >>

=head3 EXAMPLE

	my $source = App::WebPerimeterScan::Source->new(source_dirs => ['lib']);
	printf "%d route(s)\n", scalar @{ $source->routes };

=head3 API SPECIFICATION

=head4 INPUT

	{
		docroots    => { type => 'arrayref', optional => 1 },
		source_dirs => { type => 'arrayref', optional => 1 },
	}

=head4 OUTPUT

	{ type => 'object', isa => 'App::WebPerimeterScan::Source' }

=head3 MESSAGES

	Message                       Meaning / resolution
	----------------------------  ---------------------------------
	Unknown parameter 'X'         A misspelt argument name.



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