blib/lib/Plack/Middleware/CSP.pm

filter: Statementstmt 100% 40 / 40 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 100% 56 / 56 CC 3 SCAR 3.6
CC
3
Cov
100%
__ANON__
3.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.4
↑ Summary
1 1 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
2  
3 1 package Plack::Middleware::CSP 0.04 {
4 1 ▶ use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶ use HTTP::CSPHeader;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶ use parent "Plack::Middleware";
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶ use Plack::Util::Accessor qw( nonce_template_token );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 our $AUTHORITY = "cpan:ASHLEY";
11  
12 7 ▶ sub _csp { +shift->{_csp} }
Subroutine _csp: called CC=1 SCAR=0.0
13 3 ▶ sub _nonce { +shift->_csp->nonce }
Subroutine _nonce: called CC=1 SCAR=0.0
14  
15 3 ▶ sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
16 3 my $self = +shift->SUPER::new(@_);
17 # Let HTTP::CSPHeader decide and report on arguments.
18 $self->{_csp} = HTTP::CSPHeader->new(
19 policy => delete $self->{policy},
20 nonces_for => delete $self->{nonces_for},
21 3 );
22  
23 2 $self;
24 }
25  
26 2 ▶ sub call {
Subroutine call: called CC=1 SCAR=0.0
Pod documented
27 2 my ( $self, $env ) = @_;
28  
29 2 $env->{"CSP_NONCE"} = $self->_nonce;
30 2 my $res = $self->app->($env);
31  
32 2 ▶ Plack::Util::response_cb($res, sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
33 2 my $res = shift;
34 2 $self->_csp->reset; # Request is done, reset for response.
35 2 my $h = Plack::Util::headers($res->[1]);
36 2 ▶ if ( my $token = $self->nonce_template_token )
Branch: 1 branch
BranchTrueFalseTotal
if (my $token = $self->nonce_template_token) 1 1 2
37 {
38 1 my $nonce = $self->_nonce;
39 # Content type?!? Restrict to… sane values? text, html…?
40 1 s/\Q$token/$nonce/g for @{ $res->[2] };
41 }
42 2 $h->set("Content-Security-Policy" => $self->_csp->header );
43 2 });
44 }
45 1 1;
46 };
47  
48 __DATA__
↑ Summary