line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package HTTP::CSPHeader; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# ABSTRACT: manage dynamic content security policy headers |
4
|
|
|
|
|
|
|
|
5
|
1
|
|
|
1
|
|
125474
|
use v5.14; |
|
1
|
|
|
|
|
15
|
|
6
|
|
|
|
|
|
|
|
7
|
1
|
|
|
1
|
|
604
|
use Moo; |
|
1
|
|
|
|
|
74781
|
|
|
1
|
|
|
|
|
8
|
|
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
4335
|
use Fcntl qw/ O_NONBLOCK O_RDONLY /; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
59
|
|
10
|
1
|
|
|
1
|
|
7
|
use List::Util 1.29 qw/ pairmap pairs /; |
|
1
|
|
|
|
|
27
|
|
|
1
|
|
|
|
|
112
|
|
11
|
1
|
|
|
1
|
|
528
|
use Math::Random::ISAAC; |
|
1
|
|
|
|
|
1816
|
|
|
1
|
|
|
|
|
43
|
|
12
|
1
|
|
|
1
|
|
555
|
use Types::Standard qw/ ArrayRef is_ArrayRef Bool HashRef Str /; |
|
1
|
|
|
|
|
150702
|
|
|
1
|
|
|
|
|
13
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
# RECOMMEND PREREQ: Math::Random::ISAAC::XS |
15
|
|
|
|
|
|
|
# RECOMMEND PREREQ: Type::Tiny::XS |
16
|
|
|
|
|
|
|
|
17
|
1
|
|
|
1
|
|
1853
|
use namespace::autoclean; |
|
1
|
|
|
|
|
14862
|
|
|
1
|
|
|
|
|
5
|
|
18
|
|
|
|
|
|
|
|
19
|
|
|
|
|
|
|
our $VERSION = 'v0.2.0'; # TRIAL |
20
|
|
|
|
|
|
|
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
has _base_policy => ( |
23
|
|
|
|
|
|
|
is => 'ro', |
24
|
|
|
|
|
|
|
isa => HashRef, |
25
|
|
|
|
|
|
|
required => 1, |
26
|
|
|
|
|
|
|
init_arg => 'policy', |
27
|
|
|
|
|
|
|
); |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
has policy => ( |
30
|
|
|
|
|
|
|
is => 'lazy', |
31
|
|
|
|
|
|
|
isa => HashRef, |
32
|
|
|
|
|
|
|
clearer => '_clear_policy', |
33
|
|
|
|
|
|
|
init_arg => undef, |
34
|
|
|
|
|
|
|
); |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
sub _build_policy { |
37
|
4
|
|
|
4
|
|
39
|
my ($self) = @_; |
38
|
4
|
|
|
|
|
7
|
my %policy = %{ $self->_base_policy }; |
|
4
|
|
|
|
|
23
|
|
39
|
4
|
100
|
|
|
|
10
|
if ( my @dirs = @{ $self->nonces_for } ) { |
|
4
|
|
|
|
|
82
|
|
40
|
2
|
|
|
|
|
47
|
my $nonce = "'nonce-" . $self->nonce . "'"; |
41
|
2
|
|
|
|
|
19
|
for my $dir (@dirs) { |
42
|
4
|
50
|
|
|
|
9
|
if ( defined $policy{$dir} ) { |
43
|
4
|
|
|
|
|
12
|
$policy{$dir} .= " " . $nonce; |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
else { |
46
|
0
|
|
|
|
|
0
|
$policy{$dir} = $nonce; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
} |
49
|
2
|
|
|
|
|
63
|
$self->_changed(1); |
50
|
|
|
|
|
|
|
} |
51
|
4
|
|
|
|
|
162
|
return \%policy; |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
has _changed => ( |
55
|
|
|
|
|
|
|
is => 'rw', |
56
|
|
|
|
|
|
|
isa => Bool, |
57
|
|
|
|
|
|
|
lazy => 1, |
58
|
|
|
|
|
|
|
default => 0, |
59
|
|
|
|
|
|
|
init_arg => undef, |
60
|
|
|
|
|
|
|
); |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
has nonces_for => ( |
64
|
|
|
|
|
|
|
is => 'lazy', |
65
|
|
|
|
|
|
|
isa => ArrayRef [Str], |
66
|
1
|
|
|
1
|
|
26
|
builder => sub { return [] }, |
67
|
|
|
|
|
|
|
coerce => sub { my $val = is_ArrayRef( $_[0] ) ? $_[0] : [ $_[0] ] }, |
68
|
|
|
|
|
|
|
); |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
has nonce => ( |
72
|
|
|
|
|
|
|
is => 'lazy', |
73
|
|
|
|
|
|
|
isa => Str, |
74
|
|
|
|
|
|
|
clearer => '_clear_nonce', |
75
|
|
|
|
|
|
|
unit_arg => undef, |
76
|
|
|
|
|
|
|
); |
77
|
|
|
|
|
|
|
|
78
|
|
|
|
|
|
|
sub _build_nonce { |
79
|
2
|
|
|
2
|
|
540
|
my ($self) = @_; |
80
|
|
|
|
|
|
|
|
81
|
2
|
|
|
|
|
4
|
state $rng = do { |
82
|
1
|
50
|
|
|
|
49
|
sysopen( my $fh, '/dev/urandom', O_NONBLOCK | O_RDONLY ) or die $!; |
83
|
1
|
50
|
|
|
|
15
|
sysread( $fh, my $data, 16 ) or die $!; |
84
|
1
|
|
|
|
|
11
|
close $fh; |
85
|
|
|
|
|
|
|
|
86
|
1
|
|
|
|
|
17
|
Math::Random::ISAAC->new( unpack( "C*", $data ) ); |
87
|
|
|
|
|
|
|
}; |
88
|
|
|
|
|
|
|
|
89
|
2
|
|
|
|
|
44
|
return sprintf( '%x', $rng->irand ^ $$ ); |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
has header => ( |
94
|
|
|
|
|
|
|
is => 'lazy', |
95
|
|
|
|
|
|
|
isa => Str, |
96
|
|
|
|
|
|
|
clearer => '_clear_header', |
97
|
|
|
|
|
|
|
init_arg => undef, |
98
|
|
|
|
|
|
|
); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub _build_header { |
101
|
8
|
|
|
8
|
|
6476
|
my ($self) = @_; |
102
|
8
|
|
|
|
|
126
|
my $policy = $self->policy; |
103
|
8
|
|
|
27
|
|
177
|
return join( "; ", pairmap { $a . " " . $b } %$policy ); |
|
27
|
|
|
|
|
217
|
|
104
|
|
|
|
|
|
|
} |
105
|
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
sub reset { |
108
|
2
|
|
|
2
|
1
|
2353
|
my ($self) = @_; |
109
|
2
|
50
|
|
|
|
51
|
return unless $self->_changed; |
110
|
2
|
|
|
|
|
50
|
$self->_clear_nonce; |
111
|
2
|
|
|
|
|
42
|
$self->_clear_policy; |
112
|
2
|
|
|
|
|
41
|
$self->_clear_header; |
113
|
2
|
|
|
|
|
37
|
$self->_changed(0); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
sub amend { |
118
|
4
|
|
|
4
|
1
|
5070
|
my ($self, @args) = @_; |
119
|
4
|
|
|
|
|
101
|
my $policy = $self->policy; |
120
|
|
|
|
|
|
|
|
121
|
4
|
50
|
|
|
|
38
|
if (@args) { |
122
|
|
|
|
|
|
|
|
123
|
4
|
|
|
|
|
29
|
for my $pol ( pairs @args ) { |
124
|
|
|
|
|
|
|
|
125
|
4
|
|
|
|
|
15
|
my ( $dir, $val ) = @$pol; |
126
|
|
|
|
|
|
|
|
127
|
4
|
100
|
|
|
|
14
|
if ( $dir =~ s/^\+// ) { # append to directive |
128
|
1
|
50
|
|
|
|
4
|
if ( exists $policy->{$dir} ) { |
|
|
0
|
|
|
|
|
|
129
|
1
|
|
|
|
|
6
|
$policy->{$dir} .= " " . $val; |
130
|
|
|
|
|
|
|
} |
131
|
|
|
|
|
|
|
elsif ( defined $val ) { |
132
|
0
|
|
|
|
|
0
|
$policy->{$dir} = $val; |
133
|
|
|
|
|
|
|
} |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
} |
136
|
|
|
|
|
|
|
else { |
137
|
3
|
100
|
|
|
|
8
|
if ( defined $val ) { |
138
|
2
|
|
|
|
|
7
|
$policy->{$dir} = $val; |
139
|
|
|
|
|
|
|
} |
140
|
|
|
|
|
|
|
else { |
141
|
1
|
|
|
|
|
4
|
delete $policy->{$dir}; |
142
|
|
|
|
|
|
|
} |
143
|
|
|
|
|
|
|
} |
144
|
|
|
|
|
|
|
} |
145
|
|
|
|
|
|
|
|
146
|
4
|
|
|
|
|
78
|
$self->_clear_header; |
147
|
4
|
|
|
|
|
81
|
$self->_changed(1); |
148
|
|
|
|
|
|
|
} |
149
|
|
|
|
|
|
|
|
150
|
4
|
|
|
|
|
111
|
return $policy; |
151
|
|
|
|
|
|
|
} |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
1; |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
__END__ |