lib/Dancer2/Handler/AutoPage.pm

filter: Statementstmt 89.0% 57 / 64 Branchbran 64.2% 18 / 28 Conditioncond 38.8% 7 / 18 MC/DCmcdc 18.1% 2 / 11 Subroutinesub 100% 10 / 10 Podpod 100% 4 / 4 totaltotal 72.5% 98 / 135 CC 22 SCAR 23.4
CC
22
Cov
75%
_dir_is_within
16.5
__ANON__
12.4
register
3.0
CRAP 10.4
« blib/lib/Dancer2/Core/Role/SessionFactory/File.pm ↑ Summary blib/lib/Dancer2/Test.pm »
1 package Dancer2::Handler::AutoPage;
2 # ABSTRACT: Class for handling the AutoPage feature
3 162 $Dancer2::Handler::AutoPage::VERSION = '2.3.0';
4 162 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 162 ▶use Carp 'croak';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 162 ▶use Dancer2::Core::Types;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 162 ▶use Path::Tiny ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 162 with qw<
10 Dancer2::Core::Role::Handler
11 Dancer2::Core::Role::StandardResponses
12 >;
13  
14 280 ▶sub register {
Subroutine register: called CC=3 SCAR=11.0
Pod documented
15 280 my ( $self, $app ) = @_;
16  
17 280 ▶ return unless $app->config->{auto_page};
Branch: 1 branch
BranchTrueFalseTotal
unless $app->config->{"auto_page"} 275 5 280
18  
19 $app->add_route(
20 method => $_,
21 regexp => $self->regexp,
22 code => $self->code,
23 5 ) for $self->methods;
24 }
25  
26 10 ▶sub code {
Subroutine code: called CC=1 SCAR=0.2
Pod documented
27 19 ▶ sub {
Subroutine __ANON__: called CC=10 SCAR=25.2
28 19 my $app = shift;
29 19 my $prefix = shift;
30  
31 19 my $template = $app->template_engine;
32 19 ▶ if ( !defined $template ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $template) 0 19 19
33 0 $app->response->has_passed(1);
34 0 return;
35 }
36  
37 19 my $page = $app->request->path;
38 19 my $layout_dir = $template->layout_dir;
39  
40 # Cheap fast path: if the request path is spelled with the layout
41 # directory's own case, refuse it without touching the filesystem.
42 # This is only an optimisation - it is not the authoritative check,
43 # because on a case-insensitive filesystem (macOS/Windows) a
44 # differently-cased request would miss it while still resolving to
45 # the same file. The check below decides from the resolved
46 # file instead, so it catches that case too.
47 19 ▶ if ( defined $layout_dir && $page =~ m{^/\Q$layout_dir\E/} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $layout_dir and $page =~ m[^/\Q$layout_dir\E/]) 5 14 19
Condition66%2/3
defined $layout_dir and $page =~ m[^/\Q$layout_dir\E/]
!ll&&!rl&&r
0145
MC/DC50%1/2
defined $layout_dir and $page =~ m[^/\Q$layout_dir\E/]
defined $layout_dir $page =~ m[^/\Q$layout_dir\E/]
Truth table66%2/3
A and B
A= defined $layout_dirB= $page =~ m[^/\Q$layout_dir\E/]
ABresult
0X0
100
111
48 5 $app->response->has_passed(1);
49 5 return;
50 }
51  
52 # remove leading '/', ensuring paths relative to the view
53 14 $page =~ s{^/}{};
54 14 my $view_path = $template->view_pathname($page);
55  
56 14 ▶ if ( ! $template->pathname_exists( $view_path ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template->pathname_exists($view_path)) 8 6 14
57 8 $app->response->has_passed(1);
58 8 return;
59 }
60  
61 # Authoritative check: is the page's own directory the layout
62 # directory, or inside it? Decided from what is actually on disk
63 # rather than from the request path's spelling.
64 #
65 # This does not use $view_path/view_pathname for the comparison:
66 # that method is engine-specific, and Dancer2::Template::TemplateToolkit
67 # overrides it to return a bare template name rather than a
68 # filesystem path, leaving TT2's own INCLUDE_PATH search to resolve
69 # it - so it cannot be relied on to name a location on disk here.
70 # $template->views is guaranteed absolute by
71 # Dancer2::Core::Role::Template, so joining it with $page and
72 # taking the parent gives the page's real directory regardless of
73 # which template engine is in use. Its parent directory is
74 # guaranteed to exist at this point - pathname_exists just
75 # confirmed the page resolves to a real file.
76 6 ▶ if ( defined $layout_dir ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $layout_dir) 6 0 6
77 6 my $layout_dir_path =
78 Path::Tiny::path( $template->views, $layout_dir );
79 6 my $page_dir_path =
80 Path::Tiny::path( $template->views, $page )->parent;
81  
82 6 ▶ if ( $layout_dir_path->is_dir
Branch: 1 branch
BranchTrueFalseTotal
if ($layout_dir_path->is_dir and $page_dir_path->is_dir and _dir_is_within($page_dir_path, $layout_dir_path, Path::Tiny::path($template->views))) 0 6 6
Condition33%2/6
$layout_dir_path->is_dir and $page_dir_path->is_dir and _dir_is_within($page_dir_path, $layout_dir_path, Path::Tiny::path($template->views))
!ll&&!rl&&r
060
$layout_dir_path->is_dir and $page_dir_path->is_dir
!ll&&!rl&&r
006
MC/DC0%0/3
$layout_dir_path->is_dir and $page_dir_path->is_dir and _dir_is_within($page_dir_path, $layout_dir_path, Path::Tiny::path($template->views))
$layout_dir_path->is_dir $page_dir_path->is_dir _dir_is_within($page_dir_path, $layout_dir_path, Path::Tiny::path($template->views))
Truth table25%1/4
A and B and C
A= $layout_dir_path->is_dirB= $page_dir_path->is_dirC= _dir_is_within($page_dir_path, $layout_dir_path, Path::Tiny::path($template->views))
ABCresult
0XX0
10X0
1100
1111
83 && $page_dir_path->is_dir
84 && _dir_is_within(
85 $page_dir_path,
86 $layout_dir_path,
87 Path::Tiny::path( $template->views ),
88 )
89 ) {
90 0 $app->response->has_passed(1);
91 0 return;
92 }
93 }
94  
95 6 my $ct = $template->process( $page );
96 6 ▶ return ( $app->request->method eq 'GET' ) ? $ct : '';
Branch: 1 branch
BranchTrueFalseTotal
$app->request->method eq 'GET' ? : 6 0 6
97 10 };
98 }
99  
100 # Is $dir the same directory as $ancestor, or inside it?
101 #
102 # Comparing resolved path strings is not enough. realpath() resolves
103 # symlinks but does not canonicalise case, so on a case-insensitive
104 # filesystem - macOS and Windows by default - 'views/Layouts' and
105 # 'views/layouts' name one directory while comparing as two. That is
106 # precisely the case this guard exists to catch, so the comparison is made
107 # on filesystem identity (device and inode) instead, which no spelling can
108 # disguise. It settles symlinks and hardlinks on the way past.
109 #
110 # The walk stops at $stop_at (the views directory) rather than climbing to
111 # the filesystem root: nothing above views is ours to reason about, and a
112 # page cannot be inside the layout directory without being under views too.
113 #
114 # Some Windows configurations report an inode of 0 for every file, which
115 # would make every directory compare equal to every other. Where that is
116 # so, identity is unusable and the comparison falls back to matching the
117 # paths case-insensitively - imperfect, since it assumes the folding rules
118 # rather than asking the filesystem, but it fails closed on the case this
119 # guard is about rather than silently passing everything.
120 6 ▶sub _dir_is_within {
Subroutine _dir_is_within: called CC=11 SCAR=28.0
121 6 my ( $dir, $ancestor, $stop_at ) = @_;
122  
123 6 ▶ my @ancestor_id = stat "$ancestor" or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@ancestor_id) = stat "$ancestor" or return 0 6 0 6
124 6 my $usable_inode = $ancestor_id[1];
125  
126 6 ▶ if ( !$usable_inode ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($usable_inode) 0 6 6
127 0 my $a = lc Path::Tiny::path($ancestor)->stringify;
128 0 my $d = lc Path::Tiny::path($dir)->stringify;
129 0 ▶ return $d eq $a || index( $d, "$a/" ) == 0;
Condition0%0/3
$d eq $a || index($d, "$a/") == 0
l!l&&r!l&&!r
000
MC/DC0%0/2
$d eq $a || index($d, "$a/") == 0
$d eq $a index($d, "$a/") == 0
Truth table0%0/3
A || B
A= $d eq $aB= index($d, "$a/") == 0
ABresult
1X1
011
000
130 }
131  
132 6 my $stop = eval { $stop_at->realpath->stringify };
133 6 ▶ my $cursor = eval { $dir->realpath } or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my $cursor = eval { do { $dir->realpath } } or return 0 6 0 6
134  
135 6 while (1) {
136 9 ▶ my @id = stat "$cursor" or return 0;
Branch: 1 branch
BranchTrueFalseTotal
my(@id) = stat "$cursor" or return 0 9 0 9
137 # 'eq', not '=='. Where an inode number is too large for perl to
138 # hold as an integer, stat returns it as a decimal string; comparing
139 # numerically converts that to a float and rounds it, so two
140 # different inodes can compare equal. perldoc -f stat says to prefer
141 # 'eq' for exactly this reason, and it is correct for the values
142 # that are returned numerically too. Rounding here would be a
143 # fail-open: a page outside the layout directory would be served.
144 9 ▶ return 1 if $id[0] eq $ancestor_id[0] && $id[1] eq $ancestor_id[1];
Branch: 1 branch
BranchTrueFalseTotal
if $id[0] eq $ancestor_id[0] and $id[1] eq $ancestor_id[1] 0 9 9
Condition33%1/3
$id[0] eq $ancestor_id[0] and $id[1] eq $ancestor_id[1]
!ll&&!rl&&r
090
MC/DC0%0/2
$id[0] eq $ancestor_id[0] and $id[1] eq $ancestor_id[1]
$id[0] eq $ancestor_id[0] $id[1] eq $ancestor_id[1]
Truth table33%1/3
A and B
A= $id[0] eq $ancestor_id[0]B= $id[1] eq $ancestor_id[1]
ABresult
0X0
100
111
145  
146 9 ▶ last if defined $stop && $cursor->stringify eq $stop;
Branch: 1 branch
BranchTrueFalseTotal
if defined $stop and $cursor->stringify eq $stop 6 3 9
Condition66%2/3
defined $stop and $cursor->stringify eq $stop
!ll&&!rl&&r
036
MC/DC50%1/2
defined $stop and $cursor->stringify eq $stop
defined $stop $cursor->stringify eq $stop
Truth table66%2/3
A and B
A= defined $stopB= $cursor->stringify eq $stop
ABresult
0X0
100
111
147  
148 3 my $parent = $cursor->parent;
149 3 ▶ last if $parent->stringify eq $cursor->stringify; # hit the root
Branch: 1 branch
BranchTrueFalseTotal
if $parent->stringify eq $cursor->stringify 0 3 3
150 3 $cursor = $parent;
151 }
152  
153 6 return 0;
154 }
155  
156 10 ▶sub regexp {'/**'}
Subroutine regexp: called CC=1 SCAR=0.0
Pod documented
157  
158 5 ▶sub methods {qw(head get)}
Subroutine methods: called CC=1 SCAR=0.0
Pod documented
159  
160 162 1;
161  
162 __END__
« blib/lib/Dancer2/Core/Role/SessionFactory/File.pm ↑ Summary blib/lib/Dancer2/Test.pm »