use strict; use warnings; use Test::More; use Plack::Test; use HTTP::Request::Common; # Error page rendering: what gets shown, what gets hidden, and what happens # when the thing meant to render the error is itself broken. # # The stack trace and the settings/session/environment dumps only appear when # show_stacktrace is on AND the status is 5xx (Dancer2/Core/Error.pm:156). That # conjunction is the whole point of these tests: a 4xx must not leak the # environment even on a box where show_stacktrace was left on, and the censor # only runs on the path that produces those dumps. { package ErrorApp; use Dancer2; set logger => 'null'; set show_stacktrace => 1; set session => 'Simple'; # Settings that the environment dump will walk over. Three should be # censored by the default pattern (pass|card.?num|pan|secret), one should # not - otherwise "nothing leaked" could just mean "nothing was dumped". set my_password => 'PASSWORD-MUST-NOT-LEAK'; set card_number => '4111111111111111'; set api_secret => 'SECRET-MUST-NOT-LEAK'; set harmless_value => 'HARMLESS-AND-VISIBLE'; get '/die' => sub { die "kaboom\n" }; get '/error500' => sub { send_error( 'explicit five hundred', 500 ) }; get '/error404' => sub { send_error( 'no such thing', 404 ) }; get '/error400' => sub { send_error( 'bad input', 400 ) }; get '/error403' => sub { send_error( 'not allowed', 403 ) }; get '/session-die' => sub { session password => 'SESSION-SECRET-MUST-NOT-LEAK'; session nick => 'ovid'; die "boom with a session\n"; }; get '/html-400' => sub { send_error( q{ & "quoted" and 'single'}, 400 ); }; get '/html-500' => sub { send_error( q{ & "quoted"}, 500 ); }; } my $test = Plack::Test->create( ErrorApp->to_app ); subtest 'a 5xx with show_stacktrace on renders the full diagnostic page' => sub { for my $path ( '/die', '/error500' ) { my $response = $test->request( GET $path ); my $body = $response->content; is( $response->code, 500, "$path: is a 500" ); like( $body, qr/