| 1 |
use strict; |
| 2 |
use warnings; |
| 3 |
|
| 4 |
use Test::More; |
| 5 |
use Test::Fatal qw<exception>; |
| 6 |
|
| 7 |
use Dancer2::Core::Response; |
| 8 |
|
| 9 |
# Unit tests for the response object: character encoding, Content-Length, and |
| 10 |
# the conversion to a PSGI array. |
| 11 |
# |
| 12 |
# U+263A is used throughout as the non-ASCII character. It matters that it is |
| 13 |
# above U+00FF: Perl stores a string of only Latin-1 codepoints as bytes with |
| 14 |
# no UTF8 flag, and several branches in encode_content key off |
| 15 |
# utf8::is_utf8(), so "caf\x{e9}" takes a different path from "hi\x{263A}" |
| 16 |
# and would quietly test the wrong thing. |
| 17 |
my $CHAR = "\x{263A}"; |
| 18 |
my $BYTES = "\xe2\x98\xba"; # the same character, UTF-8 encoded |
| 19 |
|
| 20 |
# Helper: the PSGI triplet with the headers as a hash, plus the raw header |
| 21 |
# array for the cases where the array itself is what is being asserted. |
| 22 |
sub psgi_parts { |
| 23 |
my $response = shift; |
| 24 |
my $psgi = $response->to_psgi; |
| 25 |
my %header; |
| 26 |
for ( my $i = 0; $i < @{ $psgi->[1] }; $i += 2 ) { |
| 27 |
$header{ $psgi->[1][$i] } = $psgi->[1][ $i + 1 ]; |
| 28 |
} |
| 29 |
return { |
| 30 |
status => $psgi->[0], |
| 31 |
header => \%header, |
| 32 |
raw => $psgi->[1], |
| 33 |
body => $psgi->[2], |
| 34 |
}; |
| 35 |
} |
| 36 |
|
| 37 |
# THIS SUBTEST MUST RUN FIRST. |
| 38 |
# |
| 39 |
# The "no charset configured" report is gated by a file-scoped counter in |
| 40 |
# Dancer2::Core::Response ($WARNED_NO_CHARSET), so it fires at most once per |
| 41 |
# process and cannot be reset from outside. Any earlier subtest that assigned |
| 42 |
# character content with no charset would consume it and make this one |
| 43 |
# unfalsifiable. |
| 44 |
subtest 'a missing charset is reported once per process' => sub { |
| 45 |
my @logged; |
| 46 |
my @warnings; |
| 47 |
|
| 48 |
my $response = Dancer2::Core::Response->new( |
| 49 |
log_cb => sub { push @logged, [@_] } ); |
| 50 |
|
| 51 |
{ |
| 52 |
local $SIG{__WARN__} = sub { push @warnings, $_[0] }; |
| 53 |
$response->content("hi$CHAR"); |
| 54 |
} |
| 55 |
|
| 56 |
is( scalar @logged, 1, 'the app is told, once' ); |
| 57 |
is( $logged[0][0], 'warning', 'at warning level' ); |
| 58 |
like( $logged[0][1], qr/no charset is configured/, |
| 59 |
'saying no charset was configured' ); |
| 60 |
like( $logged[0][1], qr/assuming UTF-8/, |
| 61 |
'and what it assumed instead' ); |
| 62 |
is( scalar @warnings, 0, |
| 63 |
'and nothing goes to STDERR when the app provides a log callback' ); |
| 64 |
|
| 65 |
# Having assumed UTF-8, it must actually encode as UTF-8. |
| 66 |
my $got = psgi_parts($response); |
| 67 |
is( $got->{body}[0], "hi$BYTES", 'the body is UTF-8 encoded' ); |
| 68 |
like( $got->{header}{'Content-Type'}, qr/charset=UTF-8/, |
| 69 |
'and the assumption is declared in the Content-Type' ); |
| 70 |
|
| 71 |
# The second response is silent - the counter is per process, not per |
| 72 |
# response. Pinned because it is surprising: an app that starts warning |
| 73 |
# about this warns exactly once and then never again. |
| 74 |
my ( @logged2, @warnings2 ); |
| 75 |
my $second = Dancer2::Core::Response->new( |
| 76 |
log_cb => sub { push @logged2, [@_] } ); |
| 77 |
{ |
| 78 |
local $SIG{__WARN__} = sub { push @warnings2, $_[0] }; |
| 79 |
$second->content("again$CHAR"); |
| 80 |
} |
| 81 |
is( scalar @logged2, 0, 'a later response does not report it again' ); |
| 82 |
is( scalar @warnings2, 0, 'nor warn about it' ); |
| 83 |
|
| 84 |
# But it still encodes. Only the reporting is suppressed, not the |
| 85 |
# behavior - which is why the silence above is safe rather than a bug. |
| 86 |
is( $second->content, "again$BYTES", |
| 87 |
'and it still encodes as UTF-8 despite saying nothing' ); |
| 88 |
}; |
| 89 |
|
| 90 |
subtest 'a missing charset is fatal under strict_utf8' => sub { |
| 91 |
# Unlike the report above, the croak happens before the once-per-process |
| 92 |
# counter is consulted, so this is repeatable. |
| 93 |
for my $attempt ( 1, 2 ) { |
| 94 |
my $response = Dancer2::Core::Response->new( strict_utf8 => 1 ); |
| 95 |
my $err = exception { $response->content("hi$CHAR") }; |
| 96 |
ok( defined $err, "attempt $attempt throws" ); |
| 97 |
like( $err, qr/no charset is configured/, |
| 98 |
"attempt $attempt says why" ); |
| 99 |
} |
| 100 |
|
| 101 |
# Byte content is not affected: there is nothing to encode, so strict |
| 102 |
# mode has no complaint. |
| 103 |
my $bytes = Dancer2::Core::Response->new( strict_utf8 => 1 ); |
| 104 |
is( exception { $bytes->content("plain ascii") }, undef, |
| 105 |
'ASCII content is fine under strict_utf8' ); |
| 106 |
is( $bytes->content, 'plain ascii', 'and passes through unchanged' ); |
| 107 |
}; |
| 108 |
|
| 109 |
subtest 'text content is encoded once, and Content-Length counts bytes' => sub { |
| 110 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 111 |
$response->content("hi$CHAR"); |
| 112 |
|
| 113 |
ok( !utf8::is_utf8( $response->content ), |
| 114 |
'the stored content is bytes, not characters' ); |
| 115 |
is( $response->content, "hi$BYTES", 'encoded as UTF-8' ); |
| 116 |
ok( $response->is_encoded, 'and marked as encoded' ); |
| 117 |
|
| 118 |
my $got = psgi_parts($response); |
| 119 |
is( $got->{body}[0], "hi$BYTES", 'the PSGI body is those bytes' ); |
| 120 |
|
| 121 |
# 5 bytes, 3 characters. This is the assertion that fails if |
| 122 |
# Content-Length is ever taken before encoding. |
| 123 |
is( $got->{header}{'Content-Length'}, 5, |
| 124 |
'Content-Length is the byte count' ); |
| 125 |
is( length $got->{body}[0], 5, 'which matches the body actually sent' ); |
| 126 |
isnt( $got->{header}{'Content-Length'}, 3, |
| 127 |
'and is not the character count' ); |
| 128 |
|
| 129 |
is( $got->{header}{'Content-Type'}, 'text/html; charset=UTF-8', |
| 130 |
'the charset is declared once, appended to the default type' ); |
| 131 |
|
| 132 |
# Converting twice must not append a second charset or a second |
| 133 |
# Content-Length, and must not re-encode. |
| 134 |
my $again = psgi_parts($response); |
| 135 |
is( $again->{header}{'Content-Type'}, 'text/html; charset=UTF-8', |
| 136 |
'a second conversion does not append the charset again' ); |
| 137 |
is( $again->{header}{'Content-Length'}, 5, |
| 138 |
'nor a second Content-Length' ); |
| 139 |
is( $again->{body}[0], "hi$BYTES", 'and does not double-encode the body' ); |
| 140 |
}; |
| 141 |
|
| 142 |
subtest 'an explicit charset in the content type is respected' => sub { |
| 143 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 144 |
$response->content_type('text/plain; charset=UTF-8'); |
| 145 |
$response->content("hi$CHAR"); |
| 146 |
|
| 147 |
my $got = psgi_parts($response); |
| 148 |
is( $got->{header}{'Content-Type'}, 'text/plain; charset=UTF-8', |
| 149 |
'the content type is left exactly as given, with one charset' ); |
| 150 |
is( $got->{body}[0], "hi$BYTES", 'and the content is encoded to match' ); |
| 151 |
}; |
| 152 |
|
| 153 |
subtest 'no charset is appended to a non-text content type' => sub { |
| 154 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 155 |
$response->content_type('application/json'); |
| 156 |
$response->content(qq[{"m":"$CHAR"}]); |
| 157 |
|
| 158 |
my $got = psgi_parts($response); |
| 159 |
is( $got->{header}{'Content-Type'}, 'application/json', |
| 160 |
'the content type gains no charset parameter' ); |
| 161 |
unlike( $got->{header}{'Content-Type'}, qr/charset/, |
| 162 |
'not even an empty one' ); |
| 163 |
|
| 164 |
# Non-text content is deliberately not encoded here - a serializer is |
| 165 |
# expected to have produced bytes already and to have set is_encoded. |
| 166 |
# Pinned as current behavior: assigning characters under a non-text type |
| 167 |
# by hand leaves them as characters, and Content-Length then counts |
| 168 |
# characters rather than bytes. |
| 169 |
ok( !$response->is_encoded, 'and the content is not encoded' ); |
| 170 |
ok( utf8::is_utf8( $got->{body}[0] ), |
| 171 |
'so a character string stays characters' ); |
| 172 |
is( $got->{header}{'Content-Length'}, 9, |
| 173 |
'with Content-Length counting those characters' ); |
| 174 |
}; |
| 175 |
|
| 176 |
subtest 'replacing the content after the first set is encoded on its own merits (fixed)' => sub { |
| 177 |
|
| 178 |
# is_encoded describes the content currently assigned, not the response |
| 179 |
# for all time. The 'around content' modifier (Response.pm:137-...) |
| 180 |
# resets is_encoded on every fresh assignment before calling |
| 181 |
# encode_content, so a *second* assignment of character content is |
| 182 |
# encoded exactly like the first would have been - it does not inherit |
| 183 |
# the previous content's encoded status. |
| 184 |
# |
| 185 |
# This matters because it is reachable from ordinary code: an 'after' |
| 186 |
# hook that rewrites response->content, or halt() called once content |
| 187 |
# was already set. |
| 188 |
|
| 189 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 190 |
$response->content('first'); # ASCII, latches is_encoded |
| 191 |
ok( $response->is_encoded, 'the first assignment marks the response encoded' ); |
| 192 |
|
| 193 |
$response->content("hi$CHAR"); # characters, must be encoded afresh |
| 194 |
|
| 195 |
ok( !utf8::is_utf8( $response->content ), |
| 196 |
'the stored content is bytes, not characters' ); |
| 197 |
ok( $response->is_encoded, 'and the replacement is marked as encoded too' ); |
| 198 |
|
| 199 |
my $got = psgi_parts($response); |
| 200 |
|
| 201 |
is( $got->{body}[0], "hi$BYTES", 'the replacement content was UTF-8 encoded' ); |
| 202 |
is( $got->{header}{'Content-Length'}, 5, |
| 203 |
'Content-Length is the 5 bytes UTF-8 needs, not the 3-character count' ); |
| 204 |
like( $got->{header}{'Content-Type'}, qr/charset=UTF-8/, |
| 205 |
'and the Content-Type still declares UTF-8, now truthfully' ); |
| 206 |
|
| 207 |
# A second read/conversion must not re-encode already-encoded bytes. |
| 208 |
my $again = psgi_parts($response); |
| 209 |
is( $again->{body}[0], "hi$BYTES", |
| 210 |
'a second to_psgi does not double-encode the body' ); |
| 211 |
is( $again->{header}{'Content-Length'}, 5, |
| 212 |
'nor change the Content-Length' ); |
| 213 |
}; |
| 214 |
|
| 215 |
subtest 'statuses that forbid a body get no body and no Content-Length' => sub { |
| 216 |
for my $status ( 100, 101, 204, 304 ) { |
| 217 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 218 |
$response->status($status); |
| 219 |
$response->header( 'X-Keep' => 'yes' ); |
| 220 |
$response->content('BODY'); |
| 221 |
|
| 222 |
my $got = psgi_parts($response); |
| 223 |
is( $got->{status}, $status, "$status: the status is preserved" ); |
| 224 |
is_deeply( $got->{body}, [], "$status: the body is dropped entirely" ); |
| 225 |
is( $got->{header}{'Content-Length'}, undef, |
| 226 |
"$status: and no Content-Length is sent" ); |
| 227 |
is( $got->{header}{'X-Keep'}, 'yes', |
| 228 |
"$status: other headers are still sent" ); |
| 229 |
} |
| 230 |
|
| 231 |
# The neighbours must keep their bodies, or the check above is just |
| 232 |
# "everything is empty". |
| 233 |
for my $status ( 200, 205, 404 ) { |
| 234 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 235 |
$response->status($status); |
| 236 |
$response->content('BODY'); |
| 237 |
|
| 238 |
my $got = psgi_parts($response); |
| 239 |
is_deeply( $got->{body}, ['BODY'], "$status: keeps its body" ); |
| 240 |
is( $got->{header}{'Content-Length'}, 4, |
| 241 |
"$status: and reports its length" ); |
| 242 |
} |
| 243 |
}; |
| 244 |
|
| 245 |
subtest 'the default content type is applied when none was set' => sub { |
| 246 |
my $default = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 247 |
$default->content('x'); |
| 248 |
is( psgi_parts($default)->{header}{'Content-Type'}, |
| 249 |
'text/html; charset=UTF-8', |
| 250 |
'text/html is the default' ); |
| 251 |
|
| 252 |
my $configured = Dancer2::Core::Response->new( |
| 253 |
charset => 'UTF-8', |
| 254 |
default_content_type => 'text/plain', |
| 255 |
); |
| 256 |
$configured->content('x'); |
| 257 |
is( psgi_parts($configured)->{header}{'Content-Type'}, |
| 258 |
'text/plain; charset=UTF-8', |
| 259 |
'a configured default is used instead of text/html' ); |
| 260 |
|
| 261 |
# A response nothing was ever done to still converts cleanly - this is |
| 262 |
# the "all routes passed" case. |
| 263 |
my $untouched = Dancer2::Core::Response->new; |
| 264 |
my $got = psgi_parts($untouched); |
| 265 |
is( $got->{status}, 200, 'an untouched response is a 200' ); |
| 266 |
is( $got->{header}{'Content-Type'}, 'text/html', |
| 267 |
'with the default content type' ); |
| 268 |
is( $got->{header}{'Content-Length'}, 0, 'a zero Content-Length' ); |
| 269 |
is_deeply( $got->{body}, [''], 'and an empty body' ); |
| 270 |
}; |
| 271 |
|
| 272 |
subtest 'CR and LF are stripped from header values' => sub { |
| 273 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 274 |
$response->content('x'); |
| 275 |
|
| 276 |
# A CRLF in a header value is how a response-splitting attack injects a |
| 277 |
# header of its own. It must not survive into the PSGI array. |
| 278 |
$response->header( 'X-Split' => "ok\r\nInjected: yes" ); |
| 279 |
|
| 280 |
# Folded continuation lines collapse to a single space instead. |
| 281 |
$response->header( 'X-Folded' => "a\r\n\tb" ); |
| 282 |
|
| 283 |
my $got = psgi_parts($response); |
| 284 |
|
| 285 |
is( $got->{header}{'X-Split'}, 'okInjected: yes', |
| 286 |
'the CRLF is removed, leaving one header value' ); |
| 287 |
unlike( $got->{header}{'X-Split'}, qr/[\r\n]/, |
| 288 |
'no carriage return or line feed remains' ); |
| 289 |
|
| 290 |
is( $got->{header}{'X-Folded'}, 'a b', |
| 291 |
'linear whitespace folding collapses to a single space' ); |
| 292 |
|
| 293 |
# Nothing anywhere in the array may carry a bare CR or LF. |
| 294 |
my @offending = grep { /[\r\n]/ } @{ $got->{raw} }; |
| 295 |
is_deeply( \@offending, [], |
| 296 |
'no element of the PSGI header array contains CR or LF' ); |
| 297 |
}; |
| 298 |
|
| 299 |
subtest 'CR and LF are stripped from a header name (fixed)' => sub { |
| 300 |
|
| 301 |
# headers_to_array strips CR and LF from each header *value* |
| 302 |
# (Dancer2/Core/Response.pm:70-..., commented "remove CR and LF since the |
| 303 |
# char is invalid here") and now applies the same sanitisation to the |
| 304 |
# *name*, a few lines away in the same loop. A CRLF injected into the |
| 305 |
# name must not reach the PSGI array intact - that is the same |
| 306 |
# response-splitting shape the value is guarded against. |
| 307 |
|
| 308 |
my $response = Dancer2::Core::Response->new( charset => 'UTF-8' ); |
| 309 |
$response->content('x'); |
| 310 |
$response->header( "X-Bad\r\nInjected: yes" => 'v' ); |
| 311 |
|
| 312 |
my $got = psgi_parts($response); |
| 313 |
|
| 314 |
# Nothing anywhere in the array may carry a bare CR or LF - name or |
| 315 |
# value. |
| 316 |
my @with_newlines = grep { /[\r\n]/ } @{ $got->{raw} }; |
| 317 |
is_deeply( \@with_newlines, [], |
| 318 |
'no element of the PSGI header array contains CR or LF' ); |
| 319 |
|
| 320 |
# The sanitised name is still present - the header was neutralised, not |
| 321 |
# silently dropped. (HTTP::Headers::Fast canonicalises the field name |
| 322 |
# case, which is why "yes" becomes "Yes" here.) |
| 323 |
ok( ( grep { /^X-BadInjected: Yes$/ } @{ $got->{raw} } ), |
| 324 |
'the sanitised header name is still present' ); |
| 325 |
}; |
| 326 |
|
| 327 |
done_testing(); |