| 1 | package Plack::Middleware::SecureHeaders; | ||||||||||
| 2 | 5 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 3 | 5 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 5 | ▶ | use parent qw( Plack::Middleware ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 5 | our $VERSION = "0.01"; | |||||||||
| 7 | |||||||||||
| 8 | 5 | ▶ | use HTTP::SecureHeaders; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 5 | ▶ | use Plack::Util::Accessor qw( | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | secure_headers | ||||||||||
| 11 | 5 | ); | |||||||||
| 12 | |||||||||||
| 13 | 12 | ▶ | sub prepare_app { | ||||||||
|
Subroutine
prepare_app: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 14 | 12 | my $self = shift; | |||||||||
| 15 | 12 | ▶ | unless (defined $self->secure_headers) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 16 | 6 | $self->secure_headers(HTTP::SecureHeaders->new) | |||||||||
| 17 | } | ||||||||||
| 18 | } | ||||||||||
| 19 | |||||||||||
| 20 | 12 | ▶ | sub call { | ||||||||
|
Subroutine
call: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 21 | 12 | my($self, $env) = @_; | |||||||||
| 22 | |||||||||||
| 23 | 12 | my $res = $self->app->($env); | |||||||||
| 24 | 12 | my $headers = Plack::Util::headers($res->[1]); | |||||||||
| 25 | |||||||||||
| 26 | 12 | ▶ | unless ($headers->exists('Content-Type')) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 27 | 1 | die sprintf('Required Content-Type header. %s %s', $env->{REQUEST_METHOD}, $env->{PATH_INFO}); | |||||||||
| 28 | } | ||||||||||
| 29 | |||||||||||
| 30 | # NOTE: the charset attribute is necessary to prevent XSS in HTML pages | ||||||||||
| 31 | # https://cheatsheetseries.owasp.org/cheatsheets/HTTP_Headers_Cheat_Sheet.html#content-type | ||||||||||
| 32 | 11 | ▶ | if ($headers->get('Content-Type') =~ qr!^text/html!i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 2 | ▶ | unless ($headers->get('Content-Type') =~ qr!charset=\w+!) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 34 | die sprintf('Required charset for text/html. %s %s', $env->{REQUEST_METHOD}, $env->{PATH_INFO}) | ||||||||||
| 35 | 1 | } | |||||||||
| 36 | } | ||||||||||
| 37 | |||||||||||
| 38 | 10 | $self->secure_headers->apply($headers); | |||||||||
| 39 | |||||||||||
| 40 | 10 | return $res; | |||||||||
| 41 | } | ||||||||||
| 42 | |||||||||||
| 43 | 5 | 1; | |||||||||
| 44 | __END__ | ||||||||||