Debian Codegraph @

t/integration/error/rendering.t

raw
1 use strict;
2 use warnings;
3
4 use Test::More;
5 use Plack::Test;
6 use HTTP::Request::Common;
7
8 # Error page rendering: what gets shown, what gets hidden, and what happens
9 # when the thing meant to render the error is itself broken.
10 #
11 # The stack trace and the settings/session/environment dumps only appear when
12 # show_stacktrace is on AND the status is 5xx (Dancer2/Core/Error.pm:156). That
13 # conjunction is the whole point of these tests: a 4xx must not leak the
14 # environment even on a box where show_stacktrace was left on, and the censor
15 # only runs on the path that produces those dumps.
16
17 {
18 package ErrorApp;
19 use Dancer2;
20 set logger => 'null';
21 set show_stacktrace => 1;
22 set session => 'Simple';
23
24 # Settings that the environment dump will walk over. Three should be
25 # censored by the default pattern (pass|card.?num|pan|secret), one should
26 # not - otherwise "nothing leaked" could just mean "nothing was dumped".
27 set my_password => 'PASSWORD-MUST-NOT-LEAK';
28 set card_number => '4111111111111111';
29 set api_secret => 'SECRET-MUST-NOT-LEAK';
30 set harmless_value => 'HARMLESS-AND-VISIBLE';
31
32 get '/die' => sub { die "kaboom\n" };
33 get '/error500' => sub { send_error( 'explicit five hundred', 500 ) };
34 get '/error404' => sub { send_error( 'no such thing', 404 ) };
35 get '/error400' => sub { send_error( 'bad input', 400 ) };
36 get '/error403' => sub { send_error( 'not allowed', 403 ) };
37
38 get '/session-die' => sub {
39 session password => 'SESSION-SECRET-MUST-NOT-LEAK';
40 session nick => 'ovid';
41 die "boom with a session\n";
42 };
43
44 get '/html-400' => sub {
45 send_error( q{<script>alert(1)</script> & "quoted" and 'single'}, 400 );
46 };
47 get '/html-500' => sub {
48 send_error( q{<script>alert(1)</script> & "quoted"}, 500 );
49 };
50 }
51
52 my $test = Plack::Test->create( ErrorApp->to_app );
53
54 subtest 'a 5xx with show_stacktrace on renders the full diagnostic page' => sub {
55 for my $path ( '/die', '/error500' ) {
56 my $response = $test->request( GET $path );
57 my $body = $response->content;
58
59 is( $response->code, 500, "$path: is a 500" );
60 like( $body, qr/<div class="title">Stack</, "$path: shows the stack" );
61 like( $body, qr/<div class="title">Settings</, "$path: dumps settings" );
62 like( $body, qr/<div class="title">Session</, "$path: dumps the session" );
63 like( $body, qr/<div class="title">Environment</,"$path: dumps the environment" );
64 }
65 };
66
67 subtest 'a 4xx never shows a stack trace, even with show_stacktrace on' => sub {
68 # This is the assertion that matters: the setting is on for this whole
69 # app, and the 5xx subtest above proves it is working.
70 for my $path ( '/error400', '/error403', '/error404' ) {
71 my $response = $test->request( GET $path );
72 my $body = $response->content;
73
74 unlike( $body, qr/<div class="title">Stack</,
75 "$path: no stack" );
76 unlike( $body, qr/<div class="title">Settings</,
77 "$path: no settings dump" );
78 unlike( $body, qr/<div class="title">Environment</,
79 "$path: no environment dump" );
80
81 # Nothing from the settings reaches a 4xx page at all.
82 unlike( $body, qr/HARMLESS-AND-VISIBLE/,
83 "$path: not even a harmless setting is dumped" );
84
85 # It is still a recognisable error page with its own status in it.
86 like( $body, qr/<!DOCTYPE html>/, "$path: still renders an error page" );
87 }
88
89 is( $test->request( GET '/error404' )->code, 404, '404 keeps its status' );
90 is( $test->request( GET '/error400' )->code, 400, '400 keeps its status' );
91 };
92
93 subtest 'sensitive-looking settings are censored from the dump' => sub {
94 my $body = $test->request( GET '/die' )->content;
95
96 # The control first: if this fails, the dump is not happening and the
97 # absences below prove nothing.
98 like( $body, qr/HARMLESS-AND-VISIBLE/,
99 'a non-sensitive setting does appear in the dump' );
100 like( $body, qr/harmless_value/, 'under its own key' );
101
102 # The keys are still listed - it is the values that are replaced, so the
103 # developer can see what was hidden.
104 like( $body, qr/my_password/, 'the sensitive key is still shown' );
105 like( $body, qr/card_number/, 'and so is the card number key' );
106 like( $body, qr/api_secret/, 'and the secret key' );
107
108 unlike( $body, qr/PASSWORD-MUST-NOT-LEAK/,
109 'a value under a key matching "pass" is not shown' );
110 unlike( $body, qr/4111111111111111/,
111 'a value under a key matching "card.?num" is not shown' );
112 unlike( $body, qr/SECRET-MUST-NOT-LEAK/,
113 'a value under a key matching "secret" is not shown' );
114
115 like( $body, qr/Hidden \(looks potentially sensitive\)/,
116 'the replacement text is shown in their place' );
117 like( $body, qr/sensitive-looking keys hidden/,
118 'and the page says how many values were hidden' );
119 };
120
121 subtest 'sensitive-looking session values are censored too' => sub {
122 my $body = $test->request( GET '/session-die' )->content;
123
124 like( $body, qr/<div class="title">Session</,
125 'the session is dumped' );
126 like( $body, qr/ovid/,
127 'an ordinary session value is visible' );
128 unlike( $body, qr/SESSION-SECRET-MUST-NOT-LEAK/,
129 'but a session value under a "password" key is not' );
130 };
131
132 subtest 'an error message containing HTML is escaped, not rendered' => sub {
133 # A 4xx takes the _html_encode branch; a 5xx takes the backtrace branch.
134 # Both encode, but by different code, so both are checked.
135 for my $path ( '/html-400', '/html-500' ) {
136 my $body = $test->request( GET $path )->content;
137
138 unlike( $body, qr/<script>alert\(1\)<\/script>/,
139 "$path: the script tag is not emitted raw" );
140 like( $body, qr/&lt;script&gt;/,
141 "$path: it is escaped instead" );
142 like( $body, qr/&amp;/,
143 "$path: the ampersand is escaped" );
144 unlike( $body, qr/"quoted"/,
145 "$path: double quotes are escaped" );
146 like( $body, qr/&quot;quoted&quot;/,
147 "$path: to &quot; entities" );
148 }
149
150 # Single quotes matter for attribute-context injection, and only the 4xx
151 # path puts the message where that could apply.
152 my $body_400 = $test->request( GET '/html-400' )->content;
153 like( $body_400, qr/&#39;single&#39;/, 'single quotes are escaped as well' );
154 };
155
156 done_testing();