| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Plack::Test; |
| 6 |
use HTTP::Request; |
| 7 |
use Path::Tiny (); |
| 8 |
|
| 9 |
# The AutoPage handler serves any request whose path matches an existing view, |
| 10 |
# with no route declared for it. It is off by default and switched on with |
| 11 |
# 'auto_page: 1'. |
| 12 |
# |
| 13 |
# Because it turns the request path into a view path, it needs a carve-out so |
| 14 |
# that layout templates are not served as pages, and it has one: |
| 15 |
# Dancer2/Handler/AutoPage.pm:36-40 passes the request on when the path starts |
| 16 |
# with the layout directory. That guard is what the last subtest here probes. |
| 17 |
# |
| 18 |
# auto_page has to come from a config file rather than 'set', because |
| 19 |
# route_handlers - which is what registers this handler - is read once when the |
| 20 |
# application object is constructed at import time. |
| 21 |
|
| 22 |
my ( $DIR, $VIEWS, $CASE_INSENSITIVE_FS ); |
| 23 |
|
| 24 |
BEGIN { |
| 25 |
$DIR = Path::Tiny->tempdir; |
| 26 |
$VIEWS = $DIR->child('views'); |
| 27 |
$VIEWS->child('layouts')->mkpath; |
| 28 |
$VIEWS->child('sub')->mkpath; |
| 29 |
$DIR->child('conf')->mkpath; |
| 30 |
|
| 31 |
$VIEWS->child('autopage.tt')->spew_utf8('AUTO-PAGE-CONTENT'); |
| 32 |
$VIEWS->child('shadowed.tt')->spew_utf8('VIEW-CONTENT'); |
| 33 |
$VIEWS->child( 'sub', 'deep.tt' )->spew_utf8('DEEP-CONTENT'); |
| 34 |
$VIEWS->child( 'layouts', 'main.tt' )->spew_utf8('MAIN{[% content %]}'); |
| 35 |
|
| 36 |
# Does this filesystem resolve a differently-cased path to the same file? |
| 37 |
# macOS and Windows normally do; most Linux filesystems do not. The last |
| 38 |
# subtest is only meaningful where it does, so ask rather than assume. |
| 39 |
$CASE_INSENSITIVE_FS = -f $VIEWS->child( 'Layouts', 'main.tt' )->stringify; |
| 40 |
|
| 41 |
$DIR->child( 'conf', 'config.yml' )->spew_utf8( <<"YML" ); |
| 42 |
auto_page: 1 |
| 43 |
layout: main |
| 44 |
views: "@{[ $VIEWS->stringify ]}" |
| 45 |
template: template_toolkit |
| 46 |
logger: "null" |
| 47 |
YML |
| 48 |
} |
| 49 |
|
| 50 |
BEGIN { $ENV{DANCER_CONFDIR} = $DIR->child('conf')->stringify } |
| 51 |
|
| 52 |
{ |
| 53 |
package AutoPageApp; |
| 54 |
use Dancer2; |
| 55 |
|
| 56 |
# A declared route, to check it takes precedence over a view of the |
| 57 |
# same name. |
| 58 |
get '/shadowed' => sub { 'ROUTE-CONTENT' }; |
| 59 |
} |
| 60 |
|
| 61 |
BEGIN { delete $ENV{DANCER_CONFDIR} } |
| 62 |
|
| 63 |
my $test = Plack::Test->create( AutoPageApp->to_app ); |
| 64 |
|
| 65 |
sub get_path { |
| 66 |
my $path = shift; |
| 67 |
return $test->request( HTTP::Request->new( GET => $path ) ); |
| 68 |
} |
| 69 |
|
| 70 |
subtest 'a request matching a view is served without a route' => sub { |
| 71 |
my $response = get_path('/autopage'); |
| 72 |
|
| 73 |
is( $response->code, 200, 'the page is served' ); |
| 74 |
is( $response->content, 'MAIN{AUTO-PAGE-CONTENT}', |
| 75 |
'rendered through the configured layout' ); |
| 76 |
|
| 77 |
my $nested = get_path('/sub/deep'); |
| 78 |
is( $nested->code, 200, 'a nested view is served too' ); |
| 79 |
is( $nested->content, 'MAIN{DEEP-CONTENT}', 'also with the layout' ); |
| 80 |
}; |
| 81 |
|
| 82 |
subtest 'a request matching no view passes on' => sub { |
| 83 |
my $response = get_path('/no-such-page'); |
| 84 |
|
| 85 |
# The handler passes rather than answering, so this reaches the |
| 86 |
# application's own 404 instead of an empty 200. |
| 87 |
is( $response->code, 404, 'a path with no matching view 404s' ); |
| 88 |
}; |
| 89 |
|
| 90 |
subtest 'a declared route wins over a view of the same name' => sub { |
| 91 |
my $response = get_path('/shadowed'); |
| 92 |
|
| 93 |
is( $response->code, 200, 'the request is served' ); |
| 94 |
is( $response->content, 'ROUTE-CONTENT', |
| 95 |
'by the declared route, not the view' ); |
| 96 |
unlike( $response->content, qr/VIEW-CONTENT/, |
| 97 |
'the view is not used' ); |
| 98 |
}; |
| 99 |
|
| 100 |
subtest 'a layout cannot be requested as a page' => sub { |
| 101 |
my $response = get_path('/layouts/main'); |
| 102 |
|
| 103 |
# The guard passes the request on, so it 404s like any unmatched path. |
| 104 |
is( $response->code, 404, |
| 105 |
'a path under the layout directory is refused' ); |
| 106 |
unlike( $response->content, qr/MAIN\{/, |
| 107 |
'and the layout template is not rendered as a page' ); |
| 108 |
|
| 109 |
# Paths that try to reach the layout directory indirectly are also |
| 110 |
# refused - here because the view lookup itself does not resolve them. |
| 111 |
for my $path ( '/x/../layouts/main', '/./layouts/main', '/sub/../layouts/main' ) { |
| 112 |
is( get_path($path)->code, 404, "$path is refused too" ); |
| 113 |
} |
| 114 |
}; |
| 115 |
|
| 116 |
subtest 'the layout guard holds under a different case' => sub { |
| 117 |
|
| 118 |
# This used to be reachable: the guard compared the request path |
| 119 |
# against the layout directory name with a case-sensitive match |
| 120 |
# (Dancer2/Handler/AutoPage.pm:38), so on a case-insensitive filesystem - |
| 121 |
# macOS and Windows by default - a request for /Layouts/main missed the |
| 122 |
# guard but still resolved to the same file, serving the layout template |
| 123 |
# as a page. |
| 124 |
# |
| 125 |
# The fix decides from the resolved file rather than the request path's |
| 126 |
# spelling (the same containment approach send_file uses: |
| 127 |
# $dir->realpath->subsumes($file_path)), so it closes the gap regardless |
| 128 |
# of the filesystem's case sensitivity. On a case-sensitive filesystem the |
| 129 |
# differently-cased request never reached the same file to begin with, so |
| 130 |
# it already 404s there for the unrelated reason that the view lookup |
| 131 |
# simply misses - the assertion below is meaningful on both kinds of |
| 132 |
# filesystem, just for different underlying reasons, which is why it no |
| 133 |
# longer needs the SKIP block the old bug-pinning version used. |
| 134 |
# |
| 135 |
# $CASE_INSENSITIVE_FS (computed in the BEGIN block above) is kept and |
| 136 |
# reported here so a failure can be told apart: on a case-insensitive |
| 137 |
# filesystem a regression would mean the containment check stopped |
| 138 |
# working; on a case-sensitive one it would mean the view lookup started |
| 139 |
# resolving differently-cased paths, which would be a different problem. |
| 140 |
note( 'this filesystem is ', |
| 141 |
( $CASE_INSENSITIVE_FS ? 'case-insensitive' : 'case-sensitive' ), |
| 142 |
' - the 404 below is reached ', |
| 143 |
( $CASE_INSENSITIVE_FS |
| 144 |
? 'via the containment check (the case the fix was about)' |
| 145 |
: 'because the view lookup itself already misses' ), |
| 146 |
); |
| 147 |
|
| 148 |
# The control: the correctly-cased path is refused. |
| 149 |
is( get_path('/layouts/main')->code, 404, |
| 150 |
'the correctly-cased layout path is refused' ); |
| 151 |
|
| 152 |
my $response = get_path('/Layouts/main'); |
| 153 |
is( $response->code, 404, |
| 154 |
'the same layout under a different case is refused too' ); |
| 155 |
unlike( $response->content, qr/MAIN\{/, |
| 156 |
'and the layout template is not rendered as a page' ); |
| 157 |
}; |
| 158 |
|
| 159 |
done_testing(); |